Submission #168145

# Submission time Handle Problem Language Result Execution time Memory
168145 2019-12-11T15:19:43 Z rzbt Krov (COCI17_krov) C++14
112 / 140
1500 ms 19320 KB
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define MAXN 100005
typedef long long ll;


using namespace std;

ll n;
ll niz[MAXN];
ll bitl[MAXN],bitd[MAXN],imal[MAXN],imad[MAXN];
ll slikal[MAXN],slikad[MAXN];
set<pair<ll,ll> > tvL,tvD;
ll zbirL,zbirD;

void dodaj(ll p,ll x,ll *bit){
    for(;p<MAXN;p+=(-p)&p)
        bit[p]+=x;
}
ll dobij(ll p,ll *bit){
    ll z=0;
    for(;p>0;p-=(-p)&p)
        z+=bit[p];
    return z;
}

ll resi(ll h,ll krov ){
    ll vl=h-krov;
    ll vd=h+krov;

    auto pokl=tvL.upper_bound(mp(vl,1e9+7));
    ll pl;
    if(pokl==tvL.end())pl=n;
    else pl=slikal[pokl->second]-1;

    auto pokd=tvD.upper_bound(mp(vd,1e9+7));
    ll pd;
    if(pokd==tvD.end())pd=n;
    else pd=slikad[pokd->second]-1;


    ll prel=vl*dobij(pl,imal)-dobij(pl,bitl);
    ll poslel=(dobij(n,bitl)-dobij(pl,bitl))-vl*(dobij(n,imal)-dobij(pl,imal));

    ll pred=vd*dobij(pd,imad)-dobij(pd,bitd);
    ll posled=(dobij(n,bitd)-dobij(pd,bitd))-vd*(dobij(n,imad)-dobij(pd,imad));

    //printf("  %lld %lld   %lld %lld     %lld %lld     %lld %lld     %lld %lld\n",krov,h,vl,vd,prel,poslel,pred,posled,pl,pd);

    return prel+poslel+pred+posled+(h-niz[krov]>0?h-niz[krov]:-h+niz[krov]);

}


ll ternarna(ll l,ll d,ll krov,ll sol){
    if(d-l<=6){
        for(ll i=l;i<=d;i++){
            sol=min(sol,resi(i,krov));
        }
        return sol;
    }
    ll mid1=(l+l+d)/3;
    ll mid2=(l+d+d)/3;
    ll v1=resi(mid1,krov);
    ll v2=resi(mid2,krov);
    if(v1<v2)return ternarna(l,mid2,krov,min(v1,sol));
    else return ternarna(mid1,d,krov,min(v2,sol));

}

ll res=LLONG_MAX;

int main()///PROMENI U LL
{

    scanf("%lld", &n);
    for(ll i=1;i<=n;i++)
        scanf("%lld",niz+i);
    for(ll i=1;i<=n;i++){
        tvL.insert(mp(niz[i]-i,i));
    }
    ll brojac=1;
    for(auto x:tvL){
        slikal[x.S]=brojac;
        brojac++;
    }
    for(ll i=1;i<=n;i++){
        tvD.insert(mp(niz[i]+i,i));
    }
    brojac=1;
    for(auto x:tvD){
        slikad[x.S]=brojac;
        brojac++;
    }
    for(auto x:tvD)
        dodaj(slikad[x.S],x.F,bitd);
    for(auto x:tvD)
        dodaj(slikad[x.S],1,imad);

    for(ll i=1;i<=n;i++){
        dodaj(slikad[i],-niz[i]-i,bitd);
        dodaj(slikad[i],-1,imad);
        res=min(res,ternarna(max(i,n-i+1),2e9,i,LLONG_MAX));
        dodaj(slikal[i],niz[i]-i,bitl);
        dodaj(slikal[i],1,imal);
    }
    printf("%lld",res);

    return 0;
}

Compilation message

krov.cpp: In function 'int main()':
krov.cpp:80:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%lld", &n);
     ~~~~~^~~~~~~~~~~~
krov.cpp:82:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%lld",niz+i);
         ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 14 ms 632 KB Output is correct
2 Correct 14 ms 636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 632 KB Output is correct
2 Correct 13 ms 632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 760 KB Output is correct
2 Correct 21 ms 760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 888 KB Output is correct
2 Correct 35 ms 888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 1016 KB Output is correct
2 Correct 36 ms 888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 67 ms 1432 KB Output is correct
2 Correct 77 ms 1556 KB Output is correct
3 Correct 35 ms 988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 498 ms 5144 KB Output is correct
2 Correct 619 ms 5376 KB Output is correct
3 Correct 517 ms 5368 KB Output is correct
4 Correct 685 ms 6276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 934 ms 7836 KB Output is correct
2 Correct 1064 ms 8980 KB Output is correct
3 Correct 698 ms 7956 KB Output is correct
4 Correct 747 ms 7696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1580 ms 13680 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1580 ms 19320 KB Time limit exceeded
2 Halted 0 ms 0 KB -