P8480 of Luogu Problem Solving

(Theme Portal)

Title description (simplified here for billions of points)

Given an integer sequence of length n a_{i}, and perform m operations on this sequence, each operation can arbitrarily choose a number in the sequence  a_{i}, so that a_{i}it becomes one of the four results of a_{i}+2, a_{i}-2, a_{i}*2, . \left \lfloor \frac{a_{i}}{2} \right \rfloorUltimately, 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 a_{i}

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< 10^6+1, 0<m<11, 0 a_{i}<<10^9

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.mx:=mx+2

        2.mx:=mx*2

        3.mn:=mn-2

        4.mn:=\left \lfloor \frac{mn}{2} \right \rfloor

Let's consider which operation contributes more to the answer: set the contribution of an operation to the answer f_{x}. Then the contributions of these four operations are:

        1.f_{1}=2

        2.f_{2}=mx

        3.f_{3}=2

        4.f_{4}=mn-\left \lfloor \frac{mn}{2} \right \rfloor

Obviously there is f_{4}\leqslant mn\leqslant mx=f_{2}. So f_{4}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 f_{1}and f_{2}operate. It is not difficult to find that when mx>2, the f_{2}operation is better. When mx=2, the two operations have the same effect. When mx<2, f_{1}it is better to operate. And when an f_{1}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;
}

Guess you like

Origin blog.csdn.net/FlyFree56/article/details/126586540