您的位置:首页 > 其它

HDU 1348 && POJ 1113 Wall(凸包)

2016-07-27 14:34 405 查看

题目链接:

HDU :http://acm.hdu.edu.cn/showproblem.php?pid=1348

POJ :http://poj.org/problem?id=1113

题解:

求出凸包后,算出凸包的周长,再加上以l为半径的圆的周长就好了,最后输出要一个整数并且四舍五入,HDU上还会卡一手输出,最后一个t不要再输出一行空格。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

const int maxn = 1050;
const double PI = acos(-1.0);

struct node
{
int x, y;
}e[maxn], tubao[maxn];

int cmp(const node& a , const node& b)
{
if(a.x == b.x)
return a.y < b.y;
return a.x < b.x;
}

int cross(node a, node b, node c)
{
return (a.x - b.x) * (c.y - b.y) - (a.y - b.y) * (c.x - b.x);
}

int convex(int n)
{
sort(e, e+n, cmp);
int m = 0;
for(int i = 0; i < n; i++)
{
while(m > 1 && cross(tubao[m-1], tubao[m-2], e[i]) <= 0)
m--;
tubao[m++] = e[i];
}
int k = m;
for(int i = n-2; i >= 0; i--)
{
while(m > k && cross(tubao[m-1], tubao[m-2], e[i]) <= 0)
m--;
tubao[m++] = e[i];
}
if(n > 1)m--;
return m;
}

double dis(const node &a, const node &b)//一定要double
{
return sqrt(1.0 * (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

int main()
{
int t,m,l;
cin >> t;
while(t--)
{
scanf("%d%d", &m, &l);
for(int i = 0; i < m; i++)
scanf("%d%d", &e[i].x, &e[i].y);
int num = convex(m);
double ans = 0;
for(int i = 1; i < num; i++)
{
ans += dis(tubao[i], tubao[i-1]);
}
ans += dis(tubao[num-1], tubao[0]);
ans += PI * l * 2;
printf("%.0f\n", ans);//这样写才不会WA
if(t)
cout << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm