hiho 175 周 贪心 (北美startup的面试题)

#1340 : Robots Crossing River

时间限制: 10000ms
单点时限: 1000ms
内存限制: 256MB

描述

Three kinds of robots want to move from Location A to Location B and then from Location B to Location C by boat.

The only one boat between A and B and only one between B and C. Moving from A to B (and vise versa) takes 2 hours with robots on the boat. Moving from B to C (and vice versa) takes 4 hours. Without robots on the boat the time can be reduced by half. The boat between A and B starts at time 0 moving from A to B. And the other boat starts 2 hours later moving from B to C.

You may assume that embarking and disembarking takes no time for robots.

There are some limits:

1. Each boat can take 20 robots at most.

2. On each boat if there are more than 15 robots, no single kind of robots can exceed 50% of the total amount of robots on that boat.

3. At most 35 robots are allowed to be stranded at B. If a robot goes on his journey to C as soon as he arrives at B he is not considered stranded at B.

Given the number of three kinds robots what is the minimum hours to take them from A to C?

输入

Three integers X, Y and Z denoting the number of the three kinds of robots. (0 ≤ X, Y and Z ≤ 1000)

输出

The minimum hours.

样例输入
40 4 4 
样例输出
24


官方题解:



对于  x <=y+z 的理解,可以用三角形来理解

若     x>y+z 的时候,每次运20,会导致最后 x 无法运完

例如

x  ----------------------

y  ------- z -------

按照这种思路去想就行了


然后发现一个坑点:

最后非 x 类小于8的时候其实我们可以运15个一船


#include<math.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

int cmp(const int &x,const int &y)
{
    return x>=y?1:0;
}

int main()
{
    int a[10];
    //freopen("in.txt","r",stdin);
    while(scanf("%d%d%d",&a[1],&a[2],&a[3])!=EOF)
    {
        sort(a+1,a+4,cmp);
        int ans=0;
        if(a[1]<=(a[2]+a[3])) ans=ceil((a[1]+a[2]+a[3])/20.0);
        else{
            ans+=(a[2]+a[3])/10;
            a[1]-=ans*10;
            int x=(a[2]+a[3])%10;
            if(x&&x<8) a[1]-=(15-x),ans++;
            else if(x) a[1]-=x,ans++;
            ans+=ceil(a[1]/15.0);
        }
        printf("%d\n",ans*6);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值