有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. 搞IT的技术人员为什么会如此苦逼

    http://www.cnblogs.com/springmvc-hibernate/archive/2012/05/10/2493733.html ————————————————————————— ...

  2. cookie做订单

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. DeepLearnToolbox-master代码学习笔记

    卷积神经网络(CNN)博大精深,网上资料浩如烟海,让初学者无从下手.笔者以为,学习编程还是从代码实例入们最好.目前,学习CNN最好的代码实例就是,DeepLearnToolbox-master,不用装 ...

  4. Prime pair connection (Project Euler 134)

    题目大意: 对于连续的质数$p1$, $p2$, 满足$5 <= p1 <= 1000000$ 求出最小的整数$S$, 它以 $p1$结尾并且能够被$p2$整除. 求$S$的和. 思路: ...

  5. ios -生成推广海报

    #import "ViewController.h" #import "Masonry.h" @interface ViewController () @end ...

  6. PHP设置时区的方法

    第一种方法:修改php.ini文件   即:date.timezone = '修改的时区名称'   对全局有效 第二种方法:date_default_timezone_set()动态设置时区,只是当前 ...

  7. SQL.Cookbook 读书笔记5 元数据查询

    第五章 元数据查询 查询数据库本身信息 表结构 索引等 5.1 查询test库下的所有表信息 MYSQL SELECT * from information_schema.`TABLES` WHERE ...

  8. 【BZOJ4825】[Hnoi2017]单旋 线段树+set

    [BZOJ4825][Hnoi2017]单旋 Description H 国是一个热爱写代码的国家,那里的人们很小去学校学习写各种各样的数据结构.伸展树(splay)是一种数据结构,因为代码好写,功能 ...

  9. Introduction to Mathematical Thinking - Week 9

    错题 评分出错 题目要求的是 "any" ,而答案只给出了一个.所以认为回答者没有理解题意,连 any 都没有理解.所以 0 分. 第一,标准的归纳法只能对自然数使用,而题目要求的 ...

  10. TFS二次开发-基线文件管理器(3)-源码文件的读取

    TFS登录成功后,就可以开始读取源码目录树了. 一般来说,我不建议将整个树全部读取出来,因为这里不光存有项目文件,还有项目源码.如果全部读取出会是非常大的一棵树.因此我建议只读出根目录,每一次点击打开 ...