【HDU5813】Elegant Construction(贪心)

记录一个菜逼的成长。。

将顶点按能到达的点数从小到大排序,排好序之后每个点只能往前面的点连边. 因而如果存在一个排在第i位的点,要求到达的点数大于i-1,则不可行;否则就可以按照上述方法构造出图. 复杂度O(N^2).
PS:一道水题,训练的时候条件漏了。。好TM菜啊。Orz.

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <list>
#include <deque>
#include <cctype>
#include <bitset>
#include <cmath>
using namespace std;
#define ALL(v) (v).begin(),(v).end()
#define cl(a) memset(a,0,sizeof(a))
#define fin freopen("D://in.txt","r",stdin)
#define fout freopen("D://out.txt","w",stdout)
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<PII> VPII;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int maxn = 1000 + 10;
VPII a,b;
bool cmp(PII a,PII b)
{
    if(a.first != b.first)return a.first < b.first;
    return a.second < b.second;
}
int main()
{
    //fin;
    //fout;
    int T,cas = 1;
    scanf("%d",&T);
    while(T--){
        a.clear();
        b.clear();
        int n;
        scanf("%d",&n);
        bool flag = true;
        for( int i = 1,x; i <= n; i++ ){
            scanf("%d",&x);
            a.push_back(PII(x,i));
        }
        sort(ALL(a),cmp);
        for( int i = 0; i < n; i++ ){
            if(a[i].first > i){
                flag = false;
                break;
            }
            if(!a[i].first){
                for( int j = i + 1; j < n; j++ ){
                    if(a[j].first){
                        a[j].first--;
                        b.push_back(PII(a[j].second,a[i].second));
                    }
                }
            }
        }
        printf("Case #%d: %s\n",cas++,flag?"Yes":"No");
        if(flag){
            printf("%d\n",b.size());
            for( int i = 0; i < b.size(); i++ ){
                printf("%d %d\n",b[i].first,b[i].second);
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值