题目
题目链接:http://poj.org/problem?id=1065
题目来源:《挑战》练习题
简要题意: n 根木棍长度
li ,重量 wi ,需要以一个序列处理它们。
前一根木棍长度重量都不超过当前的则不需要处理时间,否则需要 1 时间,求最短处理时间。数据范围:
1⩽n⩽5000;li,wi⩽10000
题解
这题很像导弹拦截。
基本上就是排序一下,然后反向做个LIS就行了。
可以这么做的原因是偏序集的最少划分链等于反向最长链。
排序按照一维升序,一维相同,另一位升序就行了,正好暗合pair。
实现
写题目的姿势很重要,我觉得我这个pair+lower_bound的姿势相当优雅。
代码
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
const int N = 5005;
PII a[N];
int lis[N];
int main()
{
int t, n;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d%d", &a[i].fi, &a[i].se);
}
sort(a, a+n);
int ans = 0;
for (int i = n-1; ~i; i--) {
int pos = lower_bound(lis, lis+ans, a[i].se)-lis;
lis[pos] = a[i].se;
if (pos == ans) ans++;
}
printf("%d\n", ans);
}
return 0;
}