NYOJ 1289 ABS 【贪心】
来源:互联网 发布:洛丽塔的经典语录知乎 编辑:程序博客网 时间:2024/04/28 10:22
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 91 -1 10 1 3 0 0 0 1 2 7 3 7
- 样例输出
8 1 6
- 来源
- 某校校赛
- 上传者
MQLYES
我们要求所有数的差绝对值。假设目前都为正数(全是负数等效于正数,有负数有正数就把负数加到最值里),这几个数进行完运算的值一定小于等于最大的那个值,现在,我们就要把最大的拿下来,让其他的数进行运算使其值最小,让最大值减去就是答案。
对于剩下的部分,每次取区间的两个端点做差,得到的是一个相对大的值,再用它与剩下的元素中最大的做差,再和最小的做差,减小这个值,重复上面的过程。
这是我的思路,不会证明,只是测了一些数据,不知道对不对,可能是数据比较水,可能是对了,求思路,求数据hack。题解好像说用dp解决,明天看看。
代码是比赛时候写的,比较乱。#include <cmath>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int n, m,ar[205], a, an[205];int main() {int t;scanf("%d", &t);while (t--) {scanf("%d", &m); int num = 0;int cnt = 0; n = 0;for (int i = 0; i < m; i++) {scanf("%d", &a);if (a < 0) an[cnt++] = a;else ar[n++] = a;}if (!n && cnt) {n = cnt;for (int i = 0; i < n; i++) {ar[i] = -an[i];}}else if (n && cnt) {for (int i = 0; i < cnt; i++) {num -= an[i];}}sort(ar, ar + n); int ans = 0;for (int i = 0; i < (n - 1)/2; i++) {ans = abs(ans - ar[n - i - 2]);ans = abs(ans - ar[i]);}if (n%2 == 0) ans = abs(ans - ar[n/2 - 1]);printf("%d\n", abs(ar[n - 1]+num - ans));}return 0;}
0 0
- NYOJ 1289 ABS 【贪心】
- NYOJ-【ABS】
- 【nyoj 1291 ABS 】
- ABS
- abs
- abs
- abs
- abs
- abs
- NYOJ 733 万圣节派对(abs函数的认识)
- NYOJ - 找点【贪心】
- 过河问题 NYOJ 贪心
- 贪心-nyoj-过河问题
- NYOJ 915 +-字符串【贪心】
- NYOJ 757 期末考试【贪心】
- NYOJ 6 贪心
- NYOJ 891贪心
- nyoj+区间覆盖贪心
- POJ 2152 Check the difficulty of problems(三维dp)
- sell命令-第一集
- 0-1背包问题
- 1078. Hashing (25)-PAT甲级真题(二次方探查法)
- 图结构练习——BFS——从起始点到目标点的最短步数
- NYOJ 1289 ABS 【贪心】
- 黑马Android:带缓存的网络图片查看器
- win7中体验win8
- Android 标题栏(二)
- 多媒体开发计算机颜色相关知识
- NYOJ 1261 音痴又音痴的LT
- 学习shell script
- poj 2528 Mayor's posters
- Android应用程序的Activity启动过程简要介绍和学习计划