Submission #1867578


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

using vl = vector<ll>;

vl f(const vl &v)
{
    int n = v.size();
    vl r(v);

    r[n-1] -= n;
    rep(i,n-1) ++r[i];
    sort(all(r));
    return r;
}

bool valid(const vl &v)
{
    int n = v.size();
    return v[n-1]<n;
}

void test(const vl &V)
{
    vl v = V;

    int n = v.size();
    int ct = 0;
    while(!valid(v))
    {
        v = f(v);
        int D = v[n-1]-v[0];
        printf("%3d: diff: %3d ", ++ct, D);
        // if(D<n) break;
        dbg(v);
    }
}

ll solve(const vl &V)
{
    vl v = V;
    int n = v.size();

    ll ret = 0;

    for(int i=n-1; i>0; --i)
    {
        if(valid(v)) break;

        ll step = n-i;
        ll dec = i+1;

        ll x = (v[i]-v[i-1])/(step+dec);

        ret += step*x;
        rep(j,i) v[j] += x*step;
        for(int j=i; j<n; ++j) v[j] -= x*dec;
        sort(all(v));
    }

    if(!valid(v))
    {
        while(!valid(v) && v[n-1]-v[0]>=2*n)
        {
            ++ret;
            v = f(v);
        }

        ll LIM = 2*n;
        ll y = v[n-1]-LIM;
        if(y>0)
        {
            ret += y*n;
            rep(i,n) v[i] -= y;
        }

        while(!valid(v))
        {
            ++ret;
            v = f(v);
        }
    }

    return ret;
}

int main()
{
    int n;
    cin >>n;
    vector<ll> a(n);
    rep(i,n) cin >>a[i];

    sort(all(a));

    // test(a);
    cout << solve(a) << endl;
    return 0;
}

Submission Info

Submission Time
Task E - Decrease (Judge ver.)
User imulan
Language C++14 (GCC 5.4.1)
Score 600
Code Size 1983 Byte
Status AC
Exec Time 4 ms
Memory 256 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 5
AC × 35
Set Name Test Cases
Sample example0, example1, example2, example3, example4
All example0, example1, example2, example3, example4, maxrand0, maxrand1, maxrand2, maxrand3, maxrand4, maxrand5, maxrand6, maxrand7, maxrand8, maxrand9, rand0, rand1, rand2, rand3, rand4, rand5, rand6, rand7, rand8, rand9, small0, small1, small2, small3, small4, small5, small6, small7, small8, small9
Case Name Status Exec Time Memory
example0 AC 1 ms 256 KB
example1 AC 1 ms 256 KB
example2 AC 1 ms 256 KB
example3 AC 1 ms 256 KB
example4 AC 1 ms 256 KB
maxrand0 AC 1 ms 256 KB
maxrand1 AC 4 ms 256 KB
maxrand2 AC 1 ms 256 KB
maxrand3 AC 4 ms 256 KB
maxrand4 AC 1 ms 256 KB
maxrand5 AC 4 ms 256 KB
maxrand6 AC 1 ms 256 KB
maxrand7 AC 4 ms 256 KB
maxrand8 AC 1 ms 256 KB
maxrand9 AC 3 ms 256 KB
rand0 AC 1 ms 256 KB
rand1 AC 1 ms 256 KB
rand2 AC 2 ms 256 KB
rand3 AC 2 ms 256 KB
rand4 AC 1 ms 256 KB
rand5 AC 1 ms 256 KB
rand6 AC 1 ms 256 KB
rand7 AC 2 ms 256 KB
rand8 AC 1 ms 256 KB
rand9 AC 1 ms 256 KB
small0 AC 1 ms 256 KB
small1 AC 1 ms 256 KB
small2 AC 1 ms 256 KB
small3 AC 1 ms 256 KB
small4 AC 2 ms 256 KB
small5 AC 2 ms 256 KB
small6 AC 2 ms 256 KB
small7 AC 1 ms 256 KB
small8 AC 2 ms 256 KB
small9 AC 2 ms 256 KB