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!

InputThe 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.OutputFor 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 启发来自于队内大佬余神和楼主
首先判读能否构成这样的有向无环图,对拿到的出度进行排序,然后看比一个点出度小的点数有没有小于等于它本身的出度,如果有大于的则输出no,全部小于等于则输出yes
判断yes后,有一个最傻瓜的思路,就是全先连0,然后连1,然后连下去。因为小的数连完以后再连大的数之后就不用考虑大数所连数,直接+1即可。
 #include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std; int b[]; struct node
{
int u,v;
node(int x,int y){u=x;v=y;}
}; struct node2
{
int num,id;
}a[]; bool cmp(node2 m,node2 n)
{
return m.num<n.num;
}
int main()
{
int T;
scanf("%d",&T);
for(int t=;t<=T;t++)
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i].num);
a[i].id=i;
}
printf("Case #%d: ",t);
sort(a+,a+n+,cmp);
memset(b,,sizeof(b));
int s=,tmp=;
for(int i=;i<=n;i++)
{ if(a[i].num==a[i-].num)
{
b[i]=s;
tmp++;
}
else
{
s+=tmp;
b[i]=s;
tmp=;
}
}
bool flag=true;
for(int i=;i<=n;i++)
if(a[i].num>b[i])
{
flag=false;
break;
}
if(!flag)
printf("No\n");
else
{
printf("Yes\n");
queue<node>Q;
int num=;
for(int i=;i<=n;i++)
{
for(int j=;j<=a[i].num;j++)
{
Q.push(node(a[i].id,a[j].id));
num++;
}
}
printf("%d\n",num);
while(!Q.empty())
{
printf("%d %d\n",Q.front().u,Q.front().v);
Q.pop();
}
}
}
return ;
}
 

2016 多校联赛7 Elegant Construction的更多相关文章

  1. hdu5737(2016多校联赛第2场D)

    题意:给2组数据a和b数组,每次有2种操作:(+,l,r,x)把a数组第l个到第r个元素全置为x,(?,l,r)查询[l,r]之间哪些位置满足a[i]>=b[i](i>=l &&a ...

  2. 2016 多校联赛7 Joint Stacks (优先队列)

    A stack is a data structure in which all insertions and deletions of entries are made at one end, ca ...

  3. 2016 多校联赛7 Balls and Boxes(概率期望)

    Mr. Chopsticks is interested in random phenomena, and he conducts an experiment to study randomness. ...

  4. HDU 5813 Elegant Construction (贪心)

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

  5. HDU5813 Elegant Construction

    Elegant Construction                                                                         Time Li ...

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

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

  7. HDU 5813 Elegant Construction 构造

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

  8. 2015 HDU 多校联赛 5363 Key Set

    2015 HDU 多校联赛 5363 Key Set 题目: http://acm.hdu.edu.cn/showproblem.php? pid=5363 依据前面给出的样例,得出求解公式 fn = ...

  9. 2015 HDU 多校联赛 5317 RGCDQ 筛法求解

    2015 HDU 多校联赛 5317 RGCDQ 筛法求解 题目  http://acm.hdu.edu.cn/showproblem.php? pid=5317 本题的数据量非常大,測试样例多.数据 ...

随机推荐

  1. springboot使用redis的配置application.yml连接池以及存取

    第一步:添加pom文件依赖: <!-- redis --> <dependency> <groupId>redis.clients</groupId> ...

  2. MongoDB 第一篇

    概述 MongoDB是一个高性能,开源,无模式的文档型数据库,使用C++开发.是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的.他支持的数据结构非常松散,是 ...

  3. MSMQ消息传递的优先级

    一.消息传递的优先级 在MSMQ中消息在队列里传输是分有优先级的,这里我就以实例的形式介绍下关于优先级的使用,优先级一共有七种,MessagePriority枚举里全部进行了封装.因这里只作程序演示就 ...

  4. Redis 系列之CentOS下Redis的安装

    前言 安装Redis需要知道自己需要哪个版本,有针对性的安装,比如如果需要redis GEO这个地理集合的特性,那么redis版本就不能低于3.2版本,由于这个特性是3.2版本才有的.另外需要注意的是 ...

  5. Python---字典常用方法总结

    字典是一种key-value的数据类型,字典里必须写Key和value,字典的优点是取数方便和速度快.字典的特性: 1.字典是无序的,因为它没有下标,用key来当索引,所以是无序的 2.字典的key必 ...

  6. [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)

    转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...

  7. PowerShell使用教程

    一.说明 1.1 背景说明 个人对PowerShell也不是很熟悉,开始的时候就突然看到开始菜单中多了个叫PowerShell的文件夹,后来一点就看到某个教程视频说PowerShell很厉害但也没怎么 ...

  8. 在springboot中验证表单信息(六)

    构建工程 创建一个springboot工程,由于用到了 web .thymeleaf.validator.el,引入相应的起步依赖和依赖,代码清单如下: 1 2 3 4 5 6 7 8 9 10 11 ...

  9. grep console

    FF6464 9F6B00 8A8A00 4B5E76 8A7674 980B4F 980B4F

  10. 转【面向代码】学习 Deep Learning(二)Deep Belief Nets(DBNs)

    [面向代码]学习 Deep Learning(二)Deep Belief Nets(DBNs) http://blog.csdn.net/dark_scope/article/details/9447 ...