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. svn 备份后双机同步热备失效,提示 W200007 target server does not support atomic revision property edits svynsync:E170009

    svn 备份后双机同步热备失效,提示 W200007 target server does not support atomic revision property edits; consider u ...

  2. FZU 2093 找兔子 状压DP

    题目链接:找兔子 n的范围是[1, 15],可以用0 到 (1<<n)-1 的数表示全部状态,用dp[i] = t表示到达状态i的最少时间是t,对于每个点,如果它能到达的所有点在t秒时都已 ...

  3. 学习记录013-NFS相关知识点

    一.NFS相关知识点 1.NFS常用的路径/etc/exports NFS服务主配置文件,配置NFS具体共享服务的地点/usr/sbin/exportfs NFS服务的管理命令,exportfs -a ...

  4. 在Ios里UIWebView参入js

    //修改图片大小适应webView宽高度            [webView stringByEvaluatingJavaScriptFromString:       @"var sc ...

  5. Java中断言的使用(转)

    相信学过c,c++的朋友一定不会对断言感到陌生.下面介绍一下断言在JAVA中的使用,JAVA是从JDK1.4才开始支持断言的(添加了关键字assert),请注意老版的JRE不支持. 断言概述 编写代码 ...

  6. [Elasticsearch] 多字段搜索 (三) - multi_match查询和多数字段 <译>

    multi_match查询 multi_match查询提供了一个简便的方法用来对多个字段执行相同的查询. NOTE 存在几种类型的multi_match查询,其中的3种正好和在“了解你的数据”一节中提 ...

  7. Redis系列-存储篇string主要操作函数小结

    通过上两篇的介绍,我们的redis服务器基本跑起来.db都具有最基本的CRUD功能,我们沿着这个脉络,开始学习redis丰富的数据结构之旅,当然先从最简单且常用的string开始. 1.新增 a)se ...

  8. 数据结构-AVL树

    实现: #ifndef AVL_TREE_H #define AVL_TREE_H #include "dsexceptions.h" #include <iostream& ...

  9. source 命令与“ . ”点命令

    http://wenku.baidu.com/link?url=r3_WjJwQziv5wooIiatYbIMotPHcop56ZyakNGFor5DgJLQD-orAwVmOwp80RAnJ3tRD ...

  10. Installing Cygwin and Starting the SSH Daemon

    This chapter explains how to install Cygwin and start the SSH daemon on Microsoft Windows hosts. Thi ...