sgu138:Games of Chess
来源:互联网 发布:2016年车险业务数据 编辑:程序博客网 时间:2024/05/20 19:50
138. Games of Chess
time limit per test: 0.25 sec.
memory limit per test: 4096 KB
N friends gathered in order to play chess, according to the following rules. In the first game, two of the N friends will play. In the second game, the winner of the first game will play against another friend (maybe even the same friend who lost the first game). In the third game, the winner of the second game will play against someone else and so on.. No game will end as a draw (tie). Given the number of games each of the N friends played, find a schedule for the games, so that the above rules are obeyed.
Input
The first line contains the number of friends N (2<=N<=100). The second line contains N integers, separated by blanks, representing the number of games each friend played. The first number represents the number of games played by the first friend, the second number represents the number of games played by the second friend and so on..
Output
The first line should contain the number of games played by all the friends (it will be an integer between 1 and 10 000, for every test case). Let's suppose this number is G. Then, G lines follow, each of them containing two integers, describing the games. The first line contains the numbers of the two friends who played the first game. The friend printed first is considered to be the winner. Each of the next G-1 lines contain the integersa and b, where a<>b and a or b is the winner of the previous game. The friend printed first on the line is considered to be the winner of the game.
It is guaranteed that for every test case there will be at least one possible scheduling of the games.
Sample Input
42 4 1 5
Sample Output
64 34 12 42 14 22 4
第一问为总局数/2。
首先我们从大到小将sum[i]排序,再根据从大到小的顺序填入其中。具体如下:
就比如样例,排序后5(4),4(2),2(1),1(3)括号里为原来编号。
然后分成两排:
win 4 4 4 4 2 2
lose 2 2 1 1 4 3
当sum[i]大于1时,加入win的那一排;当sum[i]==1时,加入对应的lose那一排,再接着从win排开始填sum[i+1]的编号。如上例。
#include <cstdio>#include <algorithm>using namespace std;const int MAXN = 105, MAXA = 10005, INF = 1<<30;int N = 0;struct node{ int i, v;}sum[MAXN] = {0};int ans = 0;int ans2[2][MAXA] = {0};bool cmp(const node &A, const node &B) { return A.v > B.v; }int main(){ scanf("%d", &N); for(int i = 1; i <= N; ++i) { scanf("%d", &sum[i].v); ans += sum[i].v; sum[i].i = i; } ans >>= 1; printf("%d\n", ans); sort(sum+1, sum+N+1, cmp); for(int i = 1, j = 1; i <= ans; ++i) { if(sum[j].v == 1) ans2[1][i] = sum[j].i, --sum[j++].v, i--; else ans2[0][i] = sum[j].i, --sum[j].v; } for(int i = 1, j = 1; i <= N && j <= ans; ++j) { while(!sum[i].v) i++; if(ans2[1][j] == 0) ans2[1][j] = sum[i].i, --sum[i].v; } for(int i = 1; i <= ans; ++i) printf("%d %d\n", ans2[0][i], ans2[1][i]); return 0;}
- sgu138:Games of Chess
- SGU138 Games of Chess
- sgu138: Games of Chess
- [SGU]138. Games of Chess
- SGU 138 Games of Chess 构造
- SGU 138 Games of Chess(构造)
- the history of video games
- The Sprague-Grundy theory of impartial games
- Best Practice of cross-platform games
- CF 455B-- A Lot of Games
- codeforces 455B. A Lot of Games
- (CF) D. A Lot of Games
- codeforces 455B A Lot of Games
- codeforces 455B A Lot of Games
- CodeForces 455B A Lot of Games
- codeforces 455B A Lot of Games
- EightQueens in the game of chess
- chess
- Android测量地球任意两点之间的距离
- maven配置
- Autolayout和VFL
- NYOJ 267郁闷的C小加(二)
- putty的设置
- sgu138:Games of Chess
- zookeeper学习笔记——leader选举
- 推荐一款ios软件叫你出我猜
- 分治算法
- sgu-169 Numbers 很好的数学题
- 转自Computer World. 12项雇主最无法拒绝的IT技能
- Be a part of making the world’s first people powered tablet
- Objective-c协议
- Android以后台Service方式获取GPS数据,并定时发送到服务器