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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值