题意:有n个人,然后任意两个人可以交谈任意次,给你n个数,表示第i个人在谈论
次后就会离开,如果
为0,那么这个人在聚会开始就会离开。最后让你输出最多能交谈多少次,并且输出每次交谈的两个人。
思路:贪心的一个题,因为要尽可能的交谈多次,那么就要可以交谈次数最多的人先去交谈,因为如果少的人先交谈了后面可以交谈次数多的人没有可以交谈的对象(1 2 8 如果先前两个那么只能有2次,如果先都和第三个就可以交谈3次)。那么就是不断用可以交谈的对象去跟比他小的一个对象交谈,然后加入队列,因为总共都不会超过2e5,所以时间复杂度肯定是够的,直接用优先队列做就可以了。
代码:
#include<cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include<vector>
#include<queue>
#include<map>
#define sc_int(x) scanf("%d", &x)
#define sc_ll(x) scanf("%lld", &x)
#define pr_ll(x) printf("%lld", x)
#define pr_ll_n(x) printf("%lld\n", x)
#define pr_int_n(x) printf("%d\n", x)
#define ll long long
using namespace std;
typedef pair<int,int>PII;
const int N=1000000+100;
int n ,m,h;
ll s[N];
void solve()
{
cin>>n;
priority_queue<PII, vector<PII>, less<PII>>q;
for(int i=1;i<=n;i++)
{
int x;
cin>>x;
if(x!=0)
q.push({x,i});
}
vector<PII>cnt;
while(q.size()>=2)
{
PII lk =q.top();
q.pop();
PII lk2=q.top();
q.pop();
cnt.push_back({lk.second,lk2.second});
if(lk.first-1!=0)q.push({lk.first-1,lk.second});
if(lk2.first-1!=0)q.push({lk2.first-1,lk2.second});
}
cout<<cnt.size()<<endl;
for(int i=0;i<cnt.size();i++)
cout<<cnt[i].first<<" "<<cnt[i].second<<endl;
return ;
}
int main()
{
int t;
sc_int(t);
while(t--)solve();
return 0;
}