码迷,mamicode.com
首页 > 其他好文 > 详细

URAL 1900. Brainwashing Device(dp+输出路径)

时间:2015-08-03 19:11:55      阅读:369      评论:0      收藏:0      [点我收藏+]

标签:

1900. Brainwashing Device

Time limit: 1.0 second
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

input output
4 1
5 0 6
5 3
5
14
3
Problem Author: Alex Samsonov (prepared by Alexander Fetisov)
Problem Source: Ural Championship 2012

大致题意:

有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 second
typedef 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':' ');
        }
}




版权声明:本文为博主原创文章,未经博主允许不得转载。

URAL 1900. Brainwashing Device(dp+输出路径)

标签:

原文地址:http://blog.csdn.net/kalilili/article/details/47259521

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!