【HackerRank】Bus Station
有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的更多相关文章
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- 【hackerrank】Weather Observation Station 18
题目如下: Consider and to be two points on a 2D plane. happens to equal the minimum value in Northern ...
- 【leetcode】Gas Station
Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...
- 【HackerRank】How Many Substrings?
https://www.hackerrank.com/challenges/how-many-substrings/problem 题解 似乎是被毒瘤澜澜放弃做T3的一道题(因为ASDFZ有很多人做过 ...
- 【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 ...
- 【HackerRank】Running Time of Quicksort
题目链接:Running Time of Quicksort Challenge In practice, how much faster is Quicksort (in-place) than I ...
- 【二分】Base Station Sites @ICPC2017HongKong/upcexam5559
时间限制: 1 Sec 内存限制: 128 MB 5G is the proposed next telecommunications standards beyond the current 4G ...
- 【hackerrank】Week of Code 30
Candy Replenishing Robot Find the Minimum Number 直接模拟 Melodious password dfs输出方案 Poles 题意:有多个仓库,只能从后 ...
- 【hackerrank】Week of Code 26
在jxzz上发现的一个做题网站,每周都有训练题,题目质量……前三题比较水,后面好神啊,而且类型差不多,这周似乎是计数专题…… Army Game 然后给出n*m,问需要多少个小红点能全部占领 解法:乘 ...
随机推荐
- Crontab使用方式
Liunx系统的定时任务需要Crontab来完成 一.添加 添加定时脚本 crontab -e 或者直接编辑/etc/crontab文件进行任务添加 vim /etc/crontab 二.格式 三.举 ...
- eclipse通过maven远程发布应用到Tomcat
好久没有写博客了,今天为大家分享一下如何在eclipse通过maven远程发布应用到Tomcat. 一般情况下,我们发布应用到服务器需要现将应用导出成war包,然后连接服务器部署更新,这样是很耗时的, ...
- Win10:如何修改双网卡的优先级?
很多使用双网卡的IT之家网友可能遇到一种情况,比如笔记本电脑在插上网线后还是用WiFi,得手动关闭无线连接才能转换到有线连接.如何才能调整合适的网络优先级呢?一般来说,有两种方法比较常用. 一.调整网 ...
- Linux Linux程序练习四
编写两个不同的可执行程序,名称分别为a和b,b为a的子进程. 在a程序中调用open函数打开a.txt文件. 在b程序不可以调用open或者fopen,只允许调用read函数来实现读取a.txt文件. ...
- Working with JSON in C# & VB
Introduction Whilst JSON is a compact and easy to read cross-language storage and data exchange form ...
- WPF TextBox 验证输入
//验证输入为数字private void txt_time_KeyDown(object sender, KeyEventArgs e){ if (!((e.Key >= Key.D0 &am ...
- Unity3D学习笔记——NGUI之UIButton
前言:用于接受点击,悬停,触摸等事件.UIButton还有重要的用途,就是改变控件不同状态下的颜色. 一:使用方式: 1.在UI Root中右键创建一个Sprite 2.为其添加一个碰撞组件,就添加B ...
- Unity3D学习笔记——IDE工作视图
Unity3D中五个界面的使用: Project视图:存放游戏资源,比如贴图,音频,JS脚本等 Project中可创建的文件如下: Hierarchy视图:主要存放游戏场景中的对象,如摄像机,精灵,箱 ...
- MathType公式行距设置的方法
在使用普通的文档编辑器编辑数学公式的时候,大家会发现一些数学上特殊的符号.公式很难给编辑出来,有时候就算编辑出来了也不符号一些学术的规范.这个时候就可以使用MathType这款公式编辑器来编辑.但是在 ...
- SET ANSI_NULLS ON 在T-SQL中是什么意思
from:https://www.cnblogs.com/kekong/p/6731321.html Transact-SQL 支持在与空值进行比较时,允许比较运算符返回 TRUE 或 FALSE. ...