有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. MVVMLight-Mensenger 学习笔记

    MVVMLight-Mensenger  最近一直在使用WPF,配合美工很容易能搭配出效果不错的UI来,开发效率也明显加快 自己也是最近才开始使用Mvvmlight,通过view和viewmodel分 ...

  2. LOCAL_SHARED_LIBRARIES 与 LOCAL_LDLIBS,LOCAL_LDFLAGS的区别

    LOCAL_LDLIBS :链接的库不产生依赖关系,一般用于不需要重新编译的库,如库不存在,则会报错找不到.且貌似只能链接那些存在于系统目录下本模块需要连接的库.如果某一个库既有动态库又有静态库,那么 ...

  3. 【转】Monkey测试5-运行中停止monkey

    停止monkey自动测试步骤: 1.ps命令  查找uiautomator的进程 打开cmd命令行窗口 输入: adb shell ; ps | grep monkey; 返回来的第一个数字,即是mo ...

  4. Genymotion Android模拟器与fiddler 数据包拦截

    Genymotion: https://www.genymotion.com/fun-zone/ https://www.genymotion.com/account/create/ cls清空记录 ...

  5. Log4Net 笔记

    Log4net框架简介: Log4net 是 Apache 下的一个开源项目,log4net框架基于Apache log4j™.Log4net用于日志记录或跟踪API,它允许开发人员控制以任意粒度输出 ...

  6. NetBeans执行项目报错

    1.错误描写叙述 严重: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start ...

  7. jqGrid单元格编辑配置,事件及方法

    转自 http://blog.csdn.net/xueshijun666/article/details/18151055 // var ret = $("#in_store_list_de ...

  8. chm文件无法阅读

    当我们费劲千辛万苦从网上下载好chm文件资料后,打开后发现竟然是这个样子的: 其中主要原因是CHM文件被阻止显示了,CHM文件在NTFS格式的硬盘里的时候就会被阻止显示.我们返回我的电脑,点中我们存放 ...

  9. 【BZOJ1977】[BeiJing2010组队]次小生成树 Tree 最小生成树+倍增

    [BZOJ1977][BeiJing2010组队]次小生成树 Tree Description 小 C 最近学了很多最小生成树的算法,Prim 算法.Kurskal 算法.消圈算法等等. 正当小 C ...

  10. Grafana-----Singlestat Panel

    singlestat面板允许你显示a SINGLE series的主要的汇总统计的一系列.它将series 简化为单个数(通过查看序列中的最大值.最小值.平均值或总和).singlestat还提供阈值 ...