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 ...
随机推荐
- (转)淘淘商城系列——导入商品数据到索引库——Service层
http://blog.csdn.net/yerenyuan_pku/article/details/72894187 通过上文的学习,我相信大家已经学会了如何使用Solrj来操作索引库.本文我们将把 ...
- [转]Js获取当前日期时间及其它操作
转载自:http://www.cnblogs.com/carekee/articles/1678041.html Js获取当前日期时间及其它操作 var myDate = new Date();myD ...
- RC: blkio throttle 测试
本文将测试一下使用cgroup的blkio组来控制IO吞吐量 : 测试环境CentOS 7.x x64 创建一个继承组 [root@150 rg1]# cd /sys/fs/cgroup/blkio/ ...
- xmpp 消息和好友上下线(3)
原始地址:XMPPFrameWork IOS 开发(四) 消息 //收到消息 - (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XM ...
- Python使用Flask框架,结合Highchart,搭配数据功能模块处理csv数据
参考链接:https://www.highcharts.com.cn/docs/data-modules 1.javascript代码 var csv = document.getElementByI ...
- Struts2学习笔记:DMI,多个配置文件,默认Action,后缀
动态方法调用有三种方法: 1.同一Action多次映射,每个action标签的method对应要调用的方法. 当要调用的方法多了就会增加struts.xml文件的复杂性. 2.struts.Dynam ...
- ubutun 创建左面快捷方式
#http://blog.csdn.net/jizi7618937/article/details/51012552
- Python学习——字典
字典 字典是另一种可变容器模型,且可存储任意类型对象. 1.创建字典 字典由键和对应值成对组成.每个键与值之间用:隔开,每对之间逗号隔开. 每个键应当互不相同,值可以相同.若同时出现两个相同的键,则后 ...
- 超星toPDF
* ssReader to pdf Note: editor: Emacs-org 1. download and open the book with sspreader 2. click ...
- 后台工具screen
之前在putty之类的远程命令行操作服务器的时候,遇到关闭软件,对应的操作就会关闭.很多时候,就是开着电脑,然后挂在那里,虽然不用电脑跑,但是也耗电...主要是putty这些软件有时候会伴随黑屏崩掉. ...