Submission #185768

# Submission time Handle Problem Language Result Execution time Memory
185768 2020-01-12T04:18:43 Z qkxwsm JOIRIS (JOI16_joiris) C++14
100 / 100
9 ms 504 KB
#include <bits/stdc++.h>

using namespace std;

template<class T, class U>
void ckmin(T &a, U b)
{
    if (a > b) a = b;
}

template<class T, class U>
void ckmax(T &a, U b)
{
    if (a < b) a = b;
}

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define MAXN 51

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

int N, K;
int arr[MAXN];
int sum[MAXN];
vpi ans;

void solve()
{
    // ////cerr << "PART 1\n";
    // FOR(i, 0, N)
    // {
    //     ////cerr << arr[i] << ' ';
    // }
    // ////cerr << endl;
    FOR(j, 1, N)
    {
        while(arr[j] < arr[j - 1])
        {
            ans.PB({0, j});
            arr[j] += K;
        }
    }
    int sb[K];
    FOR(i, 0, K) sb[i] = 0;
    int d;
    FOR(i, K, N)
    {
        d = arr[i] - arr[i - 1];
        sb[i % K] += d;
        while(d--)
        {
            for (int j = i - K; j >= 0; j -= K)
            {
                ans.PB({1, j});
                FOR(k, j, j + K) arr[k]++;
            }
        }
    }
    //cerr << "PART 1\n";
    FOR(i, 0, N)
    {
        //cerr << arr[i] << ' ';
    }
    //cerr << endl;
    // FOR(i, 0, K) //cerr << sb[i] << endl;
    d = arr[N - 1];
    FOR(i, 0, K)
    {
        FOR(j, 0, N)
        {
            arr[j] -= sb[i];
        }
        //cerr << "PART 2\n";
        FOR(i, 0, N)
        {
            //cerr << arr[i] << ' ';
        }
        //cerr << endl;
        sb[i] = 0;
        // d = arr[K - 1] - arr[i];
        // FOR(j, i + 1, K) d += sb[j];
        if (i == K - 1) break;
        FOR(j, 0, (d + K - 1) / K)
        {
            ans.PB({0, i});
            arr[i] += K;
        }
    }
    FOR(i, 0, N)
    {
        arr[i] -= arr[N - 1];
    }
    // //cerr << "PART 3\n";
    // FOR(i, 0, N)
    // {
    //     //cerr << arr[i] << ' ';
    // }
    // //cerr << endl;
    d = 0;
    FOR(i, N % K, K) ckmax(d, arr[i]);
    FOR(i, N % K, K)
    {
        while(arr[i] < d)
        {
            ans.PB({0, i});
            arr[i] += K;
        }
    }
    FOR(i, 0, N % K) ckmax(d, arr[i]);
    FOR(i, 0, N % K)
    {
        while(arr[i] < d)
        {
            ans.PB({0, i});
            arr[i] += K;
        }
    }
    d = arr[K - 1];
    FOR(i, K, N)
    {
        FOR(j, 0, d / K)
        {
            ans.PB({0, i});
            arr[i] += K;
        }
    }
    FOR(i, 0, N)
    {
        arr[i] -= d;
    }
    // cerr << "PART 4\n";
    // FOR(i, 0, N)
    // {
    //     cerr << arr[i] << ' ';
    // }
    // cerr << endl;
    if (N % K)
    {
        d = arr[0];
        FOR(i, 0, d)
        {
            for (int j = N % K; j < N; j += K)
            {
                ans.PB({1, j});
            }
            FOR(j, 0, N % K)
            {
                arr[j]--;
            }
        }
    }
    // else
    // {
    //     d = arr[0];
    //     FOR(i, 0, N)
    //     {
    //         while(arr[i] < d)
    //         {
    //             ans.PB({0, i});
    //             arr[i] += K;
    //         }
    //     }
    //     FOR(i, 0, N)
    //     {
    //         arr[i] -= d;
    //     }
    // }
    FOR(i, 0, N)
    {
        assert(arr[i] == 0);
    }
    return;
}

int32_t main()
{
    cout << fixed << setprecision(12);
    ////cerr << fixed << setprecision(4);
    ios_base::sync_with_stdio(false); cin.tie(0);
    cin >> N >> K;
    FOR(i, 0, N)
    {
        cin >> arr[i];
        sum[i % K] += arr[i];
    }
    if (K == 1)
    {
        cout << 50 * N - sum[0] << '\n';
        FOR(i, 0, N)
        {
            FOR(j, 0, 50 - arr[i])
            {
                cout << 1 << ' ' << i + 1 << '\n';
            }
        }
        return 0;
    }
    FOR(i, 0, N % K)
    {
        if ((sum[i] - sum[0]) % K)
        {
            cout << "-1\n";
            return 0;
        }
    }
    FOR(i, N % K, K)
    {
        if ((sum[i] - sum[K - 1]) % K)
        {
            cout << "-1\n";
            return 0;
        }
    }
    //it's possible
    solve();
    assert(SZ(ans) <= 10000);
    cout << SZ(ans) << '\n';
    for (pii p : ans)
    {
        cout << p.fi + 1 << ' ' << p.se + 1 << '\n';
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 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 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 296 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 348 KB Output is correct
14 Correct 2 ms 420 KB Output is correct
15 Correct 3 ms 380 KB Output is correct
16 Correct 3 ms 380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 424 KB Output is correct
7 Correct 2 ms 252 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 412 KB Output is correct
13 Correct 2 ms 380 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 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 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 296 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 348 KB Output is correct
14 Correct 2 ms 420 KB Output is correct
15 Correct 3 ms 380 KB Output is correct
16 Correct 3 ms 380 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 380 KB Output is correct
20 Correct 2 ms 252 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 380 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 3 ms 376 KB Output is correct
28 Correct 0 ms 376 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 2 ms 376 KB Output is correct
31 Correct 6 ms 504 KB Output is correct
32 Correct 2 ms 376 KB Output is correct
33 Correct 2 ms 376 KB Output is correct
34 Correct 2 ms 376 KB Output is correct
35 Correct 2 ms 376 KB Output is correct
36 Correct 2 ms 376 KB Output is correct
37 Correct 2 ms 376 KB Output is correct
38 Correct 9 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 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 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 296 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 348 KB Output is correct
14 Correct 2 ms 420 KB Output is correct
15 Correct 3 ms 380 KB Output is correct
16 Correct 3 ms 380 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 348 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 424 KB Output is correct
23 Correct 2 ms 252 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 2 ms 412 KB Output is correct
29 Correct 2 ms 380 KB Output is correct
30 Correct 2 ms 376 KB Output is correct
31 Correct 2 ms 376 KB Output is correct
32 Correct 2 ms 376 KB Output is correct
33 Correct 2 ms 376 KB Output is correct
34 Correct 2 ms 376 KB Output is correct
35 Correct 2 ms 380 KB Output is correct
36 Correct 2 ms 252 KB Output is correct
37 Correct 2 ms 376 KB Output is correct
38 Correct 2 ms 380 KB Output is correct
39 Correct 2 ms 376 KB Output is correct
40 Correct 2 ms 376 KB Output is correct
41 Correct 2 ms 376 KB Output is correct
42 Correct 2 ms 376 KB Output is correct
43 Correct 3 ms 376 KB Output is correct
44 Correct 0 ms 376 KB Output is correct
45 Correct 2 ms 376 KB Output is correct
46 Correct 2 ms 376 KB Output is correct
47 Correct 6 ms 504 KB Output is correct
48 Correct 2 ms 376 KB Output is correct
49 Correct 2 ms 376 KB Output is correct
50 Correct 2 ms 376 KB Output is correct
51 Correct 2 ms 376 KB Output is correct
52 Correct 2 ms 376 KB Output is correct
53 Correct 2 ms 376 KB Output is correct
54 Correct 9 ms 376 KB Output is correct
55 Correct 2 ms 504 KB Output is correct
56 Correct 2 ms 376 KB Output is correct
57 Correct 2 ms 376 KB Output is correct
58 Correct 2 ms 376 KB Output is correct
59 Correct 2 ms 376 KB Output is correct
60 Correct 2 ms 380 KB Output is correct
61 Correct 2 ms 376 KB Output is correct
62 Correct 2 ms 376 KB Output is correct
63 Correct 2 ms 376 KB Output is correct
64 Correct 2 ms 376 KB Output is correct
65 Correct 2 ms 376 KB Output is correct
66 Correct 2 ms 380 KB Output is correct
67 Correct 2 ms 376 KB Output is correct
68 Correct 2 ms 376 KB Output is correct
69 Correct 2 ms 376 KB Output is correct
70 Correct 2 ms 376 KB Output is correct
71 Correct 2 ms 412 KB Output is correct
72 Correct 2 ms 376 KB Output is correct
73 Correct 2 ms 376 KB Output is correct
74 Correct 2 ms 376 KB Output is correct
75 Correct 3 ms 376 KB Output is correct
76 Correct 2 ms 416 KB Output is correct
77 Correct 2 ms 376 KB Output is correct
78 Correct 2 ms 376 KB Output is correct
79 Correct 2 ms 412 KB Output is correct
80 Correct 2 ms 376 KB Output is correct
81 Correct 2 ms 376 KB Output is correct
82 Correct 2 ms 376 KB Output is correct
83 Correct 2 ms 376 KB Output is correct
84 Correct 1 ms 388 KB Output is correct
85 Correct 0 ms 504 KB Output is correct
86 Correct 2 ms 376 KB Output is correct
87 Correct 2 ms 376 KB Output is correct
88 Correct 2 ms 376 KB Output is correct
89 Correct 2 ms 376 KB Output is correct
90 Correct 2 ms 376 KB Output is correct
91 Correct 2 ms 376 KB Output is correct
92 Correct 3 ms 384 KB Output is correct