仍然是凸包,不过这题数据很强,发现我第一次写的那个凸包是有问题,只是那题数据比较弱没有WA,关键在于极角排序的时候,叉积相等时需要再比较两点和原点的距离,距原点较近的排前面,这样能保证距离原点较远的那个点成为凸包的点。。这也是看了别人的文章才发现的,不然自己调的估计DEBUG死也不知道原因。。
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
namespace
{
pair<int, int> p;
inline int cross(const pair<int, int> &p1, const pair<int, int> &p2)
{
return p1.first * p2.second - p1.second * p2.first;
}
inline pair<int, int> vect(const pair<int, int> &p1,
const pair<int, int> &p2)
{
return make_pair(p2.first - p1.first, p2.second - p1.second);
}
bool cmp(const pair<int, int> &p1, const pair<int, int> &p2)
{
int cp = cross(vect(p, p2), vect(p, p1));
if (cp == 0)
return (p1.first - p.first) * (p1.first - p.first)
+ (p1.second - p.second) * (p1.second - p.second)
< (p2.first - p.first) * (p2.first - p.first)
+ (p2.second - p.second) * (p2.second - p.second);
return cp < 0;
}
bool point_cmp(const pair<int, int> &p1, const pair<int, int> &p2)
{
return p1.second != p2.second ?
p1.second < p2.second : p1.first < p2.first;
}
double dist(const pair<int, int> &p1, const pair<int, int> &p2)
{
return sqrt(
(p1.first - p2.first) * (p1.first - p2.first)
+ (p1.second - p2.second) * (p1.second - p2.second));
}
}
int main()
{
int T, N, L, x, y;
vector<pair<int, int> > V;
vector<pair<int, int> > Q;
scanf("%d", &T);
for (int t = 0; t < T; t++)
{
if (t)
puts("");
scanf("%d %d", &N, &L);
V.clear();
Q.clear();
while (N--)
{
scanf("%d %d", &x, &y);
V.push_back(make_pair(x, y));
}
vector<pair<int, int> >::iterator it = min_element(V.begin(), V.end(),
point_cmp);
p = *it;
V.erase(it);
sort(V.begin(), V.end(), cmp);
Q.push_back(p);
Q.push_back(V[0]);
Q.push_back(V[1]);
for (size_t i = 2; i < V.size(); i++)
{
while (Q.size() > 1
&& cross(vect(Q[Q.size() - 2], Q.back()),
vect(Q[Q.size() - 2], V[i])) <= 0)
Q.pop_back();
Q.push_back(V[i]);
}
double sum = dist(Q.front(), Q.back());
for (size_t i = 1; i < Q.size(); i++)
sum += dist(Q[i - 1], Q[i]);
sum += 2 * M_PI * L;
printf("%d\n", int(sum + 0.5));
}
return 0;
}