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禁止的正整数,那么输出他们之间的数只要把他两 ...
随机推荐
- sql server查询语句条件判断字段值是否为NULL
判断字段是否为null select * from table where c is null select * from table where c is not null 判断字段是否为空 ...
- [日常] HTTP连接管理
HTTP连接管理: 1.误解的Connection首部 当http报文经过中间客户端到服务端中间的各种代理设备时,对标签中列出的头信息进行删除,close是事务结束后关掉此条连接 2.消除串行化的时延 ...
- vue(三)-父子组件通信
原因 : Vue 的组件作用域都是孤立的,不允许在子组件的模板内直接引用父组件的数据.必须使用特定的方法才能实现组件之间的数据传递. props 父组件给子组件传递数据 props:作用是父组件给 ...
- Js 控制随机数概率
如: 取 1~10 之间的随机数,那么他们的取值范围是: 整数 区间 概率 1 [0,1) 0.1 2 [1,2) 0.1 3 [2,3) 0.1 4 [3,4) 0.1 5 [4,5) 0.1 6 ...
- 4:Python的while循环
while循环: while count<10: print(count) #count=100 count=count+1 print(123) while循环break:终止所有循环 cou ...
- 详解bootstrap-fileinput文件上传控件的亲身实践
经理让我帮服务器开发人员开发一个上传文件功能界面,我就想着以前使用过bootstrap-fileinput插件进行文件上传,很不错.赶紧就撸起来了. 1.下载压缩包.插件地址https://githu ...
- Python国内豆瓣源
Python国内豆瓣源 https://pypi.doubanio.com/simple/
- 常用的JVM调优参数总结汇总【随时查阅学习】
本文章参数根据后期用的参数会持续更新 --- (1)-Xms20M 表示设置JVM启动内存的最小值为20M,必须以M为单位 (2)-Xmx20M 表示设置JVM启动内存的最大值为20M,必须以M为单 ...
- mysql数据库的基本操作:创建数据库、查看数据库、修改数据库、删除数据库
本节相关: 创建数据库 查看数据库 修改数据库 删除数据库 首发时间:2018-02-13 20:47 修改: 2018-04-07:考虑到规范化,将所有语法中“关键字”变成大写;以及因为整理“mys ...
- 【转】c# [Serializable]的作用
http://blog.csdn.net/chinarenkai/article/details/3220452 如果你做远程方法调用(RPC)时,比如,服务器端有个类A及对象a,客户端需要无视网络的 ...