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\)轴正方向作边,使这 ...
随机推荐
- Linux Centos 上一些常用的命令
1.查看端口被哪个进程占用 netstat -lnp | grep <端口号> 2.查看某个进程号详细信息 ps <进程号> 3.检查指定服务是否开启(例如 telnet) c ...
- ch2-4:遇到嵌套列表进行缩进打印
1.增加一个参数来控制缩进打印:level '''这是一个模块,可以打印列表,其中可能包含嵌套列表''' def print_list(the_list,level): ""&qu ...
- asp显示多条记录的代码
asp显示多条记录的代码 仅供参考 <%for i=1 to RS.PageSize%> <% if RS.EOF then exit for end if %> <tr ...
- Unicode : RLO
分类:备忘,Unicode,Perl 我们一般的输入文字的方向是从左往右,但是世界上总有特例,阿拉伯国家是从右到左的书写方式.经常看到微信里面好友得瑟,也就拿过来总结一下. 每个语言都能实现字符串反转 ...
- Linux, Mac下Shell 数组 Array 的修理工
我的测试基本都是在Mac,及Unix环境下测试的,如无特别注明,默认就是Mac 不论你看到这篇随笔是被shell array的奇淫巧技,还是发现shell array就在一对{}里面就可以做那么多勾当 ...
- Ubuntu 14.04下NFS安装配置
1.安装nfs-server # apt-get install nfs-kernel-server 2.建立nfs专用文件夹 # mkdir /data/disk1 3.配置nfs # vi /et ...
- CentOS系统没有javac命令
自己捯饬的linux系统(CentOS)安装了jdk后,只能识别java命令而不识别javac.根据网上的教程设置环境变量后还是不行. 后来看了下/usr/java/jdk**目录下面根本没有java ...
- python8
编译和解释性语言的区别 编译 典型的C C++ 编译完成之后是可执行文件. 机器码-底层(外文书,但是不懂中文,翻译成中文就可以看) 解释性语言----看一句英文书,让翻译解释一句 解释器写的代码便于 ...
- php之属性重载和方法重载
<?php /** * * @authors Your Name (you@example.org) * @date 2016-06-13 20:40:19 * @version $Id$ */ ...
- WebView的返回功能
WebView 实现返回到最后一个 在退出 import android.app.Activity; import android.os.Bundle; import android.view.Key ...