답안 #164752

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
164752 2019-11-23T07:38:37 Z ho94949 Naan (JOI19_naan) C++17
29 / 100
4000 ms 38672 KB
#include<bits/stdc++.h>
using namespace std;
using ll = __int128;
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

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]);
             ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 1 ms 376 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 3 ms 376 KB Output is correct
12 Correct 3 ms 376 KB Output is correct
13 Correct 3 ms 376 KB Output is correct
14 Correct 3 ms 376 KB Output is correct
15 Correct 3 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 4 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 3 ms 376 KB Output is correct
9 Correct 4 ms 376 KB Output is correct
10 Correct 4 ms 376 KB Output is correct
11 Correct 3 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 3 ms 376 KB Output is correct
14 Correct 4 ms 376 KB Output is correct
15 Correct 4 ms 376 KB Output is correct
16 Correct 4 ms 376 KB Output is correct
17 Correct 4 ms 376 KB Output is correct
18 Correct 5 ms 376 KB Output is correct
19 Correct 5 ms 376 KB Output is correct
20 Correct 4 ms 376 KB Output is correct
21 Correct 5 ms 376 KB Output is correct
22 Correct 5 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 4 ms 376 KB Output is correct
25 Correct 3 ms 376 KB Output is correct
26 Correct 3 ms 376 KB Output is correct
27 Correct 4 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 1 ms 376 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 3 ms 376 KB Output is correct
12 Correct 3 ms 376 KB Output is correct
13 Correct 3 ms 376 KB Output is correct
14 Correct 3 ms 376 KB Output is correct
15 Correct 3 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 3 ms 376 KB Output is correct
18 Correct 3 ms 376 KB Output is correct
19 Correct 4 ms 376 KB Output is correct
20 Correct 3 ms 376 KB Output is correct
21 Correct 3 ms 376 KB Output is correct
22 Correct 3 ms 376 KB Output is correct
23 Correct 3 ms 376 KB Output is correct
24 Correct 4 ms 376 KB Output is correct
25 Correct 4 ms 376 KB Output is correct
26 Correct 3 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 3 ms 376 KB Output is correct
29 Correct 4 ms 376 KB Output is correct
30 Correct 4 ms 376 KB Output is correct
31 Correct 4 ms 376 KB Output is correct
32 Correct 4 ms 376 KB Output is correct
33 Correct 5 ms 376 KB Output is correct
34 Correct 5 ms 376 KB Output is correct
35 Correct 4 ms 376 KB Output is correct
36 Correct 5 ms 376 KB Output is correct
37 Correct 5 ms 376 KB Output is correct
38 Correct 2 ms 376 KB Output is correct
39 Correct 4 ms 376 KB Output is correct
40 Correct 3 ms 376 KB Output is correct
41 Correct 3 ms 376 KB Output is correct
42 Correct 4 ms 376 KB Output is correct
43 Correct 532 ms 3804 KB Output is correct
44 Correct 1905 ms 21496 KB Output is correct
45 Correct 616 ms 13560 KB Output is correct
46 Correct 57 ms 2424 KB Output is correct
47 Correct 1038 ms 16736 KB Output is correct
48 Correct 1830 ms 13688 KB Output is correct
49 Correct 376 ms 7148 KB Output is correct
50 Correct 2158 ms 22012 KB Output is correct
51 Correct 672 ms 12664 KB Output is correct
52 Correct 1728 ms 21540 KB Output is correct
53 Correct 1621 ms 19064 KB Output is correct
54 Correct 4 ms 376 KB Output is correct
55 Correct 1661 ms 7396 KB Output is correct
56 Correct 1728 ms 15328 KB Output is correct
57 Correct 1630 ms 13424 KB Output is correct
58 Correct 2737 ms 18312 KB Output is correct
59 Correct 1594 ms 13812 KB Output is correct
60 Correct 3735 ms 38672 KB Output is correct
61 Correct 3729 ms 38500 KB Output is correct
62 Execution timed out 4006 ms 38640 KB Time limit exceeded
63 Halted 0 ms 0 KB -