forked from krishna14kant/Data-Structures-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1579D_CF.cpp
67 lines (57 loc) · 1.3 KB
/
1579D_CF.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
void solve(){
ll n;cin>>n;
ll a[n+1];
for(int i=0;i<n;i++){
cin>>a[i];
}
multiset<pair<ll,ll>,greater<pair<ll,ll>>>st;
for(int i=0;i<n;i++){
if(a[i]!=0)
st.insert({a[i],i+1});
}
if(st.size()<=1){
cout<<0<<endl;
return;
}
ll k=0;
vector<pair<ll,ll>>me;
while(st.size()!=0){
if(st.size()<=1){
break;
}
auto it1=st.begin();
auto it2=st.begin();it2++;
// cout<<it1->first<<" "<<it2->first<<endl;
k++;
me.push_back({it1->second,it2->second});
ll val1=it1->first;
ll val2=it2->first;
if(val1-1==0&&val2-1==0){
}else if(val1-1==0){
st.insert({val2-1,it2->second});
}else if(val2-1==0){
st.insert({val1-1,it1->second});
}else{
st.insert({val1-1,it1->second});
st.insert({val2-1,it2->second});
}
st.erase(it1);
st.erase(it2);
}
cout<<k<<endl;
for(int i=0;i<me.size();i++){
cout<<me[i].first<<" "<<me[i].second<<endl;
}
}
int main() {
ll int t=1;
cin>>t;
while(t--){
solve();
}
return 0;
}