Monkey and Banana HDU - 1069(dp)
原题地址:点击
一道简单的dp题,求最长的递减子序列,不过得先排个序,先给出运算符重载的代码
struct node {
int x,y,z;
bool operator < (const node & a)const {
if(x != a.x)
return x > a.x;
return y > a.y;
}//排序方式,长度相同则看宽度
bool operator >(const node & a)const {
return x > a.x && y > a.y;//方便后面做比较
}
};
一个长方体有6种摆放的方式,可以用不定长数组来写,再用初始化列表代码会更简介
附上ac码:
#include<bits/stdc++.h>
using namespace std;
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
struct node {
int x,y,z;
bool operator < (const node & a)const {
if(x != a.x)
return x > a.x;
return y > a.y;
}
bool operator >(const node & a)const {
return x > a.x && y > a.y;
}
};
int dp[200];
int main() {
fio
int n, x, y, z, ca = 0;;
while(cin >> n,n) {
ca++;
memset(dp, 0, sizeof(dp));
vector<node> q;
while(n--) {
cin >> x >> y >> z;
q.push_back(node{x,y,z});q.push_back(node{y,x,z});
q.push_back(node{x,z,y});q.push_back(node{z,x,y});
q.push_back(node{z,y,x});q.push_back(node{y,z,x});
}
sort(q.begin(), q.end());
int len = q.size(), Max = 0;
for (int i = 0; i < len; i++) {
dp[i] = q[i].z;
for (int j = 0; j < i; j++) {
if(q[j] > q[i])
dp[i] = max(dp[i], dp[j] + q[i].z);
Max = max(Max, dp[i]);
}
}
printf("Case %d: maximum height = %d\n", ca, Max);
}
}