D. Equalize Them All Codeforces Round #550 (Div. 3)

D. Equalize Them All
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an array aa consisting of nn integers. You can perform the following operations arbitrary number of times (possibly, zero):

  1. Choose a pair of indices (i,j)(i,j) such that |ij|=1|i−j|=1 (indices ii and jj are adjacent) and set ai:=ai+|aiaj|ai:=ai+|ai−aj|;
  2. Choose a pair of indices (i,j)(i,j) such that |ij|=1|i−j|=1 (indices ii and jj are adjacent) and set ai:=ai|aiaj|ai:=ai−|ai−aj|.

The value |x||x| means the absolute value of xx. For example, |4|=4|4|=4, |3|=3|−3|=3.

Your task is to find the minimum number of operations required to obtain the array of equal elements and print the order of operations to do it.

It is guaranteed that you always can obtain the array of equal elements using such operations.

Note that after each operation each element of the current array should not exceed 10181018 by absolute value.

Input

The first line of the input contains one integer nn (1n21051≤n≤2⋅105) — the number of elements in aa.

The second line of the input contains nn integers a1,a2,,ana1,a2,…,an (0ai21050≤ai≤2⋅105), where aiai is the ii-th element of aa.

Output

In the first line print one integer kk — the minimum number of operations required to obtain the array of equal elements.

In the next kk lines print operations itself. The pp-th operation should be printed as a triple of integers (tp,ip,jp)(tp,ip,jp), where tptp is either 11 or 22 (11means that you perform the operation of the first type, and 22 means that you perform the operation of the second type), and ipip and jpjp are indices of adjacent elements of the array such that 1ip,jpn1≤ip,jp≤n, |ipjp|=1|ip−jp|=1. See the examples for better understanding.

Note that after each operation each element of the current array should not exceed 10181018 by absolute value.

If there are many possible answers, you can print any.

Examples
input
Copy
5
2 4 6 6 6
output
Copy
2
1 2 3 
1 1 2 
input
Copy
3
2 8 10
output
Copy
2
2 2 1 
2 3 2 
input
Copy
4
1 1 1 1
output
Copy
0





这个题目比较简单,非常简单


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 100;
int a[maxn];
int vis[maxn];
struct node
{
	int tp, x, y;
	node(int tp = 0, int x = 0, int y = 0) :tp(tp), x(x), y(y){}
}exa[maxn];
int main()
{
	int n;
	cin >> n;
	int ans = 0, mark = 0,flag=0;
	memset(vis, 0, sizeof(vis));
	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &a[i]);
		vis[a[i]]++;
		if(vis[a[i]]>ans)
		{
			ans = vis[a[i]];
			mark = a[i];
			flag = i;
		}
	}
	int cnt = 0;
	for(int i=flag;i>=1;i--)
	{
		if (a[i] == mark) continue;
		if(a[i]>mark)
		{
			exa[cnt] = node(2,i, i+1);
			cnt++;
		}
		else
		{
			exa[cnt] = node(1, i, i + 1);
			cnt++;
		}
	}
	for(int i=flag;i<=n;i++)
	{
		if (a[i] == mark) continue;
		if(a[i]>mark)
		{
			exa[cnt] = node(2, i, i - 1);
			cnt++;
		}
		else
		{
			exa[cnt] = node(1, i, i - 1);
			cnt++;
		}
	}
	printf("%d\n", cnt);
	for(int i=0;i<cnt;i++)
	{
		printf("%d %d %d\n", exa[i].tp, exa[i].x, exa[i].y);
	}
	return 0;
}

  












猜你喜欢

转载自www.cnblogs.com/EchoZQN/p/10645757.html