Codeforces1144D(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):
- 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|ai:=ai+|ai−aj|;
- 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|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.
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.
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.
代码:
#include<iostream>
#include<algorithm>
using namespace std;
int n,a[],cnt[],mx=-,mxd;
int main() {
cin>>n;
for(int i=; i<=n; i++) {
cin>>a[i];
cnt[a[i]]++;
}
for(int i=; i<=; i++)
if(cnt[i]>mx) {
mx=cnt[i];
mxd=i;
}
cout<<n-mx<<endl;
for(int i=; i<=n; i++) {
if(a[i-]!=mxd)continue;
if(a[i]>a[i-])cout<<<<" "<<i<<" "<<i-<<endl;
if(a[i]<a[i-])cout<<<<" "<<i<<" "<<i-<<endl;
a[i]=a[i-];
}
for(int i=n-; i>=; i--) {
if(a[i+]!=mxd)continue;
if(a[i]>a[i+])cout<<<<" "<<i<<" "<<i+<<endl;
if(a[i]<a[i+])cout<<<<" "<<i<<" "<<i+<<endl;
a[i]=a[i+];
}
return ;
}
思路分析:先找出最多次数的数字,然后向左向右循环比较,向左时,如果左元素小,通过2方式来变为出现最多次数字。如果左元素大,通过1方式来变为出现最多次数字。向右同理。
Codeforces1144D(D题)Equalize Them All的更多相关文章
- 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem C: The Trip(水题)
Problem C: The Trip Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 19 Solved: 3[Submit][Status][Web ...
- CF1234A Equalize Prices
洛谷 CF1234A Equalize Prices Again 洛谷传送门 题目描述 You are both a shop keeper and a shop assistant at a sma ...
- java基础集合经典训练题
第一题:要求产生10个随机的字符串,每一个字符串互相不重复,每一个字符串中组成的字符(a-zA-Z0-9)也不相同,每个字符串长度为10; 分析:*1.看到这个题目,或许你脑海中会想到很多方法,比如判 ...
- 【Java每日一题】20170106
20170105问题解析请点击今日问题下方的"[Java每日一题]20170106"查看(问题解析在公众号首发,公众号ID:weknow619) package Jan2017; ...
- 【Java每日一题】20170105
20170104问题解析请点击今日问题下方的"[Java每日一题]20170105"查看(问题解析在公众号首发,公众号ID:weknow619) package Jan2017; ...
- 【Java每日一题】20170104
20170103问题解析请点击今日问题下方的"[Java每日一题]20170104"查看(问题解析在公众号首发,公众号ID:weknow619) package Jan2017; ...
- 【Java每日一题】20170103
20161230问题解析请点击今日问题下方的"[Java每日一题]20170103"查看(问题解析在公众号首发,公众号ID:weknow619) package Jan2017; ...
- SQL面试笔试经典题(Part 1)
本文是在Cat Qi的原贴的基础之上,经本人逐题分别在MySql数据库中实现的笔记,持续更新... 参考原贴:http://www.cnblogs.com/qixuejia/p/3637735.htm ...
- 刷LeetCode的正确姿势——第1、125题
最近刷LeetCode比较频繁,就购买了官方的参考电子书 (CleanCodeHandbook),里面有题目的解析和范例源代码,可以省去非常多寻找免费经验分享内容和整理这些资料的时间.惊喜的是,里面的 ...
随机推荐
- SYN4104型 数字网同步时钟
SYN4104型 数字网同步时钟 产品概述 SYN4104型数字网同步时钟是由西安同步电子科技有限公司精心设计.自行研发生产的一款高精度锁相时钟频率源,接收GPS信号,使恒温晶振输出频率同步于GPS卫 ...
- python+Django 下JWT的使用
figure:first-child { margin-top: -20px; } #write ol, #write ul { position: relative; } img { max-wid ...
- 设计模式之装饰器模式(decorator pattern)
装饰器模式主要对现有的类对象进行包裹和封装,以期望在不改变类对象及其类定义的情况下,为对象添加额外功能.是一种对象结构型模式.需要注意的是,该过程是通过调用被包裹之后的对象完成功能添加的,而不是直接修 ...
- MySQL8.0 DDL原子性特性
1. DDL原子性概述 8.0之前并没有统一的数据字典dd,server层和引擎层各有一套元数据,sever层的元数据包括(.frm,.opt,.par,.trg等),用于存储表定义,分区表定义,触发 ...
- 设计模式之策略模式和状态模式(strategy pattern & state pattern)
本文来讲解一下两个结构比较相似的行为设计模式:策略模式和状态模式.两者单独的理解和学习都是比较直观简单的,但是实际使用的时候却并不好实践,算是易学难用的设计模式吧.这也是把两者放在一起介绍的原因,经过 ...
- Linux下编译PHP常见错误及解决方法
1.configure: error: xml2-config not found. Please check your libxml2 installation.yum install libxml ...
- Win32小游戏--蜘蛛纸牌
前一段时间完成了蜘蛛纸牌的仿写,现将过程和思路记录下来 首先,为了符合复用性,在win32的基本框架中,把可变的部分用c++封装起来成为一系列虚函数,这样如果再继续写游戏的话,只需要继承这个类就可以了 ...
- map的实现--红黑树
一.什么是红黑树??? 红黑树首先是一棵搜索二叉树,树中的每一个结点的颜色不是黑色就是红色.它的特性如下: 1.根节点是黑色 2.每一个结点不是黑色就是红色 3.不能有连续的两个红色结 ...
- Python Day_1
听说Python很6,然后我的偶像小甲鱼竟然在6年前(现在2019年)就开始制作Python的教程,而前不久(世界读书日前一个星期左右),京东有活动,小甲鱼的Python第一版本打折,顺便买了本(还凑 ...
- kuangbin专题 专题一 简单搜索 非常可乐 HDU - 1495
题目链接:https://vjudge.net/problem/HDU-1495 题意:有两个空杯(分别是N升和M升)和一罐满的可乐S升,S = N + M,三个容器可以互相倾倒,如果A倒入B,只有两 ...