For a given array A of N integers and a sequence S of N integers from the set {−1, 1}, we define val(A, S) as follows:

val(A, S) = |sum{ A[i]*S[i] for i = 0..N−1 }|

(Assume that the sum of zero elements equals zero.)

For a given array A, we are looking for such a sequence S that minimizes val(A,S).

Write a function:

int solution(int A[], int N);

that, given an array A of N integers, computes the minimum value of val(A,S) from all possible values of val(A,S) for all possible sequences S of N integers from the set {−1, 1}.

For example, given array:

A[0] = 1 A[1] = 5 A[2] = 2 A[3] = -2

your function should return 0, since for S = [−1, 1, −1, 1], val(A, S) = 0, which is the minimum possible value.

Assume that:

  • N is an integer within the range [0..20,000];
  • each element of array A is an integer within the range [−100..100].

Complexity:

  • expected worst-case time complexity is O(N*max(abs(A))2);
  • expected worst-case space complexity is O(N+sum(abs(A))), beyond input storage (not counting the storage required for input arguments).

Elements of input arrays can be modified.

题目大意:给n个数,每个数的范围在-100到100之间,然后对于每个数,我们可以选着乘上-1或1,然后让你求出这些数的和的绝对值的最小值。

这题,可以转化成背包问题。

很明显,我们对这些数求和得sum ,用多重背包求得接近<=sum/2的最大值ans,那么sum-ans-ans就是结果了....这里不好语言描述,不过想一想还是很正确的.

把多重背包转换成01背包求。时间复杂度:O(V*Σlog n[i])  这里的V=sum/2

// you can use includes, for example:
// #include <algorithm> // you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int dp[];
int V;
inline void dp1(int cost,int weight){
for(int v=V;v>=cost;v--)
dp[v]=max(dp[v],dp[v-cost]+weight);
}
inline void dp2(int cost,int weight){
for(int v=cost;v<=V;v++){
dp[v]=max(dp[v],dp[v-cost]+weight);
}
}
inline void dp3(int cost,int weight,int num){
if(cost*num>=V){
dp2(cost,weight);
return ;
}
int k=;
while(k<num){
dp1(k*cost,k*weight);
num-=k;
k*=;
}
dp1(num*cost,num*weight);
}
int cnt[];
int solution(vector<int> &A) {
// write your code in C++11 (g++ 4.8.2)
int n=A.size();
int sum,m;
sum=m=;
dp[]=;
for(int i=;i<n;i++){
int x=(A[i]>=)?A[i]:(-A[i]);
sum+=x;
cnt[x]++;
if(x>m)m=x;
}
if(m==)return ;
V=(sum>>);
for(int i=;i<=m;i++){
if(cnt[i])dp3(i,i,cnt[i]);
}
return (sum-(dp[V]<<)); }

more about 多重背包:http://love-oriented.com/pack/P03.html

codility MinAbsSum的更多相关文章

  1. [codility]Min-abs-sum

    https://codility.com/demo/take-sample-test/delta2011/ 0-1背包问题的应用.我自己一开始没想出来.“首先对数组做处理,负数转换成对应正数,零去掉, ...

  2. Codility NumberSolitaire Solution

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

  3. codility flags solution

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

  4. GenomicRangeQuery /codility/ preFix sums

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

  5. *[codility]Peaks

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

  6. *[codility]Country network

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

  7. *[codility]AscendingPaths

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

  8. *[codility]MaxDoubleSliceSum

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

  9. *[codility]Fish

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

随机推荐

  1. POSIX多线程—概述

    作者:阿波链接:http://blog.csdn.net/livelylittlefish/article/details/7918110 (整半年没有更新,发几篇以前的读书笔记.) Content ...

  2. 查询条件中,不进sql语句 也不进后台bug

    前端代码:本来代码中少写了value="1",后来加上value值之后,可以正常进方法 <div class="row"> <label cl ...

  3. 如何在MONO 3D寻找最短路路径

    前段时间有个客户说他们想在我们的3D的机房中找从A点到B点的最短路径,然而在2D中确实有很多成熟的寻路算法,其中A*是最为常见的,而这个Demo也是用的A*算法,以下计算的是从左上角到右下角的最短路径 ...

  4. 浅析Oracle中的不等于号

    前几天碰到一个关于Oracle不等于的问题,最后搜索了一下,发现下面资料,拿来跟大家分享一下,需要的朋友可以参考下     关于Oracle中的不等于号: 在Oracle中, <> != ...

  5. Extjs中Store小总结

    http://blog.csdn.net/without0815/article/details/7798170 1.什么是store? Store类似于一个本地仓库(即数据存储器),包括有 Arra ...

  6. React和Jquery比较

    Jquery的工作方式: 假如你需要给一个按扭添加一个点击事件. 首先根据CSS规则找到对应的dom元素,挂上一个匿名事件处理函数,在事件处理函数中,选中那个需要被修改的DOM元素,读取他的文本值,加 ...

  7. 洛谷——P2018 消息传递

    P2018 消息传递 题目描述 巴蜀国的社会等级森严,除了国王之外,每个人均有且只有一个直接上级,当然国王没有上级.如果A是B的上级,B是C的上级,那么A就是C的上级.绝对不会出现这样的关系:A是B的 ...

  8. with一个对象,自动触发__enter__方法

    class Foo(object): def __init__(self): pass def __enter__(self): print("__enter__") def __ ...

  9. PAT 1125 Chain the Ropes

    Given some segments of rope, you are supposed to chain them into one rope. Each time you may only fo ...

  10. js中细小点

    昨天在写前端代码的时候,遇到一个神奇的问题,我判断序号 num 和数组 arr 的 length 是否相等,如果相等则做想要的处理,伪码如下; if (num === arr.length) { fu ...