Hdu 5813 Elegant Construction【贪心】

来源:互联网 发布:java long保留两位小数 编辑:程序博客网 时间:2024/04/30 03:51

Elegant Construction

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

2016 Multi-University Training Contest 7

 

题目大意:给出N个点接下来一行N个数,每个数表示当前编号节点能够到达其他节点的个数。让你构造出来一个图,保证其实DAG图,有SPJ。


思路: 


1、按照能够达到的节点个数按照从小到大排序,然后我们逆序处理,从能够到达点个数的点开始处理,对于这个点能够到达的点的特征:那个点能够到达的点的个数一定小于当前点能够到达点的个数。


2、那么我们贪心,从能够到达的节点个数大的开始处理设定为点i,对于当前节点,我们再从能够到达的节点个数小的开始枚举设定为点j,如果点j的数值小于点i的数值,那么就是i能够到达的点之一,因为有SPJ,我们直接输出这条边即可,并且记录下来已经有一个点是点i能够到达的了,cont++;一直枚举到cont==点i能够到达的点的个数为止。


3、那么在枚举过程中,发现了cont!=点i能够到达的点的个数,输出NO.


Ac代码:


#include<stdio.h>#include<algorithm>#include<string.h>using namespace std;struct node{    int pos,val;}a[1500];int cmp(node a,node b){    return a.val<b.val;}int ans[1500000][2];int main(){    int kase=0;    int t;    scanf("%d",&t);    while(t--)    {        int n;        scanf("%d",&n);        for(int i=0;i<n;i++)        {            scanf("%d",&a[i].val);            a[i].pos=i+1;        }        int flag=0;        int cont=0;        sort(a,a+n,cmp);        for(int i=n-1;i>=0;i--)        {            int tmpp=0;            for(int j=0;j<n;j++)            {                if(a[i].val>a[j].val)                {                    ans[cont][0]=a[i].pos;                    ans[cont++][1]=a[j].pos;                    tmpp++;                }                if(tmpp==a[i].val)break;            }            if(tmpp==a[i].val)continue;            else flag=1;        }        printf("Case #%d: ",++kase);        if(flag==1)        {            printf("No\n");            continue;        }        else printf("Yes\n");        printf("%d\n",cont);        for(int i=0;i<cont;i++)        {            printf("%d %d\n",ans[i][0],ans[i][1]);        }    }}




0 0
原创粉丝点击