HDU 5813 Elegant Construction (贪心)
来源:互联网 发布:江苏网络电视台回看 编辑:程序博客网 时间:2024/04/29 10:21
题目链接
Elegant Construction
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 0 Accepted Submission(s): 0
Special Judge
Problem 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!
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!
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.
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.
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.
Sample Input
332 1 021 143 1 1 0
Sample Output
Case #1: Yes21 22 3Case #2: NoCase #3: Yes41 21 32 43 4
题意:要求构造一个有向图,使得点i能够恰好到达Ai个点.(直接间接皆可)
输出任意满足条件的图即可,没有要求最小
题解:如果不存在出度为零的点,那么一定不可能(会有环),直接输出,然后按照点能到达的城市从小到大排序,然后从左往右遍历,对于每一个点,如果前面的点的数量小于当前点能到达的城市的数量,也不可能,然后每次优先往最前面开始连边,用一个vector存就好了。详见代码。
官方题解:将顶点按能到达的点数从小到大排序,排好序之后每个点只能往前面的点连边. 因而如果存在一个排在第i位的点,要求到达的点数大于i-1,则不可行;否则就可以按照上述方法构造出图. 复杂度O(N^2).
#include <cstring>#include <queue>#include <map>#include <set>#include <cstdio>#include <cstring>#include <cmath>#include <iostream>#include <algorithm>using namespace std;typedef long long int LL;const int maxn=1000+10;struct fun{ int num,pos; friend bool operator < (fun a,fun b){ return a.num<b.num;}}a[maxn];struct res{ int from,to; res(int a,int b):from(a),to(b){}};int main(){ int cas; scanf("%d",&cas); for(int p=1;p<=cas;p++) { int n; scanf("%d",&n); if(n==1){printf("Case #%d: Yes\n0\n",p);continue;} bool have=false; for(int i=1;i<=n;i++) { a[i].pos=i; scanf("%d",&a[i].num); if(a[i].num==0) have=true; } if(!have){ printf("Case #%d: No\n",p);continue; } sort(a+1,a+n+1); vector<res> ans; bool flag=true; for(int i=1;i<=n;i++) { if(i<a[i].num){ flag=false;break;} for(int j=1;j<=a[i].num;j++) ans.push_back(res(a[i].pos,a[j].pos)); } if(!flag){ printf("Case #%d: No\n",p);continue; } int m=ans.size(); printf("Case #%d: Yes\n%d\n",p,m); for(int i=0;i<m;i++){ printf("%d %d\n",ans[i].from,ans[i].to);} }}
0 0
- Hdu 5813 Elegant Construction【贪心】
- HDU 5813 Elegant Construction (贪心)
- hdu 5813 Elegant Construction (贪心)
- HDU-5813-Elegant Construction-贪心
- HDU 5813 Elegant Construction 【贪心】
- hdu 5813 Elegant Construction 贪心(多校)
- hdu 5813 Elegant Construction(贪心)
- HDU 5813 Elegant Construction(贪心)
- HDU-5813-Elegant Construction【多校2016】【贪心】
- hdu 5813 Elegant Construction 贪心构造
- HDU 5813 Elegant Construction (贪心)
- 贪心(Elegant Construction,HDU 5813)
- hdu 5813 Elegant Construction
- hdu 5813 Elegant Construction
- hdu 5813 Elegant Construction (模式)
- hdu 5813 Elegant Construction 构造
- HDU 5813 Elegant Construction(构造)
- Elegant Construction HDU-5813 构造
- Error 3 error C4996: 'cv::flann::Index_<T>': was declared deprecated c:\open
- java中日期的格式化 时间类
- Linux下I/O多路转接之select --fd_set
- RxJava操作符(2)-变换
- 求所有前缀-后缀字符串相等的长度(kmp算法中的nxet数组)
- HDU 5813 Elegant Construction (贪心)
- validates不要校验布尔型 presence: true
- 【计算机组成原理】流水线处理器
- Centos磁盘管理 进阶篇
- 某病毒使用的API CreateDesktop
- 洛谷 1717 钓鱼(贪心)
- mysql 计算字段 学习笔记
- OJ-----密码强度等级
- sizeof和strlen区别详解