【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
时间: 2024-07-29 16:42:21

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

「Codeforces」758D(贪心细节/dp)

题意:原题在这 Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10.

SPOJ 417 The lazy programmer(贪心)

417. The lazy programmer Problem code: LAZYPROG A new web-design studio, called SMART (Simply Masters of ART), employs two people. The first one is a web-designer and an executive director at the same time. The second one is a programmer. The directo

SPOJ MSTICK. Wooden Sticks 贪心 结构体排序

MSTICK - Wooden Sticks There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some time, called setup time, for the machine

POJ - 1456 贪心 堆常用操作 注意细节

题意:给定n个商品的deadline和profit,求每天卖一件的情况下的最大获利 显然是一道贪心 按deadline从小到大排序好,动态维护小根(profit)堆的大小<=当前deadline的天数,往里面符合条件的尽可能塞更优解 注意有n为0的情况 还有脑抽导致判断条件缺斤少两,下次不要这样了 /*H E A D*/ struct Node{ ll p,d,id; }a[maxn]; bool cmp(Node a,Node b){ if(a.d!=b.d)return a.d<b.d;

SPOJ - LOCKER 数论 贪心

题意:求出\(n\)拆分成若干个数使其连乘最大的值 本题是之江学院网络赛的原题,计算规模大一点,看到EMAXX推荐就做了 忘了大一那会是怎么用均值不等式推出结果的(还给老师系列) 结论倒还记得:贪心分解3,不够就用2凑 #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #

SPOJ:Decreasing Number of Visible Box(不错的,背包?贪心?)

Shadowman loves to collect box but his roommates woogieman and itman don't like box and so shadowman wants to hide boxes as many as possible. A box can be kept hidden inside of another box if and only if the box in which it will be held is empty and

SPOJ:PATHETIC STRINGS(分配问题&amp;贪心)

Problem statement: A string is said to be “PATHETIC” if all the characters in it are repeated the same number of times. You are given a string of length n, what is the minimum number of changes required to make a string “PATHETIC”. The string has onl

UVALive 6911 Double Swords (Set,贪心,求区间交集)

首先左边的是必须要选的,然后右边的需要注意,有些区间是可以舍掉的.1.区间里有两个不同的A. 2.区间里有一个A,而且这个A不是这个区间对应的A. 这个题我一开始错在了第2个判定条件上,我定义的id记录的是最后一个出现位置,不能进行判断,所以干脆在结构体里记录了他对应的A值. 舍掉后留下的区间,可以按照区间左边界排序,然后求交集即可. 总体来说,贪心的思想还是不难的,就是有一些细节需要注意. 代码如下: #include<iostream> #include<cstdio> #in

SPOJ 962 Intergalactic Map

Intergalactic Map Time Limit: 6000ms Memory Limit: 262144KB This problem will be judged on SPOJ. Original ID: IM64-bit integer IO format: %lld      Java class name: Main Jedi knights, Qui-Gon Jinn and his young apprentice Obi-Wan Kenobi, are entruste