(原创)Codeforces Round #550 (Div. 3) D. Equalize Them All
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|;
- 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| ;
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⋅105,0≤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 (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 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
解题思路:这道题就是说给你两个操作:
选择1 :ai:=ai+|ai−aj|;
选择2 :ai:=ai−|ai−aj|
问你如何通过最少操作数,使其元素全部变为相同;
1)我们可先通过一个循环记录每个数出现的个数;
2)通过循环找出出现次数最多的元素;并记录下标,这里记录的是出现次数最多的那个元素的最后的下标;
3)由该元素向两边延申(主要是怕越界这个情况);向序号比它小的一边和向序号比它大的一边,分情况延申;
4)特别注意输出的序号要往那个元素最多的序号靠,不然可能会有很多细节上的错误,比如越界等等,我在这里被坑了一个多小时,自己体会一下为什么要往元素最多的那个序号靠;
代码如下:
#include<iostream>
#include<algorithm>
using namespace std; int n ;
int vis[];
long long int a[];
int maxn = -;
int flag = -;
int num = ;
int count1 = ;
int main()
{ cin>>n;
for(int i = ; i <= n ;i++)
{
cin>>a[i];
vis[a[i]]++; //用一个vis数组记录每个元素出现的个数;
}
for(int i = ; i <= n ;i++)
{ if(maxn<=vis[a[i]])
{
maxn = vis[a[i]]; //找出出现最多次数的元素;
flag = a[i]; //记下该元素;
num = i; //并记下它的下标(这里是相同元素的最后一个);
} } for(int i = num- ; i >= ; i--)
{
if(a[i]==flag)
continue;
else
count1++; //计算一共要修改几次;只要不相等就修改;这是往左边的;
}
for(int i = num + ;i <= n ;i++)
{
if(a[i]==flag)
continue;
else
count1++; //计算一共要修改几次;只要不相等就修改;这是往右边的;
}
cout<<count1<<endl;
if(count1!=) //当有元素需要修改;
{
for(int i = num- ; i >= ; i--) //往其左边;
{
if(a[i]==flag)
continue; else
if(a[i]>flag) //如果该元素比它大,则用操作二;且输出的序号为i和i+1;(就是往flag的序号靠);
{
cout<<<<" "<<i<<" "<<i+<<endl;
}
else
if(a[i]<flag) //如果该元素比它小,则用操作一;且输出的序号为i和i+1;(就是往flag的序号靠)
{
cout<<<<" "<<i<<" "<<i+<<endl;
}
}
for(int i = num ;i <= n ;i++) //往右边;
{
if(a[i]==flag)
{
continue;
}else
if(a[i]>flag) //如果该元素比它大,则用操作二;且输出的序号为i和i-1;(就是往flag的序号靠);
{
cout<<<<" "<<i<<" "<<i-<<endl;
}
else
if(a[i]<flag)
{
cout<<<<" "<<i<<" "<<i-<<endl; //如果该元素比它小,则用操作一;且输出的序号为i和i-1;(就是往flag的序号靠);
}
} } return ;
}
(原创)Codeforces Round #550 (Div. 3) D. Equalize Them All的更多相关文章
- Codeforces Round #550 (Div. 3) D. Equalize Them All (贪心,模拟)
题意:有一组数,可以选择某个数\(a_i\)相邻的一个数\(a_j\),然后可以让\(a_i\)加上或者减去\(|a_i-a_j|\),问最少操作多少次使得数组中所有数相同. 题解:不难发现,每次操作 ...
- D. Equalize Them All Codeforces Round #550 (Div. 3)
D. Equalize Them All time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- (原创)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
http://codeforces.com/contest/1144 A. Diverse Strings A string is called diverse if it contains cons ...
- Codeforces Round #590 (Div. 3) A. Equalize Prices Again
链接: https://codeforces.com/contest/1234/problem/A 题意: You are both a shop keeper and a shop assistan ...
- Codeforces Round #570 (Div. 3) B. Equalize Prices
原文链接https://codeforces.com/contest/1183/problem/B 题意:进行Q组测试,在每组中有长度为n的数组a[i],然后现在给你一个K,问你找到一个bi使得|ai ...
- 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 ...
随机推荐
- [转载]Ubuntu下ssh服务的安装与登陆(ssh远程登陆)
转载地址:http://blog.csdn.net/zht666/article/details/9340633 Ubuntu默认并没有安装ssh服务,如果通过ssh远程连接到Ubuntu,需要自己手 ...
- L2-014. 列车调度(set的使用,最长递增子序列)
L2-014. 列车调度 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 火车站的列车调度铁轨的结构如下图所示. Figure ...
- dijstra+输出路径总结
#include<iostream> #include<math.h> #include<memory.h> using namespace std; #defin ...
- Identity4 Clientcredentials 自定义客户端授权验证
看了许多教程 ,大多数都是提前定义好客户端,但是这样有个弊端,我们并不知道以后会有多少客户端.有可能从数据库读取数据,也有可能通过json文件获取,总之 各种方式. 然后 网上大多数教程都是提前定义好 ...
- bash姿势-没有管道符执行结果相同于管道符
听起来比较别口: 直接看代码: shell如下: [root@sevck_linux ~]# </etc/passwd grep root root:x:::root:/root:/bin/ba ...
- Hibernate面试总结
SSH原理总结 Hibernate工作原理及为什么要用: 原理: hibernate,通过对jdbc进行封装,对 java类和 关系数据库进行mapping,实现了对关系数据库的面向对象方式的操作,改 ...
- 转载 : JSP取得绝对路径
转自:https://www.aliyun.com/jiaocheng/770177.html 转自:http://www.cnblogs.com/xdp-gacl/p/3707243.html 在J ...
- Android Tombstone 分析
1.什么是tombstone 当一个动态库(native 程序)开始执行时,系统会注册一些连接到 debuggerd 的 signal handlers,当系统 crash 的时候,会保存一个 tom ...
- <c:set var="ctx" value="${pageContext.request.contextPath}" />的学习
${pageContext.request.contextPath},是获取当前根目录 set var="ctx",是给这个路径定义了一个变量,用的时候可以通过EL表达式获取:${ ...
- 数据库理论-范式(1NF、2NF、3NF)
范式是“符合某一种级别的关系模式的集合,表示一个关系内部各属性之间的联系的合理化程度”. 第一范式(1NF)是指数据库表的每一列都是不可分割的基本数据项.(每个属性不可分割)第二范式(2NF)要求数据 ...