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的更多相关文章

  1. Codility NumberSolitaire Solution

    1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...

  2. codility flags solution

    How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...

  3. GenomicRangeQuery /codility/ preFix sums

    首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...

  4. *[codility]Peaks

    https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...

  5. *[codility]Country network

    https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...

  6. *[codility]AscendingPaths

    https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...

  7. *[codility]MaxDoubleSliceSum

    https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...

  8. *[codility]Fish

    https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...

  9. *[codility]CartesianSequence

    https://codility.com/programmers/challenges/upsilon2012 求笛卡尔树的高度,可以用单调栈来做. 维持一个单调递减的栈,每次进栈的时候记录下它之后有 ...

随机推荐

  1. Android之Http网络编程(一)

    Android应用作为一个客户端程序绝大部分都是需要进行网络请求和访问的,而http通信是一种比较常见并常用的通信方式. 在Android中http网络编程中有两种实现方式,一种是使用HttpURLC ...

  2. 使用repeater控件显示列表替代treeview

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  3. iss 防火墙

    控制面板\系统和安全\Windows 防火墙\允许的程序 如下图 将万维网服务(HTTP)打勾即可访问你的网站. 转自:http://bbs.pcbeta.com/viewthread-604506- ...

  4. 此一生 一个纯js的ajax

    /** * 得到ajax对象 */ function getajaxHttp() { var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp ...

  5. shell环境

    1 引言 一个进程运行在shell环境中,理解进程运行的环境是十分重要的.环境影响着进程的行为,利用环境提供的便利,可以极大地提高开发效率.本节深入讨论shell中与进程有关的环境问题,包括命令行参数 ...

  6. Linux---文件类型及权限操作

    文件类型: 用ls命令查看目录下所属文件时,每行的第一个字母标识着文件对应的文件类型 '-':代表普通文件 'd':代表目录 'c':字符设备文件 'b':块设备文件 's':套接字文件 'l':符号 ...

  7. IE专用CSS,最全的CSS hack方式一览

    http://blog.csdn.net/freshlover/article/details/12132801

  8. sae-多个file_put_contents('saestor://public/text.txt',$data);只写第一次

    多个file_put_contents('saestor://public/text.txt',$data); 只执行第一个文件的写入,永久存储也只需要一次写入 如果需要用户中间缓存文件,用tmpfs ...

  9. SQL中约束和触发器的停用与启用

    如何对SQL中的约束和触发器进行停用与启用,如果有外键约束则相关联表都要进行相应操作. ALTER TABLE TableName CHECK CONSTRAIT ALL --检查约束 ALTER T ...

  10. 恢复Delphi XE2的Library Path

    Delphi XE2好好的,手贱乱删,结果新建一个工程都不能编译了,出现:DELPHI X2 [DCC Fatal Error] KjcxClient.dpr(1): F1027 Unit not f ...