Codeforces Round #393 (Div. 2) - B. Frodo and pillows(模拟)

本文探讨了一个有趣的算法问题——Frodo与枕头问题。在有限的枕头条件下,如何合理分配给n个床位上的住客,确保每个住客至少有一个枕头,并且相邻床位的枕头数差距不超过1,同时使Frodo所在的床位枕头数最多。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. Frodo and pillows
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

n hobbits are planning to spend the night at Frodo's house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of course, it's not always possible to share pillows equally, but any hobbit gets hurt if he has at least two pillows less than some of his neighbors have. 

Frodo will sleep on the k-th bed in the row. What is the maximum number of pillows he can have so that every hobbit has at least one pillow, every pillow is given to some hobbit and no one is hurt?

Input

The only line contain three integers nm and k (1 ≤ n ≤ m ≤ 1091 ≤ k ≤ n) — the number of hobbits, the number of pillows and the number of Frodo's bed.

Output

Print single integer — the maximum number of pillows Frodo can have so that no one is hurt.

Examples
input
4 6 2
output
2
input
3 10 3
output
4
input
3 6 1
output
3
Note

In the first example Frodo can have at most two pillows. In this case, he can give two pillows to the hobbit on the first bed, and one pillow to each of the hobbits on the third and the fourth beds.

In the second example Frodo can take at most four pillows, giving three pillows to each of the others.

In the third example Frodo can take three pillows, giving two pillows to the hobbit in the middle and one pillow to the hobbit on the third bed.


题意:

给你n个床位,m个枕头。主人睡在第k个床。分配枕头,每个相邻的床位的枕头数差距不能大于1,给你m个枕头,让你让第k张床的枕头最多。

POINT:

先模拟一下阶梯。让床位1 2 3 4 5 k k-1 k-2 k-3……这样模拟下去。

如果这个数量小于等于m,那么在一起给每个床加一个枕头每次加n。加完就是答案。

如果大于m了,那么说明这个阶梯没那么规律。那么我们模拟跑一遍:

先所有床都给1个枕头。111111,然后给k床一个枕头,然后给k,k-1,k+1一个枕头。让平稳的平地一次次凸起来。

最后得到答案。

#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
#include <math.h>
#include <map>
using namespace std;
#define LL long long
LL a[100000];
int main()
{
    LL n,m,k;
    cin>>n>>m>>k;
    LL i;
    a[1]=0;
    for(i=1;i<=40000;i++){
        a[i]=a[i-1]+i;
    }
    LL num=max(k,n-k+1);
    if(num==k){
        k=n-k+1;
    }
    LL now;
    LL ans;
    if(k<40000&&num<40000){
            now=a[num]+(a[num-1]-a[num-k]);
        if(m>=now){
            ans=num+(m-now)/n;
            printf("%lld\n",ans);
            return 0;
        }
    }
    ans=1;
    LL cnt=1;
    LL yu=m-n;
    if(yu>=1) yu--,ans++;
    while(yu){
        LL l=k;
        LL r=n-k+1;
        LL ll=min(l-1,cnt);
        LL rr=min(cnt,r-1);
        if(ll+rr+1<=yu){
            yu-=ll+rr+1;
            cnt++;
            ans++;
        }else{
            break;
        }
    }
    printf("%lld\n",ans);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值