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 ...
随机推荐
- FireDAC 连接SQL Server一些要注意的地方(转)
TFDConnection: FetchOptions.Mode 设置为fmAll, 返回全部结果, 否则默认只返回前50条, 效果与open以后再执行FetchAll一样 Specifies how ...
- IDEA与eclipse:vm参数调优笔记
我的电脑配置12G内存,64位,win10系统. 首先,idea,是在idea中安装目录下的bin目录下面找到位的同学注意找到idea32.exe.vmoptions的文件去编辑. 更改文件,无非大多 ...
- spring中Constructor、@Autowired、@PostConstruct的顺序
其实从依赖注入的字面意思就可以知道,要将对象p注入到对象a,那么首先就必须得生成对象p与对象a,才能执行注入.所以,如果一个类A中有个成员变量p被@Autowired注解,那么@Autowired注入 ...
- 让新版appium支持by_name定位
org.openqa.selenium.InvalidSelectorException: Locator Strategy 'name' is not supported for this sess ...
- 在CentOS6.9 x86下编译libusb-1.0.22遇到的两个问题
OS版本:CentOS 6.9 x86,内核版本2.6.32 问题一:configure.ac:36: error: Autoconf version 2.69 or higher is requir ...
- 初识TCP协议
一.引言 发送一段TCP数据大致需要经过:用户封装 –> TCP封装 –> IP封装 –>帧封装 Note:用户封装没啥好说的,都是客户自己决定的,在一些简单的应用情况下,这个步骤可 ...
- 手眼标定之相机随动eye-in-hand 示例:handeye_movingcam_calibration
* * This example explains how to use the hand eye calibration for the case where* the camera is atta ...
- Android 添加第三方jar包
1,拷贝jar包到项目的\app\libs文件夹下. 2,打开项目下的build.gradle(Module:app)文件,在“dependencies {}”中添加“compile files('l ...
- select简单循环嵌套
访问学生的物理最高成绩,并且打印出来,单个要打印出所有的信息 在添加几个 and 就可以啦. select student.gender,student.sname from student whe ...
- python测试开发django-3.url配置
前言 我们在浏览器访问一个网页是通过url地址去访问的,django管理url配置是在urls.py文件.当一个页面数据很多时候,通过会有翻页的情况,那么页数是不固定的,如:page=1.也就是url ...