一开始写的dfs进行记忆化结果不知道怎么进行路径的记录。。。改成循环就好了
dp[i] = max(dp[j]) + 1 , weight[j] < weight[j] && speed[j] > speed[i]
一开始进行一次排序使得重量递增,这样只需要考虑速度就好了
#include<cstdio> #include<algorithm> using namespace std; const int maxn = 10005; struct Mouse{ int w,s,id; friend bool operator < (Mouse p,Mouse q){ return p.w < q.w; } }mouse[maxn]; int cnt = 1; int dp[maxn]; int pre[maxn]; void print_path(int pos){ if(pos == 0) return; print_path(pre[pos]); printf("%d\n",pos); } void debug(){ for(int i = 1; i < cnt; i++) printf("(%d %d %d)\n",mouse[i].w,mouse[i].s,mouse[i].id); } int main(){ while(scanf("%d%d",&mouse[cnt].w,&mouse[cnt].s) != EOF){ //if(mouse[cnt].w == 0) break; dp[cnt] = 1; mouse[cnt].id = cnt; pre[cnt] = 0; cnt ++; } sort(mouse + 1,mouse + cnt); //debug(); int ans = 0,start = 0; for(int i = 1; i < cnt; i++){ for(int j = 1; j < i; j++){ if(mouse[j].w < mouse[i].w && mouse[j].s > mouse[i].s){ if(dp[j] + 1 > dp[i]){ dp[i] = dp[j] + 1; pre[mouse[i].id] = mouse[j].id; } } } if(ans < dp[i]){ start = mouse[i].id; ans = dp[i]; } } printf("%d\n",ans); print_path(start); return 0; } /* 1 2 3 4 5 6 7 9 9 10 3 8 */
【HDU】 1160 FatMouse's Speed (DP)
时间: 2024-10-12 17:55:00