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. Java之三大基础排序(冒泡、选择、插入)

    注:以下排序均为从小到大 一.冒泡排序 package com.yunche.testsort; import java.util.Arrays; /** * @ClassName: BubbleSo ...

  2. 60s倒计时

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. [Luogu] P3225 [HNOI2012]矿场搭建

    题目描述 煤矿工地可以看成是由隧道连接挖煤点组成的无向图.为安全起见,希望在工地发生事故时所有挖煤点的工人都能有一条出路逃到救援出口处.于是矿主决定在某些挖煤点设立救援出口,使得无论哪一个挖煤点坍塌之 ...

  4. <MySQL>入门六 变量

    /* 变量 系统变量: 全局变量 会话变量 自定义变量 用户变量 局部变量 */ -- ------------系统变量-------------------- /* 变量由系统提供,不是用户定义,属 ...

  5. apacheAB测试指标

    在进行性能测试过程中有几个指标比较重要: 1.吞吐率(Requests per second) 服务器并发处理能力的量化描述,单位是reqs/s,指的是在某个并发用户数下单位时间内处理的请求数.某个并 ...

  6. Linux学习笔记记录(七八)

  7. python3.x Day3 文件操作

    文件操作:操作文件实际是4步骤1.描述文件是哪个 2.打开文件 3.操作文件 4.关闭文件 1.打开文件使用open方法,代码举例: data=open("wait_you",en ...

  8. eclipse自动提示配置

    打开Window->Preferences

  9. hdu 2647拓扑排序 容器

    #include<stdio.h> #include<queue> #include<vector> #include<iostream> using ...

  10. 1827 tarjan+缩点

    #include<stdio.h> #include<stack> #include<iostream> #include<string.h> #inc ...