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

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.

队列模拟

队列存下a[i-1]<a[i]<a[i+1] 中a[i]的下标,判断下标之间的间距。
若,后一个下标 - 前一个下标的值 = 2,将其中间的值修改为max(a[前],a[后]);
若,后一个下标 - 前一个下标的值 > 2, 单独修改 a[前]=max(a[前-1],a[前+1]);
若,队列中元素只剩一个,下标为k,单独修改 a[k]=max(a[k-1],a[k+1]);
用队列写的话,一定要注意pop()元素的地方
ps:用数组简单模拟是可以的,但是当时打的时候想到队列直接用队列打了

#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;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值