SDNU_ACM_ICPC_2020_Winter_Practice_1st B

题目

Maxim wants to buy an apartment in a new house at Line Avenue of Metropolis. The house has n apartments that are numbered from 1 to n and are arranged in a row. Two apartments are adjacent if their indices differ by 1. Some of the apartments can already be inhabited, others are available for sale.

Maxim often visits his neighbors, so apartment is good for him if it is available for sale and there is at least one already inhabited apartment adjacent to it. Maxim knows that there are exactly k already inhabited apartments, but he doesn’t know their indices yet.

Find out what could be the minimum possible and the maximum possible number of apartments that are good for Maxim.

Input
The only line of the input contains two integers: n and k (1 ≤ n ≤ 109, 0 ≤ k ≤ n).

Output
Print the minimum possible and the maximum possible number of apartments good for Maxim.

Example
Input
6 3
Output
1 3

大意:
有n套房子,k套已经有人居住,这n套房子编号为1~n。
Maxim想卖出空房子,但是空房子旁边至少有一户已经居住才能卖出。求他能卖出的最少和最大房子数。

思路

注意条件:空房子旁边至少有一户已经居住才能卖出。
(1)若n=k,即无空房子时, 输出 0 0
(2)若k=0,即无人居住时,受条件限制,则卖不出去。
输出 0 0
(3)排除以上两种情况后
要想是卖出数最少,这些房子需连在一起,即所有已入住的房子为1~k这k栋,那么只能卖出第k+1这一栋房子。

要想卖出数最少,就要把入住房子的利用最大化,入住房子两边的房子均可卖出,那么三个为一组。
在这里插入图片描述
如图,三个为一组,对勾为入住房子,其两边的房子均可卖出。

AC代码

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int n,k,mmin,mmax;
    cin>>n>>k;
    if(n==k){cout<<'0'<<" "<<'0';return 0;}
    if(k==0){cout<<'0'<<" "<<'0';return 0;}
    int a=n/3;
        if(k<=a){mmin=1;mmax=k*2;}
        else
        {
            mmin=1;
            mmax=n-k;
        }
    cout<<mmin<<" "<<mmax;
    return 0;
}
发布了46 篇原创文章 · 获赞 0 · 访问量 1162

猜你喜欢

转载自blog.csdn.net/weixin_45719073/article/details/104069175