洛谷 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. nextjs作为koa中间件的使用

    react客户端渲染的缺点:首屏速度慢,对SEO不友好 浏览器请求步骤                                                        客户端跳转 1. ...

  2. 选择排序法(C语言)

    基本思路 在每一次的排序中选出最小(或最大)的数,将其放在数组最前端,然后在后面的数中重复此步骤,最终达到排序的目的. 算法描述 将存于数组首位数与后面的数依次比较,将其中的较小的数放到数组放到数组首 ...

  3. Introduction to Semidefinite Programming (SDP)

    https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-251j-introduction-to-mathe ...

  4. 执行shell脚本遇到错误syntax error: unexpected "then" (expecting "}")

    今天执行脚本的时候遇到错误,如下图: root@ApFree:/usr/sbin# ./conntrack_num_graph.sh ./conntrack_num_graph.sh: line : ...

  5. 剑指offer:对称的二叉树(镜像,递归,非递归DFS栈+BFS队列)

    1. 题目描述 /** 请实现一个函数,用来判断一颗二叉树是不是对称的. 注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的 */ 2. 递归 思路: /** 1.只要pRoot.left和 ...

  6. c#菜单动态合并 z

    说明 在程序中经常使用弹出菜单,并且一个窗体中可以存在多个弹出菜单.开发过MDI窗体的读者可能都知道,当MDI子窗体最大化时,子窗体和主窗体的菜单能够自动的合并.这是如何实现的呢?本例实现了将两个弹出 ...

  7. (二十)golang--变量的作用域

    (1)函数内部申明/定义的变量叫局部变量,作用域仅限于函数的内部: (2)函数外部申明/定义的变量叫全局变量,作用域在整个包都有效,如果其首字母为大写,则作用域是整个程序: (3)如果变量是在一个代码 ...

  8. MyBatis 获取插入记录的 id

    现在的项目改用 Guns 了,也是一个很不错的框架,用起来也感觉很不错,上手也挺方便的.毕竟对于只是应用层面的知识,也基本上就是看看手册,熟悉熟悉就可以轻松上手了.如果是想要深入,或者刨根问底,那么就 ...

  9. Flink之state processor api实践

    前不久,Flink社区发布了FLink 1.9版本,在其中包含了一个很重要的新特性,即state processor api,这个框架支持对checkpoint和savepoint进行操作,包括读取. ...

  10. 如何让 C# 在运行时自动选择合适的重载方法?

    如题:假设我们有一段代码: static void Main(string[] args) { ; // 假设这里的 obj 的值来自于外部方法 PrintType(obj); } public st ...