【CodeForces】701A - Cards(暴力 || 思维)
来源:互联网 发布:网络歌曲《鸽子》歌词 编辑:程序博客网 时间:2024/04/30 23:12
点击打开题目
There are n cards (n is even) in the deck. Each card has a positive integer written on it. n / 2 people will play new card game. At the beginning of the game each player gets two cards, each card is given to exactly one player.
Find the way to distribute cards such that the sum of values written of the cards will be equal for each player. It is guaranteed that it is always possible.
The first line of the input contains integer n (2 ≤ n ≤ 100) — the number of cards in the deck. It is guaranteed that n is even.
The second line contains the sequence of n positive integers a1, a2, ..., an (1 ≤ ai ≤ 100), where ai is equal to the number written on the i-th card.
Print n / 2 pairs of integers, the i-th pair denote the cards that should be given to the i-th player. Each card should be given to exactly one player. Cards are numbered in the order they appear in the input.
It is guaranteed that solution exists. If there are several correct answers, you are allowed to print any of them.
61 5 7 4 4 3
1 36 24 5
410 10 10 10
1 23 4
In the first sample, cards are distributed in such a way that each player has the sum of numbers written on his cards equal to 8.
In the second sample, all values ai are equal. Thus, any distribution is acceptable.
n最大才100,思维不好的暴力跑(比如我)。然而机智的媳妇儿用了一种比较聪明的方法:
将数字排序,然后前面取一个,后面取一个就行了(想想就能明白)。因为它保证了至少一个解的存在。
暴力代码:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define CLR(a,b) memset(a,b,sizeof(a))#define INF 0x3f3f3f3fint main(){int n;int sum;bool used[111];int num[111];while (~scanf ("%d",&n)){CLR(used,false);sum = 0;for (int i = 1 ; i <= n ; i++){scanf ("%d",&num[i]);sum += num[i];}sum /= (n >> 1);for (int i = 1 ; i <= n ; i++){if (!used[i]){used[i] = true;printf ("%d ",i);for (int j = 1 ; j <= n ; j++){if (!used[j] && num[i]+num[j] == sum){printf ("%d\n",j);used[j] = true;break;}}}}}return 0;}
机智的代码:
#include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespace std;#define CLR(a,b) memset(a,b,sizeof(a))struct node{int num,ant;}a[110];bool cmp(node x,node y){return x.ant<y.ant;}int main(){int n;while(~scanf("%d",&n)){for(int i=1;i<=n;i++){scanf("%d",&a[i].ant);a[i].num=i;}sort(a+1,a+n+1,cmp);for(int i=1;i<=(n/2);i++)printf("%d %d\n",a[i].num,a[n+1-i].num);}return 0;}
- 【CodeForces】701A - Cards(暴力 || 思维)
- 【Codeforces】-701A-Cards(思维)
- Codeforces 398A Cards 暴力
- CodeForces 398A Cards 贪心 暴力 瞎搞
- codeforces--701A Cards
- CodeForces 701A Cards
- codeforces 701 A. Cards
- codeforces 701A Cards
- Codeforces 701A.Cards(贪心)
- CodeForces 701A Cards(水)
- CodeForces 626B Cards(数学思维)
- Codefores 398A Cards(贪心+暴力)
- CodeForces 701A Cards(水水的玩扑克)
- !codeforces 399C Cards-数论&贪心-(暴力枚举)
- Codeforces 398 A. Cards
- CodeForces 398A Cards
- Cards(Codeforces 701A)(Codeforces Round #364Div.2 A)
- codeforces 734 (思维 暴力)
- linux kernel file_open
- 位运算入门应用以及技巧
- artist-mode & ditaa
- PyQt5教程(四)——事件与信号
- 【解题报告】2016.8.5·Day2·状压DP
- 【CodeForces】701A - Cards(暴力 || 思维)
- 关于java的IO流
- 数据结构实验之栈六:下一较大值(二)
- 【字符串7】字符流中第一个不重复的字符
- 【杭电oj4857】逃生
- 个人的C语言笔记
- 【Codeforces】-701B-Cells Not Under Attack(思维,好)
- java 判断两个二叉树是否完全相同
- Android中常见的热门标签的流式布局的实现