题目链接: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 —— 二分的更多相关文章

  1. codechef May Challenge 2016 FORESTGA: Forest Gathering 二分

    Description All submissions for this problem are available. Read problems statements in Mandarin Chi ...

  2. 第十四届华中科技大学程序设计竞赛 K Walking in the Forest【二分答案/最小化最大值】

    链接:https://www.nowcoder.com/acm/contest/106/K 来源:牛客网 题目描述 It's universally acknowledged that there'r ...

  3. CodeChef FORESTGA 二分

    Forest Gathering   Problem code: FORESTGA Tweet     ALL SUBMISSIONS All submissions for this problem ...

  4. CodeChef FAVNUM FavouriteNumbers(AC自动机+数位dp+二分答案)

    All submissions for this problem are available. Chef likes numbers and number theory, we all know th ...

  5. 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 ...

  6. [Codechef SSTORY] A Story with Strings - 后缀数组,二分

    [Codechef SSTORY] A Story with Strings Description 给定两个字符串,求它们的最长公共子串.如果解不唯一,输出最先在第二个字符串中出现的那个. Solu ...

  7. 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,数字 ...

  8. [JZOJ6089]【CodeChef 2014 April Challenge】Final Battle of Chef【数据结构】【整体二分】

    Description \(n,q,V\leq 100000,w_i\leq 10^9\) Solution 又是一道大数据结构 由于有一个下取整,这就导致了不同时间的修改值是不能简单的直接加在一起的 ...

  9. 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 ...

随机推荐

  1. maven打包自动配置数据库链接信息

    pom.xml加入下面代码 <profiles> <profile> <id>dev</id> <activation> <activ ...

  2. 【hibernate】Hibernate中save, saveOrUpdate, persist, merge, update 区别

    Hibernate Save hibernate save()方法能够保存实体到数据库,正如方法名称save这个单词所表明的意思.我们能够在事务之外调用这个方法,这也是我不喜欢使用这个方法保存数据的原 ...

  3. 转:WebRTC技术及应用2 – NAT穿越技术的使用

    评:webrtc自带的打洞,穿透协议. 转: http://www.unclekevin.org/?p=924 959 views WebRTC技术及应用2 – NAT穿越技术的使用 发表回复 (题图 ...

  4. centos 升级内核失败回救

    在升级 centos6.3上使用, yum -y update  ... 灾难出现了!!! 解决方法: 1. 在机器启动的时候, 按F1, 会出现选择内核,选一个原来的. 2. vim /etc/gr ...

  5. hdu5340 Three Palindromes(manacher算法)

    题目描写叙述: 推断能否将字符串S分成三段非空回文串. 解题思路: 源码: #include <cstdio> #include <algorithm> #define MAX ...

  6. openERP server action,最强大的功能,没有之一

    Jeffery9@gmail.com 出品 @jeffery-陈帆 原理 ations OE定义了ir.actions.actions,并从中派生了众多的子类 ir.actions.client ir ...

  7. S3C2440 IIS操作 uda134x录放音

    IIS(Inter-IC Sound)由飞利浦公司开发.是一种经常使用的音频设备接口,主要用于CD.MD.MP3等设备. s3c2440一共同拥有5个引脚用于IIS:IISDO.IISDI.IISSC ...

  8. C# Main(string[] args)方法参数

    Main 方法是 C# 控制台应用程序或窗口应用程序的入口点,以下是各种情况下方法参数的输入方式: 1.通过vs启动,需要在“项目属性-调试-启动选项-命令行参数”中配置参数,格式为:参数一 参数二 ...

  9. POJ 1141 Brackets Sequence (区间DP)

    Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a r ...

  10. iOS应用数据存储的经常使用方式

    ios程序中数据数据存储有下列5种方式 XML属性列表(plist)归档 Preference(偏好设置) NSKeyedArchiver归档(NSCoding) SQLite3 Core Data ...