codeforces 761D - Dasha and Very Difficult Problem
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
给一个数列a,给一个数列p,已知数列p是数列c按个元素大小编号1~n后的所谓的压缩数列,求任意一个符合条件的数列b=a+c(条件:数列a、b都在某个范围内);
思路:
例如对于第二组样例,
3 4 8 9
根据 3 2 1 4的大小重排后得到
8 4 3 9 (a)
1 2 3 4 (p)
相对应的,不妨假设数列b对应的第一位为下界2,那么可知数列c的对应第一位为-6
8 4 3 9 (a)
1 2 3 4 (p)
-6 (c)
2 (b)
再看第二位,不妨假设b还是下界,则c为-2,-2 > -6,符合p给定的大小顺序,则可以有:
8 4 3 9 (a)
1 2 3 4 (p)
-6 -2 (c)
2 2 (b)
再到第三位,不妨设b还是下界,则c为-1,依然符合p的大小顺序
8 4 3 9 (a)
1 2 3 4 (p)
-6 -2 -1 (c)
2 2 2 (b)
再到第四位,设b为下界,则c为-7,这时候发现不符合顺序,那么就让c等于前一位+1
8 4 3 9 (a)
1 2 3 4 (p)
-6 -2 -1 0 (c)
2 2 2 9 (b)
这时候,得到的b为9,不大于上界,符合条件,因此我们得到了一个符合条件的b:
2 2 2 9 (b)
最后我们在将数列b排回3 2 1 4的顺序即可(这步很重要……在这个样例里重新排序之后没有变化,但其他样例就不一定了……)
3 2 1 4 (p)
2 2 2 9 (b)
感觉自己写的代码一点都不优雅……(羞耻……
#include<cstdio>
int main()
{
int n,l,r,a[+],temp_a[+],p[+],c[+];
scanf("%d %d %d",&n,&l,&r);
for(int i=;i<=n;i++) scanf("%d",&temp_a[i]);
for(int i=;i<=n;i++) scanf("%d",&p[i]);
for(int i=;i<=n;i++) a[ p[i] ]=temp_a[i]; //for(int i=1;i<=n;i++) printf("%d ",a[i]);printf("\n"); int now=l-a[]-;
for(int i=;i<=n;i++)
{
if(l-a[i] > now) c[i]=(now=l-a[i]);
else c[i]=(now+=);
if( c[i]+a[i] > r ){
printf("-1\n");
return ;
}
} for(int i=;i<=n;i++){
if(i!=) printf(" ");
printf("%d",c[p[i]]+a[p[i]]);
}
printf("\n");
return ;
}
codeforces 761D - Dasha and Very Difficult Problem的更多相关文章
- Codeforces 761D Dasha and Very Difficult Problem(贪心)
题目链接 Dasha and Very Difficult Problem 求出ci的取值范围,按ci排名从小到大贪心即可. 需要注意的是,当当前的ci不满足在这个取值范围内的时候,判为无解. #in ...
- 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 —— 贪心
题目链接:http://codeforces.com/contest/761/problem/D D. Dasha and Very Difficult Problem time limit per ...
- 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 761D】Dasha and Very Difficult Problem
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- codeforces 761 D. Dasha and Very Difficult Problem(二分+贪心)
题目链接:http://codeforces.com/contest/761/problem/D 题意:给出一个长度为n的a序列和p序列,求任意一个b序列使得c[i]=b[i]-a[i],使得c序列的 ...
- D. Dasha and Very Difficult Problem 二分
http://codeforces.com/contest/761/problem/D c[i] = b[i] - a[i],而且b[]和a[]都属于[L, R] 现在给出a[i]原数组和c[i]的相 ...
- Educational Codeforces Round 40 F. Runner's Problem
Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...
- BNU 4356 ——A Simple But Difficult Problem——————【快速幂、模运算】
A Simple But Difficult Problem Time Limit: 5000ms Memory Limit: 65536KB 64-bit integer IO format: %l ...
随机推荐
- Java使用ListIterator逆序ArrayList
对于列表而言,除了Iterator,还提供了一个功能更加强大的ListIterator.它可以实现逆序遍历列表中的元素.本示例将使用其逆序遍历ArrayList. 思路分析:要逆序遍历某个列表,首先要 ...
- SpringMVC由浅入深day01_12参数绑定(12.1参数绑定过程_12.2默认支持的类型_12.3简单类型)
12 参数绑定 处理器适配器在执行Handler之前需要把http请求的key/value数据绑定到Handler方法形参数上. 注解适配器对RequestMapping标记的方法进行适配,对方法中的 ...
- mysql存储引擎ARCHIVE
mysql常用引擎MyISAM和InnoDB,前者插入快 查询快,后者修改快 支持事务,各有优缺点,在网上突然看到一个引擎叫ARCHIVE,还蛮特别的 这个引擎只允许插入和查询,不允许修改和删除.相当 ...
- spring + Mybatis + pageHelper + druid 整合源码分享
springMvc + spring + Mybatis + pageHelper + druid 整合 spring 和druid整合,spring 整合druid spring 和Mybatis ...
- dedeCMS解码
var str = 'arrs1[]=99&arrs1[]=102&arrs1[]=103&arrs1[]=95&arrs1[]=100&arrs1[]=98& ...
- PHPMailer命令执行及任意文件读取漏洞
今天在thinkphp官网闲逛,无意下载了一套eduaskcms,查看了一下libs目录中居然存在PHPMailer-5.2.13,想起了之前看到的PHPMailer的漏洞,可惜这套CMS只提供了一个 ...
- java中调用groovy
Groovy在Java中的应用,做几个小例子以备查 package com.boco.efficiency.groovy; import groovy.lang.Binding; import gro ...
- Android使用Handler造成内存泄露的分析及解决方法
一.什么是内存泄露? Java使用有向图机制,通过GC自动检查内存中的对象(什么时候检查由虚拟机决定),如果GC发现一个或一组对象为不可到达状态,则将该对象从内存中回收.也就是说,一个对象不被任何引用 ...
- Android中实现定时器的3中方法
在Android开发中,定时器一般有以下3种实现方法: 一.采用Handler与线程的sleep(long)方法: 二.采用Handler的postDelayed(Runnable, long)方法: ...
- myeclipse使用小技巧
1 如何关掉maven自动更新,去掉前面的勾 2 myeclipse中如何设置不校验js