D. Productive Meeting Solution | Codeforces Round #744 (Div. 3)


D. Productive Meeting
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

An important meeting is to be held and there are exactly n people invited. At any moment, any two people can step back and talk in private. The same two people can talk several (as many as they want) times per meeting.

Each person has limited sociability. The sociability of the i-th person is a non-negative integer ai. This means that after exactly ai talks this person leaves the meeting (and does not talk to anyone else anymore). If ai=0, the i-th person leaves the meeting immediately after it starts.

A meeting is considered most productive if the maximum possible number of talks took place during it.

You are given an array of sociability a, determine which people should talk to each other so that the total number of talks is as large as possible.

Input

The first line contains an integer t (1t1000) — the number of test cases.

The next 2t lines contain descriptions of the test cases.

The first line of each test case description contains an integer n (2n2105) —the number of people in the meeting. The second line consists of n space-separated integers a1,a2,,an (0ai2105) — the sociability parameters of all people.

It is guaranteed that the sum of n over all test cases does not exceed 2105. It is also guaranteed that the sum of all ai (over all test cases and all i) does not exceed 2105.

Output

Print t answers to all test cases.

On the first line of each answer print the number k — the maximum number of talks possible in a meeting.

On each of the next k lines print two integers i and j (1i,jn and ij) — the numbers of people who will have another talk.

If there are several possible answers, you may print any of them.

Example
input
Copy
8
2
2 3
3
1 2 3
4
1 2 3 4
3
0 0 2
2
6 2
3
0 0 2
5
8 2 0 1 1
5
0 1 0 0 6
output
Copy
2
1 2
1 2
3
1 3
2 3
2 3
5
1 3
2 4
2 4
3 4
3 4
0
2
1 2
1 2
0
4
1 2
1 5
1 4
1 2
1
5 2


The solution to the above Problem is 

We will make a max priority queue, then we will take the top two elements and they will talk, we will then decrease the value of them by one and then again push it back to the priority queue.

We will do this till there are two people left.

All the answer will be stored in vector.


#include <bits/stdc++.h>

using namespace std;

#define int            long long int
#define F              first
#define S              second
#define pb             push_back
#define si             set <int>
#define vi             vector <int>
#define pii            pair <int, int>
#define vpi            vector <pii>
#define vpp            vector <pair<int, pii>>
#define mii            map <int, int>
#define mpi            map <pii, int>
#define spi            set <pii>
#define endl           "\n"
#define sz(x)          ((int) x.size())
#define all(p)         p.begin(), p.end()
#define double         long double
#define que_max        priority_queue <int>
#define que_min        priority_queue <int, vi, greater<int>>
#define bug(...)       __f (#__VA_ARGS__, __VA_ARGS__)
#define print(a)       for(auto x : a) cout << x << " "; cout << endl
#define print1(a)      for(auto x : a) cout << x.F << " " << x.S << endl
#define print2(a,x,y)  for(int i = x; i < y; i++) cout<< a[i]<< " "; cout << endl

inline int power(int a, int b)
{
  int x = 1;
  while (b)
  {
    if (b & 1) x *= a;
    a *= a;
    b >>= 1;
  }
  return x;
}

template <typename Arg1>
void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
template <typename Arg1, typename... Args>
void __f (const char* names, Arg1&& arg1, Args&&... args)
{
  const char* comma = strchr (names + 1, ',');
  cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
}

const int N = 200005;

void solve() {

int n;
cin>>n;
priority_queue<pair<int,int>>pq;
for(int i=1; i<=n; i++){
    int x;
    cin>>x;
    if(x>0){pq.push({x,i});}
}
vector<pair<int,int>>ans;
while(pq.size()>1){
auto f=pq.top();
pq.pop();
auto s=pq.top();
pq.pop();
// bug(f.S);
// bug(s.S);
ans.pb({s.S,f.S});
f.F--;
s.F--;
if(f.F)pq.push(f);
if(s.F)pq.push(s);
}
cout<<ans.size()<<endl;
for(auto i:ans)
cout<<i.F<<" "<<i.S<<endl;



}

int32_t main()
{
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);


  clock_t z = clock();

  int t = 1;
  cin >> t;
  while (t--) solve();



  return 0;
}

Thank you for reading.

Post a Comment

0 Comments