Elegant Construction

                                                                        Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
                                                                                             Total Submission(s): 1021    Accepted Submission(s): 534
                                                                                                                                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!

 
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.

 
Sample Input
3
3
2 1 0
2
1 1
4
3 1 1 0
 
Sample Output
Case #1: Yes
2
1 2
2 3
Case #2: No
Case #3: Yes
4
1 2
1 3
2 4
3 4
 
Author
SYSU
 
Source
 
Recommend
wange2014   |   We have carefully selected several similar problems for you:  6032 6031 6030 6029 6028 
————————————————————————————————
题目:构造一张有向图。保证每个点能到达的点得数量恰好等于a[i]。

思路:按照a[i]排序,针对每个点i,对于每个小于i的点j,看是否有a[i]个数满足a[j]<a[i]。然后依次连边即可。由于每次都贪心选择小的所以不会重复

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional> using namespace std; #define LL long long
const int INF = 0x3f3f3f3f; int n,u[10005*1005],v[1005*1005];
struct node
{
int id,x;
friend bool operator<(node a,node b)
{
return a.x<b.x;
}
}a[1005]; int main()
{
int t,cas=0;
scanf("%d",&t);
while(t--)
{
int flag=1,cnt=0;
printf("Case #%d: ",++cas);
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i].x),a[i].id=i;
sort(a+1,a+1+n);
if(a[1].x!=0) {printf("No\n");continue;}
int k=2;
while(a[k].x==0) k++;
for(int i=k;i<=n;i++)
{
if(a[i].x>i-1) {flag=0;break;}
for(int j=1;j<=a[i].x;j++)
u[cnt]=a[i].id,v[cnt++]=a[j].id;
}
if(!flag) {printf("No\n");continue;}
printf("Yes\n%d\n",cnt);
for(int i=0;i<cnt;i++)
printf("%d %d\n",u[i],v[i]);
}
return 0;
}

 

HDU5813 Elegant Construction的更多相关文章

  1. hdu-5813 Elegant Construction(贪心)

    题目链接: Elegant Construction Time Limit: 4000/2000 MS (Java/Others)     Memory Limit: 65536/65536 K (J ...

  2. HDU 5813 Elegant Construction(优雅建造)

    HDU 5813 Elegant Construction(优雅建造) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65 ...

  3. HDU 5813 Elegant Construction (贪心)

    Elegant Construction 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5813 Description Being an ACMer ...

  4. HDU 5813 Elegant Construction 构造

    Elegant Construction 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5813 Description Being an ACMer ...

  5. 2016 多校联赛7 Elegant Construction

    Being an ACMer requires knowledge in many fields, because problems in this contest may use physics, ...

  6. HDU 5813 Elegant Construction

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

  7. HDU 5813 Elegant Construction ——(拓扑排序,构造)

    可以直接见这个博客:http://blog.csdn.net/black_miracle/article/details/52164974. 对其中的几点作一些解释: 1.这个方法我们对队列中取出的元 ...

  8. 2016 Multi-University Training Contest 7

    6/12 2016 Multi-University Training Contest 7 期望 B Balls and Boxes(BH) 题意: n个球放到m个盒子里,xi表示第i个盒子里的球的数 ...

  9. 2016 Multi-University Training Contest 7 solutions BY SYSU

    Ants 首先求出每个点的最近点. 可以直接对所有点构造kd树,然后在kd树上查询除本身以外的最近点,因为对所有点都求一次,所以不用担心退化. 也可以用分治做,同样是O(NlogN)的复杂度. 方法是 ...

随机推荐

  1. Could not HEAD 'https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.2.0/gradle-3.2.0.pom'.

  2. Systemd 教程

    目录 Systemd 教程 sshd.service配置模板 开机启动 启动服务 停止服务 配置文件 [Unit] 区块:启动顺序与依赖关系 [Service] 区块:启动行为 1.启动命令 2.启动 ...

  3. java第二章继承

    继承(extends)面向对象三大特征之一 类中有许多相同的属性和方法,代码重复,如果需要修改涉及较多类修改量增多 将子类中共有的属性和方法提取到父类,让子类继承父类,减少代码量,扩展性维护性,子类可 ...

  4. SQL0803问题 键值重复

    工作中遇到SQL0803问题  使用DB2AS400数据库 报数据库键值重复错误 经同事分析为索引的起始值与当前已有记录的最大索引值不匹配造成的,验证过程如下: 1.SELECT max(被索引字段) ...

  5. css文本垂直居中的实现

    本案例已经有新的比较简便的解决方案,可以直接采用 vertical-align:middle 样式对行内元素进行垂直居中布局,详见: 微信小程序开发——如何让商品标题类文本根据内容长度垂直居中. 以下 ...

  6. java集合类,HashMap,ArrayList

    集合类 Collection LinkedList.ArrayList.HashSet是非线程安全的, Vector是线程安全的; ArrayXxx:底层数据结构是数组,连续存放,所以查询快,增删慢. ...

  7. 线程同步的实现方式(volatile、synchronized、CountDownLatch)

    题目: 自定义容器,提供新增元素(add)和获取元素数量(size)方法.启动两个线程. 线程1向容器中新增10个数据.线程2监听容器元素数量,当容器元素数量为5时,线程2输出信息并终止. 方法一:v ...

  8. 实验五:任意输入10个int类型数据,排序输出,再找出素数

    一.程序编写 import java.util.*;public class Port {  public static void main(String[] args) {  // TODO 自动生 ...

  9. NC 63凭证辅助相关表

    1:通过公司主键(pk_org)在AccChartVO(表:bd_accchart)找:pk_accchart 2:通过科目编码和pk_accchart在AccAsoaVO(表:bd_accasoa) ...

  10. NC 创建表空间数据库

    1.首先创建表空间 CREATE SMALLFILE TABLESPACE "NNC_DATA01" LOGGING DATAFILE 'D:\NCOracle\nc633sd\N ...