HDU 5813 Elegant Construction(优雅建造)
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Description |
题目描述 |
Being an ACMer requires knowledge in many fields, because problems in this contest may use physics, biology, and even musicology as background. And now in this problem, you are being a city architect! A city with N towns (numbered 1 through N) is under construction. You, the architect, are being responsible for designing how these towns are connected by one-way roads. Each road connects two towns, and passengers can travel through in one direction. For business purpose, the connectivity between towns has some requirements. You are given N non-negative integers a1 .. aN. For 1 <= i <= N, passenger start from town i, should be able to reach exactly ai towns (directly or indirectly, not include i itself). To prevent confusion on the trip, every road should be different, and cycles (one can travel through several roads and back to the starting point) should not exist. Your task is constructing such a city. Now it's your showtime! |
成为一个ACMer需要广阔的知识面,因为比赛中的故事背景可能是物理、生物、甚至是音乐。现在,你将变成一个城市建筑师!一个城市有N个镇(编号从1到N)正在建设。你作为建筑师,负责设计单行道连通这些城镇。每条路连接两个镇子,游客可以单向浏览。 为了商业利益,镇子间的连通性有所要求。给你N个非负整数a1 .. aN。对于1 <= i <= N,游客以城镇i为起点,能够恰好达ai个城镇(直接或间接,不包括i本身)。为了避免行程混乱,每条路各不相同,并且不存在环(某人可以通过若干跳路回到起点)。 你的任务就是规划这座城市。快去干活! |
Input |
输入 |
The first line is an integer T (T <= 10), indicating the number of test case. Each test case begins with an integer N (1 <= N <= 1000), indicating the number of towns. Then N numbers in a line, the ith number ai (0 <= ai < N) has been described above. |
第一行是一个整数T (T <= 10),表示测试用例的数量。每个测试用例以N (1 <= N <= 1000)打头,表示城镇的数量。下一行有N个数,第i个数ai (0 <= ai < N)意思同上。 |
Output |
输出 |
For each test case, output "Case #X: Y" in a line (without quotes), where X is the case number starting from 1, and Y is "Yes" if you can construct successfully or "No" if it's impossible to reach the requirements. If Y is "Yes", output an integer M in a line, indicating the number of roads. Then M lines follow, each line contains two integers u and v (1 <= u, v <= N), separated with one single space, indicating a road direct from town u to town v. If there are multiple possible solutions, print any of them. |
对于每个用例,输出一行"Case #X: Y"(没有引号),X是从1开始的用例编号,如果能规划成功则Y为"Yes",否则为"No"。 如果Y为"Yes",输出一行一个整数M,表示路的数量。随后M行,每行两个整数u和v (1 <= u, v <= N),用一个空格分隔,表示一条从城镇u到城镇v的路。如果存在多解,输出任意一种。 |
Sample Input - 输入样例 |
Sample Output - 输出样例 |
3 |
Case #1: Yes |
【题解】
贪心,按可以到达的城镇数量升序排序(降序需要考虑重复节点)。
由于题目对于输出没有限制,所以能多暴力就多暴力,一个节点需要连几个节点就输出几条边。从头开始往后选择节点可以避免重复。
(居然因为手抖变量打错可耻地WA了……)
【代码 C++】
#include <cstdio>
#include <algorithm>
struct Town{
int no, sv;
bool operator<(Town &r){
return sv<r.sv;
}
}data[];
int main(){
int t, n, iT, i, j, s;
bool isContinue;
scanf("%d", &t);
for (iT = ; iT <= t; ++iT){
printf("Case #%d: ", iT);
scanf("%d", &n);
for (i = ; i < n; ++i){
scanf("%d", &data[i].sv); data[i].no = i + ;
}
std::sort(data, data + n); isContinue = ;
for (i =s= ; i < n; ++i){
if (data[i].sv > i){ isContinue = ; break; }
s += data[i].sv;
} if (isContinue){
printf("Yes\n%d\n", s);
for (i = ; i < n; ++i){
for (j = ; j < data[i].sv; ++j) printf("%d %d\n", data[i].no, data[j].no);
}
}
else puts("No");
}
return ;
}