USACO Pearl Pairing
洛谷 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
2
2
4
Sample Output
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的更多相关文章
- 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&& ...
- USACO wormhole
洛谷 P1444 [USACO1.3]虫洞wormhole https://www.luogu.org/problemnew/show/P1444 JDOJ 2386: USACO 2013 Dec ...
- 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 ...
- 【USACO 3.1】Stamps (完全背包)
题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...
- USACO翻译:USACO 2013 NOV Silver三题
USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...
- USACO翻译:USACO 2013 DEC Silver三题
USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...
- USACO翻译:USACO 2014 DEC Silver三题
USACO 2014 DEC SILVER 一.题目概览 中文题目名称 回程 马拉松 奶牛慢跑 英文题目名称 piggyback marathon cowjog 可执行文件名 piggyback ma ...
- USACO翻译:USACO 2012 FEB Silver三题
USACO 2012 FEB SILVER 一.题目概览 中文题目名称 矩形草地 奶牛IDs 搬家 英文题目名称 planting cowids relocate 可执行文件名 planting co ...
- USACO翻译:USACO 2012 JAN三题(3)
USACO 2012JAN(题目三) 一.题目概览 中文题目名称 放牧 登山 奶牛排队 英文题目名称 grazing climb lineup 可执行文件名 grazing climb lineup ...
随机推荐
- Vue todos示例 在class中的应用表达式
本篇博客用到了v-if,v-on,v-for三个没有写到的知识点,以后会详细写三篇博客,详细写这三个方法的用处 直接上代码: <!doctype html> <html lang=& ...
- 【正则】day02
正则表达式的应用匹配.查找.分割.替换对于普通字符串处理函数和正则表达式字符处理函数,使用的原则:能用普通字符串处理函数处理的,就不要用正则表达式处理. 原因:普通字符串处理函数:效率高.简单.正则表 ...
- [算法模版]Prim-完全图最小生成树
[算法模版]Prim-完全图最小生成树 众所周知,对于常用的Kruskal算法,算法复杂度为\(O(m \log m)\).这在大多数场景下已经够用了.但是如果遇到及其稠密的完全图,Prim算法就能更 ...
- python3 消耗CPU的性能,使CPU满载(可以设置进程名称)
需要安装库:setproctitle 1.1.10,设置进程名称,测试操作系统centos 7.0 # -*- coding: utf-8 -*- from multiprocessing im ...
- playtime-浙大羽协裁判部训练方案[随机事件序列的应用]
首先随机一列人名 然后按比例随机一列事件项. 然后将不确定项的人名更正为“某人”[比如发球违例,,,你怎么知道谁在发球] 最后定义一个初始化. 初始化呢,就是挑边. 球权还是场权? 发球还是接发? 谁 ...
- WPF 中如何变相让 ListBox 宽度(Width) 100%,高度(Height) 100%,从而达到 Filled 的效果
直接贴代码了: XAML: <Window x:Class="HelloWorld.MainWindow" xmlns="http://schemas.micros ...
- 基于MicroPython结合ESP8266模块实现TCP通信(AT指令版)
转载请注明文章来源,更多教程可自助参考docs.tpyboard.com,QQ技术交流群:157816561,公众号:MicroPython玩家汇 实验目的 - 学习ESP8266模块(ESP-01) ...
- 《数据结构》《C++程序设计》《计算机组成原理》中的英语名词
一.数据结构 data 数据data element 数据元素data item 数据项data object 数据对象data structure 数据结构ADT (Abstruct Date Ty ...
- Winforn中设置ZedGraoh的GraphPane恢复到初始比例大小
场景 Winform中实现ZedGraph中曲线右键显示为中文: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100115292 ...
- C#中的一些对话框问题处理
1. 对于打开文件对话框处理 #region 打开文件对话框 string StrPath; OpenFileDialog Flag = new OpenFileDialog(); Flag.Mult ...