lcs...
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 1001
typedef struct NODE_ {
int w, s, idx;
}NODE;
NODE val[MAXN];
int dp[MAXN], pre[MAXN];
int cmp(const NODE &a, const NODE &b)
{
return a.w < b.w;
}
void back_track(const int &idx)
{
if( -1 == idx ) {
return;
}
back_track(pre[idx]); printf("%d\n", val[idx].idx);
}
int dynamic_programming(const int &n)
{
int max_len(0), ans_idx;
memset(dp, 0, sizeof(dp)); memset(pre, -1, sizeof(pre));
for(int i = 1; i < n; i ++) {
for(int j = 1; j < i; j ++) {
if( val[i].w > val[j].w && val[i].s < val[j].s && dp[i] < dp[j]+1 ) {
dp[i] = dp[j]+1; pre[i] = j;
if( max_len < dp[i] ) {
max_len = dp[i]; ans_idx = i;
}
}
}
}
printf("%d\n", max_len+1); back_track(ans_idx);
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
#endif
int pos(1);
while( ~scanf("%d %d", &val[pos].w, &val[pos].s) ) {
val[pos].idx = pos ++;
}
sort(val+1, val+pos, cmp); dynamic_programming(pos);
return 0;
}
uva_10131 Is Bigger Smarter?
最新推荐文章于 2020-04-04 00:19:47 发布