CodeChef Forest Gathering —— 二分
题目链接:https://vjudge.net/problem/CodeChef-FORESTGA
题解:
现场赛。拿到这题很快就知道是二分,但是一直wa,怎么修改也wa,后来又换了种错误的思路,最后还是觉得二分是正确的,但还是wa。人生,就是在这些瞬间被怀疑的……
结束后,才发现,test()的时候溢出了。应该把判断放在循环里面,如果达到要求的,就立刻返回。因为达到要求后,后面的运算都是多余的。
反思:
在某些判断中,如果达到要求后,就立刻执行。不要等所有操作完成后,再执行,因为可能出现意想不到的结果。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
//#define LOCAL
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = 1e5+;
const double pi = acos(-1.0); LL n,w, li;
int h[maxn], r[maxn]; bool test(LL m)
{
LL s = ;
for(int i = ; i<=n; i++)
{
LL t = 1LL*h[i]+1LL*m*r[i];
if(t<li) continue;
s += t;
if(s>=w)
return ;
}
return ;
} //bool test(LL m)
//{
// LL s = 0;
// for(int i = 1; i<=n; i++)
// {
// LL t = 1LL*h[i]+1LL*m*r[i];
// if(t<li) continue;
// s += t;
// }
// return s>=w; //加完再判断会溢出。
//} int main()
{
scanf("%lld%lld%lld",&n,&w,&li);
for(int i = ; i<=n; i++)
scanf("%d%d",&h[i],&r[i]); LL l = , r = 1e18;
while(l<r)
{
LL m = (l+r)>>;
if(test(m))
r = m;
else
l = m + ;
}
printf("%lld\n",r);
}
CodeChef Forest Gathering —— 二分的更多相关文章
- codechef May Challenge 2016 FORESTGA: Forest Gathering 二分
Description All submissions for this problem are available. Read problems statements in Mandarin Chi ...
- 第十四届华中科技大学程序设计竞赛 K Walking in the Forest【二分答案/最小化最大值】
链接:https://www.nowcoder.com/acm/contest/106/K 来源:牛客网 题目描述 It's universally acknowledged that there'r ...
- CodeChef FORESTGA 二分
Forest Gathering Problem code: FORESTGA Tweet ALL SUBMISSIONS All submissions for this problem ...
- CodeChef FAVNUM FavouriteNumbers(AC自动机+数位dp+二分答案)
All submissions for this problem are available. Chef likes numbers and number theory, we all know th ...
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) E. Arson In Berland Forest 二分 前缀和
E. Arson In Berland Forest The Berland Forest can be represented as an infinite cell plane. Every ce ...
- [Codechef SSTORY] A Story with Strings - 后缀数组,二分
[Codechef SSTORY] A Story with Strings Description 给定两个字符串,求它们的最长公共子串.如果解不唯一,输出最先在第二个字符串中出现的那个. Solu ...
- 2019.02.15 codechef Favourite Numbers(二分+数位dp+ac自动机)
传送门 题意: 给444个整数L,R,K,nL,R,K,nL,R,K,n,和nnn个数字串,L,R,K,数字串大小≤1e18,n≤65L,R,K,数字串大小\le1e18,n\le65L,R,K,数字 ...
- [JZOJ6089]【CodeChef 2014 April Challenge】Final Battle of Chef【数据结构】【整体二分】
Description \(n,q,V\leq 100000,w_i\leq 10^9\) Solution 又是一道大数据结构 由于有一个下取整,这就导致了不同时间的修改值是不能简单的直接加在一起的 ...
- CodeChef - ELHIDARR Find an element in hidden array(二分交互)
Find an element in hidden array There is an array of length N consisting of non-negative integers. T ...
随机推荐
- (入门SpringBoot)SpringBoot结合redis(四)
SpringBoot整合redis: 1.引入jar <!-- 引入redis依赖 --><dependency> <groupId>org.springf ...
- JVM加载的初始化类
首先Throws(抛出)几个自己学习过程中一直疑惑的问题: 1.什么是类加载?什么时候进行类加载? 2.什么是类初始化?什么时候进行类初始化? 3.什么时候会为变量分配内存? 4.什么时候会为变量赋默 ...
- SQL SERVER 工具
http://www.cnblogs.com/fygh/archive/2012/04/25/2469563.html
- DozerBeanMapper + 对象转Map方法
1.简介 dozer是一种JavaBean的映射工具,类似于apache的BeanUtils.但是dozer更强大,它可以灵活的处理复杂类型之间的映射.不但可以进行简单的属性映射.复杂的类型映 ...
- 使用cacheBuilder实现函数防抖
在接口中出现的相同请求重复且连续发送的情况导致一些业务BUG,需要在接口上实现防抖 使用google的cacheBuilder import com.google.common.cache.Cache ...
- persits.jpeg 水印组件
官方下载的persits.jpeg 都须要注冊.不然就有时间限制.可今天须要个persits.jpeg 破解版安装到server上,可百度了半天也没找到.最后还是找到了. 很捧的水印组件,玩serve ...
- sql导入数据库
有问题可以尝试加上这句 DROP TABLE IF EXISTS `t_saler_login_log`;
- react 通过 classnames 处理 多个class 的问题
react原生动态添加多个className会报错: import style from './style.css' <div className={style.class1 style.cla ...
- wince开发_摩托罗拉MC3100_打开条码设置
呵呵不多说,直接上图 1.打开控制面板 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0 ...
- python 大小写转换方法(全)
http://blog.csdn.net/liuxincumt/article/details/7945337 python大小写转换(全)