Pearl Pairing

描述
At Bessie’s recent birthday party, she received N (2 <= N <= 100,000; N%2 == 0) pearls, each painted one of C different colors (1 <= C <= N).

Upon observing that the number of pearls N is always even, her creative juices flowed and she decided to pair the pearls so that each pair of pearls has two different colors.

Knowing that such a set of pairings is always possible for the supplied testcases, help Bessie perform such a pairing. If there are multiple ways of creating a pairing, any solution suffices.

输入
Line 1: Two space-separated integers: N and C

Lines 2…C + 1: Line i+1 tells the count of pearls with color i: C_i
输出
Lines 1…N/2: Line i contains two integers a_i and b_i indicating that Bessie can pair two pearls with respective colors a_i and b_i.
样例输入
8 3
2
2
4
样例输出
1 3
1 3
2 3
3 2
提示
INPUT DETAILS:
There are 8 pearls and 3 different colors. Two pearls have color I; two have color II; four have color III.
题意
在Bessie最近的生日聚会上,她收到N(2<=N<=100,000; N%2==0)珍珠,每个都涂上C种不同颜色之一(1<=C<=N)要求配对两个珍珠,使两个珍珠的颜色不一样。
思路
一开使我把数据按颜色多少从小到大排序,然后从数组左边拿一个右边拿一个,过不了。因为有一个bug,比如22333444本来可以全配对的,2233 — 3444,同时从两个数组的左边拿出进行组合。但是按照我的方法从一个数组 22333444 的左右两边拿出组合却不行,因为题中说的是 is always possible就是永远可能组合完,所以从二分之一处分开就好了。
代码

#include<cstdio>
using namespace std;
int s[100001];
int n,c;
int k;
int main()
{
    scanf("%d%d",&n,&c);
    for(int i=1;i<=c;i++)
    { 
        int x;
        scanf("%d",&x);
        for(int j=1;j<=x;j++)
            s[++k]=i;
    }
    for(int i=1;i<=n/2;i++)
        printf("%d %d\n",s[i],s[i+(n/2)]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值