2018年省赛真题C/C++ B组[蓝桥杯]

1.第几天

2000年的1月1日,是那一年的第1天。
那么,2000年的5月4日,是那一年的第几天?
在这里插入图片描述
答案:125

2.明码

汉字的字形存在于字库中,即便在今天,16点阵的字库也仍然使用广泛。
16点阵的字库把每个汉字看成是16x16个像素信息。并把这些信息记录在字节中。

一个字节可以存储8位信息,用32个字节就可以存一个汉字的字形了。
把每个字节转为2进制表示,1表示墨迹,0表示底色。每行2个字节,
一共16行,布局是:

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

这道题目是给你一段多个汉字组成的信息,每个汉字用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<algorithm>
#include<cstring>
#include<vector> 
using namespace std;
const int N =  16;
int a[2][N];
long long power(int x)//9的9次方
{
    
    
	int res = 1;
	while(x--)
	{
    
    
		res *= 9;
	}
	return res;
}
void fx2(int x)//x为负数
{
    
    
	vector<int> A;
		for(int i = 7; i >= 0; i--)
    	{
    
    
 	    	A.push_back(~((-x)>>i)&1);//去反
	    }
	    A[7]++;//加一
	    for(int i = 7; i >= 0; i--)
	    {
    
    
	    	if(A[i]>1)
	    	{
    
    
	    		A[i] = 0;
	    		A[i-1]++;
			}
		}
		for(int i = 0; i <=7; i++) 
	{
    
    
	   if(A[i] == 0) cout <<' ';
	   else printf("%d",A[i]);	
	}
}
void fx1(int x)
{
    
    
	vector<int> A;
		for(int i = 7; i >= 0; i--)
    	{
    
    
 	    	A.push_back((x>>i)&1);
	    }
	
	for(int i = 0; i <=7; i++) 
	{
    
    
	   if(A[i] == 0) cout <<' ';
	   else printf("%d",A[i])	;
	}
}
int main()
{
    
    
	int n = 10;
	while(n--)
	{
    
    
		for(int i = 0;i < 16; i++) 
	    {
    
    
		for(int j = 0; j <2; j++)
    	{
    
    
		  scanf("%d", &a[i][j]);
		  if(a[i][j] >=0) fx1(a[i][j]);
		  else fx2(a[i][j]);
	    }
	    cout << endl;
    	}
    	cout << endl;
	}
    cout << power(9) <<endl;
    return 0;
}

方法二:

#include<iostream>
#include<cstdio>
using namespace std;
void toBinaryStr(int i,string &ans)
{
    
    
	if(i >= 0)
	{
    
    
		ans[0] = '-';
		for(int j = 0; j < 7; j++)
		{
    
    
			if(((i>>j)&1) == 1)//二进制位上为1 
			ans[8-j-1]='1';
		}
	}
	else
	{
    
    
		ans[0] = '1';
		for(int j = 0; j < 7; j++)
		if((((128+i)>>j)&1) == 1)//二进制位上为1 
		{
    
    
			ans[8-j-1] = '1';
		}
	}
 } 
 int main()
 {
    
    
 	for(int i = 0; i <10; i++)
 	 {
    
    
 	  	for(int j = 0; j <16; j++)
 	     {
    
    
 	 	int x,y;
 	 	cin >> x >> y;
 	 	string xx = "--------", yy= "--------";
 	 	toBinaryStr(x,xx);
 	 	toBinaryStr(y,yy);
 	 	cout << xx+yy << endl;
	   }
	   cout <<endl<<"================"<<endl;
	}
	
	long long ans = 9;
	for(int k = 0; k <8; k++)
	{
    
    
		ans *= 9;
	}
	cout << ans << endl;
	return 0;
	 
 }

在这里插入图片描述

答案:387420489

3.乘积尾零

如下的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 

方法1:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector> 
using namespace std;
typedef long long LL;
const int N = 10,mod = 1e5;;
int a[N][N];
int main()
{
    
    
	LL res = 0;
	for(int i = 0; i <10; i++)
	for(int j = 0; j <10; j++)
	{
    
    
		scanf("%d", &a[i][j]);
		while(a[i][j]%10 == 0)
		{
    
    
			res++;
			a[i][j] /= 10;
		}
	}
	LL cnt = 1;
	for(int i = 0; i <10; i++)
	for(int j = 0; j <10; j++)
	{
    
    
		cnt *= a[i][j];
		while(cnt%10 == 0)
		{
    
    
			res++;
			cnt /=10;
		}
		cnt = cnt%mod;
	}
	cout << res << endl;
	return 0;
} 

方法二:

//解题思路:将每个数分解成2和5相乘的式子
//不能分解的不用管
//最后统计有多少个2和5,输出较少的那一个即可 
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector> 
using namespace std;
int main()
{
    
    
	int data[] = {
    
    
		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
	};
	
	int c2 = 0,c5 = 0; 
	for(int i = 0; i <100; i++)
	{
    
    
		int num = data[i];
		while(num%2 == 0)
		{
    
    
			c2++;
			num /= 2;
		}
		while(num%5 == 0)
		{
    
    
			c5++;
			num /= 5;
		}
	 } 
	 cout << min(c2,c5);
	 return 0;
}

答案:31

4.测试次数

x星球的居民脾气不太好,但好在他们生气的时候唯一的异常举动是:摔手机。
各大厂商也就纷纷推出各种耐摔型手机。x星球的质监局规定了手机必须经过耐摔测试,并且评定出一个耐摔指数来,之后才允许上市流通。

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

如果手机从第7层扔下去没摔坏,但第8层摔坏了,则手机耐摔指数=7。
特别地,如果手机从第1层扔下去就坏了,则耐摔指数=0。
如果到了塔的最高层第n层扔没摔坏,则耐摔指数=n

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

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

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

#include<iostream>
#include<algorithm>
#include<cstring>
#include<climits>
using namespace std;
const int N = 1000;
int f1[N+1],f2[N+1],f3[N+1];//记录手机数为1,2,3时,对应各层的测试次数 
int main()
{
    
    
	//1部手机的情况 
    for(int i = 1; i <= N; i++)
    {
    
    
    	f1[i] = i;
	}
	
	//考虑两部手机的情况
	for(int i = 1; i <= N; i++)
	{
    
    
		int ans = INT_MAX;
		//尝试1~i若干种方案,最终记录所有方案中次数最小的
		for(int j = 1; j <= i; j++)//在j层扔第一个手机
		{
    
    
			//1 好的   2坏的
		  int maxd = 1+max(f2[i-j],f1[j-1]); 
		  ans = min(ans,maxd);
		 } 
		 f2[i] = ans;
	 } 
	 
	 //考虑三部手机的情况
	  for(int i = 1; i <= N; i++)
	{
    
    
		int ans = INT_MAX;
		//尝试1~i若干种方案,最终记录所有方案中次数最小的
		for(int j = 1; j <= i; j++)//在j层扔第一个手机
		{
    
    
			//2 好的   3坏的
		  int maxd = 1+max(f3[i-j],f2[j-1]); 
		  ans = min(ans,maxd);
		 } 
		 f3[i] = ans;
	 } 
	 cout << f3[N] <<endl;
	 return 0;
}

思路:以为是二分,实际上是dp,手机摔坏后不能再用。

dp[i][j] :i层,j个手机,的最多测试次数。

设当前在k层摔,那么就有两种可能:

  • (1)摔坏

则测试次数等于k-1层摔j-1个手机的测试次数+1. dp[k-1][j-1]+1

  • (2)没摔坏

则手机耐摔指数大于k,只需检测i-k层j个手机即可。 dp[i-k][j] + 1

最坏的情况下,则取最大值 max( dp[k-1][j-1]+1 , dp[i-k][j] + 1 ).

在最坏情况下取最好结果,则dp[i][j] = min(dp[i][j], max( dp[k-1][j-1]+1 , dp[i-k][j] + 1 ) ).


#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
 
typedef long long ll;
 
int dp[1001][4];
 
int main()
{
    
    	
  	for (int n = 1; n <=3; n++) 
    	for (int i = 1; i <=1000; i++)
		{
    
    
    		dp[i][n]=dp[i-1][n]+1;
    		for (int j = 1; j <i&&n>1; j++) 
    			dp[i][n]=min(dp[i][n], max(dp[j-1][n-1], dp[i-j][n])+1); 
   		}
  	cout<<dp[1000][3];
	return 0;
}

 

答案:19

5.快速排序

以下代码可以从数组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;
}
#include <stdio.h>
#include<math.h>
#include <stdlib.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( a,i+1,r,k-(i-l+1) ); //填空
	else return quick_select(a, l, i - 1, k);//如果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));//找第k小元素 
	return 0;
}

6.递增三元组

给定三个整数数组
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

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

【样例输入】

3
1 1 1
2 2 2
3 3 3

【样例输出】

27 

暴力

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std; 
const int N = 100010;
int a[3][N];

int main()
{
    
    
	int n;
	cin >> n;
	int res = 0;
	for(int i = 0; i <3; i++)
	 for(int j = 0; j <n; j++)
	 {
    
    
	 	scanf("%d", &a[i][j]);
	 }
	 for(int j = 0; j <n; j++)
	 {
    
    
	 	for(int k = 0; k <n; k++)
	 	 for(int t = 0; t < n; t++)
	 	if(a[0][j] < a[1][k] && a[1][k] < a[2][t]) res++;
	 	else continue;
	 }
	 
	 cout << res;
	 return 0;
}

优化

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
long long ans;
int main()
{
    
    
	int n;
	cin >> n;
	int a[n],b[n],c[n];
	for(int i = 0; i < n; i++)
	cin >> a[i];
	
	for(int i = 0; i < n; i++)
	cin >> b[i];
	
	for(int i = 0; i < n; i++)
	cin >> c[i];
	sort(a,a+n);
	sort(b,b+n);
	sort(c,c+n);
	
	int p = 0,q = 0;//均为起始下标也是个数
	for(int i = 0; i <n; i++)
	{
    
    
		while(p <n && a[p] <b[i]) p++;
		while(q <n && c[q] <= b[i]) q++;
		ans += ((long long)p*(n-q));
	}
	cout << ans<<endl;
	return 0;
	
}

7.螺旋折线

在这里插入图片描述

如图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

在这里插入图片描述

//以右下角对角线上的点为参照点,测算给定的点到参照点要走的距离 
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#define sum(t,n,d)  (2*t+(n-1)*d)*n/2
typedef long long LL; 
using namespace std;

int main()
{
    
    
	LL x, y;
	while(1)
	{
    
    
		cin >> x >> y;
		LL d = 0;//距离
	LL n = 0;//第几圈
	if(y > 0 && abs(x) <= y)//在上面那条横线上 
	{
    
    
		n = y;
		d = y-x+2*y;
	 } 
	 else if(x >0 && abs(y) <= x)//右竖线 
	 {
    
    
	 	n = x;
	 	d = y+x;
	 }
	 else if(y <= 0 && x >- y -1 &&x <= -y)//下横线 
	 {
    
    
	 	n = -y;
	 	d = -(-y-x);
	 }
	 else if(x < 0 && y >= x+1 && y <= -x)//左竖线 
	 {
    
    
	 	n = -x-1;
	 	d = -(y-x-1-2*x-1);
	 }
	 cout << sum(1,2*n,1)*2 - d << endl;
	}
	return 0;
	
}

8.日志统计

小明维护着一个程序员论坛。现在他收集了一份"点赞"日志,日志共有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<cstring>
#include<cstdio>
#include<algorithm>
#define x first
#define y second
using namespace std;
typedef pair<int,int> PII;
const int N = 100010;
int n, d,k;
PII logs[N];//记录时间与id 
int cnt[N];//记录id出现次数
bool st[N];//记录每个帖子是否是热帖
int main()
{
    
    
    scanf("%d%d%d", &n, &d, &k);
    for(int i = 0; i < n; i++) scanf("%d%d", &logs[i].x, &logs[i].y);
    
    sort(logs,logs + n);//按照时间排序
    for(int i = 0, j = 0; i < n; i++)
    {
    
    
        int id = logs[i].y;//新加帖子的id
        cnt[id]++;
        while(logs[i].x - logs[j].x >= d) //时间跨度
        {
    
    
            cnt[logs[j].y]--;
            j++;
        }
        
        if(cnt[id] >= k) st[id] = true;//如果当前这个id的赞大于等于k,标记为是热帖
    }
    
    for(int i = 0;i <= 100000;i++)//遍历id
     if(st[i])
      printf("%d\n", i);
      
      return 0;
}

9.全球变暖

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

.......
.##....
.##....
....##.
..####.
...###.
.......

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

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

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

.......
.......
.......
.......
....#..
.......
.......

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

【输入格式】
第一行包含一个整数N。 (1 <= N <= 1000)
以下N行N列代表一张海域照片。

照片保证第1行、第1列、第N行、第N列的像素都是海洋。

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

【输入样例】

7 
.......
.##....
.##....
....##.
..####.
...###.
.......  

【输出样例】

1  

算法分析
遍历所有未遍历过的陆地,通过bfs计算出当前位置连通陆地的数量total,以及被淹没陆地的数量bound,若total == bound表示完整淹没的一个岛屿

  • 1.算出有多少个连通块
  • 2.多少个会被淹没 一共有多少个岛屿连通,边界与海相连的会被淹没
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 1010;
int n;
char g[N][N];
bool st[N][N];
PII q[N*N];
int dx[4] = {
    
    -1,0,1,0}, dy[4] = {
    
    0,1,0,-1}; 
void bfs(int x, int y, int &total, int &bound)
{
    
    
    int hh = 0, tt = 0;
    q[0] = {
    
    x, y};
    st[x][y] = true;
    while(hh <= tt)
    {
    
    
        PII t = q[hh++];
        total ++;
        bool is_bound = false;
        for(int i = 0; i < 4; i++)
        {
    
    
            int a = t.x + dx[i], b = t.y + dy[i];
            if(a < 0 || b < 0|| a >= n || b >= n) continue;//出界
            if(st[a][b]) continue;
            if(g[a][b]  == '.')//如果是大海
            {
    
    
                is_bound = true;//这个块的与海相连
                continue;
            }
            q[++tt] = {
    
    a, b};
            st[a][b] = true;
            
        }
        if(is_bound) bound++;
    }
}

int main()
{
    
    
    cin >> n;
    for(int i = 0; i < n;i++) scanf("%s", g[i]);
    int cnt = 0;//所有被淹没岛屿的个数
    for(int i = 0; i <n; i++)
     for(int j = 0; j < n; j++)
      if(!st[i][j] && g[i][j] == '#')//如果当前这个格子没有被搜过并且是岛屿
      {
    
    
          int total = 0, bound = 0;//所有单元和边界的数量
          bfs(i, j, total, bound);
          if(total == bound) cnt++;//如果所有单元都在边界上,被淹没的岛屿数量++
      }
      printf("%d\n", cnt);
      return 0;
}

10.乘积最大

给定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   
2   
100000  
10000  

【输出样例】

999100009

再例如:
【输入样例】

5 3 
-100000   
-100000   
-2   
-100000  
-100000

【输出样例】

-999999829
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N = 100010,mod = 1000000009;
int a[N];
int n, k;
int main()
{
    
    
    cin >>n >>k;
    int res = 1;
    for(int i = 0; i <n; i++) 
    {
    
    
        scanf("%d", &a[i]);
    }
    sort(a,a+n);
    
    //如果k %2 == 0 答案必然是在左右两边各选一堆(绝对值前k大的数)
    //如果k%2 != 0   把最大的数选掉,就变成了偶数的情况,k变为偶数,从左右两边往中间取即可
    int l = 0, r = n-1;
    int sign = 1;//符号
    if(k%2)
    {
    
    
        res = a[r--];
        k--;
        if(res < 0) sign = -1;//如果结果为负
    }
    while(k)
    {
    
    
        LL x = (LL)a[l]*a[l+1],y = (LL)a[r-1]*a[r];
        if(x *sign >y*sign)
        {
    
    
            res = x%mod * res%mod;
            l += 2;
        }
        else 
        {
    
    
            res = y%mod*res%mod;
            r -= 2;
        }
        k -= 2;
    }
    cout << res << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Annabel_CM/article/details/113934088