[codility]tape_equilibrium
http://codility.com/demo/take-sample-test/tapeequilibrium
简单题。记录到i为止的sum就可以了。O(n)。
// you can also use includes, for example:
// #include <algorithm>
int solution(vector<int> &A) {
// write your code in C++98
int total = 0;
vector<int> sum;
for (int i = 0; i < A.size(); i++) {
total += A[i];
sum.push_back(total);
}
int ans = 1 << 30;
for (int i = 0; i < A.size() - 1; i++) {
int tmp = (total - 2 * sum[i]);
if (tmp < 0) tmp = -tmp;
if (ans > tmp) ans = tmp;
}
return ans;
}
[codility]tape_equilibrium的更多相关文章
- Codility NumberSolitaire Solution
1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...
- codility flags solution
How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...
- GenomicRangeQuery /codility/ preFix sums
首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...
- *[codility]Peaks
https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...
- *[codility]Country network
https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...
- *[codility]AscendingPaths
https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...
- *[codility]MaxDoubleSliceSum
https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...
- *[codility]Fish
https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...
- *[codility]CartesianSequence
https://codility.com/programmers/challenges/upsilon2012 求笛卡尔树的高度,可以用单调栈来做. 维持一个单调递减的栈,每次进栈的时候记录下它之后有 ...
随机推荐
- 一个layer可以跟着画完的线移动ios程序 好玩啊。
用法:采用的是关键帧实现的. 实验目的:让上层的layer子层能够跟着在另一个子层上花的线进行移动 .即当线画完之后,图形开始移动,并且能够停在最后的那个位置 效果图: 采用是直接在layer图层上进 ...
- asp:手机扫描二维码跳转手机版
如果想手机扫描用pc版网站生成的二维码跳转到对应的手机版的话,请在pc端的首页的<head></head>标签里面加入下面内容: <script src=" ...
- 对比MySQL,什么场景MongoDB更适用
原文链接: http://page.factj.com/blog/p/4078 MongoDB已经流行了很长一段时间,相对于MySQL,究竟什么场景更需要用MongoDB?下面是一些总结. 更高的写入 ...
- android 电话拨号器
电话拨号器(重点) 1.产品经理: 需求分析文档,设计原型图 2.UI工程师: 设计UI界面 3.架构师: 写架构,接口文档 4.码农: 服务端,客户端 ...
- 问题解决:form表单的button按钮问题
最近写了一个KendoUI 的kendowindow组件,组件里包含了一个form表单,表单里有一个button,不是提交保存的按钮,而是链接到另一个kendowindow的按钮,跟请求提交毫无关系, ...
- IO流04_InputStream和Reader输入流
[输入流中的字符流和字节流] [InputStream和Reader] InputStream和Reader是所有输入流的抽象基类,本身不能实例化,但是他们是所有输入流的模板. [ InputStre ...
- [leetcode] 400. Nth Digit
https://leetcode.com/contest/5/problems/nth-digit/ 刚开始看不懂题意,后来才理解是这个序列连起来的,看一下第几位是几.然后就是数,1位数几个,2位数几 ...
- bzoj1002:[FJOI2007]轮状病毒
思路:一道很裸的生成树计数问题,然而要高精度,而且听说直接行列式求值会被卡精度,所以可以模拟行列式求值的过程得到递推公式:f[i]=3*f[i-1]-f[i-2]+2,证明详见vfk博客: http: ...
- Wix: Using Patch Creation Properties - Small Update
Source Reference: wix help document -- WiX Toolset License Using Patch Creation Properties A patch ...
- away3d打包到IOS锯齿问题解决办法
很多ios设备是高清屏,一个像素顶普通设备四个像素.从这点上也许可以入手解决. 先把<requestedDisplayResolution>high</requestedDispla ...