codility MinAbsSum
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的更多相关文章
- [codility]Min-abs-sum
https://codility.com/demo/take-sample-test/delta2011/ 0-1背包问题的应用.我自己一开始没想出来.“首先对数组做处理,负数转换成对应正数,零去掉, ...
- 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 ...
随机推荐
- php利用array_filter()过滤数组空值
利用array_filter过滤数组空值 <?php $array = array( 0 => '霜天部落', 1 => false, 2 => 1, 3 => null ...
- linux 挂载数据盘
完整的阿里云挂载数据盘方法如下: 1.入手阿里云后查看有几块硬盘:(只显示概况,不显示分区情况) fdisk -l|grep Disk 2.查看硬盘分区 fdisk -l 如果有提示:disk /de ...
- BeanFactory和ApplicationContext
BeanFactory是一个类的通用工厂,可以创建并管理各种类的对象 Bean工厂是Spring框架最核心的接口,它提供了高级Ioc的配置机制.BeanFeactory使管理不同类的Java对象成为可 ...
- Bullet:关于ORACLE中的HASH JOIN的参数变化
Oracle在7.3引入了hash join. 但是在Oracle 10g及其以后的Oracle数据库版本中,优化器,实际是CBO,也是因为HASH JOIN仅适用于CBO,在解析目标SQL时是否考虑 ...
- 在centos7中使用yum安装mysql数据库并使用navicat连接
1.安装 1.查看yum列表,发现没有mysql [root@server-mysql src]# yum list mysql 已加载插件:fastestmirror Repodata is ove ...
- 【Codeforces 427C】Checkposts
[链接] 我是链接,点我呀:) [题意] 环里面的点只需要一个点就能全都保护 问你最少需要多少花费以及最少的点才能将所有的点都保护 [题解] 有向图的强连通分量求出所有的联通分量 显然每个联通分量里面 ...
- 51. spring boot属性文件之多环境配置【从零开始学Spring Boot】
原本这个章节是要介绍<log4j多环境不同日志级别的控制的>但是没有这篇文章做基础的话,学习起来还是有点难度的,所以我们先一起了解下spring boot属性文件之多环境配置,当然文章中也 ...
- iOS攻城狮修炼之路
自己总结的学习iOS的笔记,打造一个全面的知识体系,iOS攻城狮修炼之路[持续更新中] iOS学习笔记01-APP相关 iOS学习笔记02-UIScrollView iOS学习笔记03-UITable ...
- 从一行代码开始,浅谈python字符串格式化
今天看到了一行这样的代码: boundary = '%.32x' % random.randint(0, 256**16) 我知道这是一个生成格式化字符串的语句,它将随机生成的一个32位16进制数,将 ...
- 线程间的通信----wait/notify机制
wait/notify机制 实现多个线程之间的通信可以使用wait.notify.notifyAll三个方法.这三个方法都是Object类的方法.wait():导致当前线程等待,直到另一个线程调用此对 ...