洛谷 P3486 [POI2009]KON-Ticket Inspector
P3486 [POI2009]KON-Ticket Inspector
題目描述
Byteasar works as a ticket inspector in a Byteotian National Railways (BNR) express train that connects Byteburg with Bitwise.
The third stage of the BNR reform (The never ending saga of BNR reforms and the Bitwise hub was presented in the problems Railway from the third stage of XIV Polish OI and Station from the third stage of XV Polish OI. Their knowledge, however, is not required at all in order to solve this problem.) has begun. In particular, the salaries system has already been changed.
For example, to encourage Byteasar and other ticket inspectors to efficient work, their salaries now depend on the number of tickets (passengers) they inspect. Byteasar is able to control all the passengers on the train in the time between two successive stations, but he is not eager to waste his energy in doing so. Eventually he decided he would check the tickets exactly
Before setting out, Byteasar is given a detailed summary from which he knows exactly how many passengers will travel from each station to another. Based on that he would like to choose the moments of control in such a way that the number of passengers checked is maximal. Obviously, Byteasar is not paid extra for checking someone multiple times - that would be pointless, and would only disturb the passengers. Write a programme that will determine for Byteasar when he should check the tickets in order to maximise his revenue.
有n個車站,現在有一輛火車從1到n駛過,給出aij代表從i站上車j站下車的人的個數。列車行駛過程中你有K次檢票機會,所有當前在車上的人會被檢票,問最多能檢多少個不同的人的票
輸入輸出格式
輸入格式:
In the first line of the standard input two positive integers and (, ) are given. These are separated by a single space and denote, respectively, the number of stations en route and the number of controls Byteasar intends to make. The stations are numbered from to in the order of appearance on the route.
In the next lines the summary on passengers is given. The -th line contains information on the passengers who enter the train on the station - it is a sequence of ![](http://ma…
輸出格式:
Your programme should print out (in a single line) an increasing sequence of integers from the interval from to separated by single spaces to the standard output. These numbers should be the numbers of stations upon leaving which Byteasar should control the tickets.
輸入輸出樣例
輸入樣例#1: 復制7 2
2 1 8 2 1 0
3 5 1 0 1
3 1 2 2
3 5 6
3 2
1
輸出樣例#1: 復制
2 5
思路:dp
設f[i][j]表示第i次檢票在第j車站的最優解。
那麽就可以很容易列出狀態轉移方程:
sum是一個關於上車人數的一維前綴和。
summ是一個關於下車人數的二維前綴和。
f[i][j]=max(f[i][j],f[i-1][l]+sum[j]-sum[l]-tot);(tot=summ[j][j]-summ[l][j])
錯因:在計算tot時的式子表示錯了。
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int n,k,ans,pre; int sum[610],an[60]; int step[60][610],f[60][601]; int map[610][610],summ[610][610]; int main(){ scanf("%d%d",&n,&k); for(int i=1;i<=n;i++){ for(int j=1;j<=i;j++) summ[i][j]+=summ[i-1][j]+summ[i][j-1]-summ[i-1][j-1]; for(int j=i+1;j<=n;j++){ scanf("%d",&map[i][j]); sum[i]+=map[i][j]; summ[i][j]+=map[i][j]+summ[i-1][j]+summ[i][j-1]-summ[i-1][j-1]; } sum[i]+=sum[i-1]; } for(int i=1;i<=n;i++) f[1][i]=sum[i]-summ[i][i]-summ[i][0]-summ[0][i]+summ[0][0]; for(int i=2;i<=k;i++) for(int j=i;j<=(n-k+i);j++) for(int l=i-1;l<=j-1;l++){ int tot=summ[j][j]-summ[l][j]; if(f[i][j]<f[i-1][l]+sum[j]-sum[l]-tot){ step[i][j]=l; f[i][j]=f[i-1][l]+sum[j]-sum[l]-tot; } } for(int i=k;i<=n;i++) if(f[k][i]>ans){ ans=f[k][i];pre=i; } an[k]=pre; for(int i=k;i>1;i--){ an[i-1]=step[i][pre]; pre=step[i][pre]; } for(int i=1;i<=k;i++) cout<<an[i]<<" "; } /* 7 2 2 1 8 2 1 0 3 5 1 0 1 3 1 2 2 3 5 6 3 2 1 */
洛谷 P3486 [POI2009]KON-Ticket Inspector