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. QtCreator开启-O编译优化的方式

    首先,编译优化必须是在Release模式下进行,保证程序没有任何bug的条件下进行执行.编译优化能极大提升程序的运行效率,级别越高速度越快,但是对代码健壮性要求也越高! 选择编译release模式,在 ...

  2. 解决windows下tomcat端口被占用[Address already in use: JVM_Bind]

    有时候电脑比较卡,项目比较大的情况下,eclipse没有完全停止tomcat的情况下,下次启动会出现tomcat的端口被占用无法启动的情况,主要报如下错误 解决方案 window下打开命令窗口(快捷键 ...

  3. Asp.NET Core+ABP框架+IdentityServer4+MySQL+Ext JS之验证码

    验证码这东西,有人喜欢有人不喜欢.对于WebApi是否需要验证码,没去研究过,只是原来的SimpleCMS有,就加上吧. 在WeiApi上使用验证码,关键的地方在于WeiApi是没有状态的,也就是说, ...

  4. Day2-VIM(六): 恢复

    恢复在VIM里比较简单,不过想要具体恢复到某个时间段很难 就我的经验而言,有时候使用恢复还不如删了重写 这里我们来讲讲恢复.撤销和重复命令的使用 u 撤消上次命令 U 恢复整行 ctrl+r 重做 . ...

  5. HTML 5中的结构元素

    1.header:标记头部区域的内容 .footer:标记页脚区域的内容 .section:Web页面中的一块区域 4.article:独立的文章内容区域 5.aside:相关侧边内容或者引文区域 6 ...

  6. kafka集群安装和kafka-manager

    1.软件环境 (3台服务器-测试)10.11.12.31 mykafka110.11.12.32 mykafka210.11.12.33 mykafka3 [root@localhost ~]# ca ...

  7. TP-Link WR703N OpenWRT固件修改WAN LAN排序

    有一种方法就是macvlan了.添加到rc.local文件中,具体不再阐述. 此方法只适合编译固件的情况下调整WAN/LAN顺序. wr703n等(包含其他未列出的单网口路由,AP),修改WAN LA ...

  8. Winsock 示例

    #include "stdafx.h" #include <Windows.h> #include <iostream> #pragma comment(l ...

  9. 数据库连接池在Tomcat中的几种配置方法

    数据库连接是一种关键的有限的昂贵的资源,这在多用户网页应用程序中体现的尤为突出.对数据库连接的管理能显著影响到整个应用程序的伸缩性和健壮性,影响到程序的性能指标,数据库连接池正是针对这个问题提出的. ...

  10. C#引用类库时出现黄色三角加感叹号的处理

    C#引用类库时出现黄色三角加感叹号的处理方法 一个C#项目 在引用中有个引用项上有个黄色三角加感叹号 导致报错 类库的目标框架不一致,修改成一样就可以了. 选中类库右击属性:“目标框架”,修改成与引用 ...