有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. HashMap与HashTable原理及数据结构

    HashMap与HashTable原理及数据结构 hash表结构个人理解 hash表结构,以计算出的hashcode或者在hashcode基础上加工一个hash值,再通过一个散列算法 获取到对应的数组 ...

  2. 整合Thinkphp数据库基本操作CURD,界面datagrid采用EasyUi的Demo

     1 <?php  2     class CurdAction extends Action{  3         public function del($id){  4          ...

  3. php 批量处理post数据

    <?php header("Content-Type:text/html;charset=UTF-8"); include('ini.php'); foreach ($_PO ...

  4. hadoop之WordCount源代码分析

    //近期在研究hadoop.第一个想要要開始研究的必然是wordcount程序了.看了<hadoop应用开发实战解说>结合自己的理解,对wordcount的源代码进行分析. <pre ...

  5. 【Python】Python获取命令行參数

    有时候须要用同一个Python程序在不同的时间来处理不同的文件,此时假设老是要到Python程序中去改动输入.输出文件名称.就太麻烦了. 而通过Python获取命令行參数就方便多了.以下是我写得一个小 ...

  6. Android开发:《Gradle Recipes for Android》阅读笔记1.7——仓库配置

    repositories块告诉gradle哪里去寻找依赖,默认的android studio使用jcenter或者mavenCentral.jcenter仓库位于https://jcenter.bin ...

  7. 1603 限高二叉排列树(计数DP)

    1603 限高二叉排列树 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题   作为游戏魔方的编写者和管理员,Bob在很多主存模块中 ...

  8. D - Find a way

    D - Find a way Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Desc ...

  9. Hibernate 延迟加载的代理模式 和 Spring AOP的代理模式

    Hibernate 延迟加载的代理模式 和 Spring AOP的代理模式 主题 概念 Hibernate 延迟加载的代理模式 Spring AOP的代理模式 区别和联系 静态代理和动态代理 概念 代 ...

  10. border inset outset ,border-radius

    1. 例子: div{ width:256px; height:256px; border: 10px inset #f00; margin:0 auto; border-radius:255px; ...