1. 程式人生 > >poj 1065

poj 1065

class rst style cond ret space str algo spa

據說是貪心加dp,其實就是貪心

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=5000+10;
typedef pair<int,int> pp;
pp a[maxn];
int t,n;
int use[maxn];
bool cmp1(pp x,pp y)
{
    return (x.first<=y.first)||(x.first==y.first&&x.second<=y.second);
}
int main() { scanf("%d",&t); while(t--) { memset(use,0,sizeof(use)); scanf("%d",&n); for(int i=0; i<n; i++) scanf("%d%d",&a[i].first,&a[i].second); sort(a,a+n,cmp1); int ans=0; for(int i=0; i<n; i++) {
int ff=a[i].second; if(!use[i])//一個新的上升子序列開始 { for(int j=i+1; j<n; j++) if(a[j].second>=ff&&!use[j]) { use[j]=1; ff=a[j].second;//更新最大值,也就是這個上升子序列的最後一個元素,不斷更新 } ans
++; } } printf("%d\n",ans); } return 0; }

poj 1065