洛谷 P2902 [USACO08MAR]珍珠配对Pearl Pairing

https://www.luogu.org/problem/P2902

JDOJ 2577: USACO 2008 Mar Gold 3.Pearl Pairing

https://neooj.com:8082/oldoj/problem.php?id=2577

Description

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.

Input

* 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

Output

* 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.

Sample Input

8 3
2
2
4

Sample Output

1 3
1 3
2 3
3 2

HINT

INPUT DETAILS:

There are 8 pearls and 3 different colors. Two pearls have color I; two
have color II; four have color III.

OUTPUT DETAILS:

Bessie pairs each pearl of color III with one of color I and II.

 
题目大意:

在Bessie最近的生日聚会上,她收到N(2<=N<=100,000; N%2==0)珍珠,每个都涂上C种不同颜色之一(1<=C<=N)。

观察到珍珠N的数量总是均匀的,她的创意来了,决定配对珍珠,使每双珍珠有两种不同的颜色。数据保证存在答案。请帮助Bessie执行这样的配对,如果有多种创建配对的方法,任意输出即可。

SPJ开了。

我是为了刷背包才看到这道题的,想破脑袋也没想出来咋背包,后来用模拟做了一遍,思路是这个样子。

既然数据保证存在答案,我们便开始思考什么样子的数据才会保证存在答案。

然后我们发现每种颜色的数量不可能超过N/2要不然没法玩。

所以我们就想出了这样一种方案:

记录下每个珍珠的颜色,然后分成两组(因为N是偶数)

由于不可能有颜色数量超过N/2,所以我们枚举N/2次,每次取两组的第i位,就可以百分百保证不会有重复。

脑筋急转弯的味道很浓,你信我的就没问题。

其实也不需要再打一遍sort,因为你存数组的时候就是从小到大存的。

代码如下:

#include<cstdio>
using namespace std;
int s[];
int n,c;
int k;
int main()
{
scanf("%d%d",&n,&c);
for(int i=;i<=c;i++)
{
int x;
scanf("%d",&x);
for(int j=;j<=x;j++)
s[++k]=i;
}
for(int i=;i<=n/;i++)
printf("%d %d\n",s[i],s[i+(n/)]);
return ;
}

sort与否随你便。

USACO Pearl Pairing的更多相关文章

  1. USACO Section 1.3 题解 (洛谷OJ P1209 P1444 P3650 P2693)

    usaco ch1.4 sort(d , d + c, [](int a, int b) -> bool { return a > b; }); 生成与过滤 generator&& ...

  2. USACO wormhole

    洛谷 P1444 [USACO1.3]虫洞wormhole https://www.luogu.org/problemnew/show/P1444 JDOJ 2386: USACO 2013 Dec ...

  3. USACO . Your Ride Is Here

    Your Ride Is Here It is a well-known fact that behind every good comet is a UFO. These UFOs often co ...

  4. 【USACO 3.1】Stamps (完全背包)

    题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...

  5. USACO翻译:USACO 2013 NOV Silver三题

    USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...

  6. USACO翻译:USACO 2013 DEC Silver三题

    USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...

  7. USACO翻译:USACO 2014 DEC Silver三题

    USACO 2014 DEC SILVER 一.题目概览 中文题目名称 回程 马拉松 奶牛慢跑 英文题目名称 piggyback marathon cowjog 可执行文件名 piggyback ma ...

  8. USACO翻译:USACO 2012 FEB Silver三题

    USACO 2012 FEB SILVER 一.题目概览 中文题目名称 矩形草地 奶牛IDs 搬家 英文题目名称 planting cowids relocate 可执行文件名 planting co ...

  9. USACO翻译:USACO 2012 JAN三题(3)

    USACO 2012JAN(题目三) 一.题目概览 中文题目名称 放牧 登山 奶牛排队 英文题目名称 grazing climb lineup 可执行文件名 grazing climb lineup ...

随机推荐

  1. CF 938G Shortest Path Queries

    又到了喜闻乐见的写博客清醒时间了233,今天做的依然是线段树分治 这题算是经典应用了吧,假的动态图(可离线)问题 首先不难想到对于询问的时间进行线段树分治,这样就可以把每一条边出现的时间区间扔进线段树 ...

  2. 【Java并发专题之二】Java线程基础

    使用线程更好的提高资源利用率,但也会带来上下文切换的消耗,频繁的内核态和用户态的切换消耗,如果代码设计不好,可能弊大于利. 一.线程 进程是分配资源的最小单位,线程是程序执行的最小单位:线程是依附于进 ...

  3. 解决 Ubuntu16.04 + opencv4.1 源码编译错误 Makefile:160: recipe for target 'all' failed

    最近源码编译 opencv,出现下面的错误 [ %] Built target opencv_dnn Makefile:: recipe for target 'all' failed google ...

  4. H5 背景图片自适应屏幕问题解决办法

    今天也是偶然遇到这个问题的,平成很少用到关于css的内容,就在网上找了很多个方法,最后总结以下几种很实用的方法 方法1: <style> body{ background:url(beij ...

  5. mybatis的参数传递

    mybatis的参数传递分为两种:1.单参数传递  2.多参数传递 单参数 mybatis会直接取出参数值给Mapper文件赋值 例子如下: 1.Mapper文件内容如下: public void d ...

  6. 移动端&PC端CSS样式兼容代码

    CSS样式兼容代码 1.禁止选中复制文本 *{ user-select: none; -moz-user-select: none; -ms-user-select: none; -webkit-us ...

  7. POJ-2661Factstone Benchmark

    Factstone Benchmark Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5577   Accepted: 25 ...

  8. Redis 主从同步+哨兵

    简介 通过使用 Redis 自带“主从同步+哨兵守护”功能提高Redis稳定性. 主从同步:保障数据主从数据实时同步. 哨兵:实时监控主redis如果故障,将从redis作为主使用. 环境: 系统:C ...

  9. ASP.NET 中关GridView里加入CheckBox 在后台获取不到选中状态的问题

    <!-- 在GridView里添加CheckBox选择控件 !--> <ItemTemplate> <asp:CheckBox ID="CheckBox&quo ...

  10. java实现word生成并转pdf

    前言 本篇博客主要解决java后台动态生成word(docx格式),并将word转换为pdf并添加水印. 思考 项目需求是要导出带水印的pdf,表格样式还是有点复杂的,之前考虑过用itextpdf根据 ...