OJ题目:戳这里~~
描述 C小加有一些木棒,它们的长度和质量都已经知道,需要一个机器处理这些木棒,机器开启的时候需要耗费一个单位的时间,如果第i+1个木棒的重量和长度都大于等于第i个处理的木棒,那么将不会耗费时间,否则需要消耗一个单位的时间。因为急着去约会,C小加想在最短的时间内把木棒处理完,你能告诉他应该怎样做吗?
-
输入
-
第一行是一个整数T(1<T<1500),表示输入数据一共有T组。
每组测试数据的第一行是一个整数N(1<=N<=5000),表示有N个木棒。接下来的一行分别输入N个木棒的L,W(0 < L ,W <= 10000),用一个空格隔开,分别表示木棒的长度和质量。
输出
- 处理这些木棒的最短时间。 样例输入
-
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
样例输出
-
2 1 3
#include <iostream> #include <string> #include <string.h> #include <map> #include <stdio.h> #include <algorithm> #include <queue> #include <vector> #include <math.h> #include <set> #define Max(a,b) ((a)>(b)?(a):(b)) #define Min(a,b) ((a)<(b)?(a):(b)) #define SWAP(a , b){ int temp = a; a = b; b = temp;} #define inf 1000000000 using namespace std; typedef long long LL; struct Node { int x; int y; Node(){} Node(int i,int j):x(i),y(j){} }; bool cmp(const Node &A , const Node &B) { if(A.x == B.x) return A.y <= B.y; else return A.x < B.x; } int main() { int t; cin >> t; int n; while(t--) { Node node[5002]; int ans = 0; cin >> n; int a,b; for(int i = 0;i < n;i++) { scanf("%d%d",&a,&b); node[i] = Node(a,b); } sort(node, node + n, cmp); for(int i = 0;i < n;i++) { int t = node[i].y; if(t == 0) continue; ans++; for(int j = i + 1;j < n;j++) { if(t <= node[j].y) { t = node[j].y; node[j].y = 0; } } } printf("%d\n",ans); } return 0; } //下面的更牛逼点 const int Max_N = 5008 ; const int inf = 100000 ; struct Node{ int x ; int y ; friend bool operator < (const Node &A , const Node &B){ if(A.x != B.x) return A.x < B.x ; else return A.y < B.y ; } friend bool operator >= (const Node &A , const Node &B){ return A.x >= B.x && A.y >= B.y ; } }; Node node[Max_N] ; int num[Max_N] ; int now[Max_N] ; int dp[Max_N] ; int N ; int DP(){ fill(now , now+N , inf) ; for(int i = 0 ; i < N ; i++){ *lower_bound(now , now+N ,num[i]) = num[i] ; } return lower_bound(now , now+N ,inf) - now ; } int main(){ int t , i , ans ; scanf("%d" , &t) ; while(t--){ scanf("%d" ,&N) ; for(i = 1 ; i <= N ; i++) scanf("%d%d" ,&node[i].x , &node[i].y) ; sort(node+1 , node+1+N) ; for(i = N ; i >= 1 ; i--) num[N-i] = node[i].y ; printf("%d\n" , DP()) ; } return 0 ; }