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. LDM和STM指令

    LDM批量加载/STM批量存储指令可以实现一组寄存器和一块连续的内存单元之间传输数据. 允许一条指令传送16个寄存器的任意子集和所有寄存器,指令格式如下: LDM{cond}  mode  Rn{!} ...

  2. javascript中IE与ff的区别

    1.自定义属性问题:可以使用获取常规属性的方法来获取自定义属性,也可以使用getAtribute()获取自定义属性,ff下只能使用getAttribute()获取自定义属性. 2. 在IE中可以用ev ...

  3. 登陆验证前对用户名和密码加密之后传输数据---base64加密

    以下这种方法是加密传输的简单实现 1,base64.js /** * * Base64 encode / decode * * */ function Base64() { // private pr ...

  4. Java 正则表达式 向前、向后匹配

    //向后匹配 String a = "I paid $90 for 10 oranges, 12 pears and 8 apples. I saved $5 on "; Patt ...

  5. HDU 4122

    Alice's mooncake shop Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  6. F-Dining Cows(POJ 3671)

    Dining Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7584   Accepted: 3201 Descr ...

  7. php -l 检查文件是否语法错误

    有时候在进行网页开发的时候,后台文件的语法错误比较难检查出来,这时候使用php -l filename可对文件的语法进行检查.

  8. display:inline-block;如何取消标签之间的距离

    <div style="font-size:0px"> <div style=" display:inline-block; zoom:1;*displ ...

  9. 修改weblogic PermGen

    vim /weblogic/Oracle/Middleware/wlserver_10.3/common/bin/commEnv.sh 在第144行,增加环境变量:JAVA_VENDOR=Sun #根 ...

  10. 解决extjs grid 不随窗口大小自适应的问题

    解决extjs grid 不随窗口大小自适应的问题 August 30, 2010 zhai Javascript 8,403 viewsGo to comment 最近遇到的问题,在使用grid的时 ...