sgu138:Games of Chess

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

4
2 4 1 5

Sample Output

6
4 3
4 1
2 4
2 1
4 2
2 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;	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值