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. Quatz入门

    Demo SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory(); Scheduler sched = sched ...

  2. php 图片上传 500 Internal Server Error 错误

    写php简单上传图片时,发现200k的图片上传时报Internal Server Error错误,检查了upload_max_filesize,及其他post_max_size.max_input_t ...

  3. 【Ubuntu】Windows 远程桌面连接ubuntu及xrdp的一些小问题(远程桌面闪退、连接失败、tab补全功能,无菜单栏,error - problem connecting )【转】

    转:https://blog.csdn.net/u014447845/article/details/80291678 1.远程桌面闪退,shell可以用的问题:(1)需要在该用户目录创建一个.xse ...

  4. Unity Shader 修改自定义变量的值

    Properties { _R(,)) = 1.0 _ColorTex("ColorTex (RGB)", 2D) = "red" {} } SubShader ...

  5. mybatis 之 parameterType="java.util.List"

    <!-- 添加 --> <insert id="saveBatchMemberRoleConnRepModel" parameterType="java ...

  6. python3.4连接和读取oracle数据表

    想用python连接Oracle并查询数据表,就写了个Demo.参考了以下网址. Python学习之 cx_Oracle学习记录 一 http://my.oschina.net/bxxfighting ...

  7. 手机CPU

    说起手机CPU的历史,笔者给大家提一个问题:"世界上第一款智能手机是什么呢?"相信很多人的答案是爱立信的R380或诺基亚的7650,但都不对,真正的首款智能手机是由摩托罗拉在200 ...

  8. spring定时任务配置,以及不执行的解决办法

    前几天,同事问了我一个问题,我告诉他用spring的定时任务解决,并给他配置了spring的定时任务.当时随便找了一个bean写了一段代码,验证定时任务正确执行后,就没再管,昨天下午,同事写代码的时候 ...

  9. 【Laravel5.5】 Laravel 在views中加载公共页面怎么实现

    背景: 在做后台功能时候,我们需要把头部和尾部摘出来作为公共模板使用 1:我们使用了Blade模板,并创建一个header.blade.php作为通用的模板.将子页面作为yield输出: header ...

  10. 异常:Servlet class X is not a javax.servlet.Servlet

    使用Maven命令 mvn archetype:create 创建了一个简单的web项目.导入Eclipse运行时,报这样的异常信息: Servlet class X is not a javax.s ...