先排序,找出最长递减序列;
题目: http://acm.nyist.net/JudgeOnline/problem.php?pid=236
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 5010;
struct Node
{
int length;
int weight;
Node()
{
length = 0;
weight = 0;
}
}arr[MAXN];
/**
int CMP(const void* a, const void* b)
{
Node* aa = (Node *)a;
Node* bb = (Node *)b;
if(aa->length == bb->length)
return aa->weight - bb->weight;
return aa->length - bb->length;
}
*/
int CMP(Node a, Node b)
{
if(a.length == b.length)
return a.weight < b.weight;
return a.length < b.length;
}
int main()
{
int T;
int n, i, top;
int a[MAXN];
int qs, qe, mid;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(i = 0; i < n; ++i)
scanf("%d %d", &arr[i].length, &arr[i].weight);
//qsort(arr, n, sizeof(arr[0]), CMP);
sort(arr, arr+n, CMP);//稍微快点
top = 0;
memset(a, 0, sizeof(a));
a[top++] = arr[0].weight;
for(i = 1; i < n; ++i)
{
if(arr[i].weight < a[top-1])
a[top++] = arr[i].weight;
else
{
qs = 0; qe = top-1;
while(qs < qe)
{
mid = ((qs + qe) >> 1);
if(a[mid] > arr[i].weight)
qs = mid + 1;
else
qe = mid;
}
a[qe] = arr[i].weight;
}
}
printf("%d\n", top);
}
return 0;
}