zoukankan      html  css  js  c++  java
  • 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):

    1. Choose a pair of indices (i,j)(i,j) such that |ij|=1|i−j|=1 (indices ii and jj are adjacent) and set ai:=ai+|aiaj|ai:=ai+|ai−aj|;
    2. Choose a pair of indices (i,j)(i,j) such that |ij|=1|i−j|=1 (indices ii and jj are adjacent) and set ai:=ai|aiaj|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.

    Input

    The first line of the input contains one integer nn (1n21051≤n≤2⋅105) — the number of elements in aa.

    The second line of the input contains nn integers a1,a2,,ana1,a2,…,an (0ai21050≤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 1ip,jpn1≤ip,jp≤n, |ipjp|=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

  • 相关阅读:
    [Flutter] 转一个Flutter学习思维导图
    [Node.js] TypeScript 实现 sleep 函数
    [Docker] Win10中安装Docker并运行Nginx镜像
    [Zabbix] 安装MySQL5.7, 部署Zabbix到CentOS 7日记
    [Delphi]无边框窗口最大化不挡任务栏方法
    [Flutter] 一些面试可能会问基础知识
    Flutter main future mirotask 的执行顺序
    Underscore.js基础入门
    将第三方包安装到maven本地仓库
    Ubuntu下JDK1.8安装后配置环境变量
  • 原文地址:https://www.cnblogs.com/yuanhang110/p/11255891.html
Copyright © 2011-2022 走看看