!HDU 4380 三角屋内有奇数个宝藏的三角形有多少个-计算几何-(向量叉乘&线段与点的关系&暴力枚举)

题意:小明要买三座房子,这三个房子构成一个三角形,已知n个房子的坐标,任何三个房子都不在一条直线上,又已知有m个宝藏的坐标,问房子构成的三角形内有奇数个宝藏的三角形有多少个。数据范围:n(3~100),m(1~1000)

分析:

简单的计算几何。记住这题的做法。

三角形内的点的个数=上面的线段下面的点的个数 -- 下面两条线段下面的点的个数(或者下面一条线段减上面两条线段,看具体位置情况,所以直接取绝对值就好)

n个点有n(n-1)/2条线段,不超过1W,枚举每条线段,再枚举每个宝藏的坐标(10^3),判断这个宝藏是否在这个线段下面,方法是判断横坐标是否在左右两端点的横坐标内,再用一个向量叉乘<0 就行了。处理了每条线段下面有多少个点之后,再三重循环(O(n^3))枚举线段(即枚举三角形)用上面的公式得到三角形内的点然后判定是否为奇数就行了。

这里要先做的一个处理就是把房子的坐标按横坐标第一关键字,纵坐标第二关键字排序,因为这样方便枚举。

注意这题求得三角形内点的方法和线段的表示方法(用两端点来标示cnt[i][j])

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m;
struct node{
    long long x,y;
};
node a[200],b[1005];
int cnt[200][200];
bool cmp(node a,node b)
{
    if(a.x!=b.x) return a.x<b.x;
    else return a.y<b.y;
}
long long f(node a,node b,node c)
{
    long long ans=(b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
    return ans;
}
int main()
{
    int cas=1;
    while(scanf("%d%d",&n,&m)!=EOF){
        memset(cnt,0,sizeof(cnt));
        for(int i=0;i<n;i++) scanf("%lld%lld",&a[i].x,&a[i].y);
        for(int i=0;i<m;i++) scanf("%lld%lld",&b[i].x,&b[i].y);
        sort(a,a+n,cmp);
        for(int i=0;i<n;i++){
            for(int j=i+1;j<n;j++){
                for(int k=0;k<m;k++){
                    if(b[k].x>a[i].x&&b[k].x<a[j].x){
                        if(f(a[i],a[j],b[k])<0) cnt[i][j]++;
                    }
                }
            }
        }
        int ans=0;
        for(int i=0;i<n;i++){
            for(int j=i+1;j<n;j++){
                for(int k=j+1;k<n;k++){
                    int tmp=abs(cnt[i][k]-cnt[i][j]-cnt[j][k]);
                    if(tmp%2==1) ans++;
                }
            }
        }
        printf("Case %d: %d\n",cas++,ans);
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-02 02:39:40

!HDU 4380 三角屋内有奇数个宝藏的三角形有多少个-计算几何-(向量叉乘&线段与点的关系&暴力枚举)的相关文章

hdu 5247 找连续数【暴力枚举】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5247 分析:这道题是2015百度之星初赛1的2题,当时没看这道题 是队友看的,比完以后也做了一下,思路大体都是一样的,就是 暴力枚举,因为k<=1000,那么我们可以每一点x为起点跑[x,x+999] 这段区间,把每得到一段连续的子区间[x,?],则num[len]++(len=size([x,?])); 这样就可以了,最后num数组里就是对应的答案 献上代码: #include<stdio.h&

HDU 4930 Fighting the Landlords(暴力枚举+模拟)

HDU 4930 Fighting the Landlords 题目链接 题意:就是题中那几种牌型,如果先手能一步走完,或者一步让后手无法管上,就赢 思路:先枚举出两个人所有可能的牌型的最大值,然后再去判断即可 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct Player { int rank[15]; } p1, p2; int t, h

HDU 4902 (牛叉的线段树)

Nice boat Problem Description There is an old country and the king fell in love with a devil. The devil always asks the king to do some crazy things. Although the king used to be wise and beloved by his people. Now he is just like a boy in love and c

hdu 4968 Improving the GPA (水 暴力枚举)

题目链接 题意:给平均成绩和科目数,求可能的最大学分和最小学分. 分析: 枚举一下,可以达到复杂度可以达到10^4,我下面的代码是10^5,可以把最后一个循环撤掉. 刚开始以为枚举档次的话是5^10,但是这个又不要求顺序,所以只是枚举个数就行了.. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath&g

HDU 4081 Qin Shi Huang&#39;s National Road System(最小生成树+暴力枚举边)

题目大意:给你1000个点,每个点上有一个数目代表这个城市有多少人,让你把这N个点构成一颗生成树,你可以删除其中的任意一条边.让你求出一个比例A/B是的这个比例最大,A表示你删除那条边上两个城市的人口数之和,B表示的是去掉这条变这可生成树上其他的边的总长度. 解体思路:先求出来最小生成树,然后暴力枚举生成树的边,B=总数-这条边的长度.A = 将这条连断开之后左右集合中权值最大的两个数的和. 这样保证了B最小的情况下,去找最大的A,所以是可行的解.生成树的同时建边,然后dfs找最大值. PS:这

HDU 5024 Wang Xifeng&#39;s Little Plot(暴力枚举+瞎搞)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5024 Problem Description <Dream of the Red Chamber>(also <The Story of the Stone>) is one of the Four Great Classical Novels of Chinese literature, and it is commonly regarded as the best one. Thi

hdu 1086(计算几何入门题——计算线段交点个数)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1086 You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7167    Accepted Submission(s): 3480 Problem Description Ma

HDU 5616 Jam&#39;s balance(暴力枚举子集)

题目链接:点击打开链接 题意:有一个没有游标的天平,和n个秤砣,m个询问, 每次一个k,问可否秤出k这个重量. 秤砣可以放两边. 思路:因为n最大20, 暴力枚举子集. 因为可以放两边, 所以每次再跑一遍, 减去每个的重量, 将答案保存. 比赛的时候忘了限制边界,虽然过了终测数据, 却被人用大数据hack了(RE), 还是自己程序写的不够鲁棒, 思考的不完善. 细节参见代码: #include<cstdio> #include<cstring> #include<algori

hdu 4968 Improving the GPA(暴力枚举)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4970 Problem Description Xueba: Using the 4-Point Scale, my GPA is 4.0. In fact, the AVERAGE SCORE of Xueba is calculated by the following formula: AVERAGE SCORE = ∑(Wi * SCOREi) / ∑(Wi) 1<=i<=N where S