HDU 1051 Wooden Sticks 贪心

题意:找最长不下降子序列的个数。
思路:我是这样做的,在前面所有没有访问过的序列中找一个比当前数值要小且自身数值最大的数,并把它标记为访问过,最后数没有访问过的数的个数,即是答案。因为当前数值肯定是由前面某一个比它小的转移过来的,在这些比它小的中找一个最大的转移,肯定就是最好的结果了。我是裸的写法 O(N2) ,可以通过线段树或者树状数组等数据结构将复杂度变成 O(Nlog(N))

http://acm.hdu.edu.cn/showproblem.php?pid=1051

/*********************************************
    Problem : HDU 1051
    Author  : NMfloat
    InkTime (c) NM . All Rights Reserved .
********************************************/

#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

#define rep(i,a,b)  for(int i = (a) ; i <= (b) ; i ++)
#define rrep(i,a,b) for(int i = (b) ; i >= (a) ; i --)
#define repE(p,u) for(Edge * p = G[u].first ; p ; p = p -> next)
#define cls(a,x)   memset(a,x,sizeof(a))
#define eps 1e-8

using namespace std;

const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const int MAXN = 5e3+5;
const int MAXE = 2e5+5;

typedef long long LL;
typedef unsigned long long ULL;

int T,n,m,k;

struct Node {
    int x,y;
}t[MAXN];

int fx[] = {0,1,-1,0,0};
int fy[] = {0,0,0,-1,1};

bool vis[5005];

bool cmp(Node a1,Node a2) {
    if(a1.x == a2.x) return a1.y < a2.y;
    return a1.x < a2.x;
}

void input() {
    scanf("%d",&n);
    rep(i,1,n) {
        scanf("%d %d",&t[i].x,&t[i].y);
    }
}

void solve() {
    int pos;
    sort(t+1,t+1+n,cmp);
    cls(vis,0);
    rep(i,1,n) {
        pos = 0;
        rep(j,1,i-1) {
            if(!vis[j] && t[j].y <= t[i].y && t[j].y > t[pos].y) {
                pos = j;
            }   
        }
        vis[pos] = 1;
    }
    int cnt = 0;
    rep(i,1,n) {
        if(!vis[i])  cnt ++;
    }
    printf("%d\n",cnt);
}

int main(void) {
    //freopen("a.in","r",stdin);
    scanf("%d",&T); while(T--) {
    //while(~scanf("%d %d",&n,&m)) {
    //while(scanf("%d",&n),n) {
        input();
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值