hdu 4001 动态规划

比赛的时候,俊祥一说题目,我就马上意识到是动规,当时我正打着那道悲剧的题目,就让海峰打了,他二十来分钟吧就打完,而且一遍就过了。昨天晚上我再打这题,打了四十分钟,还WA了好几次才过。海峰真是强啊,佩服佩服!

/*
* hdu4001/linux.cpp
* Created on: 2011-9-5
* Author : ben
*/
#include
<cstdio>
#include
<cstdlib>
#include
<cstring>
#include
<cmath>
#include
<algorithm>
using namespace std;

typedef
struct {
int a;
int b;
int c;
int d;
} Block;
typedef
long long I64;
const int MAXN = 1010;
Block block[MAXN];
I64 dp[MAXN], N;

bool operator<(const Block &b1, const Block &b2) {
if (b1.a != b2.a) {
return b1.a < b2.a;
}
if (b1.b != b2.b) {
return b1.b < b2.b;
}
return b1.d > b2.d;
}

inline
bool judge(const Block &b1, const Block &b2) {
if (b2.a < b1.a || b2.b < b1.b) {
return false;
}
if (b2.a > b1.a && b2.b > b1.b) {
return true;
}
if (b2.d == 0) {
return true;
}
if (b2.d == 1) {
return (I64) b2.a * b2.b > (I64) b1.a * b1.b;
}
return false;
}

void work();
int main() {
#ifndef ONLINE_JUDGE
freopen(
"data.in", "r", stdin);
#endif
work();
return 0;
}

void work() {
int n, temp;
I64 ans;
while (scanf("%d", &n) == 1 && n > 0) {
for (int i = 0; i < n; i++) {
scanf(
"%d%d%d%d", &block[i].a, &block[i].b, &block[i].c,
&block[i].d);
if (block[i].a < block[i].b) {
temp
= block[i].a;
block[i].a
= block[i].b;
block[i].b
= temp;
}
}
sort(block, block
+ n);
memset(dp,
0, sizeof(dp));
for (int i = 1; i <= n; i++) {
dp[i]
= block[i - 1].c;
for (int j = i - 1; j > 0; j--) {
if (judge(block[j - 1], block[i - 1])) {
if (dp[j] + block[i - 1].c > dp[i]) {
dp[i]
= dp[j] + block[i - 1].c;
}
}
}
}
ans
= -0x7fffffff;
for (int i = 1; i <= n; i++) {
if (dp[i] > ans) {
ans
= dp[i];
}
}
printf(
"%I64d\n", ans);
}
}
原文地址:https://www.cnblogs.com/moonbay/p/2168369.html