D. Equalize Them All Codeforces Round #550 (Div. 3)
2 seconds
256 megabytes
standard input
standard output
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.
5
2 4 6 6 6
2
1 2 3
1 1 2
3
2 8 10
2
2 2 1
2 3 2
4
1 1 1 1
0 这个题目比较简单,非常简单
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 100;
int a[maxn];
int vis[maxn];
struct node
{
int tp, x, y;
node(int tp = 0, int x = 0, int y = 0) :tp(tp), x(x), y(y){}
}exa[maxn];
int main()
{
int n;
cin >> n;
int ans = 0, mark = 0,flag=0;
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
vis[a[i]]++;
if(vis[a[i]]>ans)
{
ans = vis[a[i]];
mark = a[i];
flag = i;
}
}
int cnt = 0;
for(int i=flag;i>=1;i--)
{
if (a[i] == mark) continue;
if(a[i]>mark)
{
exa[cnt] = node(2,i, i+1);
cnt++;
}
else
{
exa[cnt] = node(1, i, i + 1);
cnt++;
}
}
for(int i=flag;i<=n;i++)
{
if (a[i] == mark) continue;
if(a[i]>mark)
{
exa[cnt] = node(2, i, i - 1);
cnt++;
}
else
{
exa[cnt] = node(1, i, i - 1);
cnt++;
}
}
printf("%d\n", cnt);
for(int i=0;i<cnt;i++)
{
printf("%d %d %d\n", exa[i].tp, exa[i].x, exa[i].y);
}
return 0;
}
D. Equalize Them All Codeforces Round #550 (Div. 3)的更多相关文章
- (原创)Codeforces Round #550 (Div. 3) D. Equalize Them All
D. Equalize Them All time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Round #550 (Div. 3) D. Equalize Them All (贪心,模拟)
题意:有一组数,可以选择某个数\(a_i\)相邻的一个数\(a_j\),然后可以让\(a_i\)加上或者减去\(|a_i-a_j|\),问最少操作多少次使得数组中所有数相同. 题解:不难发现,每次操作 ...
- CodeForces Round #550 Div.3
http://codeforces.com/contest/1144 A. Diverse Strings A string is called diverse if it contains cons ...
- 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 ...
- 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 ...
- 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 ...
- (原创)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 ...
- Codeforces Round #550 (Div. 3)E. Median String
把字符串看作是26进制的数,从后往前翻译,那么就可以把两个串变成对应的26进制的数字,那么只要把两个数加起来除以二就得到中间的串对应的数了,同理再转化回来就行了.但是这样会有一个问题就是串的长度有2e ...
- Codeforces Round #550 (Div. 3) E. Median String (思维,模拟)
题意:给你两个字符串\(s\)和\(t\),保证\(t\)的字典序大于\(s\),求他们字典序中间的字符串. 题解:我们假设题目给的不是字符串,而是两个10禁止的正整数,那么输出他们之间的数只要把他两 ...
随机推荐
- git常用命令总结(资源来自廖雪峰)
自己把命令弄出来方便以后看看,,应该有错的emmmm 原文地址:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67 ...
- C#窗体越界时鼠标还能回到初始坐标位置
对窗体加越界限制后,鼠标拖动窗体越界时,窗体不能动,鼠标位置可动,但窗体不再越界时,鼠标位置还能回到鼠标按下时相对窗体的坐标:1.首先创建一个窗体Form1,然后在窗体上拖一个button1按钮(主要 ...
- mybatis整合springboot 以及需要注意的细节
具体怎么整合的网上有很多优秀的博客介绍,这里就直接引用一篇个人觉得非常详细的教程: https://blog.csdn.net/winter_chen001/article/details/77249 ...
- sqoop将mysql数据导入hbase、hive的常见异常处理
原创不易,如需转载,请注明出处https://www.cnblogs.com/baixianlong/p/10700700.html,否则将追究法律责任!!! 一.需求: 1.将以下这张表(test_ ...
- 腾讯云下的CentOS7 配置 FTP 服务器
第一步安装vsftp * yum -y install vsftpd 第二步配置ftp文件 要实现用户不仅可以下载ftp上的内容 还可以删改增加文件到ftp服务器 需要给ftp的目录赋予写入权限 1. ...
- mediainfo使用
1.linux安装mediainfo yum install mediainfo(epel源安装) 2.输出文件信息到xml文件 mediainfo --OUTPUT=XML -f ftp ...
- Module的加载实现
烂笔头开始记录小知识点啦- 浏览器要加载 ES6模块,: <script type="module" src="./foo.js"></scr ...
- JAX-WS Web Service小试牛刀
1.使用Eclipse新建Java工程JavaDemo 2.新建包com.kira.ws 3.在包com.kira.ws新建类Hello,代码如下 package com.kira.ws; impor ...
- vue单页应用添加百度统计
前言 申请百度统计后,会得到一段JS代码,需要插入到每个网页中去,在Vue.js项目首先想到的可能就是,把统计代码插入到index.html入口文件中,这样就全局插入,每个页面就都有了;这样做就涉及到 ...
- [总结]高效的jQuery代码编写技巧
缓存变量 DOM遍历是昂贵的,所以尽量将会重用的元素缓存. // 糟糕 h = $('#element').height(); $('#element').css('height',h-20); // ...