Submission #1258903

#TimeUsernameProblemLanguageResultExecution timeMemory
1258903Sam_arvandiJOIRIS (JOI16_joiris)C++20
30 / 100
1 ms328 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define FOR(i, j, n) for(int i = j; i<= n; i++)
#define ROF(i, n, j) for(int i = n; i>= j; i--)
#define pb push_back
#define F first
#define S second
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define G(i, j) get<j-1>(i)
#define print(x) cout << #x << ": " << x << endl;

const int mn = 2e5 + 5;
int a[mn];
int n, k;
vector<pii> out;

void f(int type, int ind)
{
        if (type == 1)
        {
                out.pb({1, ind});
                a[ind] += 2;
        }
        else
        {
                out.pb({2, ind});
                a[ind] += 1;
                a[ind+1] += 1;
        }
        return;
}

signed main()
{
        IOS;
        cin >> n >> k;
        if (k != 2) return 0;
        FOR(i,1, n) cin >> a[i];
        if (n == 2 and a[1]%2 != a[2]%2)
        {
                cout << -1;
                return 0;
        }
        int maxi = 0;
        FOR(i, 1, n) maxi = max(maxi, a[i]);
        FOR(i, 1, n)
        {
                while (a[i] < maxi-1)
                {
                        f(1, i);
                }
        }
        FOR(i,1 , n)
        {
                if (a[i] == maxi) a[i]= 1;
                else a[i]= 0;
        }
        while (true)
        {
                int ind = -1;
                bool flag = 1, flag2 = 1;
                FOR(i, 1, n)
                {
                        if (a[i] == 1) flag= 0;
                        if (a[i] == 0) flag2 = 0;
                }
                if (flag2)
                {
                        FOR(i, 1, n) a[i]--;
                        continue;
                }
                if (flag) break;
                FOR(i,1 , n)
                {
                        if (a[i] == 0)
                        {
                                ind = i;
                                break;
                        }
                }
                if (ind%2)
                {
                        for(int i = 1; i< ind; i+= 2)
                        {
                                f(2, i);
                        }
                        f(1, ind);
                        FOR(i, ind+1, n)
                        {
                                if (a[i] == 0)
                                {
                                        f(1, i);
                                }
                        }
                        continue;
                }
                if (ind != n and a[ind+1] == 0)
                {
                        f(2, ind);
                        continue;
                }
                if (ind != n)
                {
                        if ((n-ind+1)%2 == 0)
                        {
                                FOR(i, 1, ind)
                                {
                                        f(1, i);
                                }
                                FOR(i, ind+1, n)
                                {
                                        if (a[i] == 0)
                                        {
                                                f(1, i);
                                        }
                                }
                                FOR(i, ind+1, n)
                                {
                                        if (a[i] == 1 and a[i+1] == 1)
                                        {
                                                f(2, i);
                                        }
                                }
                                for(int i = ind; i < n; i+= 2)
                                {
                                        f(2, i);
                                }
                                FOR(i, 1, n) a[i]--;
                                continue;
                        }
                        FOR(i, 1, ind) f(1, i);
                        FOR(i, ind+1, n-1)
                        {
                                if (a[i] == 0 and a[i+1] == 0) f(2, i);
                        }
                        for(int i = ind+1; i < n; i+=2)
                        {
                                f(2, i);
                        }
                        FOR(i,1, n) a[i]--;
                        f(2, ind);
                        FOR(i, ind+2, n)
                        {
                                if (a[i] == 0) f(1, i);
                        }
                        continue;
                }
                cout << -1;
                return 0;
        }
        cout << out.size() << "\n";
        for(auto p: out)
        {
                cout << p.F << ' ' << p.S << "\n";
        }
        return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...