• 题意:有一组数,可以选择某个数\(a_i\)相邻的一个数\(a_j\),然后可以让\(a_i\)加上或者减去\(|a_i-a_j|\),问最少操作多少次使得数组中所有数相同.

  • 题解:不难发现,每次操作必然可以使得一个数等于它旁边的任意一个数,所以让数组中的其他数等于出现次数最多的那个数一定是最优的,然后我们就去找与那个数相邻的数去求块,正着反着模拟输出一下就可以了,具体看代码吧.

  • 代码:

    int n;
    int a[N];
    map<int,int> mp;
    int mx,res;
    bool st[N]; int main() {
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>n;
    rep(i,1,n){
    cin>>a[i];
    mp[a[i]]++;
    if(mp[a[i]]>mx){
    mx=mp[a[i]];
    res=a[i];
    }
    } int i=1;
    cout<<n-mx<<'\n'; while(i<=n){
    bool flag=false;
    if(a[i]!=res && a[i-1]==res){
    while(a[i]!=res && i<=n){
    int t=1;
    if(a[i]>res) t=2;
    cout<<t<<' '<<i<<' '<<i-1<<'\n';
    st[i]=true;
    i++;
    flag=true;
    }
    }
    if(!flag) i++;
    }
    i=n-1; while(i>=1){
    bool flag=false;
    if(a[i]!=res && a[i+1]==res){
    while(a[i]!=res && i>=1 && !st[i]){
    int t=1;
    if(a[i]>res) t=2;
    cout<<t<<' '<<i<<' '<<i+1<<'\n';
    i--;
    flag=true;
    }
    }
    if(!flag) i--;
    } return 0;
    }

Codeforces Round #550 (Div. 3) D. Equalize Them All (贪心,模拟)的更多相关文章

  1. (原创)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 ...

  2. Codeforces Round #373 (Div. 2) A B C 水 贪心 模拟(四舍五入进位)

    A. Vitya in the Countryside time limit per test 1 second memory limit per test 256 megabytes input s ...

  3. Codeforces Round #481 (Div. 3) G. Petya's Exams (贪心,模拟)

    题意:你有\(n\)天的时间,这段时间中你有\(m\)长考试,\(s\)表示宣布考试的日期,\(d\)表示考试的时间,\(c\)表示需要准备时间,如果你不能准备好所有考试,输出\(-1\),否则输出你 ...

  4. Codeforces Round #656 (Div. 3) C. Make It Good (贪心,模拟)

    题意:给你一个数组\(a\),可以删除其前缀,要求操作后得到的数组是"good"的.对于"good":可以从数组的头和尾选择元素移动到新数组,使得所有元素移动后 ...

  5. Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心

    Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  ...

  6. 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 ...

  7. CodeForces Round #550 Div.3

    http://codeforces.com/contest/1144 A. Diverse Strings A string is called diverse if it contains cons ...

  8. 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 ...

  9. Codeforces Round #570 (Div. 3) B. Equalize Prices

    原文链接https://codeforces.com/contest/1183/problem/B 题意:进行Q组测试,在每组中有长度为n的数组a[i],然后现在给你一个K,问你找到一个bi使得|ai ...

随机推荐

  1. Centos搭建Git服务端

    首先需要安装git,可以使用yum源在线安装 yum install -y git 创建一个git用户,用来运行管理git服务 adduser git 初始化git仓库(这里我们选择/home/git ...

  2. zabbix自动发现主机并注册

  3. C语言流程图画法(C语言学习笔记)

    常用符号及其含义 图片来自百度文库 https://wenku.baidu.com/view/beb410dea216147916112853.html 常用结构 N-S图

  4. 09--Docker 安装tomcat9

    1.在hub.docker.com中获取tomcat拉取地址 docker pull tomcat:9.0.41-jdk8-corretto 2.查看Dockerfile 中WORKDIR 为/use ...

  5. uni-app开发经验分享八: 实现微信APP支付的全过程详解

    背景 最近项目使用uni-app实现微信支付,把过程简单记录下,帮助那些刚刚基础uni-app,苦于文档的同学们.整体来说实现过程和非uni-app的实现方式没有太大不同,难点就在于uni-app对于 ...

  6. K8s secret解密

    root@ubuntu:~# kubectl get secret rbd-db -n rbd-system -o yaml apiVersion: v1 data: mysql-password: ...

  7. 转 Fiddler5 发送HTTP请求

    Fiddler5 发送HTTP请求  文章转自:https://www.cnblogs.com/zhengna/p/10879573.html 1.Fiddler Composer发送HTTP请求 C ...

  8. The Garbage Collection Handbook

    The Garbage Collection Handbook The Garbage Collection Handbook http://gchandbook.org/editions.html ...

  9. secure hashes message digests 安全哈希 消息摘要

    hashlib --- 安全哈希与消息摘要 - Python 3.8.3 文档 https://docs.python.org/zh-cn/3.8/library/hashlib.html hashl ...

  10. ftp协议服务器与tinyhttp服务demo

    https://www.jianshu.com/p/fb9fcb69efc9 ....... https://www.jianshu.com/p/e9a2e0755496 ============== ...