A Mini Locomotive POJ - 1976(动态规划+思维)
題意:有三個火車頭,n個車廂,每個車廂里面對應的有一定的人數。規定每個火車頭最多 拉m個連續的車廂而且他們拉的車廂一定是從左到右連續的,問它能夠拉的最多的人數;
思路:類似01背包的解法,首先每個火車最多拉m個連續的車廂,這里我們把只要存在 連續的m個車廂的就看成一個物品。相當于往背包容量為3的背包里面放物品所得的最 大價值量。但是這里注意每連續的m個車廂為一個物品, f[i][j] = max(f[i - 1][j],f[i - m][j - 1] + sum[i] - sum[i - m]); 這里對于每個物品要么不放,要么就是放(放連續的m個車廂) sum[i] = a[0] + a[1] + ... + a[i]; 之前看到這個解題思路感覺一點有疑問:會不會有重復,第一節拉1,2;第二節拉 2,3這樣的,最后結論是不會;因為不取這個車廂的話,那必然就是【i-1】【j】, 如果取的話那么肯定就是【i-m】【j-1】,跳到了i-m了,所以不會重
Time limit? ? ? 1000 ms
Memory limit? ? ? ?30000 kB
OS? ? Linux
Source? ? ??Tehran Sharif 2004 Preliminary
B - A Mini Locomotive
?POJ - 1976?
A train has a locomotive that pulls the train with its many passenger coaches. If the locomotive breaks down, there is no way to pull the train. Therefore, the office of railroads decided to distribute three mini locomotives to each station. A mini locomotive can pull only a few passenger coaches. If a locomotive breaks down, three mini locomotives cannot pull all passenger coaches. So, the office of railroads made a decision as follows:?
1. Set the number of maximum passenger coaches a mini locomotive can pull, and a mini locomotive will not pull over the number. The number is same for all three locomotives.?
2. With three mini locomotives, let them transport the maximum number of passengers to destination. The office already knew the number of passengers in each passenger coach, and no passengers are allowed to move between coaches.?
3. Each mini locomotive pulls consecutive passenger coaches. Right after the locomotive, passenger coaches have numbers starting from 1.?
For example, assume there are 7 passenger coaches, and one mini locomotive can pull a maximum of 2 passenger coaches. The number of passengers in the passenger coaches, in order from 1 to 7, is 35, 40, 50, 10, 30, 45, and 60.?
If three mini locomotives pull passenger coaches 1-2, 3-4, and 6-7, they can transport 240 passengers. In this example, three mini locomotives cannot transport more than 240 passengers.?
Given the number of passenger coaches, the number of passengers in each passenger coach, and the maximum number of passenger coaches which can be pulled by a mini locomotive, write a program to find the maximum number of passengers which can be transported by the three mini locomotives.?
Input
The first line of the input contains a single integer t (1 <= t <= 11), the number of test cases, followed by the input data for each test case. The input for each test case will be as follows:?
The first line of the input file contains the number of passenger coaches, which will not exceed 50,000. The second line contains a list of space separated integers giving the number of passengers in each coach, such that the i?thnumber of in this line is the number of passengers in coach i. No coach holds more than 100 passengers. The third line contains the maximum number of passenger coaches which can be pulled by a single mini locomotive. This number will not exceed 1/3 of the number of passenger coaches.?
Output
There should be one line per test case, containing the maximum number of passengers which can be transported by the three mini locomotives.
Sample Input
1 7 35 40 50 10 30 45 60 2Sample Output
240題意:有三個火車頭,n個車廂,每個車廂里面對應的有一定的人數。規定每個火車頭最多 拉m個連續的車廂而且他們拉的車廂一定是從左到右連續的,問它能夠拉的最多的人數;
思路:類似01背包的解法,首先每個火車最多拉m個連續的車廂,這里我們把只要存在 連續的m個車廂的就看成一個物品。相當于往背包容量為3的背包里面放物品所得的最 大價值量。但是這里注意每連續的m個車廂為一個物品, f[i][j] = max(f[i - 1][j],f[i - m][j - 1] + sum[i] - sum[i - m]); 這里對于每個物品要么不放,要么就是放(放連續的m個車廂) sum[i] = a[0] + a[1] + ... + a[i]; 之前看到這個解題思路感覺一點有疑問:會不會有重復,第一節拉1,2;第二節拉 2,3這樣的,最后結論是不會;因為不取這個車廂的話,那必然就是【i-1】【j】, 如果取的話那么肯定就是【i-m】【j-1】,跳到了i-m了,所以不會重
#include<iostream> #include<algorithm> #include<string.h> using namespace std; int m,n,w[50010],dp[4][50010],s[50010]; int main() {int t;cin>>t;while(t--){cin>>m;memset(s,0,sizeof(s));for(int i=1; i<=m; i++){cin>>w[i];s[i]=s[i-1]+w[i];}cin>>n;memset(dp,0,sizeof(dp));for(int i=1; i<=3; i++)//對于每段max車廂都有拉或不拉兩種情況,for(int j=n; j<=m; j++)//但是需要比較的不是前一次最多可拉多少乘客,因為這樣被選車廂有沖突,dp[i][j]=max(dp[i][j-1],dp[i-1][j-n]+s[j]-s[j-n]);/*所以需要和前max次比較。*/cout<<dp[3][m]<<endl;}return 0; } /*題意概括:在車站有的時候需要用迷你火車頭來拉乘客,現在知道車站 有3個迷你火車頭,給出每個迷你火車頭可以拉幾節車廂,已經n個車廂即 每個車廂的乘客數,小火車必須拉連續的車廂,乘客不能換車廂。問3個 迷你火車頭最多能拉多少乘客? 解題思路:看完題也能想到這是一個貪心的問題。首先可以根據給出的火 車頭最多拉車廂的數max算出每max節車廂有多少乘客,然后對這些連續的 max車廂的乘客數進行選擇,但是如果選擇一段車廂,那么后面算出的max 節車廂會有沖突,需要把這些沖突的車廂跳過去。關鍵就是要退出狀態轉 移方程,這題和01背包類似*/?
總結
以上是生活随笔為你收集整理的A Mini Locomotive POJ - 1976(动态规划+思维)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Dollar Dayz POJ - 31
- 下一篇: FATE HDU - 2159(二维完全