Jizhong 10 T1 2313. Dynamic cactus

Jizhong 10, 2313. Dynamic cactus 

(File IO): input:dinosaur.in output:dinosaur.out

Time limit:  1500 MS space constraints:  524288 KB specific restrictions  

Goto ProblemSet

Title Description

Sand Sculpture Anyway, I have not played the game ...... ......

Entry

Export

Sample input

Data range limit

Solution

I think the exam is right

Why is not divided?

First look at the cactus Several situations

situation1

Comparison of two separate cactus ......

According to such a small dinosaur go (jump), and no special operation. This time, the maximum height is two cacti highest level.

situation2

Two more recent Cactus rely on ......

A first look, as small as possible in order height, of course, choose grazing jumped

 

But the dinosaur can only fall danced ...... If it dance through the first on landing, take-off again, will be installed on the second cactus!

So it only once to skip two cactus ......

Seeking the highest point:

Setting a first height of a, a second distance between the height, b, of two C;

punctuation

solution:

Since △ ABC is an isosceles right triangle

Therefore AD = DC = a

Similarly available:

EG=FG=b

Therefore AF = AD + DG + GF = a + b + c

Extension AB, EF, intersect at point H

Also isosceles right triangle △ AHF

Therefore AH = HF = AF / 2 = (a + b + c) / 2

So we can put two cactus into one of the higher

Code

#include<iostream>//不想OI一场空,千万别用万能头
#include<algorithm>//快排sort()
#include<cstdio>//能不用cin就不用
#include<cstring>
#include<map>
#include<vector>
#define IL inline
using namespace std;
IL void fin(){freopen("dinosaur.in","r",stdin);}
IL void fout(){freopen("dinosaur.out","w",stdout);}
IL void fio()
{
    fin();
    fout();
}

struct node{
    double p;
    double h;
}din[100000];
bool cmp(node ta,node tb)
{
    return ta.p<tb.p;
}
int n,tp,th,maxn;
double ans=-1;
int main()
{
//    fio();
    cin>>n;
    for(int i=0;i<n;i++)
    {
        scanf("%d%d",&tp,&th);
        din[i].p=tp;
        din[i].h=th;
        ans=max(ans,(double)din[i].h);
        if(din[i].p-din[i].h<0){
            cout<<"-1\n";
            return 0;
        }
    }
    sort(din,din+n,cmp);
    int a,b,c;
    for(int i=0;i<n-1;i++)
    {
        c=din[i+1].p+din[i].p;
        a=din[i].h;
        b=din[i+1].h; 
        if(c>=a+b)
        {
            ans=max(ans,(double)max(a,b));
        }
        else
        {
            ans=max(ans,(a+b+c)/2.0);
            din[i+1].p=(a+din[i].p+din[i+1].p-b)/2.0;
            din[i+1].h=(a+b+c)/2.0;
        } 
    }
    printf("%.1lf",ans);
    return 0;
}

 

Code std

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
struct cly
{
    int longn,left,right;
}a[300100];
double ans;
int n,p[300100],h[300100],l,r,i;
bool cmd(cly x,cly y)
{
    return x.left<y.left;
}
int main()
{
//    freopen("dinosaur.in","r",stdin);
//    freopen("dinosaur.out","w",stdout);
    cin>>n;
    for(i=1;i<=n;i++)
    {
        scanf("%d%d",p+i,h+i);
        a[i]=(cly){h[i],p[i]-h[i],p[i]+h[i]};
    }
    sort(a+1,a+1+n,cmd);
    if(a[1].left<0)
    {
        cout<<"-1";
        return 0;
    }
    l=a[1].left;
    r=a[1].right;
    i=1;
    while(i<n)
    {
        if(r<=a[i+1].left)
        {
            ans=max(ans,1.0*(r-l)/2);
            i++;
            l=a[i].left;
            r=a[i].right;
        }
        else if(r>=a[i+1].right)
        {
            i++;
        }
        else
        {
            i++;
            r=a[i].right;
        }
    }
    ans=max(ans,1.0*(r-l)/2);
    printf("%.1lf",ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/send-off-a-friend/p/11359021.html