博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[CF]Equalize Them All
阅读量:4690 次
发布时间:2019-06-09

本文共 2846 字,大约阅读时间需要 9 分钟。

 

D. Equalize Them All

Description

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 10181018by 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 aiaiis 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 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 10181018by absolute value.

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

 

正确解法:

题目保证最后数列一定会化成一样的。我们看前两个选择,其实就是把 ai 化成 aj。

其中的不同就是 一个 ai < aj 另一个 ai > aj

那么我们找最多的那一个数字就是最小的次数

剩下的就是一个个划嘛,从最多的那一个数字的第一个开始往前化,往后化。

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 using namespace std;10 typedef long long ll;11 const int inf=0x7fffffff;12 const int N=200000+100;13 int n,a[N],cnt[N],pos,maxx=0,xx;14 int main()15 {16 scanf("%d",&n);17 for(int i=1;i<=n;i++)18 {19 scanf("%d",&a[i]);20 cnt[a[i]]++;21 if(cnt[a[i]]>maxx)22 {23 maxx=cnt[a[i]];24 xx=a[i];25 }26 }27 for(int i=1;i<=n;i++)28 if(a[i]==xx)29 {30 pos=i;31 break;32 }33 cout<
<
=1;i--)35 {36 if(a[i]
a[i-1])50 cout<<2<<" ";51 else52 cout<<1<<" ";53 cout<
<<" "<
<
View Code

 

 

 

 

转载于:https://www.cnblogs.com/Kaike/p/10716131.html

你可能感兴趣的文章
2018二月实现计划成果及其三月规划
查看>>
类名.class和getClass()区别
查看>>
12/17面试题
查看>>
LeetCode 242. Valid Anagram
查看>>
JSP表单提交乱码
查看>>
如何适应现代雇佣关系
查看>>
团队项目(第五周)
查看>>
SQL 优化经验总结34条
查看>>
开源 视频会议 收藏
查看>>
核心J2EE模式 - 截取过滤器
查看>>
.net开源CMS
查看>>
JdbcTemplate
查看>>
第一次使用maven记录
查看>>
SharePoint服务器端对象模型 之 使用CAML进展数据查询
查看>>
Building Tablet PC Applications ROB JARRETT
查看>>
Adobe® Reader®.插件开发
查看>>
【POJ 3461】Oulipo
查看>>
Alpha 冲刺 (5/10)
查看>>
使用Siege进行WEB压力测试
查看>>
斑马为什么有条纹?
查看>>