题意:

给出n个非严格递增的整数(可能有负数),必须操作k次。每次能够把当中一个数变为它的相反数,使得终于的数列和最大。

输出这个最大和。

考验怎样出坑数据卡自己的程序...

#include <cstdio>

const int maxn = 105;

int n, k, num[maxn];

inline int iread() {
int f = 1, x = 0; char ch = getchar();
for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
return f * x;
} int main() {
int T = iread();
while(T--) {
n = iread(); k = iread();
int cnt = 0, sum = 0;
for(int i = 1; i <= n; i++) {
num[i] = iread();
cnt += (num[i] < 0);
sum += num[i];
} if(k <= cnt) for(int i = 1; i <= k; i++) sum -= 2 * num[i];
else if(k > cnt) {
for(int i = 1; i <= cnt; i++) sum -= 2 * num[i];
if(cnt == 0) sum -= ((k - cnt) & 1) * 2 * num[1];
else if(cnt < n) {
if(-num[cnt] < num[cnt + 1]) sum += ((k - cnt) & 1) * 2 * num[cnt];
else sum -= ((k - cnt) & 1) * 2 * num[cnt + 1];
} else sum += ((k - cnt) & 1) * 2 * num[cnt];
}
printf("%d\n", sum);
}
return 0;
}

提供一些数据:

20

1 100
0 1 1
0 2 1
-9 -1 2 100
-9 -1 2 101
-9 -1 3 1
-3 -1 10 3 2
-3 -1 10 3 3
-3 -1 10 3 100
-3 -1 10 3 101
-3 -1 10 3 200
-3 -2 -1 5 200
-3 -2 -1 0 1 5 200
-3 -2 -1 4 5 5 7
0 1 2 3 4 6 4
-3 -2 -1 1 2 3 5 3
0 0 0 0 1 5 4
-2 -1 0 2 5 6 4
-30 -20 -10 100 100 200 6 4
-30 -20 -10 1 2 3 6 1
1 2 3 4 5 6

输出:

0
0
8
10
8
12
14
12
14
12
4
7
13
10
10
1
10
440
64
19

【SPOJ-GSHOP】Rama and Friends【贪心】【细节】的更多相关文章

  1. CF767 B. The Queue 贪心+细节

    LINK 题意:一个业务开始时间为s,结束时间为f,一个人办护照的时间需要m分(如果在x时开始服务,且x+m==f那么还是合法的),你可以选择任意时间到达,但如果你和其他人同时到达,你要排在他的后面. ...

  2. BZOJ 4278: [ONTAK2015]Tasowanie 后缀数组 + 贪心 + 细节

    Code: #include <bits/stdc++.h> #define setIO(s) freopen(s".in", "r", stdin ...

  3. 【bzoj2424】[HAOI2010]订货 费用流

    原文地址:http://www.cnblogs.com/GXZlegend/p/6825296.html 题目描述 某公司估计市场在第i个月对某产品的需求量为Ui,已知在第i月该产品的订货单价为di, ...

  4. Codeforces Round #392 (Div. 2)-758D. Ability To Convert(贪心,细节题)

    D. Ability To Convert time limit per test 1 second Cmemory limit per test 256 megabytes input standa ...

  5. Codeforces Round #515 (Div. 3) B. Heaters【 贪心 区间合并细节 】

    任意门:http://codeforces.com/contest/1066/problem/B B. Heaters time limit per test 1 second memory limi ...

  6. 贪心:SPOJ Backup Files

    BACKUP - Backup Files no tags  You run an IT company that backs up computer data for large offices. ...

  7. hdu1052Tian Ji -- The Horse Racing(贪心,细节多)

    Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  8. Codeforces Round #398 (Div. 2) A B C D 模拟 细节 dfs 贪心

    A. Snacktower time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  9. 2015多校第6场 HDU 5353 Average 贪心,细节处理

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5353 题意:有n个人围城一个环,每一个人手里都有一些糖果,第i个人有ai块.现在有三种操作:第i个人给 ...

随机推荐

  1. POJ 1944

    明天补上... 这道题的思路确实很精致.考虑到连的边肯定不会是一个环,所以至少有一个断点.于是,可以枚举这个断点.断点一确定,那么连边的走向也就确定了.用D[i]表示由i开始可以到达的最远点即可.对于 ...

  2. 因一段JavaScript代码引发的闲扯

    前两天,一朋友给我发了一段JavaScript代码: function f1(){ var n=999; nAdd=function(){ n+=1 }; function f2(){ alert(n ...

  3. 一个关于 UIPickerView 的 bug

    首先,我下描写叙述一下bug的发生情况: 在使用UIPickerView实现选择城市的时候.出现这样一个Bug 1.在iOS 6的系统上 2.Picker的数据上省份一栏选择了"香港&quo ...

  4. Line(扩展欧几里得)

    题意:本题给出一个直线,推断是否有整数点在这条直线上: 分析:本题最重要的是在给出的直线是不是平行于坐标轴,即A是不是为0或B是不是为0..此外.本题另一点就是C输入之后要取其相反数,才干进行扩展欧几 ...

  5. 全面具体介绍一个P2P网贷领域的ERP系统的主要功能

        一般的P2P系统,至少包含PC站点的前端和后端.前端系统的功能.能够參考"P2P系统哪家强,功能事实上都一样" http://blog.csdn.net/fansunion ...

  6. JavaScript组成部分——ECMAScript、DOM、BOM、

    1.JavaScript组成部分 虽然 JavaScript 和 ECMAScript 通常被人们用来表达相同的含义,但 JavaScript 的含义却比ECMA-262标准中规定的要多得多. 一个完 ...

  7. HTML5学习笔记(四):关于表格

    在一个实例中碰到表格,总结下,先上代码,例: <table border="1"> <thead> <th>表头</th> < ...

  8. 7.gcc的使用

    什么是gcc gcc编译器(GNU C Compiler) 现在我们所说的 gcc 是 GUN Compiler Collection的缩写,可以支持多种语言编译,比如 C,C++,Java, pas ...

  9. extjs 与html相结合 自定义

    http://skirtlesden.com/articles/html-and-extjs-components

  10. tp5数据库操作 Db类

    一.链接数据库 1.配置文件定义  application\database.php 注意:数据表前缀更改,在文件的prefix选项 2.类定义 二.数据库的基本使用 namespace app\de ...