accm_problem_1014

来源:互联网 发布:在4a做文案策划 知乎 编辑:程序博客网 时间:2024/06/06 12:43

题目描述:

Problem 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. <br> <br>You may assume that 0 < n, t, m < 1440. The arrival times for each test case are in non-decreasing order.
 

Sample Input
22 10 1001020304050607080902 10 3103040
 

Sample Output
100 550 2
 

大意:m个人,一辆车送n个人,运送一个来回2t时间,求最短时间和最少运送次数。。


想法:m是n的倍数,此时就是商,时间依此求出。。m<n,次数一次,时间也能求。。剩下那种情况,先把m%n当第一次,剩下的就是那个商了。。


代码:

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    int i,n,t,m;
    int time[10000];
    int C;
    //freopen("r.txt", "r", stdin);
    cin>>C;
    while(cin>>n>>t>>m&&C!=0)
    {
        C=C-1;
        int sum,sum1;
         for(i=1;i<=m;i++)
        {
            cin>>time[i];
        }
        int t1=0;
        if(m%n!=0)
        sum=m/n+1;
        else
        sum=m/n;
        if(m<=n)
        sum1=time[m]+t;
        else if(m%n==0)
        {
            for(i=n;i<m;i+=n)
            {
                if((time[i+n]-time[i])>2*t)
                    t1=t1+(time[i+n]-time[i]-2*t);
            }
            sum1=t+t1+2*t*(m/n-1)+time[n];
        }
        else
        {
            for(i=m%n;i<m;i+=n)
            {
                if((time[i+n]-time[i])>2*t)
                    t1=t1+(time[i+n]-time[i]-2*t);
            }
            sum1=t+t1+2*t*(m/n)+time[m%n];
        }
        cout<<sum1<<" "<<sum<<endl;
    }
}

感想:这题感觉还是挺难的,一开始最后一种情况没考虑好,导致总是错误,后来改了好多改才成功。。

0 0
原创粉丝点击