Codeforces Round #394 (Div. 2) D. Dasha and Very Difficult Problem —— 贪心
题目链接:http://codeforces.com/contest/761/problem/D
2 seconds
256 megabytes
standard input
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.
The first line contains three integers n, l, r (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.
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.
5 1 5
1 1 1 1 1
3 1 5 4 2
3 1 5 4 2
4 2 9
3 4 8 9
3 2 1 4
2 2 2 9
6 1 5
1 1 1 1 1 1
2 3 5 4 1 6
-1
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 —— 贪心的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #394 (Div. 2) C. Dasha and Password 暴力
C. Dasha and Password 题目连接: http://codeforces.com/contest/761/problem/C Description After overcoming ...
- Codeforces Round #394 (Div. 2) B. Dasha and friends 暴力
B. Dasha and friends 题目连接: http://codeforces.com/contest/761/problem/B Description Running with barr ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- JDK1.8和Spring 3.2.0 的坑
上午 org.apache.catalina.core.StandardContext listenerStart严重: Exception sending context initialized e ...
- 小W摆石子
可以确定, 最后围成是 一个长方形 + 多出一列 的形状. 而且多出的那一列应该是和较短的边相邻. 贴代码. #include<iostream> #include<algorith ...
- composer配置和安装php框架
第一步:安装composerwin环境安装:下载地址:https://getcomposer.org/Composer-Setup.exe 下载后直接点击安装即可测试:cmd ->compose ...
- php命令行查看扩展信息
通常,在php的开发过程中,我们会使用到第三方扩展,这时候,我们对于php扩展的信息的查看就显得尤为重要了.一般情况下,我们查看到扩展信息,都是直接通过 cat *.ini 文件来进行,这样子的效率 ...
- Go -- FIFO类(缓存淘汰算法)(转)
1 FIFO 1.1. 原理 按照“先进先出(First In,First Out)”的原理淘汰数据. 1.2. 实现 FIFO队列,具体实现如下: 1. 新访问的数据插入FIFO队列尾部,数据在FI ...
- 系统网站架构(淘宝、京东)& 架构师能力
来一张看上去是淘宝的架构的图: 参考地址:http://hellojava.info/?p=520 说几点我认可的地方: 架构需要掌握的点: 通信连接方式:大量的连接通常会有两种方式: 1. 大量cl ...
- Android--------------几个ADB经常使用命令
1. 显示当前执行的所有模拟器: adb devices 2. 安装应用程序: adb install -r 123.apk 3. 获取模拟器中的文件: adb pull &l ...
- 前言(CSDN也有Markdown了,好开森)
实战出精华 在具体的C++网络编程中提升你的逼格 John Torjo Boost.Asio C++ 网络编程 Copyright © 2013 Packt Publishing 关于作者 做为一名权 ...
- C语言-二维背包问题
二维费用背包问题 问题: 二维费用的背包问题是指:对于每件物品,具有两种不同的费用:选择这件物品必须同时付出这两种代价:对于每种代价都有 一个可付出的最大值(背包容量).问怎样选择物品可以得到最大的价 ...
- HTML5开发移动web应用—JQuery Mobile(1)
JQuery Mobile是一个简单易用的web移动app开发框架.使用它就像使用jQuery一样,引入必要的文件就可以. 最基础的jQuery Mobile文件的结构代码例如以下: <body ...