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 ... 
随机推荐
- 介绍一个比较强大的网页剪藏工具——web-clipper
			网址:https://clipper.website/ 在语雀的第三方工具处遇到的. 感觉可以代替印象笔记网页端的剪藏,还是很强大的. 
- Harbor + Https 部署
			关闭防火墙和selinux systemctl stop firewalld sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/sysconfig/selin ... 
- maven pom项目的dependencies转gradle格式
			1.新建一个文件件 2.创建pom.xm,放到新建的文件夹中 3.在命令行切换到新建文件夹中,执行: gradle init --type pom 
- Docker学习4-学会如何让容器开机自启服务
			前言 小龙亲测重启服务器后 docker 容器没跑起来,相信有不少小伙伴在用docker部署容器的时候也发现每次开机服务就没有自启了,需要手动去执行把容器服务开启起来,但有没有可以让它开机自启呢?显然 ... 
- Linux查找文件夹下包含某字符的所有文件
			Linux grep 命令用于查找文件里符合条件的字符串.grep 指令用于查找内容包含指定的范本样式的文件,如果发现某文件的内容符合所指定的范本样式,预设 grep 指令会把含有范本样式的那一列显示 ... 
- 奥展项目笔记04--Spring cloud 通过父工程打包多个子工程,导出可运行的Jar包
			在spring cloud微服务搭建过程中,我们创建了多个微服务模块,如图: 1.父工程Pom文件 <?xml version="1.0" encoding="UT ... 
- Kafka随笔
			1.选举Leader Leader 是 Partition 级别的,当一个 Broker 挂掉后,所有 Leader 在该 Broker 上的 Partition 都会被重新选举,选出一个新 Lea ... 
- nginx代理tcp请求
			1.概述 ngx_stream_core_module 这个module在nginx1.90后开始支持.开启nginx的tcp代理支持--with-stream=dynamic --with-stre ... 
- Linux安装centos,网络net8模式ping不通www.baidu.com或者ping不通主机
			1.Linux安装centos,网络net8模式ping不通www.baidu.com或者ping不通主机. 我使用的是net8模式.配置如下所示,保证可以ping通www.baidu.com或者pi ... 
- Object.defineProperty-vuejs数据响应基石
			https://www.jianshu.com/p/07ba2b0c8fca https://juejin.im/post/5b99215d5188255c520cfe22 vuejs数据双向绑定地核 ... 
