第九届蓝桥杯c/c++ b组

过去水了一下,然后三等就滚回来了,现在看了看,我觉得我当时应该是思想出了问题,对面题目一直在我脑子了,他为什么总难两级啊

目录

第一题:第几天

第二题:明码

第三题:乘积尾零

第四题:测试次数

第五题:快速排序。

第六题:递增三元组

第七题:螺旋折线

第八题:日志统计

第九题:全球变暖

第十题:乘积最大


第一题:第几天

2000年的1月1日,是那一年的第1天。

那么,2000年的5月4日,是那一年的第几天?

注意:需要提交的是一个整数,不要填写任何多余内容。

 

打开excel

答案就125了

 

第二题:明码

汉字的字形存在于字库中,即便在今天,16点阵的字库也仍然使用广泛。

16点阵的字库把每个汉字看成是16x16个像素信息。并把这些信息记录在字节中。

一个字节可以存储8位信息,用32个字节就可以存一个汉字的字形了。

把每个字节转为2进制表示,1表示墨迹,0表示底色。每行2个字节, 
一共16行,布局是:

第1字节,第2字节
第3字节,第4字节
....
第31字节, 第32字节
  • 1
  • 2
  • 3
  • 4
  • 5

这道题目是给你一段多个汉字组成的信息,每个汉字用32个字节表示,这里给出了字节作为有符号整数的值。

题目的要求隐藏在这些信息中。你的任务是复原这些汉字的字形,从中看出题目的要求,并根据要求填写答案。

这段信息是(一共10个汉字):

4 0 4 0 4 0 4 32 -1 -16 4 32 4 32 4 32 4 32 4 32 8 32 8 32 16 34 16 34 32 30 -64 0  
16 64 16 64 34 68 127 126 66 -124 67 4 66 4 66 -124 126 100 66 36 66 4 66 4 66 4 126 4 66 40 0 16  
4 0 4 0 4 0 4 32 -1 -16 4 32 4 32 4 32 4 32 4 32 8 32 8 32 16 34 16 34 32 30 -64 0  
0 -128 64 -128 48 -128 17 8 1 -4 2 8 8 80 16 64 32 64 -32 64 32 -96 32 -96 33 16 34 8 36 14 40 4  
4 0 3 0 1 0 0 4 -1 -2 4 0 4 16 7 -8 4 16 4 16 4 16 8 16 8 16 16 16 32 -96 64 64  
16 64 20 72 62 -4 73 32 5 16 1 0 63 -8 1 0 -1 -2 0 64 0 80 63 -8 8 64 4 64 1 64 0 -128  
0 16 63 -8 1 0 1 0 1 0 1 4 -1 -2 1 0 1 0 1 0 1 0 1 0 1 0 1 0 5 0 2 0  
2 0 2 0 7 -16 8 32 24 64 37 -128 2 -128 12 -128 113 -4 2 8 12 16 18 32 33 -64 1 0 14 0 112 0  
1 0 1 0 1 0 9 32 9 16 17 12 17 4 33 16 65 16 1 32 1 64 0 -128 1 0 2 0 12 0 112 0  
0 0 0 0 7 -16 24 24 48 12 56 12 0 56 0 -32 0 -64 0 -128 0 0 0 0 1 -128 3 -64 1 -128 0 0 

注意:需要提交的是一个整数,不要填写任何多余内容。

负数用的是补码,然后就是把二进制打出来看了 

#include<iostream>
#include<bitset>
#include<string>
using namespace std;
void printBit(const int &n) {
	for (int i = 7; i >=0; --i)
		printf("%d", (n&(1 << i)) >> i);
}
int main() {
	int n, m;
	string s1, s2;
	while (scanf("%d%d", &n, &m) != EOF) {
		/*
		用bitset
		bitset<8> a(n), b(m);
		s1 = a.to_string();
		s2 = b.to_string();
		cout << s1 << s2 << endl;
		*/
		//用正常方法
		printBit(n);
		printBit(m);
		printf("\n");
	}
	return 0;
}

 

下面的是一行一行输入的,然后下面的结果用黑底的(比如编译器)看比较清楚,内容是九的九次方等于多少?

4 0 4 0 4 0 4 32 -1 -16 4 32 4 32 4 32 4 32 4 32 8 32 8 32 16 34 16 34 32 30 -64 0
0000010000000000
0000010000000000
0000010000000000
0000010000100000
1111111111110000
0000010000100000
0000010000100000
0000010000100000
0000010000100000
0000010000100000
0000100000100000
0000100000100000
0001000000100010
0001000000100010
0010000000011110
1100000000000000
16 64 16 64 34 68 127 126 66 -124 67 4 66 4 66 -124 126 100 66 36 66 4 66 4 66 4 126 4 66 40 0 16
0001000001000000
0001000001000000
0010001001000100
0111111101111110
0100001010000100
0100001100000100
0100001000000100
0100001010000100
0111111001100100
0100001000100100
0100001000000100
0100001000000100
0100001000000100
0111111000000100
0100001000101000
0000000000010000
4 0 4 0 4 0 4 32 -1 -16 4 32 4 32 4 32 4 32 4 32 8 32 8 32 16 34 16 34 32 30 -64 0
0000010000000000
0000010000000000
0000010000000000
0000010000100000
1111111111110000
0000010000100000
0000010000100000
0000010000100000
0000010000100000
0000010000100000
0000100000100000
0000100000100000
0001000000100010
0001000000100010
0010000000011110
1100000000000000
0 -128 64 -128 48 -128 17 8 1 -4 2 8 8 80 16 64 32 64 -32 64 32 -96 32 -96 33 16 34 8 36 14 40 4
0000000010000000
0100000010000000
0011000010000000
0001000100001000
0000000111111100
0000001000001000
0000100001010000
0001000001000000
0010000001000000
1110000001000000
0010000010100000
0010000010100000
0010000100010000
0010001000001000
0010010000001110
0010100000000100
4 0 3 0 1 0 0 4 -1 -2 4 0 4 16 7 -8 4 16 4 16 4 16 8 16 8 16 16 16 32 -96 64 64
0000010000000000
0000001100000000
0000000100000000
0000000000000100
1111111111111110
0000010000000000
0000010000010000
0000011111111000
0000010000010000
0000010000010000
0000010000010000
0000100000010000
0000100000010000
0001000000010000
0010000010100000
0100000001000000
16 64 20 72 62 -4 73 32 5 16 1 0 63 -8 1 0 -1 -2 0 64 0 80 63 -8 8 64 4 64 1 64 0 -128
0001000001000000
0001010001001000
0011111011111100
0100100100100000
0000010100010000
0000000100000000
0011111111111000
0000000100000000
1111111111111110
0000000001000000
0000000001010000
0011111111111000
0000100001000000
0000010001000000
0000000101000000
0000000010000000
0 16 63 -8 1 0 1 0 1 0 1 4 -1 -2 1 0 1 0 1 0 1 0 1 0 1 0 1 0 5 0 2 0
0000000000010000
0011111111111000
0000000100000000
0000000100000000
0000000100000000
0000000100000100
1111111111111110
0000000100000000
0000000100000000
0000000100000000
0000000100000000
0000000100000000
0000000100000000
0000000100000000
0000010100000000
0000001000000000
2 0 2 0 7 -16 8 32 24 64 37 -128 2 -128 12 -128 113 -4 2 8 12 16 18 32 33 -64 1 0 14 0 112 0
0000001000000000
0000001000000000
0000011111110000
0000100000100000
0001100001000000
0010010110000000
0000001010000000
0000110010000000
0111000111111100
0000001000001000
0000110000010000
0001001000100000
0010000111000000
0000000100000000
0000111000000000
0111000000000000
1 0 1 0 1 0 9 32 9 16 17 12 17 4 33 16 65 16 1 32 1 64 0 -128 1 0 2 0 12 0 112 0
0000000100000000
0000000100000000
0000000100000000
0000100100100000
0000100100010000
0001000100001100
0001000100000100
0010000100010000
0100000100010000
0000000100100000
0000000101000000
0000000010000000
0000000100000000
0000001000000000
0000110000000000
0111000000000000
0 0 0 0 7 -16 24 24 48 12 56 12 0 56 0 -32 0 -64 0 -128 0 0 0 0 1 -128 3 -64 1 -128 0 0
0000000000000000
0000000000000000
0000011111110000
0001100000011000
0011000000001100
0011100000001100
0000000000111000
0000000011100000
0000000011000000
0000000010000000
0000000000000000
0000000000000000
0000000110000000
0000001111000000
0000000110000000
0000000000000000 

答案是387420489

第三题:乘积尾零

如下的10行数据,每行有10个整数,请你求出它们的乘积的末尾有多少个零?

5650 4542 3554 473 946 4114 3871 9073 90 4329  
2758 7949 6113 5659 5245 7432 3051 4434 6704 3594  
9937 1173 6866 3397 4759 7557 3070 2287 1453 9899  
1486 5722 3135 1170 4014 5510 5120 729 2880 9019  
2049 698 4582 4346 4427 646 9742 7340 1230 7683  
5693 7015 6887 7381 4172 4341 2909 2027 7355 5649  
6701 6645 1671 5978 2704 9926 295 3125 3878 6785  
2066 4247 4800 1578 6652 4616 1113 6205 3264 2915  
3966 5291 2904 1285 2193 1428 2265 8730 9436 7074  
689 5510 8243 6114 337 4096 8199 7313 3685 211 

只有2和5能产生0,所以计算2和5的因子,取比较小的那个 

#include<iostream>
int main() {
	int cnt2 = 0, cnt5 = 0, n;
	while (scanf("%d", &n) != EOF) {
		while (n % 2 == 0) {
			++cnt2;
			n >>= 1;
		}
		while (n % 5 == 0) {
			++cnt5;
			n /= 5;
		}
	}
	printf("%d\n", cnt5 < cnt2 ? cnt5 : cnt2);
	return 0;
}

答案是31

第四题:测试次数

x星球的居民脾气不太好,但好在他们生气的时候唯一的异常举动是:摔手机。

各大厂商也就纷纷推出各种耐摔型手机。x星球的质监局规定了手机必须经过耐摔测试,并且评定出一个耐摔指数来,之后才允许上市流通。

x星球有很多高耸入云的高塔,刚好可以用来做耐摔测试。塔的每一层高度都是一样的,与地球上稍有不同的是,他们的第一层不是地面,而是相当于我们的2楼。

如果手机从第7层扔下去没摔坏,但第8层摔坏了,则手机耐摔指数=7。

特别地,如果手机从第1层扔下去就坏了,则耐摔指数=0。

如果到了塔的最高层第n层扔没摔坏,则耐摔指数=n

为了减少测试次数,从每个厂家抽样3部手机参加测试。

某次测试的塔高为1000层,如果我们总是采用最佳策略,在最坏的运气下最多需要测试多少次才能确定手机的耐摔指数呢?

请填写这个最多测试次数。

注意:需要填写的是一个整数,不要填写任何多余内容。

 

这题我当时就直接二分了,现在才发现不能二分,只有3部手机,二分早凉了,这是个dp题

#include<iostream>
int min(const int &a, const int &b) {
	return a <= b ? a : b;
}
int max(const int &a, const int &b) {
	return a >= b ? a : b;
}
int main() {
	const int N = 1005;
	//dp[i][j]代表第i层,拥有j部手机要测dp[i][j]次
	int dp[N][4] = {};
	//只有1部手机只能一层层往上测
	for (int i = 1; i <= 1000; ++i)dp[i][1] = i;
	//在第一层当然只要1次
	for (int i = 1; i < 4; ++i)dp[1][i] = 1;
	for (int i = 1; i <= 1000; ++i) 
		for (int j = 2; j < 4; ++j) {
			//初始化一个很大的值
			dp[i][j] = 0x3f3f3f3f;
			//枚举从那一层摔,如果坏了就从dp[k-1][j-1]开始,如果没坏,就要测比k高的楼层
			for (int k = 1; k <= i; ++k)
				dp[i][j] = min(dp[i][j], max(dp[k - 1][j - 1], dp[i - k][j]) + 1);
		}
	printf("%d\n", dp[1000][3]);
	return 0;
}

答案是19

第五题:快速排序。

以下代码可以从数组a[]中找出第k小的元素。

它使用了类似快速排序中的分治算法,期望时间复杂度是O(N)的。

请仔细阅读分析源码,填写划线部分缺失的内容。

#include <stdio.h>

int quick_select(int a[], int l, int r, int k) {
    int p = rand() % (r - l + 1) + l;
    int x = a[p];
    {int t = a[p]; a[p] = a[r]; a[r] = t;}
    int i = l, j = r;
    while(i < j) {
        while(i < j && a[i] < x) i++;
        if(i < j) {
            a[j] = a[i];
            j--;
        }
        while(i < j && a[j] > x) j--;
        if(i < j) {
            a[i] = a[j];
            i++;
        }
    }
    a[i] = x;
    p = i;
    if(i - l + 1 == k) return a[i];
    if(i - l + 1 < k) return quick_select( _____________________________ ); //填空
    else return quick_select(a, l, i - 1, k);
}

int main()
{
    int a[] = {1, 4, 2, 8, 5, 7, 23, 58, 16, 27, 55, 13, 26, 24, 12};
    printf("%d\n", quick_select(a, 0, 14, 5));
    return 0;
}

 这道题的其实没坑,裸的快排,主要是数字1和字母l(小写的L)太像了,所以我看了半天感觉哪里不对

答案a, i+1, r, k-(i-l+1)

第六题:递增三元组

给定三个整数数组

A = [A1, A2, … AN],

B = [B1, B2, … BN],

C = [C1, C2, … CN],

请你统计有多少个三元组(i, j, k) 满足:

1. 1 <= i, j, k <= N 
2. Ai < Bj < Ck

【输入格式】 第一行包含一个整数N。 第二行包含N个整数A1, A2, ... AN。 第三行包含N个整数B1, B2, ... BN。 第四行包含N个整数C1, C2, ... CN。 对于30%的数据,1 <= N <= 100 对于60%的数据,1 <= N <= 1000 对于100%的数据,1 <= N <= 100000 0 <= Ai, Bi, Ci <= 100000

【输出格式】 一个整数表示答案

【样例输入】

1 1 1 

2 2 2 

3 3 3 

【样例输出】

27

 

我当时看的时候以为i<j<k,现在脑子清楚了,其实就3个分别排序,然后枚举b,看看a有几个小于他的,c有多少个大于他的,查找的时候用二分,相乘就好了,最后求和就是答案了

 

#include<iostream>
#include<algorithm>
using namespace std;
const int N = 100005;
int a[N], b[N], c[N], n;
int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; ++i)scanf("%d", &a[i]);
	for (int i = 0; i < n; ++i)scanf("%d", &b[i]);
	for (int i = 0; i < n; ++i)scanf("%d", &c[i]);
	sort(a, a + n);
	sort(b, b + n);
	sort(c, c + n);
	long long sum = 0;
	for (int i = 0; i < n; ++i) {
		int x = lower_bound(a, a + n, b[i]) - a;
		int y = n - (upper_bound(c, c + n, b[i]) - c);
		sum += 1LL * x*y;
	}
	printf("%lld\n", sum);
	return 0;
}

 

第七题:螺旋折线

如图p1.png所示的螺旋折线经过平面上所有整点恰好一次。

对于整点(X, Y),我们定义它到原点的距离dis(X, Y)是从原点到(X, Y)的螺旋折线段的长度。

例如dis(0, 1)=3, dis(-2, -1)=9

给出整点坐标(X, Y),你能计算出dis(X, Y)吗?

【输入格式】 X和Y 对于40%的数据,-1000 <= X, Y <= 1000 对于70%的数据,-100000 <= X, Y <= 100000 对于100%的数据, -1000000000 <= X, Y <= 1000000000

【输出格式】 输出dis(X, Y)  

【样例输入】

0 1

【样例输出】

3

 

找规律题,虽然当时没找到,现在想想,应该要那些坐标轴上的点,比如(x,0),(0,y),

当y>=0&&abs(x)<=y时,dis(x,y)=dis(0,y)+x=(1+1+2+2+...+2y-1+2y-1)+y+x=2(1+2y-1)*(2y-1)/2+y+x=4y^2-y+x

否则当x<0&&x+1<=y<=-x时dis(x,y)=dis(x,0)+y=(1+1+2+2+...2y-1+2y-1)+x+y=2(1+2(-x)-1)*(2(-x)-1)/2+x+y=4x^2+3x+y

否则当x>0&&-x<=y<=x时dis(x,y)=dis(x,0)-y=(1+1+2+2+....2x+2x)-x-y=2*(1+2x)*(2x)/2-x-y=4x^2+x-y

否则当y<0&&-abs(x)+1>=y时dis(x,y)=dis(0,y)-x=(1+1+2+2+...(-2y)+(-2y))*(-2y)-y-x=(1-2y)*(-2y)-y-x=4y^2-3y-x

#include<iostream>
#include<cmath>
int main() {
	long long x, y;
	scanf("%lld%lld", &x, &y);
	if (y >= 0 && abs(x) <= y)printf("%lld\n", 4 * y*y - y + x);
	else if (x < 0 && x + 1 <= y && y <= -x)printf("%lld\n", 4 * x*x + 3 * x + y);
	else if (x > 0 && -x <= y && y <= x)printf("%lld\n", 4 * x*x + x - y);
	else printf("%lld\n", 4 * y*y - 3 * y - x);
	return 0;
}

第八题:日志统计

小明维护着一个程序员论坛。现在他收集了一份”点赞”日志,日志共有N行。其中每一行的格式是:

ts id

表示在ts时刻编号id的帖子收到一个”赞”。

现在小明想统计有哪些帖子曾经是”热帖”。如果一个帖子曾在任意一个长度为D的时间段内收到不少于K个赞,小明就认为这个帖子曾是”热帖”。

具体来说,如果存在某个时刻T满足该帖在[T, T+D)这段时间内(注意是左闭右开区间)收到不少于K个赞,该帖就曾是”热帖”。

给定日志,请你帮助小明统计出所有曾是”热帖”的帖子编号。

【输入格式】 第一行包含三个整数N、D和K。 以下N行每行一条日志,包含两个整数ts和id。 对于50%的数据,1 <= K <= N <= 1000 对于100%的数据,1 <= K <= N <= 100000 0 <= ts <= 100000 0 <= id <= 100000

【输出格式】 按从小到大的顺序输出热帖id。每个id一行。

【输入样例】

7 10 2 

0 1 

0 10 

10 10 

10 1 

9 1 

100 3 

100 3 

【输出样例】

1 3

 

其实就是暴力+尺取法

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int N = 100005;
int d, k;
vector<int> t[N];
bool judge(const int &id) {
	int len = t[id].size();
	if (len < k)return false;
	sort(t[id].begin(), t[id].end());
	int L = 0, R = 0, sum = 0;
	while (L <= R && R < len) {
		++sum;
		if (sum >= k) {
			if (t[id][R] - t[id][L] < d)return true;
			++L;
			--sum;
		}
		++R;
	}
	return false;
}
int main() {

	int n, ts, id, ans[N], cnt = 0;
	scanf("%d%d%d", &n, &d, &k);
	for (int i = 0; i < n; ++i) {
		scanf("%d%d", &ts, &id);
		t[id].push_back(ts);
	}
	for (int i = 0; n && i < N; ++i, --n)
		if (judge(i)) {
			ans[cnt] = i;
			++cnt;
		}
	for (int i = 0; i < cnt; ++i)printf("%d\n", ans[i]);
	return 0;
}

 

第九题:全球变暖

你有一张某海域NxN像素的照片,”.”表示海洋、”#”表示陆地,如下所示:

……. 
.##…. 
.##…. 
….##. 
..####. 
…###. 
…….

其中”上下左右”四个方向上连在一起的一片陆地组成一座岛屿。例如上图就有2座岛屿。

由于全球变暖导致了海面上升,科学家预测未来几十年,岛屿边缘一个像素的范围会被海水淹没。具体来说如果一块陆地像素与海洋相邻(上下左右四个相邻像素中有海洋),它就会被淹没。

例如上图中的海域未来会变成如下样子:

……. 
……. 
……. 
……. 
….#.. 
……. 
…….

请你计算:依照科学家的预测,照片中有多少岛屿会被完全淹没。

【输入格式】 第一行包含一个整数N。 (1 <= N <= 1000) 以下N行N列代表一张海域照片。 照片保证第1行、第1列、第N行、第N列的像素都是海洋。

【输出格式】 一个整数表示答案。

【输入样例】

....... 

.##.... 

.##.... 

....##. 

..####. 

...###. 

....... 

【输出样例】

1

 

个人感觉看到的答案都是错的,但是我的答案也不敢保证是对的

思路:我认为,第一遍遍历,看到没访问过的#就bfs岛屿,把与海相邻的标记为1,否则标记为2,然后再遍历一遍,看到标记为1的就bfs岛屿,看看bfs过程中有没有2,如果有,说明没被腐蚀,否则腐蚀了,cnt++

个人案例(看到的答案都死循环了)

【输入样例】

....... 

.##.... 

.##.... 

...#### 

..##### 

...#### 

....... 

【输出样例】

1

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
class point {
public:
	int x, y;
	point(const int &x = 0, const int &y = 0) :x(x), y(y) {}
};
const int N = 1005, dir[4][2] = { -1,0,1,0,0,-1,0,1 };
char map[N][N];
int visit[N][N], n, cnt;
void bfs(const int &sx,const int &sy) {
	queue<point> q;
	q.push(point(sx, sy));
	while (!q.empty()) {
		point temp = q.front();
		int x = temp.x;
		int y = temp.y;
		q.pop();
		for (int i = 0; i < 4; ++i) {
			int xx = x + dir[i][0];
			int yy = y + dir[i][1];
			if (xx < 0 || xx >= n || yy < 0 || yy >= n)continue;
			if (map[xx][yy] == '.') {
				visit[x][y] = 1;
				break;
			}
		}
		for (int i = 0; i < 4; ++i) {
			int xx = x + dir[i][0];
			int yy = y + dir[i][1];
			if (xx < 0 || xx >= n || yy < 0 || yy >= n||map[xx][yy]!='#'||visit[xx][yy])continue;
			visit[xx][yy] = 2;
			q.push(point(xx, yy));
		}
	}
}
void bfs2(const int &sx, const int &sy) {
	queue<point> q;
	q.push(point(sx, sy));
	bool flag = true;
	while (!q.empty()) {
		point temp = q.front();
		int x = temp.x;
		int y = temp.y;
		map[x][y] = '.';
		q.pop();
		for (int i = 0; i < 4; ++i) {
			int xx = x + dir[i][0];
			int yy = y + dir[i][1];
			if (xx < 0 || xx >= n || yy < 0 || yy >= n || map[xx][yy] != '#' || !visit[xx][yy])continue;
			if (visit[xx][yy] == 2)flag = false;
			q.push(point(xx, yy));
		}
	}
	if (flag)++cnt;
}
int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; ++i)scanf("%s", map[i]);
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			if (map[i][j] == '#' && !visit[i][j]) {
				bfs(i, j);
			}
		}
	}
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			if (map[i][j] == '#'&&visit[i][j] == 1) {
				bfs2(i, j);
			}
		}
	}
	printf("%d\n", cnt);
	return 0;
}

第十题:乘积最大

给定N个整数A1, A2, … AN。请你从中选出K个数,使其乘积最大。

请你求出最大的乘积,由于乘积可能超出整型范围,你只需输出乘积除以1000000009的余数。

注意,如果X<0, 我们定义X除以1000000009的余数是负(-X)除以1000000009的余数。

即:0-((0-x) % 1000000009)

【输入格式】 第一行包含两个整数N和K。 以下N行每行一个整数Ai。 对于40%的数据,1 <= K <= N <= 100 对于60%的数据,1 <= K <= 1000 对于100%的数据,1 <= K <= N <= 100000 -100000 <= Ai <= 100000

【输出格式】 一个整数,表示答案。

【输入样例】

5 3 

-100000 

-10000 

100000 

10000 

【输出样例】

999100009

 再例如: 【输入样例】

5 3 

-100000 

-100000 

-2 

-100000 

-100000 

【输出样例】

-999999829

 

这个取余规则没有什么卵用,按平常的取余就行了

去掉0之后,按绝对值大小排序

如果数量小于k,输出0

如果全正,取前面k个

如果全负 

               如果k是奇数,取后面k个,否则取前面k个

如果有正有负

               如果前k个里面有偶数个负数,取前面k个

                否则用前k个中较小的正数换后面n-k个中绝对值最大的负数,去比较前k个中较小的负数换后面n-k个中绝对值最大的正数,取前k个相乘后结果较大的,如果还是小于0,就取后面k个

 

最后结果还是小于0,看看有没有0,有就取0

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 100000, mod = 1000000009;
int a[N], n, k;
ll max(const ll &a, const ll &b) { return a >= b ? a : b; }
bool cmp(const int &a, const int &b) {
	return abs(a) > abs(b);
}
ll getAns(const bool &front) {
	ll ans = 1;
	if (front)
		for (int i = 0; i < k; ++i)ans = ans * a[i] % mod;
	else
		for (int i = n - 1; i >= n - k; --i)ans = ans * a[i] % mod;
	return ans;
}
int main() {
	bool contain_zero = false;
	int cnt1 = 0, cnt2 = 0;
	scanf("%d%d", &n, &k);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &a[i]);
		if (!a[i]) {
			--n;
			--i;
			contain_zero = true;
		}
		else if (a[i] > 0)++cnt1;
		else ++cnt2;
	}
	sort(a, a + n, cmp);
	ll ans;
	if (n < k)ans = 0;
	else if (cnt2 == n) {
		if (k & 1)ans = getAns(false);
		else ans = getAns(true);
	}
	else if (cnt1 == n)ans = getAns(true);
	else {
		int cntMinus = 0;
		for (int i = 0; i < k; ++i)
			if (a[i] < 0)++cntMinus;
		if (cntMinus % 2 == 0)ans = getAns(true);
		else {
			int p = -1, q = -1;
			for (int i = k-1; i >= 0; --k)
				if (a[i] < 0) {
					p = i;
					break;
				}
			for (int i = k; i < n; ++i)
				if (a[i] > 0) {
					q = i;
					break;
				}
			if (p != -1 && q != -1) {
				swap(a[p], a[q]);
				ans = getAns(true);
				swap(a[p], a[q]);
			}
			p = q = -1;
			for (int i = k - 1; i >= 0; --k)
				if (a[i] > 0) {
					p = i;
					break;
				}
			for (int i = k; i < n; ++i)
				if (a[i] < 0) {
					q = i;
					break;
				}
			if (p != -1 && q != -1) {
				swap(a[p], a[q]);
				ans = max(getAns(true), ans);
				swap(a[p], a[q]);
			}
			if (ans < 0)ans = getAns(false);
		}
	}
	if (ans < 0 && contain_zero)ans = 0;
	printf("%lld\n", ans);
	return 0;
}

 

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Nightmare004

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值