题目CodeForces 854B
Maxim Buys an Apartment
Time Limit:1000MS Memory Limit:524288KB 64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 854B
use MathJax to parse formulas
Description
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.
Sample Input
Input
6 3
Output
1 3
Hint
In the sample test, the number of good apartments could be minimum possible if, for example, apartments with indices 1, 2 and 3 were inhabited. In this case only apartment 4 is good. The maximum possible number could be, for example, if apartments with indices 1, 3 and 5 were inhabited. In this case all other apartments: 2, 4 and 6 are good.
题 意:一个人去买房子,在一套公寓中有很多房子,有一些已经住了人,所有的房子均在一条直线上(1~n),如果一个房子旁边有人住了,这栋房子即满足购买条件(他方便去拜访邻居,即他要买的房子旁的房子必须已经有人住),
马克西姆想在大都市的莱恩大街买一套新房子。这所房子有n套公寓,从1到n排列成行。如果指数相差1,两套公寓相邻。其中一些公寓已经有人居住,另一些则可以出售。
马克西姆经常去拜访他的邻居,所以如果有公寓出售,而且附近至少有一套已经有人居住的公寓,对他来说是件好事。马克西姆知道确实有k套已经有人居住的公寓,但他还不知道它们的指数。
输入的唯一一行包含两个整数:n和k(1≤n≤109,0≤k≤n)。
打印出适合Maxim的最小可能和最大可能的公寓数量。
纯思维题,注意边界条件,0 0,1 0,0 1,等特殊情况
自己做做就好……^ __ ^
代码仅供参考
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,k,minn=1,maxx;
cin>>n>>k;
if(k==0||n==k)
minn=0;
if(k>n/3)//不能有等号;
maxx=n-k;
else maxx=k*2;
/*
if(k<=n/3)//此时尽可能排最大,只能每三个有两个满足,因此maxx=k*2;
maxx=k*2;
else maxx=n-k
*/
cout<<minn<<" "<<maxx<<endl;//printf("1 %lld\n",ans);
return 0;
}