Linear dp [USACO08MAR] River Crossing S (Los Valley P2904)

[USACO08MAR]River Crossing S

Title Description

Farmer John is herding his N cows (1 <= N <= 2,500) across the expanses of his farm when he finds himself blocked by a river. A single raft is available for transportation.

FJ knows that he must ride on the raft for all crossings and that that adding cows to the raft makes it traverse the river more slowly.

When FJ is on the raft alone, it can cross the river in M minutes (1 <= M <= 1000). When the i cows are added, it takes M_i minutes (1 <= M_i <= 1000) longer to cross the river than with i-1 cows (i.e., total M+M_1 minutes with one cow, M+M_1+M_2 with two, etc.). Determine the minimum time it takes for Farmer John to get all of the cows across the river (including time returning to get more cows).

Farmer John and his N (1 <= N <= 2,500) cows intends over a river, but all the tools they cross the river, just a raft. Because cows are not rowing, cross the river in the whole process, FJ must always be on the raft. On this basis, the number of cows on the raft for each additional 1, FJ take the raft to the other side would have to spend more time. FJ when a person sitting on the raft, he needs to take the raft across M (1 <= M <= 1000) min. When the number of cows from the raft carrying i-1 increased to i, FJ much to spend M_i (1 <= M_i <= 1000) minutes before the raft across the river (that is, on board a cow, FJ too flowers M + M_1 minutes crossing; when two cows on board, time becomes M + M_1 + M_2 minutes later and so on). Well, FJ least how long it takes to put all the cows to the other side of it? Of course, this time a person have to include FJ take the raft back to take over a number of times from the other side of the cow.

Input Format

  • Line 1: Two space-separated integers: N and M

  • Lines 2…N+1: Line i+1 contains a single integer: M_i

Output Format

  • Line 1: The minimum time it takes for Farmer John to get all of the cows across the river.

Did not see the beginning of the title, I have not understood the sample, and finally found a cattle across the river every time it takes is M_1, M_2, rather than M_i;

This problem is starting to think: dp [i] [j] = min (dp [i] [j], dp [i-1] [jk] + sum [k] + 2 * m);

He represents times before i, j shipped cow minimum time, and then found a degree of complexity n ^ 3, pay up 60 minutes;

Finally observed that the first few times it does not matter, could be deleted, and reducing the complexity of the one-dimensional, one-dimensional space is reduced;

This question is the greatest achievement of this reasoning, as well as appropriate optimization;

Code:

#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define ls k<<1
#define rs k<<1|1
#define inf 0x3f3f3f3f
using namespace std;
const int N=3000;
const int M=2000100;
const LL mod=1e8;
int dp[N],sum[N];
int main(){
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>sum[i],sum[i]+=sum[i-1];
	for(int i=1;i<=n;i++) dp[i]=2e9;
	dp[0]=0;
	for(int i=1;i<=n;i++){
		for(int j=0;j<=i;j++){
			dp[i]=min(dp[i],dp[i-j]+sum[j]+2*m);
		}
	}
	cout<<dp[n]-m<<endl;
	return 0;
}

60 points Code:

#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define ls k<<1
#define rs k<<1|1
#define inf 0x3f3f3f3f
using namespace std;
const int N=3000;
const int M=2000100;
const LL mod=1e8;
int dp[N][N],sum[N];
int main(){
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>sum[i],sum[i]+=sum[i-1];
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++) dp[i][j]=2e9;
	}
	dp[0][0]=0;
	for(int i=1;i<=n;i++){
		for(int j=0;j<=n;j++){
			for(int k=0;k<=j;k++){
				dp[i][j]=min(dp[i][j],dp[i-1][j-k]+sum[k]+2*m);
			}
		}
	} 
	cout<<dp[n][n]-m<<endl;
	return 0;
}
Published 264 original articles · won praise 46 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_44291254/article/details/105295050