codeforces 1041 d 二分
题意转化:有一些区间,要求选一些连续的区间。两两区间间隔的和要求小于H。要求区间的长度和尽可能长。
二分区间长度的和,check一下就行
#include <bits/stdc++.h>
using namespace std;
struct node {
int l,r;
};
node a[];
int n,h;
bool check(int mid) {
int ans = a[].r - a[].l;
if(ans >= mid) {
return true;
}
int cha = ;
int ll = ;
for(int i = ; i <= n; i++) {
//cout<<ans<<endl;
ans += a[i].r - a[i].l;
cha += a[i].l - a[i - ].r;
while(cha >= h) {
cha -= a[ll + ].l - a[ll].r;
ans -= a[ll].r - a[ll].l;
ll++;
}
//cout<<mid<<' '<<cha<<endl;
if(ans >= mid) {
return true;
}
}
return false;
}
int main() {
scanf("%d %d",&n,&h);
for(int i = ; i <= n; i++) {
scanf("%d %d",&a[i].l,&a[i].r);
}
int l = ;
int r = ;
int mid;
while(r - l > ) {
mid = l + r >> ;
if(check(mid)) {
l = mid;
}
else {
r = mid;
}
}
printf("%d",l + h);
return ;
}
codeforces 1041 d 二分的更多相关文章
- codeforces 1041 e 构造
Codeforces 1041 E 构造题. 给出一种操作,对于一棵树,去掉它的一条边.那么这颗树被分成两个部分,两个部分的分别的最大值就是这次操作的答案. 现在给出一棵树所有操作的结果,问能不能构造 ...
- codeforces 1165F1/F2 二分好题
Codeforces 1165F1/F2 二分好题 传送门:https://codeforces.com/contest/1165/problem/F2 题意: 有n种物品,你对于第i个物品,你需要买 ...
- codeforces 732D(二分)
题目链接:http://codeforces.com/contest/732/problem/D 题意:有m门需要过的课程,n天的时间可以选择复习.考试(如果的d[i]为0则只能复习),一门课至少要复 ...
- CodeForces 359D (数论+二分+ST算法)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47319 题目大意:给定一个序列,要求确定一个子序列,①使得该子序 ...
- CodeForces 163B Lemmings 二分
Lemmings 题目连接: http://codeforces.com/contest/163/problem/B Descriptionww.co As you know, lemmings li ...
- CodeForces - 589A(二分+贪心)
题目链接:http://codeforces.com/problemset/problem/589/F 题目大意:一位美食家进入宴会厅,厨师为客人提供了n道菜.美食家知道时间表:每个菜肴都将供应. 对 ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划
There are n people and k keys on a straight line. Every person wants to get to the office which is l ...
- CodeForces - 1059D(二分+误差)
链接:CodeForces - 1059D 题意:给出笛卡尔坐标系上 n 个点,求与 x 轴相切且覆盖了所有给出点的圆的最小半径. 题解:二分半径即可.判断:假设当前二分到的半径是 R ,因为要和 x ...
- Letters CodeForces - 978C (二分)
Time limit4000 ms Memory limit262144 kB There are nn dormitories in Berland State University, they a ...
随机推荐
- sprintf使用时需要注意的问题
- SQL函数 Nullif
函数 Nullif 功能: 如果两个函数都为空字符串则返回null 语法: Nullif(参数1,参数2) 一般该函数与函数coalesc一起使用 例: coalesce ( nullif ( [参数 ...
- cluvfy comp命令用法
1.获取集群验证工具cluvfy的帮助信息 grid@rac1:/home/grid>cluvfy -help USAGE: cluvfy [ -help ] cluvfy stage { -l ...
- DROP FUNCTION - 删除一个函数
SYNOPSIS DROP FUNCTION name ( [ type [, ...] ] ) [ CASCADE | RESTRICT ] DESCRIPTION 描述 DROP FUNCTION ...
- k近邻法(kNN)
<统计学习方法>(第二版)第3章 3 分类问题中的k近邻法 k近邻法不具有显式的学习过程. 3.1 算法(k近邻法) 根据给定的距离度量,在训练集\(T\)中找出与\(x\)最邻近的\(k ...
- 并发3-Volatile
Volatile关键字实现原理 1.认识volatile关键字 程序举例 用一个线程读数据,一个线程改数据 存在数据的不一致性 2.机器硬件CPU与JMM (1)CPU Cache模 (2)CPU缓存 ...
- 报错:org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n ...
- install mysql at linux
cd /usr/local wget http://repo.mysql.com//mysql57-community-release-el7-7.noarch.rpm rpm -ivh mysql5 ...
- 【MyBatis】MyBatis Tomcat JNDI原理及源码分析
一. Tomcat JNDI JNDI(java nameing and drectory interface),是一组在Java应用中访问命名和服务的API,所谓命名服务,即将对象和名称联系起来,使 ...
- jquery实现密码强度检测
jquery实现密码强度验证 jquery实现密码强度验证 JS代码: $('#pass').keyup(function(e) { var strongRegex = new RegExp( ...