SGU 482 Impudent Thief dp
来源:互联网 发布:王宝强人品知乎 编辑:程序博客网 时间:2024/06/02 02:16
题目链接:点击打开链接
题意:
给定n列箱子,下面n个数表示每列的箱子个数
每次可以拿走一列箱子,拿走中间列的箱子后两边的箱子会向中间靠拢(即中间不会有缝隙)
拿走箱子后的周长必须 *2 >= 原周长
问:最多拿走几个箱子(即使得面积最大)
输出能拿走的最大面积
输出拿走的列数
输出拿走具体哪几列
思路:
dp[i][j]表示前i列 留下第i列后剩下面积为j时最大的周长。
顺便记录下前驱什么的。。
#include<iostream>#include<stdio.h>#include<string.h>#include <algorithm>#include<string>#include<set>#include<vector>#include<queue>#include<math.h>template <class T>inline bool rd(T &ret) {char c; int sgn;if (c = getchar(), c == EOF) return 0;while (c != '-' && (c<'0' || c>'9')) c = getchar();sgn = (c == '-') ? -1 : 1;ret = (c == '-') ? 0 : (c - '0');while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');ret *= sgn;return 1;}template <class T>inline void pt(T x) {if (x <0) {putchar('-');x = -x;}if (x>9) pt(x / 10);putchar(x % 10 + '0');}using namespace std;typedef long long ll;typedef pair<int, int> pii;const int N = 51;int a[N], n;int dp[N][5005];pii pre[N][5005];int go(int x, int y){if (x >= y)return 2;else return (y - x + 1)*2;}set<int>G;vector<int> D;void dfs(pii x){if (x.first <= 0)return;G.insert(x.first);x = pre[x.first][x.second];dfs(x);}int main(){while (cin >> n){ int all = 0, len = 0;for (int i = 1; i <= n; i++){rd(a[i]);all += a[i];len += go(a[i - 1], a[i]);}memset(dp, -1, sizeof dp);for (int i = 0; i <= n; i++)for (int j = 0; j <= i * 100; j++)pre[i][j] = pii(-1, -1);dp[0][0] = 0;for (int i = 0; i < n; i++)for (int j = 0; j <= i * 100; j++){if (dp[i][j] == -1)continue;for (int k = i + 1; k <= n; k++){if (dp[k][j + a[k]] >= dp[i][j] + go(a[i], a[k]))continue;dp[k][j + a[k]] = dp[i][j] + go(a[i], a[k]);pre[k][j + a[k]] = pii(i, j);}}len = (len + 1) >> 1;int ans = 0; pii lala;for (int i = 1; i <= n; i++)for (int j = 0; j <= i * 100; j++)if (dp[i][j] >= len && ans <= all - j){ans = all - j;lala = pii(i, j);}pt(ans); puts("");G.clear();dfs(lala);D.clear();for (int i = 1; i <= n; i++)if (!G.count(i))D.push_back(i);pt((int)D.size()); puts("");for (int i = 0; i < D.size(); i++)printf("%d%c", D[i], i + 1 == D.size() ? '\n' : ' ');}return 0;}
0 0
- SGU 482 Impudent Thief dp
- sgu482 Impudent Thief (动态规划)
- sgu 482 DP 482页
- codeforces632E. Thief in a Shop (dp)
- codeforces_632E.Thief in a Shop(dp)
- [DP] Codeforces Gym 101002 H. Jewel Thief
- SGU 168 Matrix DP
- SGU 195 树形DP
- sgu 168 Matrix DP
- SGU 104 DP
- SGU 131-状压dp
- SGU 256 Balloons(dp)
- sgu 495 概率dp
- SGU 134 树形DP
- SGU 223(状压+dp)
- HDU 3469 Catching the Thief (博弈 + DP递推)
- CodeForces 632E Thief in a Shop(DP)
- [DP 决策单调性] Codeforces Gym 101002H .Jewel Thief
- linux 下route命令
- 第十五章 15.3节练习
- HDU 3779 Railroad
- Liferay Portal 连接配置MySQL数据库 (三)
- ios项目icon和default图片命名规则
- SGU 482 Impudent Thief dp
- UITableViewCell的简介与重构,以及代码
- JAVA 封装, 继承, 多态
- git安装
- 零基础带你3步快速掌握iSCSI搭建
- 调用没有通过接口的属性及方法
- 安卓程序员之殇
- 安装配置hadoop2.2.0
- 关于启动Tomcat出现异常:java.lang.ClassCastException: org.springframework.web.filter.CharacterEncodingFilter