1579D - Productive Meeting - CodeForces Solution


constructive algorithms graphs greedy *1400

Please click on ads to support us..

Python Code:

from heapq import heappush, heappop, heapify
def getIntegerInputs():
    return map(int, input().split())

def getListInput():
    return list(map(int, input().split()))
    
def inbound(r, c, n, m):
    return 0 <= r < n and 0 <= c < m
    
def solve():
    for _ in range(int(input())):
        ans = []
        n = int(input())
        arr = getListInput()
        heap = [(-val, i) for i, val in enumerate(arr)]
        heapify(heap)
        while len(heap) > 1:
            r = heappop(heap)
            l = heappop(heap)
            if r[0] == 0 or l[0] == 0:
                break
            
            heappush(heap, (l[0] + 1, l[1]))
            heappush(heap, (r[0] + 1, r[1]))
                
            ans.append((r[1]+1, l[1]+1))
                
        print(len(ans))
        for a in ans:
            print(*a)
if __name__ == "__main__":
    solve()

C++ Code:

// Author- Aryan Pundir
#include<bits/stdc++.h>
using namespace std;
#define ll          long long
#define lli         long long int
#define vl          vector<ll>
#define vi          vector<int>
#define vs          vector<string>
#define vpi          vector<pair<int,int>>
#define vpl          vector<pair<ll,ll>>
#define pi          pair<int,int>
#define pl          pair<ll,ll>
#define all(a)      a.begin(),a.end()
#define mem(a,x)    memset(a,x,sizeof(a))
#define pb          push_back
#define mpi          map<int,int>
#define mpl          map<ll,ll>
#define pqi          priority_queue<int>
#define pql          priority_queue<ll>
#define F           first
#define S           second
#define f(a,b) for(int i=a;i<b;i++)
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
    int t;
    cin>>t;
    while(t--){
        int n;cin>>n;
        vi nums(n);
        vpi ans;
        priority_queue<pi> pqp;
        for(int i=0;i<n;i++){
            cin>>nums[i];
            if(nums[i]){pqp.push({nums[i],i+1});}
        }
        while(pqp.size()>1){
            pi f=pqp.top();
            pqp.pop();
            pi s=pqp.top();
            pqp.pop();
            ans.push_back({f.second,s.second});
            if(f.first>1){pqp.push({f.first-1,f.second});}
            if(s.first>1){pqp.push({s.first-1,s.second});}
        }
        cout<<ans.size()<<endl;
        for(auto &x:ans){
            if(x.first>x.second) swap(x.first,x.second);
            cout<<x.first<<" "<<x.second<<endl;
        }
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array