p1788.cpp

来自「大概POJ上50道比较难的题的代码」· C++ 代码 · 共 42 行

CPP
42
字号
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 100001;
struct point{
       int x,y;
};
point a[MAXN];
bool cmpx(point a,point b){
    return a.x < b.x || (a.x == b.x && a.y < b.y);
}
bool cmpy(point a,point b){
    return a.y < b.y || (a.y == b.y && a.x < b.x);
}
int main(){
    int p;
    while(cin >> p && p){
        int ans(0);
        for(int i = 0;i < p;++i) scanf("%d%d",&a[i].x,&a[i].y);
        sort(a,a+p,cmpx);
        int i;
        i = 1;
        while(i < p){
            while(a[i].x == a[i-1].x && i < p){
                if(i % 2) ans += abs(a[i].y - a[i-1].y);
                ++i;
            }
            ++i;
        }
        sort(a,a+p,cmpy);
        i = 1;
        while(i < p){
            while(a[i].y == a[i-1].y && i < p){
                if(i % 2) ans += abs(a[i].x - a[i-1].x);
                ++i;
            }
            ++i;
        }
        printf("The length of the fence will be %d units.\n",ans);
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?