Codeforces1144D(D题)Equalize Them All
D. Equalize Them All
You are given an array?aa?consisting of?nn?integers. You can perform the following operations arbitrary number of times (possibly,zero):
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?1050≤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?(11means 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. 代码: 1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 int n,a[200010],cnt[200010],mx=-1,mxd; 5 int main() { 6 cin>>n; 7 for(int i=1; i<=n; i++) { 8 cin>>a[i]; 9 cnt[a[i]]++; 10 } 11 for(int i=0; i<=200000; i++) 12 if(cnt[i]>mx) { 13 mx=cnt[i]; 14 mxd=i; 15 } 16 cout<<n-mx<<endl; 17 for(int i=2; i<=n; i++) { 18 if(a[i-1]!=mxd)continue; 19 if(a[i]>a[i-1])cout<<2<<" "<<i<<" "<<i-1<<endl; 20 if(a[i]<a[i-1])cout<<1<<" "<<i<<" "<<i-1<<endl; 21 a[i]=a[i-1]; 22 } 23 for(int i=n-1; i>=1; i--) { 24 if(a[i+1]!=mxd)continue; 25 if(a[i]>a[i+1])cout<<2<<" "<<i<<" "<<i+1<<endl; 26 if(a[i]<a[i+1])cout<<1<<" "<<i<<" "<<i+1<<endl; 27 a[i]=a[i+1]; 28 } 29 return 0; 30 } 思路分析:先找出最多次数的数字,然后向左向右循环比较,向左时,如果左元素小,通过2方式来变为出现最多次数字。如果左元素大,通过1方式来变为出现最多次数字。向右同理。 题目链接:https://codeforces.com/contest/1144/problem/D (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- java – Kafka:从消费者端动态确定主题中分区数量的最佳方
- java – 将System.setout重定向回标准输出
- Java.io.IOException,“坏文件号”USB连接
- java – 获取异常,如“无法将值’0000-00-00 00:00:00’从第
- java – APT和AOP在同一个项目中,使用Maven
- 用Java编写整数数组到文件的最快方法?
- 线程生命周期的几种状态
- JSP JSTL <sql:dateParam>标签:填充日期型参数
- java – Spring Boot管理日志中重复的AsyncRequestTimeoutE
- Hibernate allEq方法:设置一系列的相等条件