hdu 1160 FatMouse's Speed(水DP)

来源:互联网 发布:原野发胶淘宝假货 编辑:程序博客网 时间:2024/06/15 20:21

最长XX子序列,预处理需要排序,我的是按体重升序排序的,相同的降序处理。

寻找具体老鼠是从后往前推,如果记录的以ind 结尾的老鼠的最长XX序列 == 下一个结尾的老鼠的个数 - 1,那么说明后面那个点是经过这个点更新的(多种情况,任一均可)。

当然这个老鼠也要符合体重,速度的性质。

发现这题在ZOJ我以前过了= =。。当时记录的是前一个点的下标。。。

#include <queue>#include <stack>#include <math.h>#include <time.h>#include <stdio.h>#include <stdlib.h>#include <iostream>#include <limits.h>#include <string.h>#include <string>#include <algorithm>#define MID(x,y) ( ( x + y ) >> 1 )#define L(x) ( x << 1 )#define R(x) ( x << 1 | 1 )#define BUG puts("here!!!")using namespace std;const int MAX = 1010;struct NODE{ int w,s,ind;};NODE a[MAX];int len[MAX];stack<int> s;bool cmp(NODE a,NODE b){if( a.w == b.w )return a.s > b.s;return a.w < b.w;}int main(){int n = 0;while( ~scanf("%d%d",&a[n].w, &a[n].s) ){a[n].ind = n + 1;n++;}sort(a, a+n, cmp);memset(len, 0, sizeof(len));int sum = 0;for(int i=0; i<n; i++){int t = 0;for(int k=0; k<i; k++)if( a[i].w > a[k].w && a[i].s < a[k].s )t = max(t, len[k]);len[i] = t + 1;}int ind = max_element(len,len+n) - len;s.push(a[ind].ind);int t = ind;for(int i=ind-1; i>=0; i--)if( a[i].w < a[t].w && a[i].s > a[t].s && len[t] == len[i] + 1 ){s.push(a[i].ind);t = i;}printf("%d\n",len[ind]);while( !s.empty() ){printf("%d\n",s.top());s.pop();} return 0;}


原创粉丝点击