POJ1788, Building a New Depot
链接:http://poj.org/problem?id=1788
“The posts are only at points where the fence changes the direction”,也就是说每个post都是拐角处。
先按X为第一关键字 Y为第二关键字排序 2n,2n-1两个相邻的y之差就是坚着的栅栏;横栅栏同理。。遍历一下加起来就ok了。
wa两次,第一次在计算长度时i每次增加1而不是增加2,第二次忘记初始化vector。。。还是不熟悉stl用法啊
Memory: 260K Time: 16MS
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef struct
{
int x, y;
}point;
bool cmpX(point elem1, point elem2)
{
if (elem1.x == elem2.x)
return elem1.y < elem2.y;
return elem1.x < elem2.x;
}
bool cmpY(point elem1, point elem2)
{
if (elem1.y == elem2.y)
return elem1.x < elem2.x;
return elem1.y < elem2.y;
}
int main(void)
{
int num;
int len;
point tmp;
vector<point> post;
while (cin >> num && num)
{
len = 0;
post.clear();
for (int i = 0; i < num; ++i)
{
cin >> tmp.x >> tmp.y;
post.push_back(tmp);
}
sort(post.begin(), post.end(), cmpX);
for (int i = 0; i < post.size() - 1; i += 2)
{
if (post[i].x == post[i + 1].x)
len += (post[i + 1].y - post[i].y);
}
sort(post.begin(), post.end(), cmpY);
for (int i = 0; i < post.size() - 1; i += 2)
{
if (post[i].y == post[i + 1].y)
len += (post[i + 1].x - post[i].x);
}
cout << "The length of the fence will be " << len << " units." << endl;
}
return 0;
}
浙公网安备 33010602011771号