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 ListBoxItem模板中添加CheckBox选中问题
原文:WPF ListBoxItem模板中添加CheckBox选中问题 是这样的,需要一个ListBox来展示照片,并添加一个选中的CheckBox.这就需要对ListBox的ItemTemplate ...
- 辛星与您彻底解决CSS浮子(下一个)
上述博客文章,我们解释如何使用CSS浮子,这是一个看我们如何解释清除CSS浮子.其实CSS浮动是很清楚easy,只需要使用clear它财产,至于如何利用好它.很多人可能会表决雾,我是个新手的时候还经常 ...
- HALCON学习之算子大全
1.1 Gaussian-Mixture-Models 1.add_sample_class_gmm 功能:把一个训练样本添加到一个高斯混合模型的训练数据上. 2.classify_class_gmm ...
- 阿里云访问控制(RAM)授权子账户管理磁盘快照
阿里云 RAM 控制台没有提供管理磁盘快照的系统策略,需要自己添加自定义授权策略. 操作步骤: 进入 RAM 控制台 -> 策略管理,点击"新建授权策略" 选中"空 ...
- PostSharp-4.3.33安装包_KeyGen发布
PostSharp-4.3.33安装包_KeyGen发布 请低调使用. PostSharp安装及注册步骤截图.rar 请把浏览器主页设置为以下地址支持本人.https://www.duba.com/? ...
- SQLServer 不允许保存更改的解决办法
启动SQL Server Management Studio 工具菜单----选项----Designers(设计器)----阻止保存要求重新创建表的更改 取消勾选即可.
- Delphi For Linux Compiler
Embarcadero is about to release a new Delphi compiler for the Linux platform. Here are some of the k ...
- ChartDirector应用笔记(可同时为Web和Qt MFC提供图表)
ChartDirector介绍 ChartDirector是一款小巧精细的商业图表库.其适用的语言范围非常广泛,包括.Net, Java, Asp, VB, PHP, Python, Ruby, C+ ...
- Google地图下载工具代码
// // Google Map Tiles Downloader in C# by coolypf // No rights reserved, neither warranty nor guara ...
- Win7 访问 数据库 慢
不让TCP/IP调谐拖累网速 在Windows Server 2008工作环境中,下载访问网络中大容量的文件内容时,我们有时会感觉到网络连接速度非常缓慢,严重的时候还会出现不能访问的现象.遭遇这类故障 ...