今天把自己想的APIO 2012 Guard的算法实现了一下,果然WA了……我把问题想的太简单了啊……
经典贪心问题?听都没听说过……
今天思考人生,想自己的弱项是什么,把贪心列了进去。果然TAT
然后在黄学长的博客里,贪心分类下最旧一篇文章,看到此题。写完之后发现跟贪心没什么关系。
问题等价于从n个有序数对(a, b)里选一些数出来扔进A集合或B集合,a只能扔进A,b只能扔进B,要求每对数至少选一个,并且最小化max{Ai}+max{Bi}。
如果我们把某个数x扔进了A,那么所有a坐标小于x的数对就不用考虑了。令x=max{Ai},枚举,我们就得到了本题的答案。
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAX_N = 100000, inf = 1<<30;
int m[MAX_N+1];
struct Pair {
int a, b;
bool operator<(const Pair& rhs) const
{
return a < rhs.a;
}
} P[MAX_N];
inline int Q(int x1, int y1, int x2, int y2)
{
return (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);
}
int main()
{
int n, x1, x2, y1, y2;
scanf("%d %d %d %d %d", &x1, &y1, &x2, &y2, &n);
for (int i = 0; i < n; ++i) {
int x, y;
scanf("%d %d", &x, &y);
P[i] = (Pair){Q(x1, y1, x, y), Q(x2, y2, x, y)};
}
sort(P, P+n);
for (int i = n-1; i >= 0; --i)
m[i] = max(P[i].b, m[i+1]);
int ans = inf;
for (int i = 0; i < n; ++i)
ans = min(ans, P[i].a + m[i+1]);
printf("%d\n", ans);
return 0;
}
今天跑去跑操集合的路上摔了一跤,好疼……得到教训:做事不可急躁。