Codeforces 760B:Frodo and pillows(二分)
http://codeforces.com/problemset/problem/760/B
题意:有n张床m个枕头,每张床可以有多个枕头,但是相邻的床的枕头数相差不能超过1,问第k张床最多能拥有的枕头数是多少。每张床至少有一个枕头。
思路:因为每张床至少需要一个枕头,所以先将m减掉n之后来考虑剩余枕头如何分配。
我们考虑一个最优的情况,假设有5张床,9个枕头,k为3的时候,那么这样分配:1 2 3 2 1,这样其实就如同阶梯一样,要让第k个最高,然后向两边递减。
我们可以二分答案,使用等差数列的公式来做,数出分配在k左边需要的枕头数,还有k右边需要分配的枕头数,然后判断剩余的枕头数是否满足需要分配的枕头数。
有一些细节需要考虑:例如上面这个样例,当我们枚举分配给k的枕头数为1的时候,是这样分配的:0 0 1 0 0。即枚举的枕头数比左边(或者右边)的床数要少,这个时候要特殊考虑一下。
#include <bits/stdc++.h>
using namespace std;
#define N 3010
#define INF 0x3f3f3f3f
typedef long long LL; LL cal(LL a, LL n) { // 等差求和公式
return a * n + n * (n - ) / ;
} int main() {
LL n, m, k;
cin >> n >> m >> k;
m -= n;
LL lcnt = k - , rcnt = n - k, l = , r = m, ans = ;
while(l <= r) {
LL mid = (l + r) >> ;
LL tmp = mid;
LL left, right;
if(mid - lcnt < ) { // 枚举的枕头数比左边的床数要少
left = cal(, mid - ); // 从1开始,数量为枚举的枕头数-1
} else left = cal(mid - lcnt, lcnt);
if(mid - rcnt < ) { // 同理
right = cal(1LL, mid - );
} else right = cal(mid - rcnt, rcnt);
tmp += left + right;
if(tmp > m) r = mid - ;
else { ans = mid; l = mid + ; }
}
cout << ans + <<endl; // 因为一开始已经每个床分配了一个枕头,所以要加回1
return ;
}
Codeforces 760B:Frodo and pillows(二分)的更多相关文章
- Codeforces 760B Frodo and pillows
题目链接:http://codeforces.com/problemset/problem/760/B 题意:n个床位,m个枕头,第k个位置最多有多少个枕头,其中相邻之间的差<=1; 第k个位置 ...
- cf 760B.Frodo and pillows
二分,判断条件就是最小情况(设当前k位取x)比剩余值(m-x)要小.(貌似又做麻烦了2333) #include<bits/stdc++.h> #define LL long long # ...
- Codefroces 760 B. Frodo and pillows
B. Frodo and pillows time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 【codeforces 760B】Frodo and pillows
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- CodeForces 377B---Preparing for the Contest(二分+贪心)
C - Preparing for the Contest Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
- Codeforces 484B Maximum Value(高效+二分)
题目链接:Codeforces 484B Maximum Value 题目大意:给定一个序列,找到连个数ai和aj,ai%aj尽量大,而且ai≥aj 解题思路:类似于素数筛选法的方式,每次枚举aj,然 ...
- Codeforces 607A - Chain Reaction - [DP+二分]
题目链接:https://codeforces.com/problemset/problem/607/A 题意: 有 $n$ 个塔排成一行,第 $i$ 个激光塔的位置为 $a_i$,伤害范围是 $b_ ...
- Codeforces 825D Suitable Replacement - 贪心 - 二分答案
You are given two strings s and t consisting of small Latin letters, string s can also contain '?' c ...
- Codeforces 749D. Leaving Auction set+二分
D. Leaving Auction time limit per test: 2 seconds memory limit per test:256 megabytes input:standard ...
随机推荐
- WPF DataGrid的LoadingRow事件
<Window x:Class="DataGridExam.MainWindow" xmlns="http://schemas.microsoft.c ...
- wpf 禁用window的systemmenu
private IntPtr WidProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { if (m ...
- WPF下Itemscontrol分组 样式
原文 WPF下Itemscontrol分组 样式 <ItemsControl Grid.Row="1" DataContext="{Binding Layouts} ...
- mysql 服务压缩包安装,用户创建
wind7上安装mysql记录: 1.下载的包中没有ini配置文件,需要根目录手动创建my.ini文件 内容如下: [client]port=3306default-character-set=utf ...
- 【Windows10 IoT开发系列】PowerShell的相关配置
原文:[Windows10 IoT开发系列]PowerShell的相关配置 可使用 Windows PowerShell 远程配置和管理任何 Windows 10 IoT 核心版设备.PowerShe ...
- 获取其他进程中“Internet Explorer_TridentCmboBx”的内容
function GetTridentCmboBxText( // 获取其他进程中“Internet Explorer_TridentCmboBx”的内容 mHandle: THandle; // ...
- Layui 是一款采用自身模块规范编写的国产前端UI框架(5600个Star)
采用自身模块规范编写的前端UI框架,遵循原生HTML/CSS/JS的书写形式,极低门槛,拿来即用. http://www.layui.com Layui 是一款采用自身模块规范编写的国产前端UI框架, ...
- Codility---Dominator
Task description A zero-indexed array A consisting of N integers is given. The dominator of array A ...
- hdu4767_Bell_矩阵快速幂+中国剩余定理
2013长春赛区网络赛的1009题 比赛的时候这道题英勇的挂掉了,原因是写错了一个系数,有时候粗心比脑残更可怕 本题是关于Bell数,关于Bell数的详情请见维基:http://en.wikipedi ...
- SYN6107型 GPS北斗双模子钟
SYN6107型 GPS北斗双模子钟 产品概述 SYN6107型GPS北斗双模子钟是由西安同步电子科技有限公司精心设计.自行研发生产的一套以接收北斗卫星信号的子钟,从北斗地球同步卫星上获取标准时钟信号 ...