Codeforces Round #550 (Div. 3) D. Equalize Them All (贪心,模拟)

题意:有一组数,可以选择某个数\(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 (贪心,模拟)的更多相关文章
- (原创)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 #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 ...
- Codeforces Round #481 (Div. 3) G. Petya's Exams (贪心,模拟)
题意:你有\(n\)天的时间,这段时间中你有\(m\)长考试,\(s\)表示宣布考试的日期,\(d\)表示考试的时间,\(c\)表示需要准备时间,如果你不能准备好所有考试,输出\(-1\),否则输出你 ...
- Codeforces Round #656 (Div. 3) C. Make It Good (贪心,模拟)
题意:给你一个数组\(a\),可以删除其前缀,要求操作后得到的数组是"good"的.对于"good":可以从数组的头和尾选择元素移动到新数组,使得所有元素移动后 ...
- 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 ...
- 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
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 ...
随机推荐
- 断言封装整合到requests封装中应用(纠错False,Result循环,tag测试)
检查json_key_value: 检查: requests.py # -*- coding: utf-8 -*-#@File :demo_04.py#@Auth : wwd#@Time : 2020 ...
- Hbase 手动执行MajorCompation
说明: Major Compaction 的作用: 1.将一个Region下的所有StoreFile合并成一个StoreFile文件 2.对于删除.过期.多余版本的数据进行清除 由于MajorComp ...
- 【Problems】端口被占用 查看是被谁占用并关闭它
文章目录 Windows Linux 经常在Windows.Linux环境下运行JavaWeb项目,Tomcat的端口被占用了. 端口被占用就查看是被谁占用关闭它就行. Windows 在Window ...
- 【Oracle】查看某个角色中有什么权限
select * from role_sys_privs where role='DBA'; 查看dba都有什么系统权限 select * from role_sys_privs where rol ...
- UVA - 387 A Puzzling Problem
题目链接: https://vjudge.net/problem/UVA-387 思路: 非常有意思的拼图,深搜+回溯, 输出硬伤:除了第一次之外,每次先输空格,再输出结果, 以及可能给的数据拼不成4 ...
- 解决ROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'creat table study_record( id int(11) not null
之前一直用的好好的,突然就出现了这个错误: ERROR 1064 (42000): You have an error in your SQL syntax; check the manual tha ...
- 网络流量预测入门(二)之LSTM介绍
目录 网络流量预测入门(二)之LSTM介绍 LSTM简介 Simple RNN的弊端 LSTM的结构 细胞状态(Cell State) 门(Gate) 遗忘门(Forget Gate) 输入门(Inp ...
- Visual Studio中自定义代码段!
Visual Studio中自定义代码段! 第一步:在编辑器中进行快捷键的输入[ctrl + shift + p] 或者 点击 查看 第一个选项就是!请看下图 第二步:选择你要配置代码段的语言, 这里 ...
- GRASP职责分配模式
https://mp.weixin.qq.com/s/IaxAnWfVqe3mM0bHFVV5Gg 软件开发必修课:你该知道的GRASP职责分配模式 原创 悟真 阿里技术 今天 收录于话题 #设计模式 ...
- ETL调优的一些分享(上)(转载)
ETL是构建数据仓库的重要一环.通过该过程用户将所需数据提取出来,并按照已定义的模型导入数据仓库.由于ETL是建立数据仓库的必经过程,它的效率将影响整个数据仓库的构建,因此它的有效调优具有很高的重要性 ...