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 ...
随机推荐
- k8s之Service资源
1.Service概述 service是kubernetes中最核心的资源对象之一,service和pod之间是通过Label串起来,相同的Service的pod的Label是一样的.同一个servi ...
- luogu P3402 最长公共子序列
题目背景 DJL为了避免成为一只咸鱼,来找Johann学习怎么求最长公共子序列. 题目描述 经过长时间的摸索和练习,DJL终于学会了怎么求LCS.Johann感觉DJL孺子可教,就给他布置了一个课后作 ...
- Java中使用new Date()和System.currentTimeMillis()获取当前时间戳的区别(转)(Java进阶-性能提升)
在开发过程中,通常很多人都习惯使用new Date()来获取当前时间,使用起来也比较方便,同时还可以获取与当前时间有关的各方面信息,例如获取小时,分钟等等,而且还可以格式化输出,包含的信息是比较丰富的 ...
- mysql存储过程命令行批量插入N条数据命令
原文:http://blog.csdn.net/tomcat_2014/article/details/53377924 delimiter $$ create procedure myproc () ...
- 【Linxu】CentOS7下安装程序报错:
进入root用户,然后编辑 vi /usr/libexec/urlgrabber-ext-down 将首行换成 #!/usr/bin/python2.
- 2.【nuxt起步】-初始化创建nuxt项目
1. 脚手架初始化: vue init nuxt-community/starter-template NuxtMyms 2.输入项目相关信息 3.切换到项目目录下 安装依赖 Cd nuxtmyms ...
- cug oj 1479 Treasure Chest Lock (区间dp 思维)
1479: Treasure Chest Lock Time Limit: 1 Sec Memory Limit: 128 MB Submit: 7 Solved: 5 [id=1479" ...
- word中更改图片和标题之间的垂直距离
word中插入图片后.往往须要给图片加上标题. 你插入图片和给图片插入标题时,word用的是默认的格式给你插入的图片和标题. 假如原来的paragraph是2倍行距.你的图片和标题之间的距离也是2倍行 ...
- sql分级汇总
--測试数据 create table tb([DB-ID] varchar(10),ENTITY varchar(10),DATE varchar(10),[CUST-NO] int,AMOUNT ...
- Android——滑动事件冲突解决
android中的事件类型分为按键事件和屏幕触摸事件,Touch事件是屏幕触摸事件的基础事件. android系统中的每个View的子类都具有下面三个与TouchEvent处理密切相关的方法: (1) ...