poj 1065 Wooden Sticks 【贪心 新思维】

时间:2024-01-09 23:20:38

题目地址:http://poj.org/problem?id=1065

Sample Input

3
5
4 9 5 2 2 1 3 5 1 4
3
2 2 1 1 2 2
3
1 3 2 2 3 1

Sample Output

2
1
3 题目抽象:给你一个序列,序列的每个元素包含两个值(x,y).现在希望找到最少数目的条件序列。
条件序列是这样的:cur.x<=(cur+1).x && cur.y<=(cur+1).y
满足条件的序列的最少的数目是多少?
代码:
 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <cmath>
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <algorithm>
#define N 100000+100 using namespace std; struct node
{
int len, weight;
bool operator<(const node &dd)const{
if(len==dd.len)
return weight<dd.weight;
else
return len<dd.len;
}
}q[]; int main()
{
int tg; scanf("%d", &tg);
int i, j, k;
int n; while(tg--){
scanf("%d", &n);
for(i=; i<n; i++){
scanf("%d %d", &q[i].len, &q[i].weight );
} sort(q, q+n);
int ans=;
node cur=q[]; bool vis[];
memset(vis, false, sizeof(vis));
vis[]=true;
//从当前出发 遍历整个数组只要有结构体满足条件就贪心吃掉
//不断的进行这样的贪心,直到整个结构体数组集合元素全被吃掉为止
//不同于以往的贪心的是:当前的不可用,不代表以后的不可用 并非遇到遇到一个不可用的
//情况就停止了本组的计算!
while(true){
for(j=; j<n; j++){
if( vis[j]==false && q[j].len>=cur.len && q[j].weight>=cur.weight ){
cur=q[j]; vis[j]=true;
}
}//
ans++; //完成了一次
for(j=; j<n; j++){
if(vis[j]==false){
cur=q[j]; vis[j]=true; break;
}
}
if(j==n) break;
}
printf("%d\n",ans);
}
return ;
}