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

来源:互联网 发布:windows 10 无法开机 编辑:程序博客网 时间:2024/05/17 08:04

#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;}


原创粉丝点击