有n组好朋友在公交车站前排队。第i组有ai个人。还有一辆公交车在路线上行驶。公交车的容量大小为x,即它可以同时运载x个人。 当车站来车时(车总是空载过来),一些组从会队头开始走向公交车。 当然,同一组的朋友不想分开,所以仅当公交车能容纳下整个组的时候,他们才会上车。另外,每个人不想失去自己的位置,即组的顺序不会改变。 问题时如何选择公交车的容量大小x使得它可以运走所有组的人,并且公交车每次从车站出发时没有空位?(在车里的总人数恰好达到x)?

输入格式

第一行只包含一个整数n。第二行包含n个空格分隔的整数a1,a2,…,an。

输出格式

按递增顺序输出所有可能的公交车的容量。


题解:假设一共total_people个人,那么满足条件的车的容量一定能够整除total_people;于是就枚举total_people的因子们,然后看每个因子是否能满足条件。再看每个因子是否能满足条件的时候,通过遍历数组就可以做到了,由于每组不愿意放弃自己的位置,所以就可以从前往后模拟上车,如果在某一趟上车时有一个组无法正好上车,那么对应的x就不满足条件了。

主要注意两点:

  • 枚举total_people因子的时候,只用从1枚举到sqrt(total_people),因为知道total_people的一个因子r,就可以通过total_people/r得到另外一个因子了。要特别处理的情况是r=total_people/r的情况,此时只留一个因子;
  • 还要注意最后一趟上车要保证剩下的人全部上车(参见代码17行)。

代码如下:

 import java.io.*;
import java.util.*;
import java.math.*; public class Solution {
private static boolean Check(int[] groups,int d){
int curPeople = 0;
for(int i = 0;i < groups.length;i++)
{
curPeople += groups[i];
if(curPeople > d)
return false;
if(curPeople == d)
curPeople = 0;
}
return curPeople == 0;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int total_people = 0;
int[] groups = new int[n]; for(int i = 0;i < n;i++){
groups[i]= in.nextInt();
total_people += groups[i];
} ArrayList<Integer> answer = new ArrayList<Integer>(); for(int d = 1;d*d <= total_people;d++){
if(total_people % d == 0){
if(Check(groups, d))
answer.add(d);
if(total_people/d != d && Check(groups, total_people/d))
answer.add(total_people/d);
}
}
answer.sort(null); StringBuffer sb = new StringBuffer();
for(Integer i:answer)
sb.append(i).append(" ");
System.out.println(sb.toString()); }
}

【HackerRank】Bus Station的更多相关文章

  1. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  2. 【hackerrank】Weather Observation Station 18

    题目如下: Consider  and  to be two points on a 2D plane. happens to equal the minimum value in Northern ...

  3. 【leetcode】Gas Station

    Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...

  4. 【HackerRank】How Many Substrings?

    https://www.hackerrank.com/challenges/how-many-substrings/problem 题解 似乎是被毒瘤澜澜放弃做T3的一道题(因为ASDFZ有很多人做过 ...

  5. 【Leetcode】【Medium】Gas Station

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  6. 【HackerRank】Running Time of Quicksort

    题目链接:Running Time of Quicksort Challenge In practice, how much faster is Quicksort (in-place) than I ...

  7. 【二分】Base Station Sites @ICPC2017HongKong/upcexam5559

    时间限制: 1 Sec 内存限制: 128 MB 5G is the proposed next telecommunications standards beyond the current 4G ...

  8. 【hackerrank】Week of Code 30

    Candy Replenishing Robot Find the Minimum Number 直接模拟 Melodious password dfs输出方案 Poles 题意:有多个仓库,只能从后 ...

  9. 【hackerrank】Week of Code 26

    在jxzz上发现的一个做题网站,每周都有训练题,题目质量……前三题比较水,后面好神啊,而且类型差不多,这周似乎是计数专题…… Army Game 然后给出n*m,问需要多少个小红点能全部占领 解法:乘 ...

随机推荐

  1. Spring Data Redis 2.x 中 RedisConfiguration 类的新编写方法

    在 Spring Data Redis 1.x 的时候,我们可能会在项目中编写这样一个RedisConfig类: @Configuration @EnableCaching public class ...

  2. python第三周文件处理和函数-----下

    #默认参数的值是在一开始定义的时候就传给了函数, # 在后来的修改中不会被修改. #默认参数的值必须放到位置形参参数的最后面 #默认参数使用的场景是一个参数不经常变得场景,所以参数一般是不可变类型.字 ...

  3. Vsphere日记02.ESXi5.5.configuration

    2.Vsphere ESXi 5.5 configuration 步骤1:启动 ESXI 服务器 步骤2:按 F2 进入用户登录界面 输入用户名及密码,进入参数设置界面.密码为我安装时候设置的&quo ...

  4. Java基础02 方法与数据成员(转载)

    对象中的数据成员表示对象的状态.对象可以执行方法,表示特定的动作. 此外,我们还了解了类(class).同一类的对象属于相同的类型(type).我们可以定义类,并使用该定义来产生对象.   调用同一对 ...

  5. php 循环简写

    <?php class a{ public function news($news){ $articles = array(); foreach ($news as $key => $va ...

  6. JavaBean属性

    一个JavaBean对象的属性应该是可访问的.这个属性可以是任意合法的Java数据类型,包括自定义Java类. 一个JavaBean对象的属性可以是可读写,或只读,或只写.JavaBean对象的属性通 ...

  7. 寒城攻略:Listo 教你用Swift 语言编写 IOS 平台流媒体播放器

    先展示播放器效果:   依然继承 Listo 本人的强迫症,还是从最初到完毕完整的写一个攻略来记录一下,这里声明 Listo 本人也是看了非常多的戴维营攻略才总结分享给大家这一篇攻略的. 首先,Lis ...

  8. Openstack(Kilo)安装系列之环境准备(二)

    控制节点.网络节点.计算节点: 一.配置源 1.配置EPEL源 yum install http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-rel ...

  9. cobbler 更换dns和dhcp服务器为dnsmasq

    1) 需要配置/etc/cobbler/module.conf, 把manage_dns和manage_dhcp改为manage_dnsmasq 2) 重启cobbler和dnsmasq服务,dnsm ...

  10. WPF 后台任务 等待动画 样例 && C# BackgroundWorker 详解

    运行效果: 前台代码: <Window x :Class="Waiting.Window1" xmlns="http://schemas.microsoft.com ...