HDU 5813 Elegant Construction
构造。从a[i]最小的开始放置,例如放置了a[p],那么还未放置的,还需要建边的那个点 需求量-1,然后把边连起来。
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<iostream> using namespace std; typedef long long LL; const double pi=acos(-1.0),eps=1e-8; void File() { freopen("D:\\in.txt","r",stdin); freopen("D:\\out.txt","w",stdout); } inline int read() { char c = getchar(); while(!isdigit(c)) c = getchar(); int x = 0; while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); } return x; } const int maxn=1000+10; int T,n,sz; struct X{int f,num; }s[maxn],ans[maxn*maxn]; bool cmp(X a, X b) {return a.num<b.num; } int main() { scanf("%d",&T); int cas=1; while(T--) { scanf("%d",&n); sz=0; for(int i=1;i<=n;i++) { scanf("%d",&s[i].num); s[i].f=i; } sort(s+1,s+1+n,cmp); bool fail=0; for(int i=1;i<=n;i++) { if(s[i].num!=0) { fail=1; break; } for(int j=i+1;j<=n;j++) { if(s[j].num==0) continue; ans[sz].f=s[i].f; ans[sz].num=s[j].f; sz++; s[j].num--; } } printf("Case #%d: ",cas++); if(fail==1) { printf("No\n"); continue; } else printf("Yes\n"); printf("%d\n",sz); for(int i=0;i<sz;i++) printf("%d %d\n",ans[i].num,ans[i].f); } return 0; }