您的位置:首页 > 其它

HDU 1051 Wooden Sticks 贪心

2015-12-18 19:57 447 查看

题意:找最长不下降子序列的个数。

思路:我是这样做的,在前面所有没有访问过的序列中找一个比当前数值要小且自身数值最大的数,并把它标记为访问过,最后数没有访问过的数的个数,即是答案。因为当前数值肯定是由前面某一个比它小的转移过来的,在这些比它小的中找一个最大的转移,肯定就是最好的结果了。我是裸的写法O(N2)O(N^2),可以通过线段树或者树状数组等数据结构将复杂度变成O(Nlog(N))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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: