题解 CF1157E 【Minimum Array】

题目描述

给定两个长度为n的序列a和b,可以对b进行任意顺序变换,得到序列c.

对于序列c,ci=(ai+bi)%n

求得字典序最小的c序列方案。

分析

对于ai(ai>0),最优的肯定是n-ai,对于ai=0的情况,最优的肯定是0,(废话

既然要求字典序最小,那就是贪心了啦,对于任意ai找到>=最优情况的一个数就行了,

最方便的就是multiset。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<bits/stdc++.h>
using namespace std;
const int big=0x7fffffff;
void read(int &x)
{
x=0;
char ch=getchar();
int pd=1;
while(ch<'0'||ch>'9')
{
if(ch=='-')
{
pd=-pd;
}
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
x*=pd;
}
void write(const int &x)
{
char ggg[10001];
int s=0;
int tmp=x;
if(tmp==0)
{
putchar('0');
return;
}
if(tmp<0)
{
tmp=-tmp;
putchar('-');
}
while(tmp>0)
{
ggg[s++]=tmp%10+'0';
tmp/=10;
}
while(s>0)
{
putchar(ggg[--s]);
}
}

int n,a[2000001],len;
int f[1001];
int l,r;
int ans[1000001];
multiset<int>s;
multiset<int>::iterator it;
int main()
{
read(n);
s.insert(big);
s.insert(-big);
for(register int i=1;i<=n;++i)
{
read(a[i]);
}
for(register int i=1;i<=n;++i)
{
int xx;
read(xx);
s.insert(xx);
}
for(register int i=1;i<=n;++i)
{
int fi=a[i]==0?0:n-a[i];
it=s.lower_bound(fi);
int now=*it;
if(now==big)
{
it=s.lower_bound(0);
now=*it;
}
s.erase(it);
write((now+a[i])%n);
putchar(' ');
}
}