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 |i−j|=1|i−j|=1 (indices ii and jj are adjacent) and set ai:=ai+|ai−aj|;
  2. Choose a pair of indices (i,j)(i,j) such that |i−j|=1|i−j|=1 (indices ii and jj are adjacent) and set 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 (1≤n≤2⋅1051≤n≤2⋅105 ) — the number of elements in aa .

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤2⋅105,0≤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 (11 means 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 1≤ip,jp≤n1≤ip,jp≤n , |ip−jp|=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
5
2 4 6 6 6
Output
2
1 2 3
1 1 2
Input
3
2 8 10
Output
2
2 2 1
2 3 2
Input
4
1 1 1 1
Output
0

解题思路:这道题就是说给你两个操作:

选择1 :ai:=ai+|ai−aj|;

选择2 :ai:=ai−|ai−aj|

问你如何通过最少操作数,使其元素全部变为相同;

1)我们可先通过一个循环记录每个数出现的个数;

2)通过循环找出出现次数最多的元素;并记录下标,这里记录的是出现次数最多的那个元素的最后的下标;

3)由该元素向两边延申(主要是怕越界这个情况);向序号比它小的一边和向序号比它大的一边,分情况延申;

4)特别注意输出的序号要往那个元素最多的序号靠,不然可能会有很多细节上的错误,比如越界等等,我在这里被坑了一个多小时,自己体会一下为什么要往元素最多的那个序号靠;

代码如下:

 #include<iostream>
#include<algorithm>
using namespace std; int n ;
int vis[];
long long int a[];
int maxn = -;
int flag = -;
int num = ;
int count1 = ;
int main()
{ cin>>n;
for(int i = ; i <= n ;i++)
{
cin>>a[i];
vis[a[i]]++; //用一个vis数组记录每个元素出现的个数;
}
for(int i = ; i <= n ;i++)
{ if(maxn<=vis[a[i]])
{
maxn = vis[a[i]]; //找出出现最多次数的元素;
flag = a[i]; //记下该元素;
num = i; //并记下它的下标(这里是相同元素的最后一个);
} } for(int i = num- ; i >= ; i--)
{
if(a[i]==flag)
continue;
else
count1++; //计算一共要修改几次;只要不相等就修改;这是往左边的;
}
for(int i = num + ;i <= n ;i++)
{
if(a[i]==flag)
continue;
else
count1++; //计算一共要修改几次;只要不相等就修改;这是往右边的;
}
cout<<count1<<endl;
if(count1!=) //当有元素需要修改;
{
for(int i = num- ; i >= ; i--) //往其左边;
{
if(a[i]==flag)
continue; else
if(a[i]>flag) //如果该元素比它大,则用操作二;且输出的序号为i和i+1;(就是往flag的序号靠);
{
cout<<<<" "<<i<<" "<<i+<<endl;
}
else
if(a[i]<flag) //如果该元素比它小,则用操作一;且输出的序号为i和i+1;(就是往flag的序号靠)
{
cout<<<<" "<<i<<" "<<i+<<endl;
}
}
for(int i = num ;i <= n ;i++) //往右边;
{
if(a[i]==flag)
{
continue;
}else
if(a[i]>flag)  //如果该元素比它大,则用操作二;且输出的序号为i和i-1;(就是往flag的序号靠);
{
cout<<<<" "<<i<<" "<<i-<<endl;
}
else
if(a[i]<flag)
{
cout<<<<" "<<i<<" "<<i-<<endl; //如果该元素比它小,则用操作一;且输出的序号为i和i-1;(就是往flag的序号靠);
}
} } return ;
}

(原创)Codeforces Round #550 (Div. 3) D. Equalize Them All的更多相关文章

  1. Codeforces Round #550 (Div. 3) D. Equalize Them All (贪心,模拟)

    题意:有一组数,可以选择某个数\(a_i\)相邻的一个数\(a_j\),然后可以让\(a_i\)加上或者减去\(|a_i-a_j|\),问最少操作多少次使得数组中所有数相同. 题解:不难发现,每次操作 ...

  2. 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 standar ...

  3. (原创)Codeforces Round #550 (Div. 3) A Diverse Strings

    A. Diverse Strings time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  4. CodeForces Round #550 Div.3

    http://codeforces.com/contest/1144 A. Diverse Strings A string is called diverse if it contains cons ...

  5. Codeforces Round #590 (Div. 3) A. Equalize Prices Again

    链接: https://codeforces.com/contest/1234/problem/A 题意: You are both a shop keeper and a shop assistan ...

  6. Codeforces Round #570 (Div. 3) B. Equalize Prices

    原文链接https://codeforces.com/contest/1183/problem/B 题意:进行Q组测试,在每组中有长度为n的数组a[i],然后现在给你一个K,问你找到一个bi使得|ai ...

  7. Codeforces Round #550 (Div. 3) F. Graph Without Long Directed Paths

            F. Graph Without Long Directed Paths time limit per test 2 seconds memory limit per test 256 ...

  8. F. Graph Without Long Directed Paths Codeforces Round #550 (Div. 3)

    F. Graph Without Long Directed Paths time limit per test 2 seconds memory limit per test 256 megabyt ...

  9. Codeforces Round #550 (Div. 3) E. Median String (模拟)

    Median String time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

随机推荐

  1. Fiddler 使用技巧

    1.Host重定向,将192.10.11.12:8091的地址重新定向到127.0.0.1:8080 if (oSession.host=="192.10.11.12:8091") ...

  2. java基础知识(14)---API

    API:(Application Programming Interface,编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件的以访问一组例程的能力,而又无需访问源码,或理 ...

  3. 2015.7.24 CAD库中列举五字代码点所属航路及终端区图,左连接的累加

    select decode(fb.tupr,null,'仅航路',decode(fc.aw,null,'仅终端区','航路及终端区')) 范围,pt 五字代码点,fb.tupr 终端区图及程序,fc. ...

  4. 网页弹出框ClientScript,ScriptManager

    网页调用客户端弹出框 this.ClientScript.RegisterStartupScript(this.GetType(), "message", "<sc ...

  5. [Python Study Notes]双层柱状图绘制

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...

  6. 剑指offer 38_统计数组中k出现的个数

    思路: 二分法,分别找出第一个和最后一个k出现的位置.相减 加一 #include <stdio.h> //获取第一个K的位置 int getFirstK (int k,int *numb ...

  7. iter创建一个可以被迭代的对象

    #!/usr/bin/env python obj = iter([11,22,33,44]) #iter 创建一个可以被迭代的对象 print(obj) r1 = next(obj) print(r ...

  8. to_date() 、to_char()、to_number的FMT格式

     元素  含义 结果:2018/01/12(周五) -     /    ,    .     ;    :  (6中不同分隔符) 分隔符         y  显示一位年份  8 yy  显示二位年 ...

  9. show table detail

    create table #t(name varchar(255), rows bigint, reserved varchar(20),data varchar(20), index_size va ...

  10. php 如何禁用eval() 函数实例详解

    在php中eval是一个函数并且不能直接禁用了,但eval函数又相当的危险并经常会出现一些问题,今天我们就一起来看看eval函数对数组的操作及php 如何禁用eval() 函数: <?php $ ...