URAL 1900. Brainwashing Device(dp+输出路径)
来源:互联网 发布:网络大电影发行流程 编辑:程序博客网 时间:2024/05/22 09:51
1900. Brainwashing Device
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
While some people travel in space from planet to planet and discover new worlds, the others who live on Earth still have to get up in the morning, go to work, return back home and try to have a rest. They don't like this situation anymore and envy people who can afford space travel.
That doesn't suit the government of the Earth. Their goal is to make everyone happy, but that is difficult. That is why the government decided to do some tests in the small town of Lux, and then, if everything goes well, to apply the experience to other cities.
Lux's distinctive feature is that it is situated in a long underground tunnel and there is only one train inside it. Almost everyone in the town uses the train. The government has bought a brainwashing device and installed it in the train. The device is new and its effects aren't well understood yet, so the government decided to limit the number of the spans where the device would be turned on. Statistics on number of passengers travelling between each pair of stations every morning have already been collected. Now the government should pick the spans where the device could be used to make most of the people happy.
Input
The first line contains integers n and k that are the total number of the stations and the number of spans between adjacent stations where the device could be turned on (2 ≤ n ≤ 500; 1 ≤ k ≤ n − 1). The i'th of the next n − 1 lines contains n − i integers. The j'th integer is the number of passengers traveling from i'th to (i + j)'th station. These numbers are non-negative and don't exceed 100. You can assume that every passenger uses the train only once a day.
Output
In the first line output the total number of people who will become happy because of the device. In the second line output k integers in the ascending order that are the indexes of the spans where the device should be turned on. The span between the station i and i + 1 has the index i. If the problem has multiple solutions you can output any of them.
Sample
4 15 0 65 35
143
Problem Author: Alex Samsonov (prepared by Alexander Fetisov)
Problem Source: Ural Championship 2012
Problem Source: Ural Championship 2012
Tags: dynamic programming
)大致题意:
有n个城市被列车道串起来(即位于一排),可以在相邻的两个城市间的车道上装一个装置,然后经过此车道的列车上的乘客就会变得开心。
输入一个邻接矩阵表示一天中列车的起始终点站,输出k个位置可以安放装置的位置,使开心的人数最大化
思路:显然的DP,记录路径即可,然后就是需要维护前缀和和后缀和之类的快速求出两城市间的乘客流量
//#pragma comment(linker, "/STACK:1024000000,1024000000")#include <iostream>#include <cstring>#include <cmath>#include <queue>#include <stack>#include <map>#include <set>#include <string>#include <vector>#include <cstdio>#include <ctime>#include <bitset>#include <algorithm>#define SZ(x) ((int)(x).size())#define ALL(v) (v).begin(), (v).end()#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)#define reveach(i, v) for (__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++ i)#define REP(i,n) for ( int i=1; i<=int(n); i++ )using namespace std;typedef long long ll;#define X first#define Y secondtypedef pair<ll,ll> pii;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');}const int N = 550;int mp[N][N];int dp[N][N];int sum[N][N];int f[N][N];int n,k;int pre[N][N];int res[N],top;void ini(){ memset(mp,0,sizeof(mp)); memset(dp,0,sizeof(dp)); memset(sum,0,sizeof(sum)); memset(f,0,sizeof(f)); memset(pre,-1,sizeof(pre)); top = 0;}int main(){ while(~scanf("%d%d",&n,&k)){ ini(); REP(i,n){ for(int j = i+1; j <= n; j++) { RD(mp[i][j]); sum[i][j] = sum[i-1][j]+mp[i][j]; } } REP(x,n) for(int y = x+1; y <= n;y++) { f[x][y] = f[x][y-1]+mp[x][y]; } REP(c,k) for(int i = c+1; i <= n;i++){ int tmp = 0; for(int j = i-1; j >= c;j--){ tmp = tmp-f[j+1][i]+sum[j][j+1]; if( dp[j][c-1]+tmp >= dp[i][c]){ dp[i][c] = dp[j][c-1]+tmp; pre[i][c] = j; } } } printf("%d\n",dp[n][k]); for(int ans = pre[n][k--]; ~ans ; ans = pre[ans][k--]){ res[++top] = ans; } sort(res+1,res+1+top); REP(i,top) printf("%d%c",res[i],i == top? '\n':' '); }}
0 0
- URAL 1900. Brainwashing Device(dp+输出路径)
- URAL 1900. Brainwashing Device
- URAL 1900 Brainwashing Device (dp)
- URAL 1900 Brainwashing Device (dp,4级)
- 【URAL 1900】Brainwashing Device(预处理区间和+DP)
- URAL 1900 Brainwashing Device
- Timus 1900 Brainwashing Device
- 【URAL 1244】Gentlemen(DP+记录路径)
- URAL 1183.Brackets Sequence ( DP+记录路径)
- 【URAL 1183】Brackets Sequence(区间DP+路径记录)
- 【URAL 1635】Mnemonics and Palindromes(区间DP+记录路径)
- URAL 1029 Ministry(记录路径的dp)
- poj 2353(简单dp+输出路径)
- poj1015(dp输出路径)
- CodeForces864E【DP+路径输出】
- ural 1017. Staircases(dp)
- ural 1057(数位dp)
- Ural 1225 Flags(DP)
- Java内存泄露的理解与解决
- source在线调试sass自动保存在服务器上
- 20150802厦门大学华为校园提前批招聘机试体验题三:Word Maze(单词迷宫)
- 指针操作一维数组
- 如何用Redis做LRU-Cache
- URAL 1900. Brainwashing Device(dp+输出路径)
- JS 基础 用法
- Touch
- hdoj-1171-Big Event in HDU【动态规划】
- JAVA 程序 对给定整数60,获取其16进制表现形式
- C++客户端 如何通过gsoap调用WebService接口(以及返回结果乱码解决办法)
- cosme大赏卸妆部门第二位:Peau Blanche卸妆水之体验报告
- java发送邮件(1)
- zoj 3612 ——Median