HackerRank "Equal Stacks"
Greedy - though simple, but fun!
#include <vector>
#include <iostream> using namespace std; int main(){
int n1;
int n2;
int n3;
cin >> n1 >> n2 >> n3;
long long l1 = , l2 = , l3 = ;
vector<int> h1(n1);
for(int h1_i = ;h1_i < n1;h1_i++){
cin >> h1[h1_i];
l1 += h1[h1_i];
}
vector<int> h2(n2);
for(int h2_i = ;h2_i < n2;h2_i++){
cin >> h2[h2_i];
l2 += h2[h2_i];
}
vector<int> h3(n3);
for(int h3_i = ;h3_i < n3;h3_i++){
cin >> h3[h3_i];
l3 += h3[h3_i];
} int i1 = , i2 = , i3 = ;
while(!(l1 == l2 && l2 == l3))
{
// which stack is highest now?
int maxi = ;
int maxl = l1;
if(maxl < l2)
{
maxi = ;
maxl = l2;
}
if(maxl < l3)
{
maxi = ;
maxl = l3;
} //
switch(maxi)
{
case :
l1 -= h1[i1++];
break;
case :
l2 -= h2[i2++];
break;
case :
l3 -= h3[i3++];
break;
} if(!l1 || !l2 || !l3)
{
l1 = l2 = l3 = ;
}
}
cout << l1 << endl;
return ;
}
HackerRank "Equal Stacks"的更多相关文章
- HackerRank - candies 【贪心】
HackerRank - candies [贪心] Description Alice is a kindergarten teacher. She wants to give some candie ...
- Hackerrank - The Grid Search
https://www.hackerrank.com/challenges/the-grid-search/forum 今天碰见这题,看见难度是Moderate,觉得应该能半小时内搞定. 读完题目发现 ...
- Hackrank Equal DP
Christy is interning at HackerRank. One day she has to distribute some chocolates to her colleagues. ...
- Infix to Prefix conversion using two stacks
Infix : An expression is called the Infix expression if the operator appears in between the operands ...
- [LeetCode] Minimum Moves to Equal Array Elements II 最少移动次数使数组元素相等之二
Given a non-empty integer array, find the minimum number of moves required to make all array element ...
- [LeetCode] Minimum Moves to Equal Array Elements 最少移动次数使数组元素相等
Given a non-empty integer array of size n, find the minimum number of moves required to make all arr ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Implement Queue using Stacks 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- Equal Sides Of An Array
参考:http://stackoverflow.com/questions/34584416/nested-loops-with-arrays You are going to be given an ...
随机推荐
- Docker简明教程
Docker简明教程 [编者的话]使用Docker来写代码更高效并能有效提升自己的技能.Docker能打包你的开发环境,消除包的依赖冲突,并通过集装箱式的应用来减少开发时间和学习时间. Docker作 ...
- Bzoj索引
1001 : http://ideone.com/4omPYJ1002 : http://ideone.com/BZr9KF1003 : http://ideone.com/48NJNh1004 : ...
- Gnu C的不同于标准C的语法
2. ,## 是与逗号合在一起用的, 表示后面有变量,则显示逗号,若后面无变量,则不显示逗号, 这种情况适用于用宏替换可变参数的函数,调用的时候可能传一个参数,或传两个参数, 这种打印语句在平台上,函 ...
- [原]centos6.5系统可用yum源(32位)以及rpmforge
[10gen] name=10gen Repository baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/i686 gpgche ...
- hibernate.hbm2ddl.auto配置详解
hibernate.cfg.xml 中hibernate.hbm2ddl.auto配置节点如下:<properties><property name="hibernate. ...
- iredmail安装脚本分析(一)---iRedmail.sh
iredmail是一套以postfix为核心的整合邮件系统的安装脚本,可以达到快速部署邮件服务器的目的.为了让自己不遗忘shell的语法,所以闲来无事,学习一下他的代码. 我从官网下载他的最新版,解压 ...
- Android 媒体存储服务(二)
Android 媒体存储服务 简介: 本文是<深入Android媒体存储服务>系列第二篇,简要介绍媒体存储服务扫描文件的流程.文中介绍的是 Android 4.2. Android 有一套 ...
- Python学习-day1
Mark一下,python学习. 今天一天已1.5x的速度看完了Alex老师的第一周的视频,先是4节鸡汤课,而且给了勺,讲述了python目前在世界的地位,发展趋势,以及未来的愿景. 最重要的还是写一 ...
- myeclipse里的调试快捷键
好多时候在调试代码时,有的时候只会用F5,其他的快捷键却一概不知.今天百度查了一下其他快捷键调试的作用,总结如下 F5 (setp into ): 跳入当前执行的方法中 F6 (step over): ...
- rsync+inotify实现自动备份
1.rsync命令 文件在不同服务器之间备份,有两种方式,一种是推,另一种是拉 推:即由主服务器推送数据到次服务器 拉:即由次服务器向主服务器要数据 一般采用拉的方式 但是这样同时多服务器备份就会分享 ...