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,则 ...
随机推荐
- 20190803 NOIP模拟测试12「斐波那契(fibonacci)· 数颜色 · 分组 」
164分 rank11/64 这次考的不算太差,但是并没有多大的可能性反超(只比一小部分人高十几分而已),时间分配还是不均,T2两个半小时,T1半个小时,T3-额十几分钟吧 然额付出总是与回报成反比的 ...
- 前端笔记之微信小程序(四)WebSocket&Socket.io&摇一摇案例&地图|地理位置
一.WebSocket概述 http://www.ruanyifeng.com/blog/2017/05/websocket.html Workerman一款开源高性能异步PHP socket即时通讯 ...
- zookeeper 集群配置
安装前要先确保配置好 jdk,这里不在讲述 一. 将zookeeper 安装包下载到你想要的目录 下载地址:http://mirrors.hust.edu.cn/apache/zookeeper/ m ...
- python异常处理-异常捕获-抛出异常-断言-自定义异常-UDP通信-socketserver模块应用-3
异常捕获 异常:程序在运行过程中出现了不可预知的错误,并且该错误没有对应的处理机制,那么就会以异常的形式表现出来 影响:整个程序无法再正常运行 异常的结构 异常的类型 NameError 异常的信息 ...
- 一文读懂tomcat组件--一个web服务器的架构演化史
1. tomcat是谁? 2. tomcat可以做什么? tomcat是一个web容器,可以将web应用部署到tomcat,由它提供web服务,一个web容器中可以部署多个web应用,这些we ...
- 9个tcpdump使用实例
tcpdump能帮助我们捕捉并保存网络包,保存下来的网络包可用于分析网络负载情况,包可通过tcpdump命令解析,也可以保存成后缀为pcap的文件,使用wireshark等软件进行查看. 以下将给出9 ...
- Redis——基础使用
Redis总体介绍: Redis特性 Redis(REmote DIctionary Server),是一个开源的内存数据库 持久化:RDB.AOF 主备复制 丰富的数据结构 Lua脚本.事务 Red ...
- java高并发系列 - 第24天:ThreadLocal、InheritableThreadLocal(通俗易懂)
java高并发系列第24篇文章. 环境:jdk1.8. 本文内容 需要解决的问题 介绍ThreadLocal 介绍InheritableThreadLocal 需要解决的问题 我们还是以解决问题的方式 ...
- 理解Go协程与并发
协程 Go语言里创建一个协程很简单,使用go关键字就可以让一个普通方法协程化: package main import ( "fmt" "time" ) fun ...
- win10文件备份、文件同步方案
用个人版onedrive同步重要数据,数据安全有保障,但免费版只有15G空间,需要合理分配.(201907与别人合租家庭版,空间1T充足) google-drive可以同步指定的文件夹,但空间也只有1 ...