Gym - 101028I March Rain 二分
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 二分的更多相关文章
- 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 ...
- Codeforces Gym 100425A Luggage Distribution 二分 数学
A - Luggage DistributionTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/c ...
- Code Forces Gym 100886J Sockets(二分)
J - Sockets Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Valera ...
- Gym - 100851L:Landscape Improved (二分+单调性)
题意: 一个宽度为N的网格图,i上有h[i]高的方块.现在你有W个方块,问怎么放使得最终的最高点最高. 当一个格子的下方,左下方和右下方都有方块那么久可以把方块放到这个格子上.最左端和最右端不能放 ...
- Gym 100971D Laying Cables 二分 || 单调栈
要求找出每个a[i],找到离他最近而且权值比它大的点,若距离相同,输出权利最大的那个 我的做法有点复杂,时间也要500+ms,因为只要时间花在了map上. 具体思路是模拟一颗树的建立过程,对于权值最大 ...
- 【Codeforces】Gym 101608G WiFi Password 二分+线段树
题意 给定$n$个数,求有最长的区间长度使得区间内数的按位或小于等于给定$v$ 二分区间长度,线段树处理出区间或,对每一位区间判断 时间复杂度$O(n\log n \log n)$ 代码 #inclu ...
- Gym 100883J palprime(二分判断点在凸包里)
题意:判断一堆小点有多少个在任意三个大点构成的三角形里面. 思路:其实就是判断点在不在凸包里面,判断的话可以使用二分来判断,就是判断该点在凸包的哪两个点和起点的连线之间. 代码: /** @xigua ...
- Gym - 101981B Tournament (WQS二分+单调性优化dp)
题意:x轴上有n个人,让你放置m个集合点,使得每个人往离他最近的集合点走,所有人走的距离和最短. 把距离视为花费,设$dp[i][k]$表示前i个人分成k段的最小花费,则有递推式$dp[i][k]=m ...
- Book Borders (Gym - 101480B)(二分)
题目链接 题解:用二分查询一下每次满足长度的下一个加上它的长度. #include <bits/stdc++.h> using namespace std; typedef long lo ...
随机推荐
- 5 -- Hibernate的基本用法 --1 3 流行的ORM框架简介
⊙ JPA : JPA本身只是一种ORM规范,并不是ORM产品.它是Java EE规范制定者向开源世界学习的结果.JPA实体与Hibernate PO十分相似,甚至JPA实体完全可作为Hibernat ...
- 【ArcGIS】ArcGIS Android SDK
1.错误提示 11-06 18:12:17.553: A/libc(11929): Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 1 ...
- Kafka(二)-- 安装配置
一.单机部署 1.下载kafka,可在apache官网上下载,kafka要和JDK版本对应,我使用的是JDK1.7,kafka为0.10 2.进入到 /usr/java 中,解压到 此文件夹中 tar ...
- commons-beanutils的使用
commons-beanutils是通过内省来完成的. 需要两个包: commons-beanutils-1.8.3.jar commons-logging-1.1.1.jar JavaBean类: ...
- sine曲线向前运动
using UnityEngine; using System.Collections; public class sineWork : MonoBehaviour { float verticalS ...
- C语言中如何计算时间差
#include <time.h> #include <stdio.h> int main() { time_t start ,end ; ...
- 简述项目中优化sql的方法,从哪些方面,sql语句性能如何分析?
查询速度慢的原因很多,常见如下几种 : .没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) .I/O吞吐量小,形成了瓶颈效应. .没有创建计算列导致查询不优化. .内存不足 .网络 ...
- IDEA试用期结束激活问题
1.试用期结束,出现IDEA License Activation界面 2.IntelliJ Idea 2017 免费激活方法 方法1. 到网站 http://idea.lanyus.com/ 获取注 ...
- Android设计和开发系列第一篇:Notifications通知(Design)
Design篇 Notifications The notification system allows users to keep informed about relevant and timel ...
- 【大数据系列】hadoop单机模式安装
一.添加用户和用户组 adduser hadoop 将hadoop用户添加进sudo用户组 sudo usermod -G sudo hadoop 或者 visudo 二.安装jdk 具体操作参考:c ...