HDU 5813 Elegant Construction(优雅建造)

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
3
2 1 0
2
1 1
4
3 1 1 0


Case #1: Yes
2
1 2
2 3
Case #2: No
Case #3: Yes
4
1 2
1 3
2 4
3 4

【题解】

  贪心,按可以到达的城镇数量升序排序(降序需要考虑重复节点)。

  由于题目对于输出没有限制,所以能多暴力就多暴力,一个节点需要连几个节点就输出几条边。从头开始往后选择节点可以避免重复。

  (居然因为手抖变量打错可耻地WA了……)

【代码 C++】

 1 #include <cstdio>
 2 #include <algorithm>
 3 struct Town{
 4     int no, sv;
 5     bool operator<(Town &r){
 6         return sv<r.sv;
 7     }
 8 }data[1005];
 9 int main(){
10     int t, n, iT, i, j, s;
11     bool isContinue;
12     scanf("%d", &t);
13     for (iT = 1; iT <= t; ++iT){
14         printf("Case #%d: ", iT);
15         scanf("%d", &n);
16         for (i = 0; i < n; ++i){
17             scanf("%d", &data[i].sv); data[i].no = i + 1;
18         }
19         std::sort(data, data + n);
20
21         isContinue = 1;
22         for (i =s= 0; i < n; ++i){
23             if (data[i].sv > i){ isContinue = 0; break; }
24             s += data[i].sv;
25         }
26
27         if (isContinue){
28             printf("Yes\n%d\n", s);
29             for (i = 1; i < n; ++i){
30                 for (j = 0; j < data[i].sv; ++j) printf("%d %d\n", data[i].no, data[j].no);
31             }
32         }
33         else puts("No");
34     }
35     return 0;
36 }
时间: 2025-01-02 00:24:27

HDU 5813 Elegant Construction(优雅建造)的相关文章

HDU 5813 Elegant Construction

构造.从a[i]最小的开始放置,例如放置了a[p],那么还未放置的,还需要建边的那个点 需求量-1,然后把边连起来. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<

hdu-5813 Elegant Construction(贪心)

题目链接: Elegant Construction Time Limit: 4000/2000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Problem Description Being an ACMer requires knowledge in many fields, because problems in this contest may use physics, biology, and even

HDU 3516 Tree Construction

区间$dp$,四边形优化. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<map> #include<set> #include<queue

【四边形】 HDU 3516 Tree Construction

通道 题意:二维坐标上的点,建一个长度和最小的树包含全部点 思路: 定义状态 dp[i,j]表示点i到点j合并在一起的最小花费(树枝的长度), 状态转移方程:dp[i,j]= min(dp[i,k]+dp[k+1,j]+cost(i,j) ) i<k<j cost(i,j)=py[k]-py[j]+px[k+1]-px[i]; 当j固定时,cost(i,j)单调递减函数 我们猜测cost(i,j)满足四边形不等式 证明: F(i)=cost(i,j+1)-cost(i,j)=py[j]-py[

unit 23

comic adj 喜剧的    comic novel 喜剧小说 ridiculous adj 愚蠢的 absurd adj 荒谬的 hysterical adj 极可笑的 commit v 犯:使承担义务      committed adj 坚定的:投入的   commitment n 承诺 devote v 致力    devoted adj 挚爱的,投入的    devotion n 深爱:奉献 dedicate v [~ onelself to]致力于   dedicated adj

ACM总结——dp专辑(转)

感谢博主——      http://blog.csdn.net/cc_again?viewmode=list       ----------  Accagain  2014年5月15日 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间效率高,代码量少,多元性强,主要考察思维能力.建模抽象能力.灵活度. 本人动态规划博客地址:http://blog.csdn.net/cc_again/article/category/1261899 ***********************

【DP专辑】ACM动态规划总结

转载请注明出处,谢谢.   http://blog.csdn.net/cc_again?viewmode=list          ----------  Accagain  2014年5月15日 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间效率高,代码量少,多元性强,主要考察思维能力.建模抽象能力.灵活度. 本人动态规划博客地址:http://blog.csdn.net/cc_again/article/category/1261899 ******************

(转)dp动态规划分类详解

dp动态规划分类详解 转自:http://blog.csdn.NET/cc_again/article/details/25866971 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间效率高,代码量少,多元性强,主要考察思维能力.建模抽象能力.灵活度. ****************************************************************************************** 动态规划(英语:Dynamic programm

这个是转的。学完就删_(:з」∠)_

原文链接:http://blog.csdn.net/cc_again/article/details/25866971 好多dp:http://blog.csdn.net/cc_again/article/category/1261899 一.简单基础dp 这类dp主要是一些状态比较容易表示,转移方程比较好想,问题比较基本常见的.主要包括递推.背包.LIS(最长递增序列),LCS(最长公共子序列),下面针对这几种类型,推荐一下比较好的学习资料和题目. 1.递推: 递推一般形式比较单一,从前往后,