Elegant Construction

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=5813

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

Hint

题意

给你n个城市,告诉你第i个城市恰好能够走到a[i]个城市,让你构造一个有向图,使得满足题意,且不存在环。

题解:

直接暴力去建图就好了,n^2扫一遍,然后只扫编号比自己小的,这样就不会存在环了。

代码

 #include<bits/stdc++.h>
using namespace std;
const int maxn = 1005;
struct node{
int a,id;
}p[1005];
bool cmp(node a,node b){
return a.a<b.a;
}
int cas = 0;
int ansx[maxn*maxn],ansy[maxn*maxn];
void solve(){
printf("Case #%d: ",++cas);
int sum = 0;
int cnt=0;
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&p[i].a),p[i].id=i;
sort(p+1,p+1+n,cmp);
for(int i=1;i<=n;i++){
if(p[i].a>=i){
printf("No\n");
return;
}
for(int j=1;j<=p[i].a;j++)
ansx[cnt]=p[i].id,ansy[cnt++]=p[j].id;
}
printf("Yes\n");
printf("%d\n",cnt);
for(int i=0;i<cnt;i++){
printf("%d %d\n",ansx[i],ansy[i]);
}
}
int main(){
//freopen("1.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--)solve();
}

HDU 5813 Elegant Construction 构造的更多相关文章

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

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

  2. HDU 5813 Elegant Construction (贪心)

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

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

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

  4. HDU 5813 Elegant Construction

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

  5. HDU5813 Elegant Construction

    Elegant Construction                                                                         Time Li ...

  6. hdu-5813 Elegant Construction(贪心)

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

  7. HDU 5573 Binary Tree 构造

    Binary Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5573 Description The Old Frog King lives ...

  8. hdu 5015 233 Matrix(构造矩阵)

    http://acm.hdu.edu.cn/showproblem.php?pid=5015 由于是个二维的递推式,当时没有想到能够这样构造矩阵.从列上看,当前这一列都是由前一列递推得到.依据这一点来 ...

  9. P3599 Koishi Loves Construction——构造题

    题目 Task1:试判断能否构造并构造一个长度 $n$ 的 $1...n$ 的排列,满足其 $n$ 个前缀和在模 $n$ 的意义下互不相同 Task2:试判断能否构造并构造一个长度 $n$ 的 $1. ...

随机推荐

  1. Mac下安装zsh(Oh My ZSH)的shell,替代原有的bash

    说明:一开始装zsh我是拒绝的,因为这个东西装简单,卸载很难,并且装了之后默认Shell的配置文件不能用了,比如~/.bashrc这些.所以在装的时候要再三考虑好! 官网:http://ohmyz.s ...

  2. 第8月第22天 python scrapy

    1. cd /Users/temp/Downloads/LagouSpider-master ls ls ls lagou/settings.py cat lagou/settings.py ls p ...

  3. Linux下select&poll&epoll的实现原理(一)【转】

    转自:http://www.cnblogs.com/lanyuliuyun/p/5011526.html 最近简单看了一把 linux-3.10.25 kernel中select/poll/epoll ...

  4. 搜索引擎ElasticSearchV5.4.2系列一之ES介绍

    相关博文: 搜索引擎ElasticSearchV5.4.2系列一之ES介绍 搜索引擎ElasticSearchV5.4.2系列二之ElasticSearchV5.4.2+kibanaV5.4.2+x- ...

  5. 解决方案:centos运行shell脚本时报“$'\r': 未找到命令”

    =============================================== 2018/9/12_第1次修改                       ccb_warlock == ...

  6. keyspace notification(键空间通知)-待验证

    一.需求分析: 设置了生存时间的Key,在过期时能不能有所提示? 如果能对过期Key有个监听,如何对过期Key进行一个回调处理? 如何使用 Redis 来实现定时任务? 二.序言: 本文所说的定时任务 ...

  7. express中间件代理实现跨域

    前端代码 var xhr = new XMLHttpRequest(); xhr.open('post', 'http://localhost:3000', true); xhr.onreadysta ...

  8. SysV和BSD启动风格的比较

    Slackware启动脚本与System V启动脚本的区别何在? Slackware 使用BSD风格的init脚本,而很多别的发行版使用System V风格的init脚本.SysV和BSD脚本都是能让 ...

  9. CMT跟踪算法学习笔记(一)

    关于这个算法,已经有网友写出笔记. 这位网友用画图的方式总结出了算法的各个流程,并总结了源码中的各个类:http://blog.csdn.net/roamer_nuptgczx/article/det ...

  10. 移动端调试利器之vconsole

    说明 由于移动端项目在手机中调试时不能使用chrome的控制台,而vconsole是对pc端console的改写 使用方法 使用 npm 安装: npm install vconsole 使用webp ...