Codeforces 760B Frodo and pillows 【二分】

题目链接:http://codeforces.com/contest/760/problem/B

 

题目大意:

有 n 个床 m 个枕头,每个床上有个人,主人在 第 k 个床上。现在要求让每个人都有一个枕头,且相邻两个人之间的枕头数之差不应超过1。问主人最多能得到多少个枕头。

题解:

二分能得到的枕头数。

 

代码:

#include 
#include 
#include 

using namespace std;

int n, m, k;
int lft, rgt;

inline void quick_IO() { ios::sync_with_stdio(false); cout.tie(0); cin.tie(0); }

int main() {
	quick_IO();
	cin >> n >> m >> k;
	m -= n;

	int ans = 1;
	lft = rgt = k;
	while ( m ) {
		if( lft == 1 && rgt == n ) break;
		int tp = (rgt-lft+1);
		if( m >= tp ) {
			m -= tp;
			ans ++;
			rgt ++; lft -- ;
			rgt = min(n, rgt);
			lft = max(1, lft);
		} else break;
	}
	ans += (m/(rgt-lft+1));
	cout << ans << endl;

	return 0;
}

 

 

 

你可能感兴趣的:(【解题报告】,==贪心&分治&二分)