Cod sursa(job #1943568)

Utilizator Bodo171Bogdan Pop Bodo171 Data 28 martie 2017 17:55:46
Problema Euro Scor 25
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <iostream>
#include <fstream>
using namespace std;
const int nmax=34567+5;
struct segm
{
    long long a,b;
}st[nmax],dr;
const long double eps=0.0000000001;
long double a1,a2,b1,b2;
long long sum[nmax],best[nmax];
long long T,ind;
int n,i,pu,u,poz,ret,x;
long double intersect(segm unu,segm doi)
{
    a1=unu.a;a2=doi.a;b1=unu.b;b2=doi.b;
    return ((b2-b1)/(a1-a2));
}
int cb(long double val)
{
    int ret=0;
    if(u==1) return 1;
    for(pu=17;pu>=0;pu--)
        if(ret+(1<<pu)<u&&intersect(st[ret+(1<<pu)],st[ret+(1<<pu)+1])+eps<val)
           ret+=(1<<pu);
    ret++;
    return ret;
}
int main()
{
    ifstream f("euro.in");
    ofstream g("euro.out");
    f>>n>>T;
    for(i=1;i<=n;i++)
    {
        f>>x;
        sum[i]=sum[i-1]+x;
    }
    for(i=n;i>=1;i--)
    {
        ind=i;
        dr.a=-ind;
        dr.b=1LL*(1LL*sum[i]*ind+best[i+1]-T);
        while(u>=1&&intersect(st[u-1],st[u])>intersect(st[u-1],dr))
            u--;
        u++;
        st[u]=dr;
        poz=cb(sum[i-1]);
        best[i]=1LL*(1LL*sum[i-1]*st[poz].a+st[poz].b);
        if(poz>1&&best[i]<1LL*(1LL*sum[i-1]*st[poz].a+st[poz].b)) best[i]=1LL*(1LL*sum[i-1]*st[poz-1].a+st[poz].b);;
        if(poz<u&&best[i]<1LL*(1LL*sum[i-1]*st[poz+1].a+st[poz].b)) best[i]=1LL*(1LL*sum[i-1]*st[poz+1].a+st[poz].b);
    }
    g<<best[1];
    return 0;
}