B. Sport Mafia 二分
2 seconds
256 megabytes
standard input
standard output
Each evening after the dinner the SIS's students gather together to play the game of Sport Mafia.
For the tournament, Alya puts candies into the box, which will serve as a prize for a winner. To do that, she performs nn actions. The first action performed is to put a single candy into the box. For each of the remaining moves she can choose from two options:
- the first option, in case the box contains at least one candy, is to take exactly one candy out and eat it. This way the number of candies in the box decreased by 11;
- the second option is to put candies in the box. In this case, Alya will put 11 more candy, than she put in the previous time.
Thus, if the box is empty, then it can only use the second option.
For example, one possible sequence of Alya's actions look as follows:
- put one candy into the box;
- put two candies into the box;
- eat one candy from the box;
- eat one candy from the box;
- put three candies into the box;
- eat one candy from the box;
- put four candies into the box;
- eat one candy from the box;
- put five candies into the box;
This way she will perform 99 actions, the number of candies at the end will be 1111, while Alya will eat 44 candies in total.
You know the total number of actions nn and the number of candies at the end kk. You need to find the total number of sweets Alya ate. That is the number of moves of the first option. It's guaranteed, that for the given nn and kk the answer always exists.
Please note, that during an action of the first option, Alya takes out and eats exactly one candy.
The first line contains two integers nn and kk (1≤n≤1091≤n≤109; 0≤k≤1090≤k≤109) — the total number of moves and the number of candies in the box at the end.
It's guaranteed, that for the given nn and kk the answer exists.
Print a single integer — the number of candies, which Alya ate. Please note, that in this problem there aren't multiple possible answers — the answer is unique for any input data.
1 1
0
9 11
4
5 0
3
3 2
1
In the first example, Alya has made one move only. According to the statement, the first move is always putting one candy in the box. Hence Alya ate 00 candies.
In the second example the possible sequence of Alya's actions looks as follows:
- put 11 candy,
- put 22 candies,
- eat a candy,
- eat a candy,
- put 33 candies,
- eat a candy,
- put 44 candies,
- eat a candy,
- put 55 candies.
This way, she will make exactly n=9n=9 actions and in the end the box will contain 1+2−1−1+3−1+4−1+5=111+2−1−1+3−1+4−1+5=11 candies. The answer is 44, since she ate 44 candies in total.
题意:n次操作,每次操作可以选择往盒子里放 i 个糖果(i <n),也可以在盒子里拿一个糖果吃掉,要求当n次操作之后盒子里恰好有k个糖果,问在n次操作中有几次是吃糖果
题解:二分枚举吃糖果的次数x,(0<=x<=n-1,因为第一次一定是放糖果)
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
typedef long long ll;
ll n,k;
ll find(ll x)
{
ll sum=;
for(int i=;i<=n-x;i++)
sum=sum+i;
if(sum-x>k)
return ;
else
return ;
}
int main()
{
while(cin>>n>>k)
{
ll l=,r=n-,mid;
while(l<=r)
{
mid=l+(r-l)/;
if(find(mid))
l=mid+;
else
r=mid-;
}
cout<<l<<endl;
}
return ;
}
B. Sport Mafia 二分的更多相关文章
- 【CF587D】Duff in Mafia 二分+前缀优化建图+2-SAT
[CF587D]Duff in Mafia 题意:给你一张n个点m条边的无向图,边有颜色和边权.你要从中删去一些边,满足: 1.任意两条删掉的边没有公共的顶点.2.任意两条剩余的.颜色相同的边没有公共 ...
- Codeforces Round #574 (Div. 2)补题
A. Drinks Choosing 统计每种酒有多少人偏爱他们. ki 为每种酒的偏爱人数. 输出ans = (n + 1)/2 > Σki / 2 ? (n + 1)/2 - Σki / ...
- Codeforce Round #574(Div.2)
...
- Codeforces Round #574 (Div. 2)
目录 Contest Info Solutions A. Drinks Choosing B. Sport Mafia C. Basketball Exercise D1. Submarine in ...
- Codeforces Round #574 (Div. 2) A~E Solution
A. Drinks Choosing 有 $n$ 个人,每个人各有一种最喜欢的饮料,但是买饮料的时候只能同一种的两个两个买(两个一对) 学校只打算卖 $\left \lceil \frac{n}{2} ...
- 2020.10.16--vj个人赛补题
D - Drinks Choosing Old timers of Summer Informatics School can remember previous camps in which eac ...
- CodeForces - 348A Mafia (巧妙二分)
传送门: http://codeforces.com/problemset/problem/348/A A. Mafia time limit per test 2 seconds memory li ...
- Codeforces Round #202 (Div. 1) A. Mafia 推公式 + 二分答案
http://codeforces.com/problemset/problem/348/A A. Mafia time limit per test 2 seconds memory limit p ...
- codeforces Mafia
/* * Mafia.cpp * * Created on: 2013-10-12 * Author: wangzhu */ /** * 每个人都想玩若干场,求至少需要玩几场才可以满足大家的需求. * ...
随机推荐
- 3_01_MSSQL课程_Ado.Net_连接数据库
1. Ado.Net 是一组微软的c#操作数据库的类库. 2.开发人员将界面上的数据(用户的操作和输入的数据)存储到数据库当中.. 3.数据库访问的驱动.即:应用程序和数据库的桥梁. 4.驱动层. S ...
- day19-Python运维开发基础(类的魔术方法)
1. __new__魔术方法 # ### __new__ 魔术方法 ''' 触发时机:实例化类生成对象的时候触发(触发时机在__init__之前) 功能:控制对象的创建过程 参数:至少一个cls接受当 ...
- redhat 7.6 常用命令
cp 复制命令 diff 对比两个文件内容是否相同 cp -rvf 复制目录 r代表递归 v显示详细步骤 f强制 ls -ah 查看目录 a查看隐藏文件 h显示文件大小单位k less 逐行 ...
- Community Cloud零基础学习(二)信誉等级设置 & Global Search设定
当我们创建了Community以后,我们需要对他进行定制页面来使community用户更好的使用.此篇主要描述两点,信誉等级设定以及Global Search 设定.其他的内容后期再慢慢描述. 一. ...
- Ansible ssh-key密钥认证配置
对于被管理服务器做免密码登录设置 1.在管理服务器生成ssh-key密钥 #ssh-keygen //生成秘钥 root@hsz:/etc/ansible# ssh-keygen Generatin ...
- Python 操作rabbitmq中的confirm模式的错误
今天使用rabbitmq的confirm模式,书上介绍的是pika版本是0.9.6,但是我用的是别的版本,发现这样的错误 Traceback (most recent call last): Fil ...
- js读取本地json/txt/xml存在跨越问题,可以用jsonp 读取本地json文件
想自己用 js写一个原生的ajax请求,访问本地文件,json/txt.但是demo,写了一个后,发现 原来是跨域了. js 写的原生ajax 请求代码如下 html代码 <div id=&qu ...
- 服务端OLEVARIANT数据之后传输
将OLEVARIANT数据流化,然后对流进行压缩,还原成OLEVARIANT以后再发送. procedure StreamToVariant(Stream: TStream; var V: OLEVa ...
- js判断数组中是否包含某个元素
参考:http://www.runoob.com/jquery/misc-inarray.html js判断数组中是否包含某个元素 $.inArray( value, array [, fromInd ...
- img标签无法显示src中名字中带有中文的图片的问题
img: <img src="/upload/${good.photo}" style="width: 120px;height: 120px;" alt ...