D. Productive Meeting (大根堆)

放到堆里,每次取出堆顶的两个元素来判断就好了

// Problem: D. Productive Meeting
// Contest: Codeforces - Codeforces Round #744 (Div. 3)
// URL: https://codeforces.com/contest/1579/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<list>
#include<unordered_map>
using namespace std;
#define ff first
#define ss second
#define lowbit(x) (x&-x)
#define pf(a) printf("%d\n",a)
#define mem(x,y) memset(x,y,sizeof(x))
#define dbg(x) cout << #x << " = " << x << endl
#define rep(i,l,r) for(int i = l; i <= r; i++)
#define fep(i,a,b) for(int i=b; i>=a; --i)
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;


const int N=2e5+100;
int n;

struct node{
	int l,r;
}ans[N];

void solve()
{
	priority_queue<PII, vector<PII>, less<PII> >s;
	scanf("%d", &n);
	rep(i,1,n)
	{
		int tmp;
		cin >> tmp;
		s.push({tmp,i});
	}
	
	int cnt = 0;
	while(true)
	{
		auto a = s.top(); s.pop();
		auto b = s.top(); s.pop();
		if(a.ff==0 or b.ff == 0) break;
		else
		{
			ans[cnt++] = {a.ss, b.ss};
			s.push({a.ff-1,a.ss});
			s.push({b.ff-1,b.ss});
			
		}
	}
	cout << cnt << endl;
	for(int i = 0; i < cnt; i++)
	{
		cout << ans[i].l << " " << ans[i].r << endl;
	}
	 
}

int main()
{

	int Case;scanf("%d", &Case);
	while(Case--)
		solve();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值