hoj1795-Ferry Loading
Ferry Loading
Source : UVA | |||
Time limit : 1 sec | Memory limit : 32 M |
Submitted : 222, Accepted : 127
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 crosses the river, and the cars exit from the other end of the ferry.
There is a ferry across the river that can take n cars across the river in t minutes and return in t minutes. m cars arrive at the ferry terminal by a given schedule. What is the earliest time that all the cars can be transported across the river? What is the minimum number of trips that the operator must make to deliver all cars by that time?
InputThe first line of input contains c, the number of test cases. Each test case begins with n, t, m. m lines follow, each giving the arrival time for a car (in minutes since the beginning of the day). The operator can run the ferry whenever he or she wishes, but can take only the cars that have arrived up to that time.
You may assume that 0 < n, t, m < 1440. The arrival times for each test case are in non-decreasing order.
OutputFor each test case, output a single line with two integers: the time, in minutes since the beginning of the day, when the last car is delivered to the other side of the river, and the minimum number of trips made by the ferry to carry the cars within that time.
Sample Input2 2 10 10 0 10 20 30 40 50 60 70 80 90 2 10 3 10 30 40Sample Output
100 5 50 2
题意:一个渡船,每次能装n个车,运到河对面要t分钟,回来也要t分钟,有m辆车,每辆到达河边的时间不一样,按升序输入,问最少需要的时间以及最少时间对应的最少运输次数。
思路:运输次数少就一定是m/n+1次(m%n!=0)每n个运一次,单运不可能,最后一次一定是运n个因为是升序排列的,所以只要从M%n开始运,余下的每n个运一次,状态转移方程dp[i]=max(dp[i-n],p[i])+2*t;
代码:
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <vector> #include <queue> #include <algorithm> #define PII pair<int,int> #define LL long long #define M 10 using namespace std; int dp[2000],p[2000]; int main() { int c; scanf("%d",&c); while(c--){ int n,t,m; scanf("%d%d%d",&n,&t,&m); for(int i=1;i<=m;i++){ scanf("%d",&p[i]); } if(m%n==0)dp[0]=0; else dp[m%n]=p[m%n]+2*t; for(int i=m%n+n;i<=m;i+=n){ dp[i]=max(dp[i-n],p[i])+2*t; } printf("%d %d\n",dp[m]-t,m%n?(m/n+1):m/n); } return 0; }
(第一次自己动手做出的dp,没看任何题解及思路神马的。。。。。。。。。。当然。。这是一道水题。。。。。。)