题目链接: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.

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) D. Dasha and Very Difficult Problem —— 贪心的更多相关文章

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

    D. Dasha and Very Difficult Problem 题目连接: http://codeforces.com/contest/761/problem/D Description Da ...

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

    D. Dasha and Very Difficult Problem time limit per test:2 seconds memory limit per test:256 megabyte ...

  3. Codeforces Round #394 (Div. 2) E. Dasha and Puzzle(分形)

    E. Dasha and Puzzle time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. Codeforces Round #394 (Div. 2) E. Dasha and Puzzle 构造

    E. Dasha and Puzzle 题目连接: http://codeforces.com/contest/761/problem/E Description Dasha decided to h ...

  5. Codeforces Round #394 (Div. 2) C. Dasha and Password 暴力

    C. Dasha and Password 题目连接: http://codeforces.com/contest/761/problem/C Description After overcoming ...

  6. Codeforces Round #394 (Div. 2) B. Dasha and friends 暴力

    B. Dasha and friends 题目连接: http://codeforces.com/contest/761/problem/B Description Running with barr ...

  7. Codeforces Round #394 (Div. 2) A. Dasha and Stairs 水题

    A. Dasha and Stairs 题目连接: http://codeforces.com/contest/761/problem/A Description On her way to prog ...

  8. Codeforces Round #394 (Div. 2) C. Dasha and Password —— 枚举

    题目链接:http://codeforces.com/problemset/problem/761/C C. Dasha and Password time limit per test 2 seco ...

  9. Codeforces Round #394 (Div. 2) B. Dasha and friends —— 暴力 or 最小表示法

    题目链接:http://codeforces.com/contest/761/problem/B B. Dasha and friends time limit per test 2 seconds ...

随机推荐

  1. 洛谷——P2196 挖地雷

    题目背景 NOIp1996提高组第三题 题目描述 在一个地图上有N个地窖(N<=20),每个地窖中埋有一定数量的地雷.同时,给出地窖之间的连接路径.当地窖及其连接的数据给出之后,某人可以从任一处 ...

  2. loj #110. 乘法逆元

    #110. 乘法逆元 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据   题目描述 这是一道模板题. 给定 ...

  3. 板子-GOD BLESS ALL OF YOU

    字符串: KMP #include<cstdio> #include<cstring> ; ]; ]; int l1,l2; void get_next() { next[]= ...

  4. Ubuntu 16.04安装Wine版的迅雷+QQ(完美方案,终极解决方法)

    安装前先备份好系统! 继上一篇安装QQ的方法http://www.cnblogs.com/EasonJim/p/7425978.html,这一篇的QQ采用的是Wine模式安装.完美解决消息记录中文乱码 ...

  5. Android开发者选项——Gpu呈现模式分析

    对于Android用户来说,无论你用的什么品牌的手机,在开发者选项中都能发现“玄学曲线”的开关,之所以称其为玄学曲线,还是因为它被很多网友用于测试一个说不清道不明的东西——流畅度.到底多流畅才叫流畅, ...

  6. 更新tensorflow支持GPU时出错

    sudo pip install --upgrade tensorflow-gpu Operation not permitted: '/tmp/pip-Sx_vMg-uninstall/System ...

  7. C#中通过反射获取类中非公有成员

    public class NGlbGlobeXComm { public static T GetPrivateField<T>(object instance, string field ...

  8. Oracle Apex 有用笔记系列 6 - 可编辑交互报告 Editable Interactive Report

    据笔者所知.Apex 4.x 是没有提供可编辑交互报告组件的.这就须要我们手动实现. 事实上这也并非非常复杂,仅仅须要简单几步. 1. 依据向导建立一个interactive report.查询语句能 ...

  9. hdu 4432 数学杂题

    http://acm.hdu.edu.cn/showproblem.php?pid=4432 6分钟写的代码,一上午去调试,, 哎,一则题目没看懂就去写了,二则,哎,,恶心了.在坚持几天然后ACM退役 ...

  10. sparkSQL1.1入门之十:总结

    回想一下,在前面几章中,就sparkSQL1.1.0基本概念.执行架构.基本操作和有用工具做了基本介绍. 基本概念: SchemaRDD Rule Tree LogicPlan Parser Anal ...