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 ...
随机推荐
- Skiing(最短路)
poj——3037 Skiing Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4921 Accepted: 1315 ...
- CF623
AIM Tech Round (Div. 1) <br > 这真是一套极好的题目啊.....虽然我不会做 <br > 代码戳这里 <br > A.Graph and ...
- Java获取指定时间(转)
说明:从LocalDate的API上看,主要用于快速获取当前年月日,而DateFormatter也基本上伴随着使用.如果是操作Date对象的,主要是用于时间戳等,伴随着使用的是SimpleDateFo ...
- USACO 1.2 Milking Cows (枚举)
标记数组(哈希) 1e6的范围,开一个char数组全然能够,有人为1,无人为0,注意边界就可以.最后线性扫描就可以. 时间复杂度,应该是O(n),n为最后结束的时间. 缺点就是--比較慢 /* ID: ...
- Crtmp Server 几个关键流程
最近在阅读Crtmp Sever 源码,有些关键流程记录下来,以备以后查阅.假设rtmp播放地址是"rtmp://127.0.0.1/live/mystream live=1" 1 ...
- Python3 与 C# 面向对象之~继承与多态 Python3 与 C# 面向对象之~封装 Python3 与 NetCore 基础语法对比(Function专栏) [C#]C#时间日期操作 [C#]C#中字符串的操作 [ASP.NET]NTKO插件使用常见问题 我对C#的认知。
Python3 与 C# 面向对象之-继承与多态 文章汇总:https://www.cnblogs.com/dotnetcrazy/p/9160514.html 目录: 2.继承 ¶ 2.1.单继 ...
- 网页编程-django前传
1.js正则表达式 http://www.cnblogs.com/wupeiqi/articles/5602773.html test - 判断字符串是否符合规定的正则 正则表达式: rep = ...
- java 回文字符串
package string.string1_5; public class Palindrome { /** * 从两头向中间移动 * @param str * @return */ public ...
- 生产追溯系统-Wifi+传感器,实现计数器以及监控机器是否停止
物联网听上去是一个高大上的词儿,还有什么大数据.云.智能制造等等,今天我也往这方面稍微靠一靠,这篇文章主要介绍的是通过 wifi 模块与传感器组合,实现感应计数器,应用场景主要如下: 1.统计 SMT ...
- 用python编写的无线AP扫描器
代码如下: #coding=utf-8 import os import sys import subprocess from scapy.all import * RSN = 48 #管理帧信息元素 ...