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 |
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 ;
}
HDU 5813 Elegant Construction(优雅建造)的更多相关文章
- HDU 5813 Elegant Construction (贪心)
Elegant Construction 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5813 Description Being an ACMer ...
- HDU 5813 Elegant Construction 构造
Elegant Construction 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5813 Description Being an ACMer ...
- HDU 5813 Elegant Construction
构造.从a[i]最小的开始放置,例如放置了a[p],那么还未放置的,还需要建边的那个点 需求量-1,然后把边连起来. #pragma comment(linker, "/STACK:1024 ...
- HDU 5813 Elegant Construction ——(拓扑排序,构造)
可以直接见这个博客:http://blog.csdn.net/black_miracle/article/details/52164974. 对其中的几点作一些解释: 1.这个方法我们对队列中取出的元 ...
- HDU5813 Elegant Construction
Elegant Construction Time Li ...
- hdu-5813 Elegant Construction(贪心)
题目链接: Elegant Construction Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (J ...
- 2016 多校联赛7 Elegant Construction
Being an ACMer requires knowledge in many fields, because problems in this contest may use physics, ...
- HDU 3516 Tree Construction (四边形不等式)
题意:给定一些点(xi,yi)(xj,yj)满足:i<j,xi<xj,yi>yj.用下面的连起来,使得所有边的长度最小? 思路:考虑用区间表示,f[i][j]表示将i到j的点连起来的 ...
- HDU.3516.Tree Construction(DP 四边形不等式)
题目链接 贴个教程: 四边形不等式学习笔记 \(Description\) 给出平面上的\(n\)个点,满足\(X_i\)严格单增,\(Y_i\)严格单减.以\(x\)轴和\(y\)轴正方向作边,使这 ...
随机推荐
- Datatables带参重绘
研究了好久,最后发现只需要加上参数("bDestory":true,) 即可实现每次刷新就是新的重绘,而无需调用什么desctory init clear等等函数..
- Add baidu map in your website (wordpress)
手动挡 访问应用(AK)Key http://lbsyun.baidu.com/apiconsole/key Basic Map Generator http://api.map.baidu.com/ ...
- windows win10上传文件到linux服务器
1.最直接当然使用终端secucrt和xshell putty之类的,然后使用sz rz 2.如果服务器端不支持sz rz可以使用scp命令,下面这个pscp.exe就是支持scp的,基于ssh,很好 ...
- jQuery.isNumeric() 和 js isNaN()
jQuery.isNumeric( value ) Description: 判断指定参数是否是一个数字值(字符串形式的数字也符合条件),返回 true 或者 false. Example: $.is ...
- let和expr比较
1.空格 let:要求任何操作符两边不能含有空格 expr:必须有空格 2.运算符转义 expr:要求 let:没有要求 [xiluhua@vm-xiluhua][~]$ let * -bash: l ...
- XCode5 真机调试及发布应用
一.XCODE 真机测试 Xcode5已经很智能,只需生成一个开发证书,安装后,插入设备会自动添加,注意,当Mac系统升级后,证书需要重新生成. 证书生成步骤: 1.生成 CertificateSig ...
- 深入理解GCD(一)
虽然 GCD 已经出现过一段时间了,但不是每个人都明了其主要内容.这是可以理解的:并发一直很棘手,而 GCD 是基于 C 的 API ,它们就像一组尖锐的棱角戳进 Objective-C 的平滑世界. ...
- 关于sql server 2008过期导致 MSSQLSERVER服务就无法启动,手动启动就报告错误代码17051。
1.基本现象:MSSQLSERVER服务就无法启动,手动启动就报告17051错误. 2.解决办法: 第一步:进入SQL2008配置工具中的安装中心, 第二步:再进入维护界面,选择版本升级, 第三步:进 ...
- C#:文件、路径(Path_File)
public class Path_File { public string AppPath { get { return AppDomain.CurrentDomain.BaseDirectory; ...
- myeclipse 常用快捷键总结
1 shift+enter 不管鼠标在当前行的什么位置,重新开启一行(向下) 2 shift+ctrl+enter 不管鼠标在当前行的什么位置,重新开启一行(向上) 3 Ctrl+D 删除一行 ...