HRBUST 2011简单dp
Posted tennant
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HRBUST 2011简单dp相关的知识,希望对你有一定的参考价值。
题意:N位士兵站成一排,长官要请其中的(N-K)位出列,使得剩下的K位士兵排成一等队形。一等队形是指这样的一种队形:设K位士兵从左到右依次编号为1,2…,K,他们的身高分别为T1,T2,…,TK, 则他们的身高满足T1<...
思路:枚举一下分割点,然后分别以递减和递增讨论一下
#include<stdio.h>
#include<string.h>
#include<queue>
#include<cmath>
#define MAX 100+10
#define INF 0x3f3f3f3f
int n;
int dp[MAX];
int a[MAX];
int b[MAX];
using namespace std;
int up(int l, int r, int tmp) {
memcpy(a, b, sizeof(b));
for (int i = l; i <= r; i++)
dp[i] = 0;
a[l - 1] = 0;
dp[l - 1] = 0;
for (int i = l; i <= r; i++) {
for (int j = l - 1; j < i; j++) {
if (a[i] >= tmp || a[j] >= tmp)
continue;
if (a[i] > a[j]) {
dp[i] = max(dp[i], dp[j] + 1);
}
}
}
int ans = 0;
for (int i = l; i <= r; i++) {
ans = max(ans, dp[i]);
}
return r - l + 1 - ans;
}
int down(int l, int r, int tmp) {
memcpy(a, b, sizeof(b));
for (int i = l; i <= r; i++)
dp[i] = 0;
a[l - 1] = INF;
dp[l - 1] = 0;
for (int i = l; i <= r; i++) {
for (int j = l - 1; j < i; j++) {
if (a[i] >= tmp)
continue;
if (a[i] < a[j]) {
dp[i] = max(dp[i], dp[j] + 1);
}
}
}
int ans = 0;
for (int i = l; i <= r; i++) {
ans = max(ans, dp[i]);
}
return r - l + 1 - ans;
}
int main(void) {
while (~scanf("%d", &n)) {
for (int i = 1; i <= n; i++)
dp[i] = 1;
for (int i = 1; i <= n; i++)
scanf("%d", &b[i]);
int ans = 0;
int minn = INF;
for (int k = 1; k <= n; k++) {
if (k == 1)
ans = down(1, n, INF);
else if (k == n)
ans = up(1, n, INF);
else {
ans = up(1, k - 1, b[k]) + down(k + 1, n, b[k]);
}
minn = min(minn, ans);
}
printf("%d\n", minn);
}
return 0;
}
以上是关于HRBUST 2011简单dp的主要内容,如果未能解决你的问题,请参考以下文章