Cards(Codeforces 701A)(Codeforces Round #364Div.2 A)
来源:互联网 发布:视频加特效软件 编辑:程序博客网 时间:2024/04/28 17:57
701A
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.
AC代码如下:
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int a[105];int visited[105];int main(){ int n; scanf("%d",&n); int sum=0; for(int i=1;i<=n;i++){ scanf("%d",&a[i]); sum+=a[i]; } memset(visited,0,sizeof(visited)); int res=sum*2/n; for(int i=1;i<=n;i++){ for(int j=1;j<=n&&j!=i;j++){ if(a[i]+a[j]==res&&!visited[i]&&!visited[j]){ printf("%d %d\n",i,j); visited[i]=1; visited[j]=1; } } } return 0;}
附上大牛的代码:(思路是排序)
#include<bits/stdc++.h>using namespace std;struct p{int i,v;};int com(p a,p b){return a.v<b.v;}int main(){int n;cin>>n;p a[n];for(int i=0;i<n;i++){cin>>a[i].v;a[i].i=i+1;}sort(a,a+n,com);for(int i=0;i<n/2;i++){cout<<a[i].i<<' '<<a[n-1-i].i<<'\n';}}
- Cards(Codeforces 701A)(Codeforces Round #364Div.2 A)
- Codeforces Round #364 (Div. 2) A. Cards
- Codeforces Round #364 (Div. 2) A. Cards
- codeforces Round #364(Div.2)701A Cards 贪心算法
- Codeforces Round #364 (Div. 2) A - Cards(水)
- 【Codeforces Round 364 (Div 2)A】【水题】Cards
- Codeforces Round #233 (Div. 1) A. Cards
- Codeforces Round #235 (Div. 2) A. Vanya and Cards
- Codeforces Round #356 (Div. 2) A - Bear and Five Cards
- Codeforces Round #364(Div. 2) A. Cards【模拟】 && B. Cells Not Under Attack【SET/规律】
- codeforces--701A Cards
- CodeForces 701A Cards
- codeforces 701 A. Cards
- codeforces 701A Cards
- Codeforces Round #364 (Div. 2)【A,C】
- Codeforces Round #364 (Div. 2)(A ~ D)
- Codeforces Round #364 (Div. 2) A
- ★【16.6.9】Codeforces Round #356 (Div. 2) A. Bear and Five Cards
- cocos2dx实现简单卡牌翻转效果
- 系统吞吐量、TPS(QPS)、用户并发量、性能测试概念和公式
- DroidPlugin源码分析插件进程管理以及预注册Activity,Service,ContentProvide的选择
- entityframework 增删改查
- Codeforces Round #364 (Div. 2) 题解
- Cards(Codeforces 701A)(Codeforces Round #364Div.2 A)
- codeforces 364 div2.c 神代码
- DroidPlugin源码分析处理Activity的启动
- oracle基于3种方法的大数据量插入更新
- 算法-巴什博奕
- DroidPlugin源码分析服务与静态广播的处理
- 暴力枚举,dfs(连连看,HDU 4272)
- 如何最有效率的学习 Java
- JavaScript创建对象