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 ...
随机推荐
- Jenkins build失败条件
在Jenkins 项目写了很多剧本.有时候,我发现脚本失败,但Jenkins运行成功. Jenkins无论是通过退出代码0比量build成功. 因此,newLISP在.您可以使用(exit)对于成功. ...
- IOS开发之KVC KVO KVB
KVC(Key Value Coding) KVO(Key Value Observing) KVB(Key Value Binding) KVO是Cocoa的一个重要机制,他提供了观察某一属性变化的 ...
- JNDI(Java Naming and Directory Interface)
# 前言 内容基本拷贝,整理出来,方便以后回忆. # What The Java Naming and Directory Interface™ (JNDI) is an application pr ...
- 【必须知道】Enum_Flags
[Flags] enum AnyThings{ A=1, B=2, C=4, D=8 } 枚举赋值必须是2^n才可以,目的是实现他们的二进制表示中的 1 ,不要重叠,如 1=0001 2=0010 ...
- MinGW64 how-to(内含编译openssl,libjpeg,libcurl等例子)
Index of contents Setting up the MinGW 64 environment Step 1) building libiconv Step 2) building lib ...
- C函数实现返回多个值的方法
C语言中,一个函数最多只能实现一个返回值. int func (int b) { int a=5; if (a>b) return a; else return b; return 0; } ...
- 核心思想:评价早期SaaS创业公司时,投资人在关注什么?(是否有机会发展成一个平台,长期的护城河)
编者按: 当聊到早期项目时,人们经常会问投资人一个问题:“在评价早期 SaaS 创业公司时,投资人会关注什么——指标还是其他方面?” Nakul Mandan 作为 Lightspeed 风投机构的合 ...
- Awesome C/C++(图像部分)
GUI Graphic User Interface CEGUI - Flexible, cross-platform GUI library. FLTK - Fast, light, cross-p ...
- 最短JS判断IE6/IE7/IE8系列的写法
常用的 var isIE=!!window.ActiveXObject; var isIE6=isIE&&!window.XMLHttpRequest; var isIE8=isIE& ...
- U盘免疫
界面如下: 关键部分代码如下: void CImmunityUDlg::OnBnClickedButtonOk() { // TODO: 在此添加控件通知处理程序代码 TCHAR szPath[MAX ...