forked from nduchuyvp123/CSES-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1697 - Chess Tournament.cpp
52 lines (49 loc) · 1.21 KB
/
1697 - Chess Tournament.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
Problem Name: Chess Tournament
Problem Link: https://cses.fi/problemset/task/1697
Author: Sachin Srivastava (mrsac7)
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r" , stdin);
freopen("output.txt", "w", stdout);
#endif
int n; cin>>n;
int a[n+1];
set<pair<int,int>> st;
int sm = 0;
for (int i = 1; i <= n; i++) {
cin>>a[i];
sm += a[i];
if (a[i])
st.insert({a[i], i});
}
sm;
vector<pair<int,int>> ans;
while(st.size()>=2) {
auto [x1, i1] = *st.begin(); st.erase({x1, i1});
vector<pair<int,int>> s;
while(x1>0 && st.size()) {
auto [x2, i2] = *st.rbegin(); st.erase({x2, i2});
ans.push_back({i1, i2});
x1--, x2--;
s.push_back({x2, i2});
}
for (auto [i, j]: s) {
if (i>0) st.insert({i, j});
}
}
if (2*ans.size() == sm) {
cout<<ans.size()<<endl;
for (auto [i, j]: ans) {
cout<<i<<' '<<j<<endl;
}
}
else
cout<<"IMPOSSIBLE";
}