1-选房子(几何计算)

hdu4380     http://acm.hdu.edu.cn/showproblem.php?pid=4380
https://www.cnblogs.com/zzyDS/p/5475465.html
/*
                      Farmer Greedy
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2095 Accepted Submission(s): 647


Problem Description
  Farmer Greedy is going to buy some houses for his farm. He has money only to buy three houses. The three houses can from a triangle area, which he can own as his farm.
  There are many houses he can choose to buy, and there are many goldstones. They are points in a 2-dimentional plane. No three points are collinear. Farmer Greedy likes odd numbers. Now Farmer Greedy wonders how many farms he can choose to have odd goldstones in it.

Input
  There are multiple test cases. In each case, the first line gives two integers N, M. In the next N lines, each line gives two integers (X, Y) indicating the coordinates of one house. In the next M lines, each line gives two integers (X, Y) indicating the coordinates of one goldstone.
Technical Specification
  3 <= N <= 100
  0 <= M <= 1000
  |X|, |Y| <= 100000

Output
  For each case, print in one line containing the case number (starting with 1) and the number of farms Farmer Greedy can choose to have odd goldstones in it.

Sample Input
4 4
-10 0
10 0
0 10
0 -10
1 1
1 2
-1 1
-1 -1

Sample Output
Case 1: 2
*/
题目:选房子,就是给定房子的坐标和金库的坐标,每次选三个房子,使得所构成的三角形内的金库数量为奇数,看有多少种选
思路:将房子的坐标排序,然后将所能构成的线段依次求出线段的下面的金库数量,最后每次枚举三边构成三角形,将两斜边的下面的金库数量之和减去最下面一边的下面金库数量,最后对差值的绝对值判断奇偶即可。

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;

typedef struct node{
	ll x, y;
}node;
node a[105], b[1005];
ll cnt[105][1005];

bool cmp(node m, node n){
	if(m.x != n.x)
		return m.x < n.x;
	else
		return m.y < n.y;
}

ll chacheng(node a, node b, node c){    //利用叉乘判断是否在选段的下面,返回负数即是在下面 
	return (b.x - a.x)*(c.y - a.y) - (c.x - a.x)*(b.y - a.y);
}

int main(){
	int n, m, i, j, k;
	int count = 0, s = 0, ct = 0;
	while(cin >> n >> m){
		count = 0;
		ct++;
		for(i = 0; i < n; i++)
			cin >> a[i].x >> a[i].y;
		for(i = 0; i < m; i++)	
			cin >> b[i].x >> b[i].y;
	
		memset(cnt, 0, sizeof(cnt));
		sort(a, a + n, cmp);	
		
		for(i = 0; i < n - 1; i++)							//求每个线段的下面的金库数 
			for(j = i + 1; j < n; j++)
				for(k = 0; k < m; k++){
					if(b[k].x > a[i].x && b[k].x <= a[j].x) //应该是半开半闭的,否则与房子同x的永远不能计数 
						if(chacheng(a[i], a[j], b[k]) < 0)
							cnt[i][j]++;
	//				cout << cnt[i][j] << " ";
				}
		for(i = 0; i < n - 2; i++)       					//枚举三角形 
			for(j = i + 1; j < n - 1; j++)
				for(k = j + 1; k < n; k++){ //note
					s = abs(cnt[i][j] + cnt[j][k] - cnt[i][k]);
					if(s&1)
						count++;
				}
		cout << "Case " << ct << ": " << count << endl;
	} 
	 
	return 0;
}

  

 

转载于:https://www.cnblogs.com/zhumengdexiaobai/p/8367370.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值