NYOJ1289 ABS

来源:互联网 发布:vb 批处理 编辑:程序博客网 时间:2024/04/28 20:30

ABS
时间限制:1000 ms | 内存限制:65535 KB
难度:3
描述

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 9
1
-1
10
1 3 0 0 0 1 2 7 3 7
样例输出
8
1
6

坑爹的题比赛的时候交了提示超时好坑nlog都超时我就没看了谁知道比赛结束重判了 还是wa了 就是没有把0算成正数QAQ
我也不要知道为什么0要是直接去掉就wa否者就能ac
思路是先把负数去除出去所有的负数其实都是加进去的然后排序
按最大的和最小的做绝对值依此类推 感觉自己是蒙出来的

#include <cstdio>#include <iostream>#include <algorithm>#include <stack>#include <queue>#include <cstring>using namespace std;int a[210];int main(){    int T; scanf("%d",&T);    while(T--){        memset(a,0,sizeof(a));        int n;        int sum=0; int k=0;        scanf("%d",&n);        for(int i=1;i<=n;i++){            int x;            scanf("%d",&x);            if(x<0)//<=就WA了                sum-=x;            else                a[++k]=x;        }        sort(a+1,a+k+1);        int ans=0;        for (int i=1,j=k-1;i<j; i++,j--) {            ans=abs(ans-a[j]);            ans=abs(ans-a[i]);        }        if(!(k&1)){            ans=abs(ans-a[k/2]);        }        sum+=abs(a[k]-ans);        printf("%d\n",sum);    }    return 0;}
0 0
原创粉丝点击