Submission #185458

# Submission time Handle Problem Language Result Execution time Memory
185458 2020-01-11T20:42:18 Z qkxwsm JOIRIS (JOI16_joiris) C++14
15 / 100
2 ms 380 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()
{
    FOR(i, 1, N)
    {
        //make it > a[i - 1].
        while(arr[i] < arr[i - 1])
        {
            ans.PB({0, i});
            arr[i] += K;
        }
    }
    FORD(i, N, 0)
    {
        arr[i] -= arr[0];
    }
    // cerr << "LOL\n";
    // FOR(i, 0, N)
    // {
    //     cerr << arr[i] << ' ';
    // }
    // cerr << endl;
    FOR(i, 1, N)
    {
        FOR(j, 0, arr[i] - arr[i - 1])
        {
            int it = i - K;
            while(it >= 0)
            {
                ans.PB({1, it});
                it -= K;
            }
        }
    }
    FORD(i, SZ(ans), 0)
    {
        if (ans[i].fi == 0) break;
        int idx = ans[i].se;
        FOR(j, idx, idx + K)
        {
            arr[j]++;
        }
    }
    // cerr << "LOL\n";
    // FOR(i, 0, N)
    // {
    //     cerr << arr[i] << ' ';
    // }
    // cerr << endl;
    int d = arr[N - 1] / K + 1;
    FOR(j, 0, d)
    {
        FOR(i, 0, K)
        {
            ans.PB({0, i});
            arr[i] += K;
        }
    }
    FOR(i, 0, N)
    {
        arr[i] -= arr[N - 1];
    }
    // cerr << "LOL\n";
    // FOR(i, 0, N)
    // {
    //     cerr << arr[i] << ' ';
    // }
    // cerr << endl;
    // cerr << "LOL\n";
    // FOR(i, 0, N)
    // {
    //     cerr << arr[i] << ' ';
    // }
    // cerr << endl;
    if (N % K)
    {
        FOR(i, 0, N % K)
        {
            ans.PB({0, i});
            arr[i] += K;
        }
        int d = (arr[0] - arr[K - 1]) % K; if (d < 0) d += K;
        FOR(i, 0, d)
        {
            for (int j = N % K; j < N; j += K)
            {
                ans.PB({1, j});
                // cerr << "PB " << 1 << ',' << j << endl;
            }
            FOR(j, 0, N % K)
            {
                arr[i]--;
            }
        }
        // cerr << "LOL\n";
        // FOR(i, 0, N)
        // {
        //     cerr << arr[i] << ' ';
        // }
        // cerr << endl;
    }
    d = 0;
    FOR(i, 0, K) ckmax(d, arr[i]);
    FOR(i, 0, K)
    {
        while(arr[i] < d)
        {
            ans.PB({0, i});
            arr[i] += K;
        }
    }
    // cerr << "LOL\n";
    // FOR(i, 0, N)
    // {
    //     cerr << arr[i] << ' ';
    // }
    // cerr << endl;
    d = arr[0];
    while(d % K)
    {
        ans.PB({1, 0});
        FOR(j, 0, K) arr[j]++;
        d++;
    }
    FOR(i, K, N)
    {
        FOR(j, 0, d / K)
        {
            ans.PB({0, i});
            arr[j] += K;
        }
    }
    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)
    {
        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 0 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 256 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 376 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 Incorrect 2 ms 380 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 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 0 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 256 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 376 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
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Incorrect 2 ms 376 KB Output isn't correct
20 Halted 0 ms 0 KB -
# 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 0 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 256 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 376 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
17 Correct 2 ms 376 KB Output is correct
18 Incorrect 2 ms 380 KB Output isn't correct
19 Halted 0 ms 0 KB -