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. Java基础--枚举Enum

    Java中的枚举是一种特殊的类,可以将一组固定常量的集合组成一种类型,使用方便且类型安全.使用enum关键字定义. enum类型父类为Enum,通过Enum.class可见Enum为抽象类,实现了Co ...

  2. [转] linux 启动文件及设置环境变量

    系统级启动文件  ==================================== 1./etc/rc  主启动文件,不要修改它 2./etc/rc.conf  决定启动哪些系统自带的守护进程 ...

  3. java中I/O类

    总结:输入流/输出流 方法,变量: package com.aini; //流类.输入输出流 import java.io.*; public class rtyeew {// (File file) ...

  4. 杂项-IIS:发布杂项

    ylbtech-杂项-IIS:发布杂项 1. 测试连接返回顶部 1.1.授权 无法验证对路径的访问. 1.2.详情信息 服务器配置为将传递身份验证和内置帐户一起使用,以访问指定的物理路径.但是,IIS ...

  5. filter中获取spring bean

    import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import ja ...

  6. Git 之Windows环境下学习系列

    Git .SVN .TFS   相同点 不同点 Git     版本控制 优点: 分布式版本控制.无需联网就能版本提交 开源 缺点 入门学习难度高 SVN   优点: 集中式版本控制. 个人开源 缺点 ...

  7. java.lang.Runtime.exec() Payload Workarounds

    由于Runtime.getRuntime().exec()中不能使用管道符等bash需要的方法,我们需要用进行一次编码 编码工具: 地址: http://jackson.thuraisamy.me/r ...

  8. [poj3041]Asteroids(二分图的最小顶点覆盖)

    题目大意:$N*N$的网格中有$n$颗行星,若每次可以消去一整行或一整列,求最小的攻击次数使得消去所有行星. 解题关键:将光束当做顶点,行星当做连接光束的边建图,题目转化为求该图的最小顶点覆盖,图的最 ...

  9. mysql sequelize 聚合

    User.findAll({attributes: [[sequelize.fn('COUNT', sequelize.col('*')), 'email']],raw: true }).then(f ...

  10. Easyui datebox单击文本框显示日期选择 eayui版本1.5.4.1

    Easyui默认是点击文本框后面的图标显示日期,体验很不好,所以我想单击文本框就显示日期选择框,网上很多版本是1.3,1.4的,于是自己就比葫芦画瓢改了一个1.5.4.1的版本. 我参考了网上这个帖子 ...