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 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?

Input

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.

Output

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.

Sample Input

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

Sample Output

100 5
50 2

【题意】给出m辆车达到码头的时间,小船每次只能运过去n辆车,耗时t一趟。求最短时间和次数

【思路】贪心

  1. 最短时间只和最后一只船有关系. 如果 m%n==0 则每次都运n个,必是最优。
  2. 否则,第一次运 m%n个, 可保证最优。
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int n,m,t;
int main()
{
int ti;
scanf("%d",&ti);
while(ti--)
{
scanf("%d%d%d",&n,&t,&m);
int a=m%n;
int k=m/n;
if(a!=)//余数不为0,多运一次
{
k++;
}
int mint=;
int tmp;
int i=;
if(a)//余数不为0,第一次将a辆车运过去
{
for(i=;i<a;i++)
{
scanf("%d",&tmp);
}
mint=tmp+*t;//a辆车中最后一辆车达到的时间出发
}
int cnt=;
while(i<m)//另外的m/n组
{
scanf("%d",&tmp);
cnt++;
if(cnt==n)//到n一组的车辆
{
if(tmp>mint)//时间晚的话,从tmp时间出发
mint=tmp+*t;
else mint+=*t;//时间早的话,直接出发,没有等待时间
cnt=;//清零
}
i++;
}
mint-=t;//最后一趟,不用回来了
printf("%d %d\n",mint,k);
}
return ;
}

Ferry Loading II_贪心的更多相关文章

  1. poj 2336 Ferry Loading II ( 【贪心】 )

    Ferry Loading II Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3704   Accepted: 1884 ...

  2. TOJ 2419: Ferry Loading II

    2419: Ferry Loading II  Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByteTotal Subm ...

  3. Ferry Loading III[HDU1146]

    Ferry Loading IIITime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...

  4. POJ-2336 Ferry Loading II(简单DP)

    Ferry Loading II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3763 Accepted: 1919 Desc ...

  5. POJ 2609 Ferry Loading(双塔DP)

    Ferry Loading Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1807   Accepted: 509   Sp ...

  6. poj-2336 Ferry Loading II(dp)

    题目链接: Ferry Loading II Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3946   Accepted: ...

  7. Ferry Loading||

    uva10440:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&am ...

  8. [POJ2336]Ferry Loading II

    题目描述 Description Before bridges were common, ferries were used to transport cars across rivers. Rive ...

  9. 【HDOJ】1406 Ferry Loading III

    模拟,注意需要比较队头与当前时间的大小关系. #include <cstdio> #include <cstring> #include <cstdlib> #de ...

随机推荐

  1. hdu---(4515)小Q系列故事——世界上最遥远的距离(模拟题)

    小Q系列故事——世界上最遥远的距离 Time Limit: 500/200 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)T ...

  2. 超详细的Xcode代码格式化教程,可自定义样式。

    超详细的Xcode代码格式化教程,可自定义样式. 为什么要格式化代码 当团队内有多人开发的时候,每个人写的代码格式都有自己的喜好,也可能会忙着写代码而忽略了格式的问题.在之前,我们可能会写完代码后,再 ...

  3. Android之ScrollView嵌套ListView

    在ScrollView中嵌套使用ListView,ListView只会显示的内容只有一行多一点 解决方案: public class Utility { public void setListView ...

  4. Struts2 用 s:if test 判断String类型的对象属性值和单字符是否相等的问题

    Struts2 用 s:if test 判断String类型的对象属性值和单字符是否相等的问题   首先,这里所指的单字符形如:Y,男. 有两种做法: a. <s:if test='news.s ...

  5. Objective-C:@property参数详解

    格式:@property(param1, param2) 类型 属性名 参数可有可无 三类参数: 1.读写属性:readwrite/readonly readwrite:产生setter\getter ...

  6. 有一种感动叫ACM(记WJMZBMR在成都赛区开幕式上的讲话)

    各位选手,各位教练,大家好,我是来自清华大学交叉信息学院的陈立杰,今天很荣幸站在这里代表全体参赛选手发言.对于我来说,这是我第一次正式参加ACM的比赛.不过我跟ACM之间的缘分,大概在很早的时候就已经 ...

  7. Java DSL简介(收集整理)

    一.领域特定语言(DSL) 领域特定语言(DSL)通常被定义为一种特别针对某类特殊问题的计算机语言,它不打算解决其领域外的问题.对于DSL的正式研究已经持续很多年,直 到最近,在程序员试图采用最易读并 ...

  8. PHP 单引号和双引号的区别

    $a = 'jfdjaff';$b = '234125';$c = '"jj $a $b"'.PHP_EOL;echo $c;$c = 'jj $a $b'.PHP_EOL;ech ...

  9. mysql 在insert 时防止出现主键冲突错误的方法

    在mysql中插入数据的时候常常因为主键存在而冲突报错,下面有两个解决方法: 1.在insert 语句中添加ignore 关键字,如:insert ignore into table (id,name ...

  10. EF中的那些批量操作

    在使用EF的过程中,我们经常会遇到需要批量操作数据的场景,批量操作有的时候不仅能提高性能,比如使用SqlBulkCopy进入批量插入的时候,而且比较方便操作,提高效率.那么这篇文章就来总结EF中的那些 ...