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代码。从来没想过java里的继承是多么的难懂。如哲学

    总结:实例.. 这里不加super("aaa",32); 运行:父类和子类的姓名,年龄是一样的.那这个super为什么没效果呢? 显示:class:Ji姓名是 小红年龄是:20课程 ...

  2. js页面埋点

    页面埋点的作用,其实就是用于流量分析.而流量的意思,包含了很多:页面浏览数(PV).独立访问者数量(UV).IP.页面停留时间.页面操作时间.页面访问次数.按钮点击次数.文件下载次数等.而流量分析又有 ...

  3. Network(lca暴力)

    Network Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other) Total Submi ...

  4. AWS + Stunnel + Squid ***

    [需求] 第一,能***. 第二,在企业网络要能突破端口限制. [原理] 利用AWS提供的一年免费EC2服务,搭建一台自己的VPS,在VPS中利用Stunnel与本机建立加密连接,将本地http请求通 ...

  5. Rails的静态资源管理(四)—— 生产环境的 Asset Pipeline

    官方文档:http://guides.ruby-china.org/asset_pipeline.html http://guides.rubyonrails.org/asset_pipeline.h ...

  6. windows黑科技-记录dns log

    昨天看到袁哥微博,看到了这篇,今天测试了一下,记录下来: The DNS Client service does not log by default. However, if a file name ...

  7. import javax.servlet 出错(真的很管用)

    Error: The import javax.servlet cannot be resolved The import javax.servlet.http.HttpServletRequest ...

  8. ffmpeg: ‘UINT64_C’ was not declared in this scope (转)

    ffmpeg 默认是用C文件来编译的,如果某个CPP文件想引用ffmpeg中的某些函数或者头文件,有可能出现 ‘UINT64_C’ was not declared in this scope的错误 ...

  9. springmvc 注解式开发 处理器方法的返回值

    1.返回void -Ajax请求 后台: 前台: 返回object中的数值型: 返回object中的字符串型: 返回object中的自定义类型对象: 返回object中的list: 返回object中 ...

  10. 1-1+zookeeper简介

     zookeeper是中间件,可以为分布式系统提供协调服务.如果没有分布式系统,zookeeper也发挥不了它的优势.