题目链接:http://codeforces.com/contest/761/problem/D

题意:给出一个长度为n的a序列和p序列,求任意一个b序列使得c[i]=b[i]-a[i],使得c序列的大小顺序和p序列一样。

p序列给出的的是1~n不重复的数。还有给出的l和r是b序列的大小范围

显然为了要使结果存在尽量使b取得最接近r,所以可以先按p排序一下,从大到小处理b,二分l,r的值使得每次的b尽量

大。

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int M = 1e5 + 10;
int a[M] , p[M] , b[M];
struct TnT {
int A , P , num;
}T[M];
bool cmp(TnT x , TnT y) {
return x.P > y.P;
}
int binsearch(int l , int r , int num , int now) {
int mid = (l + r) >> 1;
while(l <= r) {
mid = (l + r) >> 1;
if(mid - now >= num) {
r = mid - 1;
}
else {
l = mid + 1;
}
}
return r;
}
int main() {
int n , l , r;
cin >> n >> l >> r;
for(int i = 0 ; i < n ; i++) {
cin >> a[i];
}
for(int i = 0 ; i < n ; i++) {
cin >> p[i];
T[i].A = a[i] , T[i].P = p[i] , T[i].num = i;
}
sort(T , T + n , cmp);
int flag = 0; for(int i = 0 ; i < n ; i++) {
if(i == 0) {
b[T[i].num] = r;
}
else {
int gg = b[T[i - 1].num] - a[T[i - 1].num];
int pos = binsearch(l , r , gg , a[T[i].num]);
if(pos == l - 1) {
flag = 1;
break;
}
b[T[i].num] = pos;
}
}
if(flag) {
cout << - 1 << endl;
}
else {
for(int i = 0 ; i < n ; i++) {
cout << b[i] << ' ';
}
cout << endl;
}
return 0;
}

codeforces 761 D. Dasha and Very Difficult Problem(二分+贪心)的更多相关文章

  1. 【codeforces 761D】Dasha and Very Difficult Problem

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. 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]的相 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. Codeforces 761D Dasha and Very Difficult Problem(贪心)

    题目链接 Dasha and Very Difficult Problem 求出ci的取值范围,按ci排名从小到大贪心即可. 需要注意的是,当当前的ci不满足在这个取值范围内的时候,判为无解. #in ...

  7. codeforces 761D - Dasha and Very Difficult Problem

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  8. codeforces 761 C. Dasha and Password(多维dp)

    题目链接:http://codeforces.com/contest/761/problem/C 题意:给出n行的字符串每一列都从第一个元素开始可以左右移动每一行字符串都是首位相连的. 最后问最少移动 ...

  9. Codeforces Round #253 Div2 D.Andrey and Problem 概率+贪心

    概率计算:P(某set) =  令:  和   现在考虑: 1.考虑某个集合,再加一个概率为Pi的朋友后能不能使总概率提高. 即: 由公式可知, 如果 S < 1,则delta > 0,则 ...

随机推荐

  1. 记一次使用LR测试UDP和TCP的过程

    背景 最近项目要做性能测试,要出要一份性能报告,让我出一个有关Tcp和Udp的功能模块的测试,流程大概是这样,先走TCP协议协商一下会话,协商成功后走Udp收发数据. 有点简单啊,自己写个功能模块测一 ...

  2. jquery 动态载入页面,并且保证 url 变动

    最近做一个新的项目,项目页头,导航,页尾是不变的,只有中间部分是通过加载其他页面,达到内容刷新的. 大概结构如下, 要求, 1. 正文部分可以通过加载一个页面达到刷新效果 2. 保留加载的页面 url ...

  3. HBase MapReduce 一些 ClassNotFoundException 所缺少的jar包

    我们在用 java 操作 HBase  时,可能会出现相关的 ClassNotFoundException  等异常信息,但是我们又不想把 HBase lib 下的所有jar包全部导入到工程,因为会有 ...

  4. 02.Mybatis的动态代理方式实现增删改查

    动态代理的方式实现增删改查: 通过约定的方式定位sql语句 约定 > 配置文件 > 硬编码 约定的目标是省略掉通过硬编码的方式定位sql的代码,通过接口直接定位出sql语句,以下代码为通过 ...

  5. 低版本IE兼容 H5+CSS3 方案

    [主要是针对ie6 7 8对支持和让老浏览器支持html5+css3的一些js脚本] html5shiv.js  // 让IE8及耕地版本的IE识别section,article,nav等html5元 ...

  6. FutrueTask原理及源码分析

    1.前言 相信很多人了解到FutureTask是因为ThreadPoolExecutor.submit方法,根据ThreadPoolExecutor.submit的使用,我们可以先猜一下FutureT ...

  7. react-native-gesture-handler报错

    安装React Native第三方组件出现Task :react-native-gesture-handler:compileDebugJavaWithJavac FAILED报错,则使用jetifi ...

  8. Netbeans courier new 乱码问题

    Netbeans 默认的字体 monospaced,显示英文的单引号及字体非常的不好看,在网上查了下资料可以变得很好看. 1.仍然保持默认字体 monospaced 2.在Netbeans 的安装目  ...

  9. Go-TCP粘包

    TCP黏包 黏包示例 服务端代码如下: // socket_stick/server/main.go func process(conn net.Conn) { defer conn.Close() ...

  10. Goland_IDE的护眼、主题、字体等设置

    Goland_IDE的护眼.主题.字体等设置 1.代码格式化 File->Settings->Tools->File Watchers->+->go fmt->将N ...