Missile intercept (not the longest sequence does not rise)

A seemingly violent enumerate violence is actually enumerate (add a little greedy) of title

topic:

After  11 1 year keeping a low profile, a country developed a new missile interception system, with all its distance does not exceed its working radius of the missile can be successfully intercept it. When the working radius of  0 0, then it is possible to exactly the same position to intercept the missile. However, the missile interception system also has the drawback: each system can only be set once a day working radius. The cost of the day use, is the square of the radius of the work and all systems.

One day, the radar picked up incoming enemy missiles. Because the system is still in the experimental stage, so only the two systems to work. If the current requirement is to intercept all missiles, please use to calculate the minimum cost of that day.

Maximum data range:

To 100% of the data, 1≤N≤100000, and the absolute coordinates of all components does not exceed 1000

 

 

First of all, to see the title and scope of the data shows that can be saved directly coordinate differences squared and, (that is, the square of the distance between two points formula), not explode, but just save distance and two interception system, without having to save the coordinates.

Second, consider the algorithmic problems, given the sheer distance of a point compared to the distance between two points, that is, who consider this missile from recent use which, then, does not guarantee the correctness

Perhaps because a missile from point 2 close, but from the point of interception to include the missile may have 1, 2 interceptions do not need to update point range

Consider the following ideas,

When sorting according to the distance from the first set of interception system, to ensure that the range of the first system to intercept the k-th point distance, to intercept k points to the front, because the first k points to shorter distances

Then the enumeration process in the above process from the second interception of the point to point, taking the maximum, otherwise it is impossible to intercept all points

Code:

 #include<bits/stdc++.h>
using namespace std;
int n;
struct node{
    int df,ds;
    node(){
        df=0;
        ds=0;
    }
}nd[100005];
struct al{
    int x,y;
} As SC;
inline bool cmp(const node &a,const node &b){
    return a.df<b.df;
}
inline int dis(bool num,const int &x,const int &y){
    if(!num)
        return (x-fi.x)*(x-fi.x)+(y-fi.y)*(y-fi.y);
    if(num)
        return (x-sc.x)*(x-sc.x)+(y-sc.y)*(y-sc.y);
}
int main () {
    scanf("%d%d%d%d",&fi.x,&fi.y,&sc.x,&sc.y);
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int a,b;
        scanf("%d%d",&a,&b);
        nd[i].df=dis(0,a,b);
        nd[i].ds=dis(1,a,b);
    }
    sort(nd+1,nd+1+n,cmp);
    int i=n;
    int ans=2147483647;int maxn=0;
    while(i>=1){
        maxn=max(maxn,nd[i+1].ds);
        years = min (n [i] + .df maxn, years);
        i--;
    }printf("%d\n",ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/648-233/p/11030952.html