zoukankan      html  css  js  c++  java
  • POJ 3348:Cows 凸包+多边形面积

    Cows
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 7739   Accepted: 3507

    Description

    Your friend to the south is interested in building fences and turning plowshares into swords. In order to help with his overseas adventure, they are forced to save money on buying fence posts by using trees as fence posts wherever possible. Given the locations of some trees, you are to help farmers try to create the largest pasture that is possible. Not all the trees will need to be used.

    However, because you will oversee the construction of the pasture yourself, all the farmers want to know is how many cows they can put in the pasture. It is well known that a cow needs at least 50 square metres of pasture to survive.

    Input

    The first line of input contains a single integer, n (1 ≤ n ≤ 10000), containing the number of trees that grow on the available land. The next n lines contain the integer coordinates of each tree given as two integers x and y separated by one space (where -1000 ≤ x, y ≤ 1000). The integer coordinates correlate exactly to distance in metres (e.g., the distance between coordinate (10; 11) and (11; 11) is one metre).

    Output

    You are to output a single integer value, the number of cows that can survive on the largest field you can construct using the available trees.

    Sample Input

    4
    0 0
    0 101
    75 0
    75 101

    Sample Output

    151

    题意是给出各个树的位置,用这些树可以围出一块最大的面积,一头牛需要50平方米的面积,问这些树围出的面积可以养多少头牛。

    先构造凸包,在求出凸包围成的面积,最后除以50求整。

    代码:

    #include <iostream>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    #include <string>
    #include <cstring>
    #pragma warning(disable:4996)
    using namespace std;
    
    struct no
    {
    	int x, y;
    }node[10005];
    
    int n;
    int conbag[10005];
    
    no orign;//原点
    
    int dis(no n1, no n2)
    {
    	return (n1.x - n2.x)*(n1.x - n2.x) + (n1.y - n2.y)*(n1.y - n2.y);
    }
    
    int xmult(int x1, int y1, int x2, int y2)
    {
    	return x1*y2 - x2*y1;
    }
    
    int Across(no n1, no n2, no n3, no n4)
    {
    	return xmult(n2.x - n1.x, n2.y - n1.y, n4.x - n3.x, n4.y - n3.y);
    }
    
    int cmp(const void* n1, const void* n2)
    {
    	int temp = Across(orign, *(no *)n1, orign, *(no *)n2);
    
    	if (temp > 0)
    	{
    		return -1;
    	}
    	else if (temp == 0)
    	{
    		return (dis(orign, *(no *)n1) - dis(orign, *(no *)n2));
    	}
    	else
    	{
    		return 1;
    	}
    }
    
    int main()
    {
    
    	int i, j, k, min_x, pos_x;
    	double sum;
    	while (scanf("%d", &n) != EOF)
    	{
    		min_x = 1005;
    		for (i = 1; i <= n; i++)
    		{
    			cin >> node[i].x >> node[i].y;
    			if (node[i].x < min_x)
    			{
    				min_x = node[i].x;
    				pos_x = i;
    			}
    			else if (min_x == node[i].x&&node[i].y < node[pos_x].y)
    			{
    				pos_x = i;
    			}
    		}
    		orign = node[pos_x];
    		qsort(node + 1, n, sizeof(no), cmp);
    
    		int pc = 1;
    		conbag[1] = 1;
    		conbag[++pc] = 2;
    		conbag[0] = 2;
    
    		i = 3;
    		while (i <= n)
    		{
    			if (Across(node[conbag[pc - 1]], node[conbag[pc]], node[conbag[pc]], node[i]) >= 0)
    			{
    				conbag[++pc] = i++;
    				conbag[0]++;
    			}
    			else
    			{
    				pc--;
    				conbag[0]--;
    			}
    		}
    
    		if (n < 3 || conbag[0]<3)
    		{
    			cout << 0 << endl;
    		}
    		else
    		{
    			sum = 0;
    			for (i = 1; i + 1 <= conbag[0]; ++i)
    			{
    				sum += abs((node[conbag[i]].x * node[conbag[(i + 1)]].y - node[conbag[i]].y * node[conbag[(i + 1)]].x));
    			}
    			sum += abs((node[conbag[i]].x * node[1].y - node[conbag[i]].y * node[1].x));
    			sum = sum / 2.0;
    			cout << (int)(sum / 50) << endl;
    		}
    	}
    	return 0;
    }


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

  • 相关阅读:
    19. 星际争霸之php设计模式--迭代器模式
    18. 星际争霸之php设计模式--观察者模式
    17. 星际争霸之php设计模式--职责链模式
    16. 星际争霸之php设计模式--组合模式
    15. 星际争霸之php设计模式--策略模式
    14. 星际争霸之php设计模式--状态模式
    13. 星际争霸之php设计模式--正面模式
    12. 星际争霸之php设计模式--模板模式
    11. 星际争霸之php设计模式--备忘模式
    10. 星际争霸之php设计模式--原型模式
  • 原文地址:https://www.cnblogs.com/lightspeedsmallson/p/4928116.html
Copyright © 2011-2022 走看看