Codeforces Round苋340(Div.2)C.Watering Flowers暴力

2543 ワード

C.Watering Flowers
テーマ接続:
http://www.codeforces.com/contest/617/problem/C
Descriptionwl.1
A flower bed has many flowers and two fountains.
You can adjust the waterpresure and set any values r 1(r 1̵≧?0)and r 2(r 2???0)、giving the distancs aawhich the waterisisspread from the first thefirst andandandandandandand first andandandandandandandandand fofofofoconconconconconconconconconconconththththththththththththandandandandand fofoforerererererererererererererererererererererererererererered d d d.at.at.ch flower、the distance between the flower and the first fountain doesn't exceed r 1、or the distance to the second fountan doesn't exceed r 2.It's OK if some flowers ars ared by both fountains.
You need to decrease the amount of water you need、that is set such r 1 and r 2 that all the flowers are watered and the the r 12?+r 22 is minimum possible.Find this minimum value.
Input
The first line of the input contains integers n,x 1,y 1,x 2,y 2(1?≦?nn≦≦820?2000,?-?107?≤?X1)x 1,?X1,??ss s s s s s s s s s s s s s 1,_,_,_,and the second fountain.
Next follow n lineas.The i-th of these line s contains integers xi and yi(̵-?107?≦?xi,?yi≦̵107)—the coordination of the-th flower.
It is garanted that all n+̵2points in the input are distinct.
Output
Print the minimum possible value r 12 a+h 22.Note、that in this problem optimal answer is always integer.
Sample Input
2-1 0 5 3 0 2 2
Sample Output
6
ベント
題意
庭があります。噴水の蛇口が二つあります。庭の中のすべての花に水を注ぐ必要があります。
二つの噴水蛇口の噴水半径はそれぞれr 1である。r 2
r 1*r 1+r 2*r 2を最小にする必要があります。
いくらですか
クイズ:
その中の一次元を並べ替えて、残りの最大値を別の一次元で直接取ればいいです。
コード
#include<bits/stdc++.h>
using namespace std;
struct node
{
    long long x,y;
};

node p[2];
node point[2005];
long long pre[2005];
bool cmp(node a,node b)
{
    if(a.x==b.x)return a.y<b.y;
    return a.x<b.x;
}
int main()
{
    int n;scanf("%d",&n);
    for(int i=0;i<2;i++)
        scanf("%lld%lld",&p[i].x,&p[i].y);
    for(int i=1;i<=n;i++)
    {
        long long x,y;
        scanf("%lld%lld",&x,&y);
        point[i].x = (x-p[0].x)*(x-p[0].x)+(y-p[0].y)*(y-p[0].y);
        point[i].y = (y-p[1].y)*(y-p[1].y)+(x-p[1].x)*(x-p[1].x);
    }
    sort(point+1,point+1+n,cmp);
    for(int i=n;i>=1;i--)
        pre[i]=max(pre[i+1],point[i].y);
    long long ans = pre[1];
    for(int i=1;i<=n;i++)
        ans = min(ans,point[i].x+pre[i+1]);
    cout<<ans<<endl;
}