P1158 missile interceptors

P1158 missile interceptors

answer

This is not a simple greed, he will have to enumerate

We must first calculate the distances of the missile interceptor system from the two distances

then, assuming that all of them intercepted by an interceptor system, and then enumerate one by one missile

If another interception system closer to it we obviously want to update

 

 r1 denotes a radius of 1 square interception system

 r2 denotes a radius of 2 square interception system

 

We assume that the first interceptor to intercept them all

According to the distance and the missile interceptor system of small to large order, then descending to enumerate

Such farthest distance missile interception system 1 was placed in the back, but the distance from the uncertain interception system 2

(In fact, this is equivalent to sort down and maintains a prefix, a [i] .chang1 front r1 represents the minimum required interceptor missile i)

It must be equal to r1 start a [n] .chang1

Descending enumeration:

Suppose now that i have missiles, it intercepted interception system 2 (r2 premise is beyond the scope of interception), then before i missile interception system is necessarily 2 interceptions (otherwise, would not update ah), then the rest missile interception system 1 is intercepted, the calculated results of the measurement, compared with the previous results, solving a minimum

Finally, compare and update ans r1, a single interceptor to intercept or two better together better 

 

 

 

 

Code

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
#include<bits/stdc++.h>

using namespace std;

int x1, yi, x2, y2, x, y, n;
int r1, r2, years = 1e9 + 6 ;

struct node
{
    int chang1,chang2;
}a[100001];

bool cmp(node x,node y)
{
    return x.chang1 <y.chang1 ;
}

int main ()
{
    scanf("%d%d%d%d%d",&x1,&yi,&x2,&y2,&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&x,&y);
        a[i].chang1 =(x-x1)*(x-x1)+(y-yi)*(y-yi);
        a[i].chang2 =(x-x2)*(x-x2)+(y-y2)*(y-y2);
    }
    
    sort(a+1,a+n+1,cmp);
    
    r1=a[n].chang1 ;
    
    for(int i=n-1;i>=1;i--)
    {
        if(r2<a[i+1].chang2 ) r2=a[i+1].chang2 ;
        years = min (years, a [i] .chang1 + r2);
    }
    
    printf("%d",min(ans,r1));
    
}

 

Guess you like

Origin www.cnblogs.com/xiaoyezi-wink/p/11028815.html