Ferry Loading II

Ferry Loading II

时间: 1ms        内存:128M

描述:

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?

The 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. For 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.

You may assume that 0 < n, t, m < 1440. The arrival times for each test case are in non-decreasing order.

输入:

输出:

示例输入:

2
2 10 10
0
10
20
30
40
50
60
70
80
90
2 10 3
10
30
40

示例输出:

100 5
50 2

提示:

参考答案(内存最优[760]):

#include <stdio.h>

int f,i,k,m,n,t,c,nf,leave[2000];

main(){
   scanf("%d",&c);
   while (c--) {
      scanf("%d%d%d",&n,&t,&m);
      t *= 2;
      nf = (m+n-1)/n;
      k = m - (nf-1) * n;
      for (i=0;i<k;i++) scanf("%d",&leave[0]);
      for (f=1;f<nf;f++) {
         for (i=0;i<n;i++) scanf("%d",&leave[f]);
      }
      for (f=0;f+1<nf;f++) {
         if (leave[f]+t > leave[f+1]) leave[f+1] = leave[f]+t;
      }
      printf("%d %d\n",leave[nf-1]+t/2,nf);
   }
}

参考答案(时间最优[0]):

#include <stdio.h>

int f,i,k,m,n,t,c,nf,leave[2000];

main(){
   scanf("%d",&c);
   while (c--) {
      scanf("%d%d%d",&n,&t,&m);
      t *= 2;
      nf = (m+n-1)/n;
      k = m - (nf-1) * n;
      for (i=0;i<k;i++) scanf("%d",&leave[0]);
      for (f=1;f<nf;f++) {
         for (i=0;i<n;i++) scanf("%d",&leave[f]);
      }
      for (f=0;f+1<nf;f++) {
         if (leave[f]+t > leave[f+1]) leave[f+1] = leave[f]+t;
      }
      printf("%d %d\n",leave[nf-1]+t/2,nf);
   }
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注