【SPOJ-GSHOP】Rama and Friends【贪心】【细节】
题意:
给出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【贪心】【细节】的更多相关文章
- CF767 B. The Queue 贪心+细节
LINK 题意:一个业务开始时间为s,结束时间为f,一个人办护照的时间需要m分(如果在x时开始服务,且x+m==f那么还是合法的),你可以选择任意时间到达,但如果你和其他人同时到达,你要排在他的后面. ...
- BZOJ 4278: [ONTAK2015]Tasowanie 后缀数组 + 贪心 + 细节
Code: #include <bits/stdc++.h> #define setIO(s) freopen(s".in", "r", stdin ...
- 【bzoj2424】[HAOI2010]订货 费用流
原文地址:http://www.cnblogs.com/GXZlegend/p/6825296.html 题目描述 某公司估计市场在第i个月对某产品的需求量为Ui,已知在第i月该产品的订货单价为di, ...
- 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 ...
- Codeforces Round #515 (Div. 3) B. Heaters【 贪心 区间合并细节 】
任意门:http://codeforces.com/contest/1066/problem/B B. Heaters time limit per test 1 second memory limi ...
- 贪心:SPOJ Backup Files
BACKUP - Backup Files no tags You run an IT company that backs up computer data for large offices. ...
- hdu1052Tian Ji -- The Horse Racing(贪心,细节多)
Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- 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 ...
- 2015多校第6场 HDU 5353 Average 贪心,细节处理
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5353 题意:有n个人围城一个环,每一个人手里都有一些糖果,第i个人有ai块.现在有三种操作:第i个人给 ...
随机推荐
- HDU 4357
这道题写起来没难度,但这种题确实很难,这种字符串的题难在证明.以后也要注意. 奇偶性不同的字符串肯定不能转换,因为每一次交换都是字符串的和增加2. 当字符串长度为2时,可以模拟交换,最多26次. 否则 ...
- 经验总结21--抓取WEB数据,汇率,HtmlAgilityPack
网上找了非常多资料,PHP的比較多,然后找到有csv文件的.处理起来非常麻烦,国外的站点速度非常慢. 最后还是去页面上抓取数据,我是从中国银行抓取的,各位可去其它站点抓取. 1.模拟请求URL. st ...
- gwt学习资料
学习资料: 2 3
- java应用集锦9:httpclient4.2.2的几个常用方法,登录之后访问页面问题,下载文件
转账注明出处:http://renjie120.iteye.com/blog/1727933 在工作中要用到android,然后进行网络请求的时候,打算使用httpClient. 总结一下httpCl ...
- BZOJ 3720 树分块
借鉴了别人的代码-- //By SiriusRen #include <cmath> #include <cstdio> #include <cstring> #i ...
- Spring《一》
1.支持的注入方式 构建注入,set注入 2.bean属性 id.name.class.singleton(true.false).depends-on="date"(初始化依赖) ...
- 什么时候使用try-catch
上篇文章叙述了try-catch的效率问题,以及StackOverflow上各路大神们指出使用try-catch应该注意的一些问题. 这篇文章作为补充,转述下StackOverflow上各路大神对于何 ...
- Tarjan 求图点强联通,桥的应用
在图中求双联通和强联通分量是我们解决非树结构的图连通问题的利器 通过求求图的双联通和强联通分量能把图转化成DAG进行求解: 行走 Description 给出一个有向图,你可以选择从任意点出发走到任意 ...
- Photoshop保存文件时的选项
以 JPEG 格式存储 您可以使用"存储为"命令以 JPEG (*.jpg) 格式存储 CMYK.RGB 和灰度图像.JPEG 通过有选择地扔掉数据来压缩文件大小.也可以使用&qu ...
- pgpool中定义的数据库节点及pgpool支持的复制模式
/* * The first DB node id appears in pgpool.conf or the first "live" DB * node otherwise. ...