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 ...
随机推荐
- OpenLDAP给我的启发
首先这篇文章没什么技术性,但亮点是:我会给广大运维同行提一点建议,这个一点仅仅是一点,而不是很多点. 年前计划深度掌握一些诸如:Jenkins.Gitlab.ELK.k8s等的软件,但学着学着总是想学 ...
- 2012-2013 ACM-ICPC, NEERC, Central Subregional Contest
A Hanoi Tower 递归 题意: 大家都很熟悉汉诺塔的递归程序,现在给你一个组合,询问你这个组合是否会出现在汉诺塔的递归过程中. 题解: 将汉诺塔的递归程序反过来思考,考虑当前最大的那个盘,我 ...
- POJ 3268 Silver Cow Party 最短路
原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- DELPHI10.2的LINUX数据库开发环境配置
DELPHI10.2的LINUX数据库开发环境配置 ubuntu使用firedac访问mysql1.安装mysql-client包sudo apt-get install mysql-client m ...
- 阿里云云服务器ubuntu配置nginx+uwsgi+django记录文档
1 安装ssh 1 sudo apt-get update 2 sudo apt-get install openssh-server 3 sudo ps -e |grep ssh 有sshd ...
- 转: WebRTC音视频引擎研究(1)--整体架构分析
转自: http://blog.csdn.net/temotemo/article/details/7530504 目录(?)[+] WebRTC技术交流群:234795279 原文地址:ht ...
- Mac 安装配置Mysql
Mac下安装配置Mysql By 白熊花田(http://blog.csdn.net/whiterbear) 转载需注明出处,谢谢. 下载安装 去官网下载Community版本号的mysql安装文件. ...
- Git以及github的使用方法(三),git status查看工作区的状态,git diff查看具体修改内容
我们已经成功地添加并提交了一个readme.txt文件,现在,是时候继续工作了,于是,我们继续修改readme.txt文件,改成如下内容: Git is a distributed version c ...
- java性能监控工具jconsole-windows
jconsole Starts a graphical console that lets you monitor and manage Java applications. Synopsis jco ...
- Android——通过Intent传递一些二进制数据的方法有哪些
1.方法 (1)使用Serializable接口实现序列化.利用Bundle.putSerializable(Key, Object);这里objec对象需要实现serializable接口. (2) ...