• 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;
    }
    

      

  • 相关阅读:
    nepenthes用法
    honeydctl命令
    honeyd路由拓扑
    Linux Samba服务器的安装
    honeyd使用
    FreeRTOS 事件标志组
    epoll函数
    Java程序:从命令行接收多个数字,求和并输出结果
    《大道至简》第一章读后感
    【诗词歌赋】 杂感- 贺小妹
  • 原文地址:https://www.cnblogs.com/zhumengdexiaobai/p/8367370.html
Copyright © 2020-2023  润新知