SGU 138. Games of Chess 构造 难度:2
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 integers a 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
如果使用回路(1,2, 2,1, 2,3,...),可能会造成1 1 1 1 这种状态,
我们需要填充这样一个表,关键是不要让自己和自己对决即可
A B C D E....(总场次/2)
S:
L:
按场次从大到小排序,然后对于
A B C D E....(总场次/2)
S:
L:
每个人按照赢--(度只剩一个)输--(如果赢的填完了)输来填充,相当于把这条数列从中间截开,然后把后面那条和必要的转移作为输的,前面一条作为赢.
比如题目样例
排序过后是<5,4>,<4,2>,<2,1>,<1,3>
填充4
A,B,C,D,E,F
S:4,4,4,4,
L: 4,
填充2
A,B,C,D,E,F
S:4,4,4,4,2,2,
L:2,2, 4,
填充1
A,B,C,D,E,F
S:4,4,4,4,2,2,
L:2,2,1,1,4,
填充3
A,B,C,D,E,F
S:4,4,4,4,2,2,
L:2,2,1,1,4,3,
这个时候对于拐弯的那个重复数列设为i,设可填的非自身重叠长度l1,可填充自身重叠长度为l2,待填充长度为len,假设l1<len那么就有l1>已被填充的S序列长度,而场数排过序,如果i不是场数最大的人,不可能出现这种情况,如果i是第一个却发生转弯,也就是说没有答案
#include <cstdio>
#include <algorithm>
using namespace std;
typedef pair<int ,int> P;
int n;
P deg[101];
int nowdeg[101];
int heap[10001][2];
bool cmp(P p1,P p2){
return p2.first<p1.first;
}
int main(){
scanf("%d",&n);
int sum=0;
for(int i=0;i<n;i++){
scanf("%d",°[i].first);
sum+=deg[i].first;
deg[i].second=i+1;
}
sort(deg,deg+n,cmp);
int s=sum/2;
printf("%d\n",s);
int ind=0;
bool fl=false;
for(int i=0;i<n;i++){
for(int j=1;j<=deg[i].first;j++){
if(j==deg[i].first&&!fl)heap[ind][1]=deg[i].second;
else if(!fl){
heap[ind++][0]=deg[i].second;
}
else {
while(heap[ind][1]!=0){++ind;}
heap[ind][1]=deg[i].second;
}
if(ind==s){ind=0;fl=true;}
}
}
for(int i=0;i<s;i++)printf("%d %d\n",heap[i][0],heap[i][1]);
return 0;
}
SGU 138. Games of Chess 构造 难度:2的更多相关文章
- SGU 138.Games of Chess
时间限制:0.25s 空间限制:4M 题目: n个朋友在一起按照下面的规则依次下棋:在第一局游戏,n个人中的两个开始下棋.在第二局,第一局胜利的人将跟其他人下棋(也可能还是输了第一局人), 在第三局第 ...
- sgu 137. Funny Strings 线性同余,数论,构造 难度:3
137. Funny Strings time limit per test: 0.25 sec. memory limit per test: 4096 KB Let's consider a st ...
- UVa LA 4094 WonderTeam 构造 难度: 1
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- sgu 147. Black-white king 思路 坑 难度:1
147. Black-white king time limit per test: 0.25 sec.memory limit per test: 4096 KB input: standard i ...
- sgu 138
自己猜测了一下 按比赛次数 从大到小排 然后类似于模拟 先排胜的场次 当只剩一场 将它定义为败 #include <cstdio> #include <cstdlib> # ...
- sgu 121. Bridges painting 列举情况 难度:1
121. Bridges painting time limit per test: 0.25 sec. memory limit per test: 4096 KB New Berland cons ...
- 109. Magic of David Copperfield II 构造 难度:2
109. Magic of David Copperfield II time limit per test: 0.25 sec. memory limit per test: 4096 KB The ...
- sgu 183. Painting the balls 动态规划 难度:3
183. Painting the balls time limit per test: 0.25 sec.memory limit per test: 4096 KB input: standard ...
- POJ 3295 Tautology 构造 难度:1
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9580 Accepted: 3640 Descrip ...
随机推荐
- Supermarket---poj456(贪心并查集优化)
题目链接:http://poj.org/problem?id=1456 题意是现有n个物品,每个物品有一个保质期和一个利润,现在每天只能卖一个商品,问最大的利润是多少,商品如果过期了就不能卖了: 暴力 ...
- Python开发【Django】:Admin配置管理
Admin创建登录用户 数据库结构表: from django.db import models # Create your models here. class UserProfile(models ...
- Mysql varchar 把默认值设置为null和空的区别
'\0',这个表示空,需要消耗存储空间的.NULL,则表示连这个\0都没有. NULL,你可以近似理解为变量未赋值(定义了变量,但是未使用,变量不指向具体存储空间,因此,理论上不消耗存储空间),同时, ...
- mysql 数据操作 单表查询 group by 介绍
group by 是在where 之后运行 在写单表查询语法的时候 应该把group by 写在 where 之后 执行顺序 1.先找到表 from 库.表名 2.按照where 约束条件 过滤你想要 ...
- git-【六】分支的创建与合并
在版本回填退里,已经知道,每次提交,Git都把它们串成一条时间线,这条时间线就是一个分支.截止到目前,只有一条时间线,在Git里,这个分支叫主分支,即master分支.HEAD严格来说不是指向提交,而 ...
- POJ1845:Sumdiv(求因子和+逆元+质因子分解)好题
题目链接:http://poj.org/problem?id=1845 定义: 满足a*k≡1 (mod p)的k值就是a关于p的乘法逆元. 为什么要有乘法逆元呢? 当我们要求(a/b) mod p的 ...
- PAT 1080 Graduate Admission[排序][难]
1080 Graduate Admission(30 分) It is said that in 2011, there are about 100 graduate schools ready to ...
- Selenium之Chrome浏览器的启动问题及解决
System.setProperty("webdriver.chrome.driver","chromedriver.exe路径"); 配置好Chrome的驱动 ...
- 禁止复制操作 --《C++必知必会》条款32
class NoCopy{ private: //声明为私有的,则外部不可访问,即:不可复制 NoCopy(const NoCopy & );//复制构造函数 NoCopy & ope ...
- C++虚函数分析
1.虚函数(impure virtual) c++虚函数主要是提供“运行时多态”,父类提供虚函数的默认实现,子类可以虚函数进行重写. 2.纯虚函数(pure virtual) c++纯虚函 ...