German Collegiate Programming Contest 2017 G. Water Testing

2020-08-12

You just bought a large piece of agricultural land, but you noticed that – according to regulations– you have to test the ground water at specific points on your property once a year. Luckilythe description of these points is rather simple. The whole country has been mapped using aCartesian Coordinate System (where (0,0) is the location of the Greenwich Observatory). Thecorners of all land properties are located at integer coordinates according to this coordinatesystem. Test points for ground water have to be erected on every point inside a property whosecoordinates are integers.

输入

The input consists of:

  • one line with a single integer n(3≤n≤100000), the number of corner points of yourproperty;

  • n lines each containing two integers x and y(−106≤x,y≤106), the coordinates ofeach corner.

The corners are ordered as they appear on the border of your property and the polygon describedby the points does not intersect itself.

输出

The number of points with integer coordinates that are strictly inside your property.

样例输入

4
0 0
0 10
10 10
10 0

样例输出

81

分析

皮克定理: S=a+b/2-1 S: 面积,a: 内部点个数,b: 边缘点个数

多边形面积可以通过向量叉乘算得,注意凹凸多边形。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <vector>
#include <string>

typedef long long LL;

using namespace std;

long long sidepoint (LL x1, LL y1, LL x2, LL y2)
{
    return __gcd(abs(x1 - x2), abs(y1 - y2));
}

int main()
{
    //S=a+b/2-1
    double S = 0;
    long long x1, y1, x2, y2, x3, y3, xt, yt;
    int t;
    scanf("%d", &t);
    scanf("%lld%lld", &x1, &y1);
    scanf("%lld%lld", &x2, &y2);
    long long sp = sidepoint(x1, y1, x2, y2);
    t -= 2;
    while (t--) {
        scanf("%lld%lld", &x3, &y3);
        sp += sidepoint(x2, y2, x3, y3);
        S += double((x2-x1)*(y3-y1) - (y2-y1)*(x3-x1)) / 2;
        x2 = x3;
        y2 = y3;
    }
    sp += sidepoint(x2, y2, x1, y1);
    printf("%.0lf\n", fabs(S) + 1.0 - sp*1.0/2);
    //printf("%lf %lld %d ", S, sp, t);
    
    return 0;
}

by SDUST weilinfox

posted @ 2020-08-12 17:14  桜風の狐  阅读(121)  评论(0编辑  收藏  举报