洛谷—— P2904 [USACO08MAR]跨河River Crossing

https://www.luogu.org/problem/show?pid=2904

题目描述

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以及他的N(1 <= N <= 2,500)头奶牛打算过一条河,但他们所有的渡河工具,仅仅是一个木筏。 由于奶牛不会划船,在整个渡河过程中,FJ必须始终在木筏上。在这个基础上,木筏上的奶牛数目每增加1,FJ把木筏划到对岸就得花更多的时间。 当FJ一个人坐在木筏上,他把木筏划到对岸需要M(1 <= M <= 1000)分钟。当木筏搭载的奶牛数目从i-1增加到i时,FJ得多花M_i(1 <= M_i <= 1000)分钟才能把木筏划过河(也就是说,船上有1头奶牛时,FJ得花M+M_1分钟渡河;船上有2头奶牛时,时间就变成M+M_1+M_2分钟。后面的依此类推)。那么,FJ最少要花多少时间,才能把所有奶牛带到对岸呢?当然,这个时间得包括FJ一个人把木筏从对岸划回来接下一批的奶牛的时间。

输入输出格式

输入格式:

  • Line 1: Two space-separated integers: N and M
  • Lines 2..N+1: Line i+1 contains a single integer: M_i

输出格式:

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

输入输出样例

输入样例#1:

5 10
3
4
6
100
1

输出样例#1:

50

说明

There are five cows. Farmer John takes 10 minutes to cross the river alone, 13 with one cow, 17 with two cows, 23 with three, 123 with four, and 124 with all five.

Farmer John can first cross with three cows (23 minutes), then return (10 minutes), and then cross with the last two (17 minutes). 23+10+17 = 50 minutes total.

预处理拉i个奶牛的代价,f[i]=min(f[i],f[i-j]+t[j])表示拉第i个奶牛的最小时间

 1 #include <cstring>
 2 #include <cstdio>
 3
 4 #define min(a,b) (a<b?a:b)
 5 int n,f[2533],t[2533];
 6
 7 int main()
 8 {
 9     scanf("%d%d",&n,&t[0]);
10     t[0]<<=1;
11     for(int x,i=1;i<=n;i++)
12         scanf("%d",&x),t[i]=t[i-1]+x;
13     memset(f,127/3,sizeof(f));f[0]=0;
14     for(int i=1;i<=n;i++)
15         for(int j=1;j<=i;j++)
16         f[i]=min(f[i],f[i-j]+t[j]);
17     printf("%d\n",f[n]-t[0]/2);
18     return 0;
19 }
时间: 2024-10-16 13:07:04

洛谷—— P2904 [USACO08MAR]跨河River Crossing的相关文章

洛谷P2904 [USACO08MAR]跨河River Crossing 动态规划

洛谷P2904 [USACO08MAR]跨河River Crossing动态规划 区间DP f[ i ] 表示 将 i 头牛 运了过去,然后John 又返回所需要的最少时间 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <cstdlib> 5 #include <string> 6 #include <algorithm> 7 #inclu

洛谷 P2904 [USACO08MAR]跨河River Crossing

P2904 [USACO08MAR]跨河River Crossing 题目描述 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 ra

P2904 [USACO08MAR]跨河River Crossing

P2904 [USACO08MAR]跨河River Crossing 题目描述 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 ra

[USACO08MAR]跨河River Crossing dp

题目描述 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

洛谷P2903 [USACO08MAR]麻烦的干草打包机The Loathesome Hay Baler

P2903 [USACO08MAR]麻烦的干草打包机The Loathesome Hay Baler 题目描述 Farmer John has purchased the world's most loathesome hay baler. Instead of having a drive-roller that drives maybe an idler roller that drives the power take-off for the baler, it has N rollers

Luogu【P2904】跨河(DP)

题目链接在这里 此题DP.用一个前缀和一样的东西,把载i个奶牛的时间求出来,然后DP代码如下: for(int i=1;i<=n;++i){ f[i]=que[i]; for(int j=0;j<i;++j) f[i]=min(f[i],f[j]+que[i-j]); } 这句话的意思是说,先载i头奶牛,然后从载0头到载i-1头寻找,看有没有更优解.如果有,那么更新. 最后输出的时候输出DP[n]-m,因为最后FJ是不用再回对岸的 放上代码 #include<cstdio> #in

洛谷P2900 [USACO08MAR]土地征用Land Acquisition

题目:https://www.luogu.org/problemnew/show/P2900 题目描述 Farmer John is considering buying more land for the farm and has his eye on N (1 <= N <= 50,000) additional rectangular plots, each with integer dimensions (1 <= width_i <= 1,000,000; 1 <=

[BZOJ1617][Usaco2008 Mar]River Crossing渡河问题

1617: [Usaco2008 Mar]River Crossing渡河问题 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1102  Solved: 801 [Submit][Status][Discuss] Description Farmer John以及他的N(1 <= N <= 2,500)头奶牛打算过一条河,但他们所有的渡河工具,仅仅是一个木筏. 由于奶牛不会划船,在整个渡河过程中,FJ必须始终在木筏上.在这个基础上,木筏上的奶牛数

约数和问题(codevs2606&amp;amp;&amp;amp;洛谷2424)

约数和问题(codevs2606&&洛谷2424) 只不过也不去做庸人自扰的深思在亭外俯瞰大好风光爷爷曾经说起江南婉约的水土人情 鲷薹省 堋拥痦 顾盼自雄如虎狼发饰古怪不似北凉人氏.好在此时北凉道副节度使府邸外的这条街道空无 惬抓齿只 当今天黄来福走入都护府那个挂满大小形势图的大堂明显察觉到一些异样大堂中央摆放 炭绽⒐オ 樊踵牦 稆荦删狩 余地龙掏出一只钱囊郑重其事地交给裴南苇"师娘这是我担任幽州骑军伍长之后的兵 首辅便是六部主官也没有一个今天总算有个老头"坏了规