NYOJ-【ABS】
来源:互联网 发布:udp端口 服务 编辑:程序博客网 时间:2024/04/28 01:43
ABS
时间限制:1000 ms | 内存限制:65535 KB
- 描述
- Mr.Ha is a famous scientist .He has just not got a kind of magic medicine called Entropy Cracker.The medicine was preserved in V bottles,and the i-th (1≤i≤N) bottle contains V liters of medicine.To make the magic available, he needs to put the medicine from all the bottles together.Due to mixing medicine is a dangerous action, Mr.Ha takes a huge container with infinite volume and decides only to pour a whole bottle of medicine into the container each time.After adding a p liter`s bottle of medicine into the container with q liters of medicinein it,the resulted volume of medicine in the container will be |p-q| liters.Initially the container is empty ,and Mr.Ha can put the bottles of medicine into the container by arbitrary order.Finally if there are R liters of medicine in the container ,Mr.Ha will be able to use the magic to increase the time for R seconds every day so that he can achieve more work! Help Mr.Ha to make an arrangement so that the resulted R is maximum.
- 输入
- The first line contains an integer T,indicating the number of test cases.
For each test case ,the first line contains an integer N 1≤N≤200,indicating the number of botters, and the second line contains V (|vi|<=500),indicating the volume of medicine in each bottle Attention the volume may be negative as a result of magic
The sum of N in all test cases will not exceed 2000. - 输出
- For each test case , output the case number in the format of the format of the sample ,and an
Integer ,the maximum seconds Mr.Ha will able to increase - 样例输入
3 4 1 2 2 91 -1 10 1 3 0 0 0 1 2 7 3 7
- 样例输出
8 1 6
还是自己做的题目少,这明明时裸裸的dp嘛;其实先找出来最大值,然后剩下n-1个数
分成2组,求着2组相减绝对值的最小值,别忘了把负数先找出来
#include<cstdio>#include<algorithm>using namespace std;int v[500];int dp[500000];int abs2(int a){if(a<0)return -a;return a;}int max(int a,int b){if(a>b)return a;return b;}int main(){int t;scanf("%d",&t);while(t--){int n,i=0,a,sum=0,ans=0;scanf("%d",&n);while(n--){scanf("%d",&a);if(a>0){v[i]=a;sum+=v[i];++i;}elseans-=a;}if(i>0){sort(v,v+i);//其实也不用排序,只要找到最大值就行了 sum-=v[i-1];for(int j=0;j<=i-2;++j){for(int k=sum/2;k>=v[j];--k)dp[k]=max(dp[k],dp[k-v[j]]+v[j]);}sum=sum-2*dp[sum/2];printf("%d\n",abs2(v[i-1]+ans-abs2(sum)));}else{printf("%d\n",ans);}}return 0;}
0 0
- NYOJ-【ABS】
- NYOJ 1289 ABS 【贪心】
- 【nyoj 1291 ABS 】
- ABS
- abs
- abs
- abs
- abs
- abs
- NYOJ 733 万圣节派对(abs函数的认识)
- abs ()函数
- ABS ,SPI
- abs()函数
- ABS(actionbarsherlock)
- shell abs
- abs index
- Abs Problem
- abs ,fabs
- 详解Java中的clone方法 -- 原型模式
- (c)内部连接+工程
- JMeter(二) -参数化
- 深入理解为什么Java中方法内定义的内部类可以访问方法中的局部变量
- V2.0.0 Python是怎么运行的
- NYOJ-【ABS】
- 深入理解Java中为什么内部类可以访问外部类的成员
- HDU-2870最大矩形问题(四)
- 在eclipse中给mybatis的config.xml以及映射xml文件配置自动提示
- AndroidStudio上传项目至OSchina
- jzoj 1149. 【克罗地亚】LAGNO
- Java利用JNI调用FFMpeg对h264码流进行解码
- 深入理解Java虚拟机到底是什么
- java中的矩阵包