zoukankan      html  css  js  c++  java
  • 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;
    }
    

      

  • 相关阅读:
    关于智能本质的思考
    Effective C++ 条款39
    【视频教程】JEECG 入门视频教程大全+历史版本号代码下载
    HDU 4859(Bestcoder #1 1003)海岸线(网络流之最小割)
    最简单的基于FFMPEG的音频编码器(PCM编码为AAC)
    android经常使用的电话操作
    Vmware中为Mac Os安装vmtools
    win10.10 激活
    VM11安装Mac OS X 10.10
    win7系统升家庭版级为旗舰版的方法
  • 原文地址:https://www.cnblogs.com/zhumengdexiaobai/p/8367370.html
Copyright © 2011-2022 走看看