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 ...
随机推荐
- Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) D. Power Products 数学 暴力
D. Power Products You are given n positive integers a1,-,an, and an integer k≥2. Count the number of ...
- 强大的性能监测工具dstat
强大的性能监测工具dstat 本节分为以下几个部分: dstat介绍: dstat命令是一个用来替换vmstat.iostat.netstat.nfsstat和ifstat这些命令的工具,是一个全能系 ...
- touch.js - 移动设备上的手势识别与事件库
Touch.js 是移动设备上的手势识别与事件库, 由百度云Clouda团队维护,也是在百度内部广泛使用的开发工具.Touch.js手势库专为移动设备设计.Touch.js对于网页设计师来说,是一款不 ...
- 【MySQL】GTID小结
1.GTID的概念 GTID(global transaction identifier)是全局事务标识符,在MySQL5.6版本中作为一个超级特性被推出.事务标识不仅对于Master(起源)的服务器 ...
- Vue.js 源码分析(三十一) 高级应用 keep-alive 组件 详解
当使用is特性切换不同的组件时,每次都会重新生成组件Vue实例并生成对应的VNode进行渲染,这样是比较花费性能的,而且切换重新显示时数据又会初始化,例如: <!DOCTYPE html> ...
- Kubernetes Pod 资源限制
Kubernetes Pod 资源限制 官方文档:https://kubernetes.io/docs/concepts/configuration/manage-compute-resources- ...
- Window权限维持(一):注册表运行键
在红队行动中在网络中获得最初的立足点是一项耗时的任务.因此,持久性是红队成功运作的关键,这将使团队能够专注于目标,而不会失去与指挥和控制服务器的通信.在Windows登录期间创建将执行任意负载的注册表 ...
- 【杂文】CSP2019蒟蒻AFO(假)记
[杂文]CSP2019蒟蒻AFO(假)记 [初赛前 N 天] 时间:2019-10-15 今晚 \(2012\) 的初赛题做到心态爆炸,选择考计算机基础知识一脸懵逼,填空和后面一道大模拟直接跳过,最后 ...
- Prometheus监控学习笔记之Prometheus如何热加载更新配置
0x00 概述 当 Prometheus 有配置文件修改,我们可以采用 Prometheus 提供的热更新方法实现在不停服务的情况下实现配置文件的重新加载. 0x01 热更新 热更新加载方法有两种: ...
- IIS创建文件服务器(WebDAV)
1.安装IIS,选择安装WEBDAV组件.然后新建站点,站点目录不需要额外设置任何权限 安装完成后组件: 2.配置WebDAV: 添加创作规则:允许某用户写入,其他所有用户读取.(写入规则必须要放在第 ...