zoukankan      html  css  js  c++  java
  • hdu-5813 Elegant Construction(贪心)

    题目链接:

    Elegant Construction

    Time Limit: 4000/2000 MS (Java/Others)   

     Memory Limit: 65536/65536 K (Java/Others)

    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
     
    题意:
     
    问能否存在这样的一个有向无环图,使得标号为i的节点能到达a[i]个节点;
     
    思路:
     
    按大小排序,第i位的数目应该小于i;连图的时候把后面的往前连就好,连a[i]个就好,
     
    AC代码:
     
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <bits/stdc++.h>
    #include <stack>
    #include <map>
     
    using namespace std;
     
    #define For(i,j,n) for(int i=j;i<=n;i++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
     
    typedef  long long LL;
     
    template<class T> void read(T&num) {
        char CH; bool F=false;
        for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
        for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
        F && (num=-num);
    }
    int stk[70], tp;
    template<class T> inline void print(T p) {
        if(!p) { puts("0"); return; }
        while(p) stk[++ tp] = p%10, p/=10;
        while(tp) putchar(stk[tp--] + '0');
        putchar('
    ');
    }
     
    const LL mod=1e9+7;
    const double PI=acos(-1.0);
    const int inf=1e18;
    const int N=1e5+10;
    const int maxn=5e3+4;
    const double eps=1e-12;
    
    struct node
    {
        int a,id;
    }po[N];
    int cmp(node x,node y)
    {
        if(x.a==y.a)return x.id<y.id;
        return x.a<y.a;
    }
    int main()
    {
        int t,Case=0,n;
        read(t);
        while(t--)
        {
            printf("Case #%d: ",++Case);
            read(n);
            int sum=0;
            For(i,1,n)
            {
                read(po[i].a);
                sum+=po[i].a;
                po[i].id=i;
            }
            int flag=0;
            sort(po+1,po+n+1,cmp);
            For(i,1,n)
            {
                if(po[i].a>=i)
                {
                    flag=1;
                    break;
                }
            }
        if(flag)printf("No
    ");
        else 
        {
            printf("Yes
    ");
            printf("%d
    ",sum);
            int num=0;
            For(i,1,n)
            {
                for(int j=1;j<=po[i].a;j++)
                {
                    printf("%d %d
    ",po[i].id,po[j].id);
                }
            }
        }
        }
        return 0;
    }
    

      

  • 相关阅读:
    二十三种设计模式 python实现
    python logging的输出
    redis
    Django1.11序列化与反序列化
    Django1.11基础视图
    Django1.11模型类数据库操作
    Django1.11创建
    泛型全面分析和应用(二)
    泛型全面分析和应用(一)
    注解的基本盘点 -- 《Java编程思想》
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5759375.html
Copyright © 2011-2022 走看看