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. hdu 5956 The Elder

    http://acm.hdu.edu.cn/showproblem.php?pid=5956 转移方程:dp[i]=(dis[i]-dis[j])*(dis[i]-dis[j])+P+dp[j] 斜率 ...

  2. bzoj千题计划174:bzoj1800: [Ahoi2009]fly 飞行棋

    http://www.lydsy.com/JudgeOnline/problem.php?id=1800 圆上两条直径构成矩形的对角线 #include<cstdio> using nam ...

  3. [转载]ASP.NET Error – Adding the specified count to the semaphore would cause it to exceed its maximum count

    http://jwcooney.com/2012/08/13/asp-net-error-adding-the-specified-count-to-the-semaphore-would-cause ...

  4. PWN入门

    pwn ”Pwn”是一个黑客语法的俚语词 ,是指攻破设备或者系统 .发音类似“砰”,对黑客而言,这就是成功实施黑客攻击的声音——砰的一声,被“黑”的电脑或手机就被你操纵.以上是从百度百科上面抄的简介, ...

  5. 通过 EXPLAIN 分析低效 SQL 的执行计划

    每个列的简单解释如下:  select_type:表示 SELECT 的类型,常见的取值有 SIMPLE(简单表,即不使用表连接 或者子查询).PRIMARY(主查询,即外层的查询).UNION(U ...

  6. 替换openjdk的版本时遇到报错Transaction check error

    x想要使用jmap对jvm内存进行排查问题,但是默认安装的openjdk包中并不带有这个命令,需要新升级到新版本才有 而在安装新的版本时,遇到报错: :   file /usr/lib64/libns ...

  7. eclipse导入项目报错multiple annotations found at this line

    eclipsewindow-->preference-->Valdation-->将Manual和Build下复选框全部取消选择

  8. 使用管道和cronolog切割日志

    安装cronolog git clone https://github.com/fordmason/cronolog ./configure make && make install ...

  9. 一次“ora-12170 tns 连接超时”的经历

      win7    64位系统 oracle  10g   64位 plsql之前连接是好使的,突然连接不上,提示错误“ora-12170 tns 连接超时” 1.ping IP    没有问题 2. ...

  10. FileOutputSteam入门

    FileOutputSteam 字节输入流 从控制台将字节保存到本地硬盘 package com.isoftstone.io; import java.io.FileOutputStream; imp ...