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\)轴正方向作边,使这 ...
随机推荐
- android 学习随笔二十八(应用小知识点小结 )
去掉标题栏的方法 第一种:也一般入门的时候经常使用的一种方法requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏注意这句一定要写在setConte ...
- android 学习随笔五(界面)
把数据库内容显示到界面 清单文件设置为线性布局(5大布局属于ViewGroup) 在清单文件中可以增加View显示 LinearLayout ll = (LinearLayout) findViewB ...
- 网上书城分类Category模块
2 分类模块 2.1 创建分类模块相关类 在每个模块开始时,都要创建如下基本类:实体类.DAO类.Service类.Servlet类: l cn.itcast.goods.category.domai ...
- Hadoop三种安装模式:单机模式,伪分布式,真正分布式
Hadoop三种安装模式:单机模式,伪分布式,真正分布式 一 单机模式standalone单 机模式是Hadoop的默认模式.当首次解压Hadoop的源码包时,Hadoop无法了解硬件安装环境,便保守 ...
- TI CC2541的狗日的Key
被突如其来的一个bug困扰了好几天, 起因是, 按键接的红外接收器, 结果发现, 一旦按下之后, IEN1, P0IE的标识位bit5, 被不知道特么的谁归0了, 也就是说, 按键只能被按下一次, 再 ...
- Git 使用规范流程
Git教程:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 团队开发中,遵循一个合 ...
- Java、JVM和操作系统之间的关系,写给新人,
来张图:这个帖子写给新人的,老玩家就直接无视他,因为这个完完全全是白话基础原理. 解释:上面的图是从上往下依次调用的关系. 操作系统(Windows/Linux)管理硬件,让硬件能够正常.合理的运行, ...
- Codeforces Gym 101142C:CodeCoder vs TopForces(搜索)
http://codeforces.com/gym/101142/attachments 题意:每个人在TC和CF上分别有两个排名,如果有一个人在任意一个网站上大于另一个人的排名,那么这个人可以打败另 ...
- C#:实现托盘(任务栏图标与托盘图标互斥)
实现托盘(任务栏图标与托盘图标互斥),并且在点击任务栏图标时实现的最小化与点击最小化按钮分离. 具体如下: 1.向窗体上添加如下控件:MenuStrip menuStrip1, NotifyIcon ...
- JS参数传值
1.JS获取URL参数值 //js获取url参数值 function request(paras) { var url = location.href; , url.length).split(&qu ...