Codeforces Round #394 (Div. 2) D. Dasha and Very Difficult Problem —— 贪心

题目链接:http://codeforces.com/contest/761/problem/D

D. Dasha and Very Difficult Problem
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Dasha logged into the system and began to solve problems. One of them is as follows:

Given two sequences a and b of
length n each you need to write a sequence c of
length n, the i-th
element of which is calculated as follows: ci = bi - ai.

About sequences a and b we
know that their elements are in the range from l to r.
More formally, elements satisfy the following conditions: l ≤ ai ≤ r and l ≤ bi ≤ r.
About sequence c we know that all its elements are distinct.

Codeforces Round #394 (Div. 2) D. Dasha and Very Difficult Problem —— 贪心

Dasha wrote a solution to that problem quickly, but checking her work on the standard test was not so easy. Due to an error in the test system only the sequence a and
the compressed sequence of the sequence c were known from that test.

Let's give the definition to a compressed sequence. A compressed sequence of sequence c of
length n is a sequence p of
length n, so that pi equals
to the number of integers which are less than or equal to ci in
the sequence c. For example, for the sequence c = [250, 200, 300, 100, 50] the
compressed sequence will be p = [4, 3, 5, 2, 1]. Pay attention that in c all
integers are distinct. Consequently, the compressed sequence contains all integers from 1 to n inclusively.

Help Dasha to find any sequence b for which the calculated compressed sequence of
sequence c is correct.

Input

The first line contains three integers nlr (1 ≤ n ≤ 105, 1 ≤ l ≤ r ≤ 109) —
the length of the sequence and boundaries of the segment where the elements of sequences a and b are.

The next line contains n integers a1,  a2,  ...,  an (l ≤ ai ≤ r) —
the elements of the sequence a.

The next line contains n distinct integers p1,  p2,  ...,  pn (1 ≤ pi ≤ n) —
the compressed sequence of the sequence c.

Output

If there is no the suitable sequence b, then in the only line print "-1".

Otherwise, in the only line print n integers — the elements of any suitable sequence b.

Examples
input
5 1 5
1 1 1 1 1
3 1 5 4 2
output
3 1 5 4 2 
input
4 2 9
3 4 8 9
3 2 1 4
output
2 2 2 9 
input
6 1 5
1 1 1 1 1 1
2 3 5 4 1 6
output
-1
Note

Sequence b which was found in the second sample is suitable, because calculated sequence c = [2 - 3, 2 - 4, 2 - 8, 9 - 9] = [ - 1,  - 2,  - 6, 0] (note
that ci = bi - ai)
has compressed sequence equals to p = [3, 2, 1, 4].

题解:

1.首先将a数组按照p数组排序,然后按照p数组从小到大开始推出b数组。由于p数组是递增的,所以每获得一个b[i],p可取的最小值就会增加。

2.为了之后p的取值范围尽可能大,当前的p应该取范围内的最小值。

3.p合适的最小值推导:

假设当前p的最小值为minn, 则:minn<=p<=r-a。

而因为b = p+a, l<=b<=r,所以:l-a<=p<=r-a。

所以p合适的最小值 = max(minn, l-a)。

代码如下:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-6;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 1e5+10; int A[maxn], a[maxn], b[maxn], p[maxn];
int n, l, r; void init()
{
scanf("%d%d%d",&n,&l,&r);
for(int i = 1; i<=n; i++)
scanf("%d",&A[i]);
for(int i = 1; i<=n; i++)
{
scanf("%d",&p[i]);
a[p[i]] = A[i];
}
} void solve()
{
int minn = -INF;
for(int i = 1; i<=n; i++)
{
b[i] = max(minn, l-a[i])+a[i]; // b = p合适的最小值 + a
minn = max(minn, l-a[i])+1; //p数组严格递增
if(b[i]<l || b[i]>r)
{
puts("-1");
return;
}
} for(int i = 1; i<=n; i++)
printf("%d ",b[p[i]]);
} int main()
{
init();
solve();
}
上一篇:Codeforces Round #394 (Div. 2) E. Dasha and Puzzle(dfs)


下一篇:提示Can't load package:dclite70.bpl解决方法