B. Avoid Local Maximums

description

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

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 109.

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⋅105.

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.

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.

        这道题主要就是要找“极大值”,相邻两个数字一样,但比两侧的数大的不算“极大值”。

        然后,消除“极大值”。

#include<iostream>
using namespace std;
long long a[1000000];
int main() {
	int t, m;
	cin >> t;
	while (t--) {
		cin >> m;
		int sum = 0, num = 0x3f3f3f3f;//sum为需要改的数字的个数,num用于记录极大值的位置
		for (int i = 0; i < m; i++) {
			cin >> a[i];
		}
		int aa, again = 0;
		for (int i = 1; i < m - 1; i++) {
			if (a[i] > a[i - 1] && a[i] > a[i + 1]) {//寻找极大值
				int reduction = 0;
				sum++;//需要改的数字加一

				if (i - num == 2) {//判断两个极大值中间是否只差一个数
					sum--;//两个极大值中间只差一个数,只改这两个极大值中间打那个数即可,减去多改的那一个
					reduction = 1;
				}

				num = i;//记录极大值的位置

				if (reduction == 1) {//两个极大值中间只差一个数,即只改这个数跟更大的那个一样的即可
					num = 0x3f3f3f3f;//清空num值,准备记录下一个极大值的位置(三个相间隔为1个数字的极大值,必定要改两个数,所以此时,第三个极大值无需与前两个数相比较)
					a[i - 3] = aa;//还原前一个极大值前面改变的数字
					if (a[i] >= a[i - 2]) {//比较两极大值大小,使中间位置等于更大一方
						a[i - 1] = a[i];
					}
					else {
						a[i - 1] = a[i - 2];
					}
				}
				else {
					aa = a[i - 1];//发现一个极大值时,记录它前面一个数的值
					a[i - 1] = a[i];//并把其改为与极大值相等的数字
				}
			}
		}
		printf("%d\n", sum);
		for (int i = 0; i < m; i++) {
			printf("%d", a[i]);
			if (i != m - 1) {
				printf(" ");
			}
		}
		if (t != 0) {
			printf("\n");
		}
	}
	return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
To merge k sorted linked lists, one approach is to repeatedly merge two of the linked lists until all k lists have been merged into one. We can use a priority queue to keep track of the minimum element across all k linked lists at any given time. Here's the code to implement this idea: ``` struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(NULL) {} }; // Custom comparator for the priority queue struct CompareNode { bool operator()(const ListNode* node1, const ListNode* node2) const { return node1->val > node2->val; } }; ListNode* mergeKLists(vector<ListNode*>& lists) { priority_queue<ListNode*, vector<ListNode*>, CompareNode> pq; for (ListNode* list : lists) { if (list) { pq.push(list); } } ListNode* dummy = new ListNode(-1); ListNode* curr = dummy; while (!pq.empty()) { ListNode* node = pq.top(); pq.pop(); curr->next = node; curr = curr->next; if (node->next) { pq.push(node->next); } } return dummy->next; } ``` We start by initializing a priority queue with all the head nodes of the k linked lists. We use a custom comparator that compares the values of two nodes and returns true if the first node's value is less than the second node's value. We then create a dummy node to serve as the head of the merged linked list, and a current node to keep track of the last node in the merged linked list. We repeatedly pop the minimum node from the priority queue and append it to the merged linked list. If the popped node has a next node, we push it onto the priority queue. Once the priority queue is empty, we return the head of the merged linked list. Note that this implementation has a time complexity of O(n log k), where n is the total number of nodes across all k linked lists, and a space complexity of O(k).

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值