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 属性变更通知类的实现
原文:WPF 属性变更通知类的实现 平时用依赖属性多一些,普通属性的变更通知知道有这个方法,但是老是忘记名字,再写一遍吧. public class Student : INotifyProperty ...
- LeapMotion Demo2
原文:LeapMotion Demo2 官方doc有四个手势,最近尝试实现对握拳的识别,并能在我的程序界面上体现出来. 调试过程较为繁琐,幸好最终效果还差强人意! 首先看看我的效果图: ...
- EF 导航属性的使用
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- Ogre 1.7.0,VS2005编译全过程傻瓜式教程
最近下了最新版Ogre 1.7.0,从下载到最后编译运行成功Ogre自带的Sample花了将近一下午时间. 网上有很多编译Ogre的教程,这里整理我看过的教程,加上自己的经验再详细总结一遍. 第一步: ...
- 设置InputBox等提示框的字体以及样式
InputBox等窗体的字体大小设置方法 Graphics.DefFontData.Height:=48; Graphics.DefFontData.Style:=[fsBold,fsItalic, ...
- win10应用程序添加到开机启动项的两种解决办法
原文 win10应用程序添加到开机启动项的两种解决办法 在windows10系统中,如果想让应用程序在开机之后自动运行起来,可以怎么做呢? 方法一: 1.首先创建应用程序的快捷方式 找到自己想加入开机 ...
- Win8Metro(C#)数字图像处理--2.8图像线性变换
原文:Win8Metro(C#)数字图像处理--2.8图像线性变换 2.8图像线性变换 [函数名称] 图像线性变换函数LinearTransformProcess(WriteableBitmap ...
- Markdown 入门
一. Markdown语法的简要规则 标题 标题是非常重要的一个标记,一段文字标记为标题,只需要在文字前加 #.具体可以支持到1到6个# 1 2 3 4 # 一级标题 ## 二级标题 ### 三级标题 ...
- delphi xe5 中TMemo控件的应用——for android
TMemo中的两个方法: TMemo.Lines.Add(stringxxx);意思是向TMemo中增加字符串stringxxx: TMemo.Lines.Text :=stringxxx,意思是清空 ...
- 预处理器#include 指令
预处理器发现 #include 指令后,就会寻找后跟的文件名并把这个文件的内容包含到当前文件中.被包含文件中的文本将替换源代码文件中的#include指令,就像你把被包含文件中的全部内容键入到源文件中 ...