hdu 3177 Crixalis's Equipment (貪心)

小記:這題想的真頭疼,還好想出來了頭緒,證明的解法確實是正確的


思路:將Bi和Ai的差值進行從大到小的排序,這樣依次貪心,然後計算是否可以全部存放進去。

可以證明如果這樣的排好的一個次序不能全部存放進去的話,那麼結果就是不行的,反之即代表是可行的


代碼:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>

using namespace std;

#define mst(a,b) memset(a,b,sizeof(a))
#define REP(a,b,c) for(int a = b; a < c; ++a)
#define eps 10e-8

const int MAX_ = 5010;
const int N = 100010;
const int INF = 0x7fffffff;

struct node{
    int s, e;
}t[MAX_];

int vis[MAX_];


bool cmp(const node& a, const node& b)
{
    return (a.e-a.s) > (b.e-b.s);
}

int main()
{
	int T;
	int n, m;
	scanf("%d",&T);
	while(T-- && scanf("%d%d",&n, &m)) {
        int ans = 0;
        int cnt = 0, ss, tt;

        REP(i, 0, m) {
            scanf("%d%d", &t[i].s, &t[i].e);
        }
        sort(t,t+m,cmp);


        bool flag = 0;
        REP(i, 0, m){
            if(n >= t[i].e){
                n -= t[i].s;
            }
            else {
                flag = 1;
                break;
            }
        }

        if(!flag)printf("Yes\n");
        else printf("No\n");
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章