题意:在一个n * m的地图里,有p个宝藏,每次只能向横纵坐标增加的方向走,问最少走几次能把宝藏都拿走。
解法:按横坐标排序,纵坐标的一个不下降子序列就是一条合法路径,要求多少条不下降子序列可以覆盖所有点,这个问题可以转化为最长下降子序列的长度。
代码:
#include<stdio.h> #include<iostream> #include<algorithm> #include<string> #include<string.h> #include<math.h> #include<limits.h> #include<time.h> #include<stdlib.h> #include<map> #include<queue> #include<set> #include<stack> #include<vector> #define LL long long using namespace std; struct node { int x, y; }tre[100005]; int cmp(node a, node b) { if(a.x == b.x) return a.y < b.y; else return a.x < b.x; } vector <int> v; int main() { int p, n, m; while(~scanf("%d%d%d", &n, &m, &p)) { v.clear(); for(int i = 0; i < p; i++) scanf("%d%d", &tre[i].x, &tre[i].y); sort(tre, tre + p, cmp); for(int i = 0; i < p; i++) { vector <int> :: iterator it = upper_bound(v.begin(), v.end(), tre[i].y); if(it != v.begin()) { it--; *it = tre[i].y; } else v.insert(v.begin(), tre[i].y); } printf("%d ", v.size()); } return 0; }思想好神···知道真相的我眼泪掉下来QAQ