Codeforces Round #555 (Div. 3) E. Minimum Array 【数据结构 + 贪心】
一 题面
二 分析
注意前提条件:$0 \le a_{i} \lt n$ 并且 $0 \le b_{i} \lt n$。那么,我们可以在$a_{i}$中任取一个数进行分析,发现为满足字典序最小,在$b$中找到$n-a_{i}$就是最优解。
接下来分析$b$,在$b$中是不一定找得到$n-a_{i}$的。所以需要分析如何找到此情况下的最优解。
假设$a_{i} = 3$,$n = 4$,那么$b_{i}$对应的最优情况是$b_{i} = 1$,可以把所有$b_{i}$的所有取值情况分析以下:
$b_{i} = 0$ $(a_{i}+b_{i})\%n = 3$
$b_{i} = 1$ $(a_{i}+b_{i})\%n = 0$
$b_{i} = 2$ $(a_{i}+b_{i})\%n = 1$
$b_{i} = 3$ $(a_{i}+b_{i})\%n = 2$
应该已经发现了规律了,就是以$n-a_{i}$为起点的一个循环。那么我们只要每次保证贪心取最小就可以了,这里需要运用$multiset$进行复杂度优化,但一定要注意查找的时候不能用$find$而要用$lower\_bound$,这样就可以解决了。
三 AC代码
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + ;
int A[MAXN], B[MAXN]; int main()
{
//freopen("input.txt", "r", stdin);
int N, x;
multiset<int > ATree;
scanf("%d", &N);
for(int i = ; i < N; i++)
scanf("%d", &A[i]);
for(int i = ; i < N; i++)
{
scanf("%d", &B[i]);
ATree.insert(B[i]);
}
for(int i = ; i < N; i++)
{
x = N - A[i];
auto p = ATree.lower_bound(x);
if(p == ATree.end() )
p = ATree.begin();
x = *p;
printf("%d%c", (x + A[i]) % N , i == N - ? '\n':' ');
ATree.erase(p);
}
return ;
}
Codeforces Round #555 (Div. 3) E. Minimum Array 【数据结构 + 贪心】的更多相关文章
- Codeforces Round #555 (Div. 3) E. Minimum Array
题意:b数组可以自由排序,c[i]=(a[i]+b[i])%n. 题目中要求c数组的字典序是最小的.那么我们需要尽量满足前面的c[i],才能使字典序最小. 我们知道a[i]和b[i]都是[0,n-1] ...
- Codeforces Round #555 (Div. 3) E. Minimum Array (贪心,二分,set)
题意:给你两个长度为\(n\)的数组\(a\)和\(b\),元素值在\([0,n-1]\),可以对\(b\)数组的元素任意排序,求新数组\(c\),满足\(c_i=(a_i+b_i)\ mod\ n\ ...
- 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 ...
- 老年OIer的Python实践记—— Codeforces Round #555 (Div. 3) solution
对没错下面的代码全部是python 3(除了E的那个multiset) 题目链接:https://codeforces.com/contest/1157 A. Reachable Numbers 按位 ...
- CodeForces Round #555 Div.3
A. Reachable Numbers 代码: #include <bits/stdc++.h> using namespace std; ; int N; set<int> ...
- Codeforces Round #555 (Div. 3)[1157]题解
不得不说这场div3是真的出的好,算得上是从我开始打开始最有趣的一场div3.因为自己的号全都蓝了,然后就把不经常打比赛的dreagonm的号借来打这场,然后...比赛结束rank11(帮dreago ...
- Codeforces Round #555 (Div. 3) c2 d e f
c2:Increasing Subsequence (hard version) 那边小取那边,然后相等比较后面的长度 #include<bits/stdc++.h> using name ...
- Codeforces Round #258 (Div. 2) . Sort the Array 贪心
B. Sort the Array 题目连接: http://codeforces.com/contest/451/problem/B Description Being a programmer, ...
- Codeforces Round #555 (Div. 3) AB
A: http://codeforces.com/contest/1157/problem/A 题意:每次加到10的整数倍之后,去掉后面的0,问最多有多少种可能. #include <io ...
随机推荐
- GIT checkout 和 reset 区别
git checkout -- file:撤销对工作区修改:这个命令是以最新的存储时间节点(add和commit)为参照,覆盖工作区对应文件file:这个命令改变的是工作区 git reset HEA ...
- 3 Django 简介
MVC 与 MTV 模型 MVC Web 服务器开发领域里著名的 MVC 模式,所谓 MVC 就是把 Web 应用分为模型 (M),控制器(C) 和视图 (V) 三层,他们之间以一种插件式的.松耦合的 ...
- gcc支持的一种结构体赋值方式
struct info{ int a; char b; struct fd{ int c; int d; }fg;}; 其实我们也可以这样赋值:同样对于其他的类型也是一样 ...
- Jdom简单的修改xml文件实现
上一篇博客写到在打开outputStream 时总是报错,而且出现jdom有问题,后来注意到junit提示的一个错误:文件过早关闭. 顾名思义:用getResorceAsStream打开了文件作为输入 ...
- Ansible 笔记 (1) - 安装和配置
本文参考 <Ansible 自动化运维和最佳实践>,这两天刚读这本书,写写总结.主控机环境是 centos 7,被控机均是 centos 6.8 . 确保 python 版本大于 2.6 ...
- swift -懒加载创建view
// 只有外界访问到headerView的时候才会去执行闭包, 然后将闭包的返回值赋值给headerView // 注意: 一定要记住闭包后面需要写上(), 代表执行闭包 //懒加载 ...
- SqlLocalDB 的一些常用命令行
Once installed, you can interact with SqlLocalDb using the command line. The following will tell you ...
- 转:css实现强制不换行/自动换行/强制换行
css实现强制不换行/自动换行/强制换行 [日期:2007-08-22] 来源: 作者: [字体:大 中 小] 强制不换行 div{ white-space:nowrap;} 自动换行 div{ ...
- zookeeper zoo.cfg配置文件
一.zookeeper的配置文件 zoo.cfg 配置文件是我们安装zookeeper的时候复制 重命名出来的文件 命令: cp zoo_smaple.cfg zoo.cfg zkSe ...
- Android-GsonUtil工具类
JSON解析封装相关工具类 public class GsonUtil { private static Gson gson = null; static { if (gson == null) { ...