HDU1160 胖老鼠的速度 dp lis的活用,玄學改bug
阿新 • • 發佈:2018-11-13
題意大致就是:求出一個最長的重量遞增,速度遞減的序列,並記錄路徑,這道題本身並不難,只是lis的活用,難的是調bug!!!!,我改了兩天,怎末改都是WA,直到剛剛,我從g++換成了c++,就ac了,哭。
程式碼:
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; const int maxn = 1e3 + 10; struct MOUSE { int weight; int speed; int place; }mouse[maxn]; int dp[maxn], path[maxn], re[maxn];//dp[I] 表示第I個位置時的最長序列的長度; void ini(int n) { for (int i = 1; i <= n; i++) { dp[i] = 1; } //memset(dp, 0, sizeof(dp)); memset(path, 0, sizeof(path)); memset(re, 0, sizeof(re)); } void solve(int n) { for (int i = 1; i <= n; i++) { for (int j = 1; j < i; j++) { if (mouse[j].weight<mouse[i].weight&&mouse[j].speed > mouse[i].speed&&dp[i] < dp[j] + 1) { path[i] = j; dp[i] = dp[j] + 1; } } } int mx = 0; int point = 0; for (int i = 1; i <= n; i++) { if (mx < dp[i]) { mx = dp[i]; point = i; } } printf("%d\n", mx); int t = 0; int po = point; for (int i = 0; i < n; i++) { re[i] = point; point = path[point]; } for (int i = mx - 1; i >= 0; i--) { printf("%d\n", mouse[re[i]].place); } } bool cmp(MOUSE a, MOUSE b) { if (a.weight == b.weight) return a.speed > b.speed; return a.weight < b.weight; } int main() { int a, b; int i = 1, n = 0; while (scanf("%d %d", &a, &b) != EOF) { mouse[i].weight = a; mouse[i].speed = b; mouse[i].place = i; n++; i++; } ini(n); sort(mouse + 1, mouse + 1 + n, cmp); solve(n); //system("pause"); return 0; }