Submission #164753

#TimeUsernameProblemLanguageResultExecution timeMemory
164753ho94949Naan (JOI19_naan)C++17
29 / 100
1934 ms12096 KiB
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using frac = pair<ll, ll>;
int N, L;
int V[2020][2020];
frac happy[2020];
frac cut[2020];
frac cut2[2020];
int P[2020];
frac X[2020];
bool used[2020];
frac now[2020];

ll gcdx(ll a, ll b)
{
    if(b==0) return a;
    return gcdx(b, a%b);
}

frac nor(frac t)
{
    ll a, b; tie(a, b) = t;
    ll g = gcdx(a, b);
    return frac(a/g, b/g);
}

frac operator +(frac a, frac b)
{
    ll x, y, z, w;
    tie(x, y) = a; tie(z, w) = b;
    return nor(frac(x*w+y*z, y*w));
}

frac operator -(frac a, frac b)
{
    ll x, y, z, w;
    tie(x, y) = a; tie(z, w) = b;
    return nor(frac(x*w-y*z, y*w));
}

bool operator < (frac a, frac b)
{
    ll x, y, z, w;
    tie(x, y) = a; tie(z, w) = b;
    return x*w < y*z;

}

int main()
{
    scanf("%d%d", &N, &L);
    for(int i=0; i<N; ++i)
    {
        happy[i].second = N;
        for(int j=0; j<L; ++j)
        {
            scanf("%d", &V[i][j]);
            happy[i].first += V[i][j];
        }
        happy[i] = nor(happy[i]);
        cut[i] = cut2[i] = frac(0, 1);
    }

    //cut: happyness now
    //cut2: happyness future

    int phase = 0;
    for(int i=0; i<N; ++i) now[i] = frac(0, 1);
    while(phase < N-1)
    {

        int nextint = L;
        for(int i=0; i<N; ++i)
            nextint = min(nextint, (int)(now[i].first / now[i].second + 1));

        frac minv = frac(0, 1);
        int mini = -1;
        for(int i=0; i<N; ++i)
        {
            if(used[i]) continue;
            frac diff = frac(nextint, 1) - now[i];
            frac h = diff; h.first *= V[i][nextint-1];
            h = nor(h);
            cut2[i] = cut[i] + h;
            //cout << "?" << (long long)h.first << " " << (long long)h.second <<endl;
            //cout << "!" << (long long)cut2[i].first << " " << (long long)cut2[i].second <<endl;

            if(!(cut2[i] < happy[i]))
            {
                frac np = (happy[i]-cut[i]); np.second *= V[i][nextint-1];
                np = np + now[i];
                if(mini == -1 || np < minv)
                {
                    mini = i;
                    minv = np;
                }
            }
        }
        if(mini != -1)
        {

            X[phase] = minv;
            P[phase] = mini;
            used[mini] = true;
            for(int i=0; i<N; ++i) cut[i] = frac(0, 1);

            int mseg = minv.first/minv.second;
//            cout << mseg <<endl;

            for(int i=0; i<N; ++i)
            {
                int base = N*V[i][mseg];
                ll nbj = (minv.first*base+minv.second-1)/minv.second;
                now[i] = nor(frac(nbj, base));
                //cout << "*" << (long long)nbj << " " << base <<endl;
            }
            ++phase;
        }
        else
        {
            for(int i=0; i<N; ++i) cut[i] = cut2[i];
            for(int i=0; i<N; ++i) now[i] = frac(nextint, 1);
        }
    }
    for(int i=0; i<N-1; ++i)
        printf("%lld %lld\n", (long long)X[i].first, (long long)X[i].second);
    for(int i=0; i<N-1; ++i)
        printf("%d ", P[i]+1);
    for(int i=0; i<N; ++i)
        if(!used[i])
            printf("%d\n", i+1);
    
    return 0;
}

Compilation message (stderr)

naan.cpp: In function 'int main()':
naan.cpp:52:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &N, &L);
     ~~~~~^~~~~~~~~~~~~~~~
naan.cpp:58:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d", &V[i][j]);
             ~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...