POJ 2336 Ferry Loading II

POJ 2336 Ferry Loading II 贪心 || 伪dp

Description:NULL ( 233333因为我懒 )

哦。。这个解题报告是我从lzy自带解题报告的代码里摘来的233

最早到达对岸的时间,取决于最后一辆车的被运送时间
最优解是最后一辆车能够被尽早的运走
if(a > dp[i - j]) dp[i] = min(dp[i], 2 * t + a);
else dp[i] = min(dp[i], 2 * t + dp[i - j]);

dp[i]表示第i个车到达对岸花的最少时间, a是第i辆车的到达时间。

由于第 i 个车可以和它前面的 j(1 ~ n - 1)个车一起坐船,所以可以
得到dp[i] = min(dp[i], dp[i - j] + 2 * t);

但是第 i 个车到达的时间可能大于了dp[i - j],这样就会等第 i个车
,又得dp[i] = min(dp[i], a + 2 * t);

最后一次不用返回了,所以dp[m]-t;

船运的次数:num[i]=min{ num[i-j] }+1;

dp代码:

 1 #include <iostream>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 #include <algorithm>
 5 #include <stdio.h>
 6 #include <stdio.h>
 7
 8 using namespace std;
 9 int n, t, m, c, f[1500], num[1500];
10 int main()
11 {
12     scanf("%d", &c);
13     while(c --){
14         memset(num, 127, sizeof(num));
15         memset(f, 127, sizeof(f));
16         f[0] = 0;
17         num[0] = 0;
18         scanf("%d%d%d", &n, &t, &m);
19         for(int i = 1; i <= m; i ++){
20             int a; scanf("%d", &a);
21             for(int j = 1; j <= n; j ++){
22                 if(i - j < 0) break;
23                 if(a > f[i - j]) f[i] = min(f[i], 2 * t + a);
24                 else f[i] = min(f[i], f[i - j] + 2 * t);
25                 num[i] = min(num[i], num[i - j] + 1);
26             }
27         }
28         printf("%d %d\n", f[m] - t, num[m]);
29     }
30     return 0;
31 }

贪心版代码(lzy写的):

 1 #include <iostream>
 2 #include <iomanip>
 3 #include <string.h>
 4 #include <string>
 5 #include <math.h>
 6 #include <time.h>
 7 #include <cstdlib>
 8 #include <cstdio>
 9 #include <climits>
10 #include <algorithm>
11 #include <vector>
12 #include <stack>
13 #include <queue>
14 #include <sstream>
15
16 using namespace std ;
17
18 #define LL long long
19 #define PI M_PI
20 const int MAXN = 1e7 + 5 ; //int A[MAXN] ;
21 const int maxn = 1e4 + 5 ; //int a[maxn] ;
22
23 int et[1500];
24
25 int main()
26 {
27     freopen("ferry.in","r",stdin) ;
28     freopen("ferry.out","w",stdout) ;
29
30     int n, t, m ;
31     scanf("%d %d %d", &n, &t, &m) ;
32
33     for(int i = 1 ; i <= m ; i++)
34     {
35         scanf("%d", &et[i] ) ;
36     }
37
38     int dd = m%n ,ff = m/n ;
39     int ans=0 ,cnt=0 ;
40
41     if(dd!=0)
42     {
43         ans = et[dd] ;
44         ans = ans + t*2 ;
45         cnt++ ;
46     }
47     for(int i = dd+n ; i <= m ; )
48     {
49         if( ans< et[i])
50             ans=et[i] ;
51         ans = ans+2*t ;
52         i=i+n ;
53         cnt++ ;
54     }
55     printf("%d %d\n", ans-t, cnt++ ) ;
56
57     return 0;
58 }

贪心 by lzy

NAILED IT ..

------------------------------

时间: 2025-01-21 19:50:21

POJ 2336 Ferry Loading II的相关文章

poj-2336 Ferry Loading II(dp)

题目链接: Ferry Loading II Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3946   Accepted: 1985 Description Before bridges were common, ferries were used to transport cars across rivers. River ferries, unlike their larger cousins, run on a

POJ 3282 Ferry Loading IV(模拟,队列)

题意   汽车通过渡船过河  渡船开始在左边   输入按车辆来的顺序输入河两岸的车   渡船每次运输的汽车的总长度不能超过渡船自己本身的长度  先来的车先走   求轮船至少跨河多少次才能将所有的车辆都运完 简单模拟  建两个队列  分别装左边的车  和右边的车   算出两边各至少需要运输多少次就行了 #include<cstdio> #include<cstring> #include<queue> using namespace std; int main() { i

POJ 3282 Ferry Loading IV(简单模拟)

Description Before bridges were common, ferries were used to transport cars across rivers. River ferries, unlike their larger cousins, run on a guide line and are powered by the river's current. Cars drive onto the ferry from one end, the ferry cross

POJ 3282 Ferry Loading IV(模拟)

Description Before bridges were common, ferries were used to transport cars across rivers. River ferries, unlike their larger cousins, run on a guide line and are powered by the river's current. Cars drive onto the ferry from one end, the ferry cross

POJ2336 Ferry Loading II 贪心动规

题意:有m辆车,每次最多运n辆过河,过河过去需要t时间回来需要t时间,m辆车一开始并不是都在岸边的,给出m辆车抵达岸边的时间(只有车抵达河岸才能过河),问使得所有车辆过河所需要的最少次数 跟 最早时间 分析: 一开始看题目可能觉得有两个最优解,最少次数跟最早时间,次数最少猜测一下,m%n==0则刚好为m/n次 否则 为m/n+1次,然后考虑最早时间,时间要最早 其实就是最后一辆车最早过河了即可,那么最后一辆车尽早被送走就好,看样子可以贪心 举了几个案例试了一下,若m%n==0则不用说了,每次运n

UVa 10440 Ferry Loading II

问题: 一个渡口,有一艘渡船,运汽车到对面,一次能运n辆车,到达对面要t分钟,返回要t分钟,一共来m辆车. 给出m个数据,是到达渡口的时间.求最短把全部汽车运到对面的最短时间是多少,次数是多少(时间从0算). 思路:贪心. 考虑三种情况(也可以说是两种) 一:n>m.最短时间为最后一辆车到达渡口的时间+t,次数为1: 二:n<=m,m恰好整除n. 三:n<=m,m除以n有余数. 详细思路代码: #include <iostream> #include <cstdio&g

[POJ2336]Ferry Loading II

题目描述 Description Before bridges were common, ferries were used to transport cars across rivers. River ferries, unlike their larger cousins, run on a guide line and are powered by the river's current. Cars drive onto the ferry from one end, the ferry

poj 2464 Brownie Points II(扫描线)

题目链接:poj 2464 Brownie Points II 题意: 题意很迷啊. 有一些点,Stan选择某个点,经过这个点画一条竖线,Ollie选择一个经过这条直接的点画了条横线. Stan选一,三象限的点,Ollie选二.四象限的点. Stan的策略是,自己画一条竖线之后,Ollie有很多种选择,在所有选择中,Stan能获得数目的最小值的最大值,而Ollie的选择便 是让自己越多越好. 题解: 下面是cxlove dalao的题解: 首先对于某个方向离散化,我是按x排序,将y离散化. 建立

POJ 2536 之 Gopher II(二分图最大匹配)

Gopher II Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6675   Accepted: 2732 Description The gopher family, having averted the canine threat, must face a new predator. The are n gophers and m gopher holes, each at distinct (x, y) coor