COGS132珍珠分对|优先队列

题意:

作为生日礼物,BESSIE得到了N (2 <= N <= 100,000;N%2 == 0)颗珍珠, 每颗珍珠的颜色为C种颜色之一 (1 <= C <= N).

BESSIE发现N是一个偶数,她想把所有珍珠分成N/2对,使得每对的两颗珍珠的颜色都不同.

BESSIE知道这样一个分配在给定数据里都是可能的,请帮助她找到这样一个分配. 如果有多个解,任意一个,输出任意一个.

题名: ppairing

输入格式:

  • 第1行: 两个数, N 和 C
  • 第 2..C + 1 行: 第i+1行含有一个数,C_i, 颜色为i的珍珠的数目.

样例输入 (ppairing.in):

8 3
2
2
4

输入解释:

共8颗珍珠,2颗为颜色I,2颗为颜色II,4颗为颜色III.

输出格式:

  • 第 1..N/2行: 第i行包含两个数a_i和b_i. 对应于一对颜色分别为a_i和b_i的珍珠.

样例输出 (ppairing.out):

1 3
1 3
2 3
3 2

输出解释:

BESSIE把每颗颜色为III的珍珠和一颗颜色为I/II的组成一对.


优先队列维护过程即可


#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>

using namespace std;

const int MAXN=100010; 

int n,c,tot;
struct BEAD{
	int sum,col;
	bool friend operator <(BEAD a,BEAD b) {
		return a.sum<b.sum;
	}
}a[MAXN];

priority_queue<BEAD> q;

void init(){
	int i;
	scanf("%d%d",&n,&c);
	for(i=1;i<=c;++i) {
		scanf("%d",&a[i].sum);
		a[i].col=i;
		tot+=a[i].sum;
	}
	for(i=1;i<=c;++i) q.push(a[i]);
	for(i=1;i<=tot/2;++i) {
		BEAD e=q.top();q.pop();
		BEAD x=q.top();q.pop();
		printf("%d %d\n",e.col,x.col);
		e.sum--;
		x.sum--;
		if(e.sum)q.push(e);
		if(x.sum)q.push(x);
	}
}
int main(){
	freopen("ppairing.in","r",stdin);
	freopen("ppairing.out","w",stdout);
	init();	
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值