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. 流媒体技术学习笔记之(十四)FFmpeg进行笔记本摄像头+麦克风实现流媒体直播服务

    FFmpeg推送视频流,Nginx RTMP模块转发,VLC播放器播放,实现整个RTMP直播 查看本机电脑的设备 ffmpeg -list_devices true -f dshow -i dummy ...

  2. CSS3 定位| Position研究

    视区(视口) 当浏览者查看一份网页文件时,通常使用者代理(User Agents, UA, 浏览器)会提供给浏览者一个视区(视窗或者是画面里的其它可视区域).当我们调整视区大小时,UA 就有可能会改变 ...

  3. 训练赛第二场E题 Cottage Village

    题目大意:在一条X轴上,有若干个正方形,并且保证这些正方形的中心都在X轴上,然后输入n个正方形的中心的X坐标,和正方形的边长,现在要再插入一个正方形,要求是,新插入的正方形至少要有一条边与原来的正方形 ...

  4. python技巧 使用值来排序一个字典

    In [8]: a={'x':11,'y':22,'c':4} In [9]: import operator In [10]: sorted(a.items(),key=operator.itemg ...

  5. 第10月第1天 iOS crash

    1. find /Applications/Xcode6.1.app -name symbolicatecrash -type f tempdeMac-mini:crash temp$ dwarfdu ...

  6. git与代码托管工具

    1.git初识 git是一个版本管理工具,用来管理项目的不同的版本,记录下不同的提交记录,git还可以构建不同的分支,用来给不同的人来推送使用. 推荐的git教程:https://www.cnblog ...

  7. WEB开发常用软件集合

    软件 dreamweaver cs6 http://www.cr173.com/soft/74348.html navicat http://pan.baidu.com/s/1b9nNzw subli ...

  8. thinkphp5学习总结!

    数据库操作之原生sql操作 <?php namespace app\index\controller; use think\Db; class Index { public function i ...

  9. 【干货】Linux内存数据的获取与转存 直捣密码

    知识源:Unit 2: Linux/Unix Acquisition 2.1 Linux/Unix Acquistion Memory Acquisition 中的实验demo部分  小白注意,这是网 ...

  10. C# Json To Object 无废话

    json字符串如下: { success : 0, errorMsg : "错误消息", data : { total : "总记录数", rows : [ { ...