0
点赞
收藏
分享

微信扫一扫

Codeforces Round #772 (Div. 2) B. Avoid Local Maximums

攻城狮Chova 2022-02-21 阅读 93
c++

B. Avoid Local Maximums

time limit per test:2 seconds
memory limit per test:256 megabytes
inputstandard input
outputstandard output

题目描述

You are given an array a of size n. Each element in this array is an integer between 1 and 10^9.

You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between 1 and 10^9.

Output the minimum number of operations needed such that the resulting array doesn’t contain any local maximums, and the resulting array after the operations.

An element ai is a local maximum if it is strictly larger than both of its neighbors (that is, ai>ai−1 and ai>ai+1). Since a1 and an have only one neighbor each, they will never be a local maximum.

Input
Each test contains multiple test cases. The first line will contain a single integer t (1≤t≤10000) — the number of test cases. Then t test cases follow.

The first line of each test case contains a single integer n (2≤n≤2⋅10^5) — the size of the array a.

The second line of each test case contains n integers a1,a2,…,an (1≤ai≤10^9), the elements of array.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅10^5.

Output
For each test case, first output a line containing a single integer m — minimum number of operations required. Then ouput a line consist of n integers — the resulting array after the operations. Note that this array should differ in exactly m elements from the initial array.

If there are multiple answers, print any.

Example
input
5
3
2 1 2
4
1 2 3 1
5
1 2 1 2 1
9
1 2 1 3 2 3 1 2 1
9
2 1 3 1 3 1 3 1 3
output
0
2 1 2
1
1 3 3 1
1
1 2 2 2 1
2
1 2 3 3 2 3 3 2 1
2
2 1 3 3 3 1 1 1 3
Note
In the first example, the array contains no local maximum, so we don’t need to perform operations.
In the second example, we can change a2 to 3, then the array don’t have local maximums.

5
3
2 1 2
4
1 2 3 1
5
1 2 1 2 1
9
1 2 1 3 2 3 1 2 1
9
2 1 3 1 3 1 3 1 3
outputCopy
0
2 1 2
1
1 3 3 1
1
1 2 2 2 1
2
1 2 3 3 2 3 3 2 1
2
2 1 3 3 3 1 1 1 3
Note
In the first example, the array contains no local maximum, so we don’t need to perform operations.

In the second example, we can change a2 to 3, then the array don’t have local maximums.

队列模拟

#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false)
typedef long long ll;
const int N=2e5+10;
ll a[N];
int main(){
	IOS;
	ll t,n;
	while(cin>>t){
		while(t--){
			cin>>n;
			for(ll i=0;i<n;i++){
				cin>>a[i];
			}
			queue<ll>q;
			for(ll i=1;i<n-1;i++){
				if(a[i]>a[i-1]&&a[i]>a[i+1]){
					q.push(i);
				}
			}
			ll ans=0;
			while(!q.empty()){
				if(q.size()<=1){//其实感觉这里写q.size()==1就可以的
					ans++;
					ll k=q.front();
					a[k]=max(a[k-1],a[k+1]);
					q.pop();
					break;
				}
				ll u=q.front();
				q.pop();
				ll v=q.front();
//				cout<<v-u<<endl;
				if(v-u==2){
					ans++;
					a[u+1]=max(a[u],a[v]);
					q.pop();
				}else if(v-u>2){
					ans++;
					a[u]=max(a[u-1],a[u+1]);
				}
			}
			cout<<ans<<endl;//当时少输出了一行这个,白给
			for(int i=0;i<n;i++){
				if(i){
					cout<<" ";
				}
				cout<<a[i];
			}
			cout<<endl;
		}
	}
	return 0;
}

举报

相关推荐

0 条评论