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序列的大小顺序和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(二分+贪心)的更多相关文章
- 【codeforces 761D】Dasha and Very Difficult Problem
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 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]的相 ...
- 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(贪心)
题目链接 Dasha and Very Difficult Problem 求出ci的取值范围,按ci排名从小到大贪心即可. 需要注意的是,当当前的ci不满足在这个取值范围内的时候,判为无解. #in ...
- codeforces 761D - Dasha and Very Difficult Problem
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- codeforces 761 C. Dasha and Password(多维dp)
题目链接:http://codeforces.com/contest/761/problem/C 题意:给出n行的字符串每一列都从第一个元素开始可以左右移动每一行字符串都是首位相连的. 最后问最少移动 ...
- Codeforces Round #253 Div2 D.Andrey and Problem 概率+贪心
概率计算:P(某set) = 令: 和 现在考虑: 1.考虑某个集合,再加一个概率为Pi的朋友后能不能使总概率提高. 即: 由公式可知, 如果 S < 1,则delta > 0,则 ...
随机推荐
- containerd与kubernetes集成
kubernetes集群三步安装 概念介绍 cri (Container runtime interface) cri is a containerd plugin implementation of ...
- 案例实战:每日上亿请求量的电商系统,JVM年轻代垃圾回收参数如何优化?
出自:http://1t.click/7TJ 目录: 案例背景引入 特殊的电商大促场景 抗住大促的瞬时压力需要几台机器? 大促高峰期订单系统的内存使用模型估算 内存到底该如何分配? 新生代垃圾回收优化 ...
- JVM总结(二)
JVM总结(2)java内存区域.字节码执行引擎 1.内存区域 程序计数器:知道线程执行位置,保证线程切换后能恢复到正确的执行位置. 虚拟机栈:存栈帧.栈帧里存局部变量表.操作栈.动态连接.方法返回地 ...
- Java中只有值传递,(及值传递与引用传递详解)
首先呢,我们来说一下值传递与引用传递的区别(这两个玩意儿实在调用函数的时候提到的) 比如说 code( a) code( int a ) code(a)是调用函数,a是我们原本函数的一个值类型,然后使 ...
- 【0728 | 预习】第三篇 Python基础
第三篇 Python基础预习 Part 1 变量 一.什么是变量? 二.为什么要有变量? 三.定义变量 四.变量的组成 五.变量名的命名规范 六.变量名的两种风格 Part 2 常量 Part 3 P ...
- RocketMQ中PullConsumer的消息拉取源码分析
在PullConsumer中,有关消息的拉取RocketMQ提供了很多API,但总的来说分为两种,同步消息拉取和异步消息拉取 同步消息拉取以同步方式拉取消息都是通过DefaultMQPullConsu ...
- Security Guards (Gym - 101954B)( bfs + 打表 )
题意及思路 题目主要是讲先给出所有guard的位置,再给出所有incidents的位置,求出guard到达每个incident处最小的steps,其中guard每次可以向四周8个方向移动. 思路:对于 ...
- Netty源码分析--内存模型(下)(十二)
这一节我们一起看下分配过程 PooledByteBuf<T> allocate(PoolThreadCache cache, int reqCapacity, int maxCapacit ...
- zuul集成Sentinel最新的网关流控组件
一.说明 Sentinel 网关流控支持针对不同的路由和自定义的 API 分组进行流控,支持针对请求属性(如 URL 参数,Client IP,Header 等)进行流控.Sentinel 1.6.3 ...
- cmd中,查询sqlcmd命令的选项
像我这样的小白,有时候看到-d,-S,-P这些都不知道什么意思,后面知道了是一些命令的选项.如sqlcmd,打开cmd,输入sqlcmd -? 即可获得选项的含义. .