hdu 1051 Wooden Sticks (水題,貪心)

小記:這題比較水


思路:按長或重從小到大排序,然後對排好序的序列根據重或長(注意是和前面的相反的)求最長子序列,求出一個答案就加一,然後將這個最長子序列的所有值都標記

然後對剩下的值,繼續求最長子序列。

簡單點的方法就是,一路往上,碰到比你大的就標記,到末尾了就答案加一,然後重新繼續來,直到全部都被標記了。

這個方法可能有點問題,但是對hdu的數據能過。


代碼:

#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)
{
    if(a.s < b.s)return 1;
    else if(a.s == b.s){
        if(a.e < b.e)return 1;
        else return 0;
    }
    else    return 0;
}

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

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

        mst(vis, 0);
        while(cnt != n){
            int k = -1;
            REP(i, 0, n){
                if(!vis[i] &&(k==-1|| t[i].e >= t[k].e)){
                    k = i;
                    cnt++;
                    vis[i] = 1;
                }
            }
            ans ++;
        }


        printf("%d\n", ans);
	}
	return 0;
}


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