URAL 1900. Brainwashing Device (dp+ output path)

Source: Internet
Author: User

1900. Brainwashing Devicetime 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 has T O Get up in the morning, go to work, return home and try to has 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 the everyone happy, but this is difficult. That's why the government decided to does 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's situated in a long underground tunnel and there are only one train inside it. Almost everyone in the town uses the train. The government have bought a brainwashing device and installed it in the train. The device is new and it effects aren ' t well understood yet, so the government decided to limit the number of the spans W Here the device would is turned on. Statistics on number of passengers travelling between each Pair of stations every morning has already been collected. Now the government should pick the spans where the device could is used to make most of the people happy. Inputthe First line contains integers  Nand kThat is the total number of the stations and the number of spans between adjacent stations where the device could be turn Ed on (2≤ N≤500; 1≤ kN? 1). The I' Th of the next N? 1 lines contains N? IIntegers. The J' th integer is the number of passengers traveling from I' Th to ( I+ J) ' th station. These numbers is non-negative and don ' t exceed 100. You can assume this every passenger uses the train only once a day. Outputin the first line output the total number of people who'll become happy because of the device. In the second line output kIntegers in the ascending order that is the indexes of the spans where the device should is turned on. The span between the station Iand I+ 1 has the index I. If the problem have multiple solutions you can output any of the them. Sample
input Output
4 15 0) 65 35
143
problem Author:Alex Samsonov (prepared by Alexander Fetisov)
problem Source:Ural Championship 2012
Tags:Dynamic programming()

Approximate test instructions:

N cities are strung together by train lanes (that is, in a row), where a device can be installed in the driveway between adjacent two cities, and passengers on trains passing through this lane will be happy.

Enter an adjacency matrix to indicate the starting terminal of the train in the day, the output K position can be placed in the position of the device, so that the number of happy people to maximize


Idea: Obviously DP, record the path, and then you need to maintain prefixes and suffixes and the like to quickly find out the passenger traffic between two cities


#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 A 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 <cl t>inline bool RD (T &ret) {char c; int sgn;if (c = GetChar (), c = = EOF) return 0;while (c! = '-'-' && (C&L t; ' 0 ' | | C> ' 9 ')) C = GetChar (); sgn = (c = = '-')? -1:1;ret = (c = = '-')? 0: (C-' 0 '); while (c = GetChar (), c >= ' 0 ' &&c <= ' 9 ') ret = ret * + (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);p Utchar (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 ': '); }}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

URAL 1900. Brainwashing Device (dp+ output path)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.