Elegant Construction
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1021 Accepted Submission(s): 534
Special Judge
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!
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.
3
2 1 0
2
1 1
4
3 1 1 0
2
1 2
2 3
Case #2: No
Case #3: Yes
4
1 2
1 3
2 4
3 4
思路:按照a[i]排序,针对每个点i,对于每个小于i的点j,看是否有a[i]个数满足a[j]<a[i]。然后依次连边即可。由于每次都贪心选择小的所以不会重复
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional> using namespace std; #define LL long long
const int INF = 0x3f3f3f3f; int n,u[10005*1005],v[1005*1005];
struct node
{
int id,x;
friend bool operator<(node a,node b)
{
return a.x<b.x;
}
}a[1005]; int main()
{
int t,cas=0;
scanf("%d",&t);
while(t--)
{
int flag=1,cnt=0;
printf("Case #%d: ",++cas);
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i].x),a[i].id=i;
sort(a+1,a+1+n);
if(a[1].x!=0) {printf("No\n");continue;}
int k=2;
while(a[k].x==0) k++;
for(int i=k;i<=n;i++)
{
if(a[i].x>i-1) {flag=0;break;}
for(int j=1;j<=a[i].x;j++)
u[cnt]=a[i].id,v[cnt++]=a[j].id;
}
if(!flag) {printf("No\n");continue;}
printf("Yes\n%d\n",cnt);
for(int i=0;i<cnt;i++)
printf("%d %d\n",u[i],v[i]);
}
return 0;
}