Codeforces Round #394 (Div. 2) D. Dasha and Very Difficult Problem —— 贪心
来源:互联网 发布:斑马标签机打印软件 编辑:程序博客网 时间:2024/06/04 18:58
题目链接:http://codeforces.com/contest/761/problem/D
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 51 1 1 1 13 1 5 4 2
3 1 5 4 2
4 2 93 4 8 93 2 1 4
2 2 2 9
6 1 51 1 1 1 1 12 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 贪心
- Codeforces Round #394 (Div. 2)D. Dasha and Very Difficult Problem【贪心】
- Codeforces Round #394 (Div. 2) D. Dasha and Very Difficult Problem 贪心,二分
- Codeforces Round #394 (Div. 2)Dasha and Very Difficult Problem
- Codeforces Round #394 (Div. 2) Dasha and Very Difficult Problem
- Codeforces Round #394 (Div. 2) D. Dasha and Very Difficult Problem
- Codeforces 394 D. Dasha and Very Difficult Problem 贪心
- Codeforces 761D-Dasha and Very Difficult Problem 贪心
- 【codeforces 761D】Dasha and Very Difficult Problem
- Codeforces 761D-Dasha and Very Difficult Problem
- 【Codeforces 761 D Dasha and Very Difficult Problem】
- CodeForces 761D Dasha and Very Difficult Problem
- D. Dasha and Very Difficult Problem
- 761D Dasha and Very Difficult Problem
- D. Dasha and Very Difficult Problem----思维题
- 761D Dasha and Very Difficult Problem[数学][思维]
- Codeforces Round #394 (Div. 2) E. Dasha and Puzzle —— 构造 + 贪心
- i++和++i的区别
- LeetCode
- UVA 11395——Sigma Function
- 数据库SQL优化大总结之 百万级数据库优化方案
- SpringBoot中常用注解@ PathVaribale / @ RequestParam / @ GetMapping介绍 本篇博文将介绍几种如何处理URL中的参数的注解@ PathVariba
- Codeforces Round #394 (Div. 2) D. Dasha and Very Difficult Problem —— 贪心
- DispatchAction和ForwardAction的使用
- (转)布局文件中fill_parent和match_parent有什么区别?
- 左旋转字符串
- HTTPS的工作流程的理解
- Python--多线程编程--生产者消费者模型
- v-model和v-for指令
- QT使用QFileInfo类来获取文件信息
- Openstack4J API初体验之Authentication