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]的范围内。那么我们容易得到
如果a[i]+b[i]>=n,(a[i]+b[i])%n<a[i]且(a[i]+b[i])%n<b[i]。得出这样的结论之后,我们就可以进行模拟了。
如果当前a[i]能找到一个b[i]使得a[i]+b[i]>=n,那么我们就找符合条件最小的元素,否则的话a[i]加上最小的b[i]才满足的字典序最小。
我们需要做的就是维护b数组中哪些元素是已经使用的就可以了,下面是两种方法。
二分加树状数组维护
#include<iostream>
#include<vector>
#include<stack>
#include<algorithm>
#include<cstdio>
#include<map>
#include<string>
#include<cstring>
using namespace std;
map<int,int>p;
int a[];
int b[];
int c[];
bool vis[];
int sum[];
int n;
inline int lowbit(int x)
{
return x&(-x);
}
void update(int x,int num)
{
while(x<=n)
{
sum[x]+=num;
x+=lowbit(x);
}
}
int getsum(int x)
{
int s=;
while(x>)
{
s+=sum[x];
x-=lowbit(x);
}
return s;
}
int main()
{ int t,i,maxi,l,r,t1,t2,l1,r1,m1,m2,m;
while(cin>>n)
{
memset(sum,,sizeof(sum));
for(i=; i<=n; i++)
scanf("%d",&a[i]),update(i,);
for(i=; i<=n; i++)
scanf("%d",&b[i]);
sort(b+,b+n+);
memset(vis,false,sizeof(vis));
m1=; for(i=;i<=n;i++)
{
t=lower_bound(b+,b+n+,n-a[i])-(b);
while(m1<=n&&vis[m1])
m1++;
l=t-;
r=n+;
if(t!=n+)
while(l+<r)
{
m=(l+r)>>;
if(getsum(m)-getsum(t-)>)
r=m;
else
l=m;
}
t=r;
if(t>n)
{
c[i]=(a[i]+b[m1])%n;
vis[m1]=true;
update(m1,-);
while(vis[m1]&&m1<=n)
m1++;
}
else
{
c[i]=(a[i]+b[t])%n;
update(t,-);
vis[t]=true;
}
}
for(i=;i<=n-;i++)
printf("%d ",c[i]);
printf("%d\n",c[i]); } return ;
}
mutilset删除元素来维护
#include<iostream>
#include<vector>
#include<stack>
#include<algorithm>
#include<cstdio>
#include<map>
#include<string>
#include<cstring>
#include<set>
using namespace std;
map<int,int>p;
int a[];
int b[];
int c[];
bool vis[];
int sum[];
int n;
int main()
{ int t,i,maxi,l,r,t1,t2,l1,r1,m1,m2,m;
while(cin>>n)
{
multiset<int>mm;
for(i=; i<=n; i++)
scanf("%d",&a[i]);
for(i=; i<=n; i++)
scanf("%d",&b[i]),mm.insert(b[i]);
memset(vis,false,sizeof(vis));
m1=;
for(i=;i<=n;i++)
{
multiset<int>::iterator it=mm.lower_bound(n-a[i]);
if(it!=mm.end())
{
c[i]=((*it)+a[i])%n;
mm.erase(it);
}
else
{
it=mm.begin();
c[i]=(*it)+a[i];
mm.erase(it);
}
}
for(i=;i<=n-;i++)
printf("%d ",c[i]);
printf("%d\n",c[i]);
} return ;
}
Codeforces Round #555 (Div. 3) E. Minimum Array的更多相关文章
- Codeforces Round #555 (Div. 3) E. Minimum Array 【数据结构 + 贪心】
一 题面 E. Minimum Array 二 分析 注意前提条件:$0 \le a_{i} \lt n$ 并且 $0 \le b_{i} \lt n$.那么,我们可以在$a_{i}$中任取一个数 ...
- 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\ ...
- 老年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 ...
- Codeforces Round #411 div 2 D. Minimum number of steps
D. Minimum number of steps time limit per test 1 second memory limit per test 256 megabytes input st ...
随机推荐
- ---mipi command
可惜这是5.1系统: http://www.cnblogs.com/lialong1st/p/8534728.html
- Https的前世今生
1.年前会议 马上要过年了,公司业务上的需求也少了很多,这不,王小二他们召开了一场技术会议,盘点年前能干点啥. 只见C哥写了一份清单,其中一项是全站升级https. C哥说:https是一种趋势,但目 ...
- Delphi调用C#编写的WebService 注意事项
返回的字段值区分大小写,c#和Delphi的字段要一致
- Hadoop HDFS常用命令
1.查看hdfs文件目录 hadoop fs -ls / 2.上传文件 hadoop fs -put 文件路径 目标路径 在浏览器查看:namenodeIP:50070 3.下载文件 hadoop f ...
- Hash算法总结(转)
1. Hash是什么,它的作用先举个例子.我们每个活在世上的人,为了能够参与各种社会活动,都需要一个用于识别自己的标志.也许你觉得名字或是身份证就足以代表你这个人,但是这种代表性非常脆弱,因为重名的人 ...
- Java 字符串拼接四种方式的性能比较分析
一.简单介绍 编写代码过程中,使用"+"和"contact"比较普遍,但是它们都不能满足大数据量的处理,一般情况下有一下四种方法处理字符串拼接,如下: 1. 加 ...
- Android上显示生僻字的方法
安卓5.0+是可以显示所有(8万多个)Unicode汉字的,本文介绍显示生僻汉字的方法,这个方法也适用于其它特殊字符. Unicode值在0xFFFF以下的(2万多个简体.繁体)汉字早已被广泛支持,所 ...
- linux下open-vswitch安装卸载操作
一. ovs 从源码编译安装: 安装依赖项: ? 1 2 3 4 5 6 7 8 9 10 11 # apt install make # apt install gcc # apt install ...
- Ganglia监控扩展实现机制
Ganglia监控扩展实现机制 默认安装完成的Ganglia仅向我们提供基础的系统监控信息,通过Ganglia插件可以实现两种扩展Ganglia监控功能的方法.1.添加带内(in-band)插件,主要 ...
- mysql 创建备份表
mysql 中对已有表进行备份用到的语句 CREATE TABLE table_name_1 SELECT * FROM table_name_2; 这个语句是创建表1并且复制表2的结构和数据到表1 ...