(Theme Portal)
Title description (simplified here for billions of points)
Given an integer sequence of length n , and perform m operations on this sequence, each operation can arbitrarily choose a number in the sequence , so that it becomes one of the four results of , , , . Ultimately, it is hoped that the range of the entire sequence will be the largest after m operations.
input format
The first line of two integers n, m
The second line contains n integers, representing the sequence
output format
A total of one line
an integer representing the maximum range
Input and output samples
enter
3 2
0 1 0
output
6
data range
For 100% of the data, 1<n< , 0<m<11, 0 <<
The above is the problem part; the following is the solution part:
Let the current maximum value be mx and the minimum value be mn. In order to make the range larger, we obviously only operate on these two numbers. And it must be to make mx larger and mn smaller. So we can only perform these four operations:
1.
2.
3.
4.
Let's consider which operation contributes more to the answer: set the contribution of an operation to the answer . Then the contributions of these four operations are:
1.。
2.。
3.。
4.。
Obviously there is . So impossible. This means that if we change the minimum value, each operation will at most increase the answer by 2. And if you change the maximum value, each operation can at least make the answer +2. So we only need to operate on the maximum value. Next we consider how to select and operate. It is not difficult to find that when mx>2, the operation is better. When mx=2, the two operations have the same effect. When mx<2, it is better to operate. And when an operation is performed, there must be mx>1. So we only need to record the maximum and minimum values of the initial sequence. If the maximum value is less than 2, first add 2 to it, and then continuously perform *2 operations on the maximum value.
C language:
#include<stdio.h>
long long n,m,x,maxx=0,minx=1e9;
int main() {
scanf("%lld %lld",n,m);
for(long long i=0;i<n;i++) {
scanf("%lld",x);
if(x>maxx) {
maxx=x;
}
if(x<minx) {
minx=x;
}
}
if(maxx<2) {
maxx+=2;
m--;
}
printf("%lld",((1ll*maxx)<<m)-minx);
return 0;
}
C++:
#include<iostream>
using namespace std;
long long n,m,x,maxx=0,minx=1e9;
int main() {
cin>>n>>m;
for(long long i=0;i<n;i++) {
cin>>x;
if(x>maxx) {
maxx=x;
}
if(x<minx) {
minx=x;
}
}
if(maxx<2) {
maxx+=2;
m--;
}
cout<<((1ll*maxx)<<m)-minx;
return 0;
}