http://codeforces.com/gym/101028/problem/I

题意:给你n个洞,k个布条,问布条能贴到所有洞时的最小值。

题解:二分答案,因为答案越大就越容易满足条件。

技巧:两种judge写法:常规与upper_bound,嗯,就是有种可以upper_bound的感觉。

坑:VS提示upper_bound出错但是代码能ac Orz//_DEBUG_ERROR2("invalid iterator range", _File, _Line);百度不到为啥。。最后发现

 pos = upper_bound(a + pos + 1, a + n + 1, a[pos] + mid - 1) - a;改成
 pos = upper_bound(a + pos , a + n + 1, a[pos] + mid - 1) - a;就好了。有点意思

ac代码:正常写法

#define    _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
#include<stdio.h>
using namespace std;
const int maxn = 1e5 + ;
int a[maxn];
int n, k;
bool judge(int x) {
int p = , cnt = ;
for (int i = ; i <= n; i++) {
if (p < a[i]) {
cnt++;
p = a[i] + x - ;
if (cnt == k) {
if (p >= a[n])return ;
else return ;
}
if (p >= a[n])return ;
}
}
return ;
}
int main() {
int t;
cin >> t;
while (t--) { cin >> n >> k;
for (int i = ; i <= n; i++) {
scanf("%d", &a[i]);
}
int l = , r = a[n];
int mid;
while (l <= r) {
mid = l + r >> ;
if (!judge(mid)) l = mid+;
else r = mid-; }
cout << l << endl;
} }

简化版:

#define    _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
#include<stdio.h>
using namespace std;
const int maxn = 1e5 + ;
int a[maxn];
int n, k;
bool judge(int x) {
int pos = , cnt = ;
for (int i = ; i <= k; i++) {
pos = upper_bound(a + pos + , a + n + , a[pos] + x - ) - a;
}
if (pos > n)return ;
return ;
}
int main() {
int t;
cin >> t;
while (t--) { cin >> n >> k;
for (int i = ; i <= n; i++) {
scanf("%d", &a[i]);
}
int l = , r = a[n];
int mid;
while (l <= r) {
mid = l + r >> ;
int pos = ;
for (int i = ; i <= k; i++) {
pos = upper_bound(a + pos + , a + n + , a[pos] + mid - ) - a;
}
if (pos>n) r = mid - ;
else l = mid + ; }
cout << l << endl;
} }

Gym - 101028I March Rain 二分的更多相关文章

  1. 2016 Al-Baath University Training Camp Contest-1 I. March Rain —— 二分

    题目链接:http://codeforces.com/problemset/gymProblem/101028/I I. March Rain time limit per test 2 second ...

  2. Codeforces Gym 100425A Luggage Distribution 二分 数学

    A - Luggage DistributionTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/c ...

  3. Code Forces Gym 100886J Sockets(二分)

    J - Sockets Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Valera ...

  4. Gym - 100851L:Landscape Improved (二分+单调性)

    题意: 一个宽度为N的网格图,i上有h[i]高的方块.现在你有W个方块,问怎么放使得最终的最高点最高.   当一个格子的下方,左下方和右下方都有方块那么久可以把方块放到这个格子上.最左端和最右端不能放 ...

  5. Gym 100971D Laying Cables 二分 || 单调栈

    要求找出每个a[i],找到离他最近而且权值比它大的点,若距离相同,输出权利最大的那个 我的做法有点复杂,时间也要500+ms,因为只要时间花在了map上. 具体思路是模拟一颗树的建立过程,对于权值最大 ...

  6. 【Codeforces】Gym 101608G WiFi Password 二分+线段树

    题意 给定$n$个数,求有最长的区间长度使得区间内数的按位或小于等于给定$v$ 二分区间长度,线段树处理出区间或,对每一位区间判断 时间复杂度$O(n\log n \log n)$ 代码 #inclu ...

  7. Gym 100883J palprime(二分判断点在凸包里)

    题意:判断一堆小点有多少个在任意三个大点构成的三角形里面. 思路:其实就是判断点在不在凸包里面,判断的话可以使用二分来判断,就是判断该点在凸包的哪两个点和起点的连线之间. 代码: /** @xigua ...

  8. Gym - 101981B Tournament (WQS二分+单调性优化dp)

    题意:x轴上有n个人,让你放置m个集合点,使得每个人往离他最近的集合点走,所有人走的距离和最短. 把距离视为花费,设$dp[i][k]$表示前i个人分成k段的最小花费,则有递推式$dp[i][k]=m ...

  9. Book Borders (Gym - 101480B)(二分)

    题目链接 题解:用二分查询一下每次满足长度的下一个加上它的长度. #include <bits/stdc++.h> using namespace std; typedef long lo ...

随机推荐

  1. Netty权威指南之AIO编程

    由JDK1.7提供的NIO2.0新增了异步的套接字通道,它是真正的异步I/O,在异步I/O操作的时候可以传递信号变量,当操作完成后会回调相关的方法,异步I/o也被称为AIO,对应于UNIX网络编程中的 ...

  2. 【AI】Ubuntu NVIDIA CUDA CUDNN安装配置

    https://blog.csdn.net/qq_33200967/article/details/80689543 https://blog.csdn.net/sinat_29963957/arti ...

  3. 【代码审计】711cms_V1.0.5前台XSS跨站脚本漏洞分析

      0x00 环境准备 711CMS官网: https://www.711cms.com/ 网站源码版本:711CMS 1.0.5 正式版(发布时间:2018-01-20) 程序源码下载:https: ...

  4. iOS开发-编译出错 duplicate symbols for architecture x86_64

    今天对原来项目文件进行重新整理,根据文件内容进行分类,结果复制粘贴时没注意把一个文件复制了两遍 编译的时候就出现Duplicate Symbol Error 在网上搜素了一圈发现也有人遇到过这个问题, ...

  5. Jsoup(四)-- Jsoup获取DOM元素属性值

    1.获取博客园的博客标题以及博客地址,获取友情链接 2.代码实现: public static void main(String[] args) throws Exception{ // 创建http ...

  6. 系统头文件cmath,cstdlib报错

    >C:\Program Files (x86)\Microsoft Visual Studio\\Community\VC\Tools\MSVC\\include\cstdlib(): erro ...

  7. Kafka与Flink集成

    Apache Flink是新一代的分布式流式数据处理框架,它统一的处理引擎既可以处理批数据(batch data)也可以处理流式数据(streaming data).在实际场景中,Flink利用Apa ...

  8. C语言中的正负数及其输出

    在数学中,数字有正负之分.在C语言中也是一样,short.int.long 都可以带上正负号,例如: //负数 ; short a2 = -0x2dc9; //十六进制 //正数 ; ; //八进制 ...

  9. C++ template —— 智能指针(十二)

    在管理动态分配的内存时,一个最棘手的问题就是决定何时释放这些内存,而智能指针就是用来简化内存管理的编程方式.智能指针一般有独占和共享两种所有权模型.-------------------------- ...

  10. TYAttributedLabel——简单,强大的iOS属性文本控件

    本文转载至 http://www.mobile-open.com/2015/86578.html TYAttributedLabel 简单,强大的属性文本的控件(无需了解CoreText),支持图文混 ...