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;
}


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