UVALive 7500 Boxes and Balls 2015EC final 签到题 二分
分析题目后,得到要求的是最接近n的一个数,并且这个数字能写成1+2+3+....+x = ans这种形式。
要求的是最大的值。
这题就直接二分去做吧。二分出一个f(mid)<=n的最大值。
最后的end就是所求的f(end)
为什么呢?,我来分析下我这个二分是怎么实现的
- while (begin<=end)
- {
- LL mid = (begin + end) / ;
- if (f(mid) == n)
- {
- printf ("Case #%d: %lld\n",++ff,n);
- return ;
- }
- if (f(mid) < n)
- {
- begin = mid+; //去找一个可能比n大的
- }
- else end = mid-; //如果比n大了的话。就回来找一个小的。
- //cout<<f(mid)<<endl;
- }
当f(mid)<n的时候 begin = mid+1,如果这个时候[mid+1,end]的所有数字的f()值都大于n呢?那么,最后一步就肯定是begin=end,然后end-1.去到的是第一个小于n的f()值。
其他也是一样分析啦。
f(end),第一个小于n的f值。
f(begin),第一个大于n的f值。考虑最后一步begin==end的时候,就能看清楚了。
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <cmath>
- #include <algorithm>
- using namespace std;
- #define inf (0x3f3f3f3f)
- typedef long long int LL;
- #include <iostream>
- #include <sstream>
- #include <vector>
- #include <set>
- #include <map>
- #include <queue>
- #include <string>
- LL f(LL x)
- {
- if (x&)
- {
- return (x+)/*x;
- }
- else return x/*(x+);
- }
- int ff;
- void work ()
- {
- LL n;
- cin>>n;
- LL begin=,end=2e9;
- while (begin<=end)
- {
- LL mid = (begin + end) / ;
- if (f(mid) == n)
- {
- printf ("Case #%d: %lld\n",++ff,n);
- return ;
- }
- if (f(mid) < n)
- {
- begin = mid+; //去找一个可能比n大的
- }
- else end = mid-; //如果比n大了的话。就回来找一个小的。
- //cout<<f(mid)<<endl;
- }
- printf ("Case #%d: %lld\n",++ff,f(end));
- return ;
- }
- int main()
- {
- #ifdef local
- freopen("data.txt","r",stdin);
- #endif
- int t;
- cin>>t;
- while(t--) work();
- return ;
- }
UVALive 7500 Boxes and Balls 2015EC final 签到题 二分的更多相关文章
- UVaLive 7500 Boxes and Balls (数学)
题意:给定 n 个球,每次从每篮子里拿出来一个放在一个新篮子里,并移除相同的,按球的个数进行排序,问你用最多几个球能完成循环. 析:数学问题,很容易发现前n项和就是最多的球数,所以我们只要找最大的n项 ...
- uvalive 7500 Boxes and Balls
https://vjudge.net/problem/UVALive-7500 题意: 找到规律之后发现给出一个数n,要求找到1 + 2i + ... + x <= n,找出1到x的和. 思路: ...
- 【CF884D】Boxes And Balls 哈夫曼树
[CF884D]Boxes And Balls 题意:有n个箱子和若干个球,球的颜色也是1-n,有ai个球颜色为i,一开始所有的球都在1号箱子里,你每次可以进行如下操作: 选择1个箱子,将里面所有的球 ...
- Educational Codeforces Round 31- D. Boxes And Balls
D. Boxes And Balls time limit per test2 seconds memory limit per test256 megabytes 题目链接:http://codef ...
- CF388C&&2018EC Final D题——博弈&&水题
一下两个题目都是按堆取石子,轮流取,每个人都贪心的取即可,感觉都不像博弈. CF388C 有n排石子,每排有若干堆.Ciel可以选择一排,拿走这一排的第一堆石子.Jiro可以选择一排,拿走这一排的最后 ...
- A 洛谷 P3601 签到题 [欧拉函数 质因子分解]
题目背景 这是一道签到题! 建议做题之前仔细阅读数据范围! 题目描述 我们定义一个函数:qiandao(x)为小于等于x的数中与x不互质的数的个数. 这题作为签到题,给出l和r,要求求. 输入输出格式 ...
- fjwc2019 D3T1 签到题 (贪心)
#184. 「2019冬令营提高组」签到题 每次询问接近O(1).......考虑贪心 怎么贪心呢? 对于相邻的两个数,我们要保证异或x后单调不降 我们找到两个数二进制上最高的相异位 当左边的数相异位 ...
- CTF-练习平台-WEB之 签到题
一.签到题 根据提示直接加群在群公告里就能找到~
- 洛谷P3601签到题(欧拉函数)
题目背景 这是一道签到题! 建议做题之前仔细阅读数据范围! 题目描述 我们定义一个函数:qiandao(x)为小于等于x的数中与x不互质的数的个数. 这题作为签到题,给出l和r,要求求. 输入输出格式 ...
随机推荐
- Python 中list, dictionary 与 file相互操作
Python的list,dictionary可以写入file, 也可以从file中读取. 关于list: 1)写入文件 self.existedBlog.write("you ...
- get and set
(function () { function Student(name, age, gender) { this._name = name; this._age = age; this._gende ...
- oracle--基础查询(1)
--查询所有列语句 select * from emp; ---查询指定列表的查询语句 select empno,ename,sal from emp; --带有简单算术运算符的简单查询 select ...
- numpy.zeros(shape, dtype=float, order='C')
numpy.zeros Return a new array of given shape and type, filled with zeros. Parameters: shape : int o ...
- 相关符号标点的英文(IOS学习)
尖括号: angle bracket 方括号: square bracket 花括号: curly brace 圆括号: parentheses 逗号: comma 冒号: colon 逗号: sem ...
- 创建sharepoint网站
1.首先打开管理中心 输入用户名和密码(默认是本机的管理员名称及密码) 在“应用程序管理”选择“管理WEB应用程序” 新建应用程序 选择一个没有占用的端口,选择允许匿名访问 数据库名称一般为WSS_C ...
- Learning Python 008 正则表达式-003 search()方法
Python 正则表达式 - search()方法 findall()方法在找到第一个匹配之后,还会继续找下去,findall吗,就是找到所有的匹配的意思.如果你只是想找到第一个匹配的信息后,就不在继 ...
- 22、IDP-ASE
IDPASE https://github.com/bdeonovic/IDPASE.jl Prepare necessary input files (1)FASTQ file of your hy ...
- Win7环境下Sublime Text 3下安装NodeJS插件
1.首先下载安装Node.JS,配置好环境变量(安装好Node.JS默认是配置好了环境变量的). 2.Sublime Text 3下安装NodeJS插件. 参考的两篇文章:http://www.cnb ...
- hdu1057
#include <iostream> #include <string> #include <cstring> using namespace std; int ...