B - Simple Game
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 570B
Description
One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let‘s assume that Misha chose number m, and Andrew chose number a.
Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins.
Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible.
More formally, you need to find such integer a (1 ≤ a ≤ n), that the probability that is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive).
Input
The first line contains two integers n and m (1 ≤ m ≤ n ≤ 109) — the range of numbers in the game, and the number selected by Misha respectively.
Output
Print a single number — such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them.
Sample Input
Input
3 1Output
2Input
4 3Output
2
这道题没有涉及什么算法问题,题意就是Misha和Andrew宝宝玩猜数字游戏,总共有n(1~n)个数字,Misha给出的数字记为m,Andrew给出的数字记为a,随机取(1~n)中的一个数,所取数值距此数最近的人获胜。现已知m和n,问a给多少才能使Andrew获胜的几率最大。(如果有两个或多个点获胜的几率都是最大,取数值最小的点)对了,如果两个人距离相等则Misha胜,毕竟女孩子~
先找出n个数的中点,看m在哪边,先分点的个数是奇是偶,再判断m是否在中点上,分别判断。
比较朴素的思路,AC代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 int main() 6 { 7 int n,m,mid,ans; 8 scanf("%d %d",&n,&m); 9 if(n%2==0) {//n为偶数时点的个数为偶数,中点分为两个,当m取任意一个时,a取另一个 10 mid=n/2; 11 if(m<=mid) ans=m+1; 12 else ans=m-1; 13 } 14 else { 15 mid=n/2+1; //n/2取较小的整数值 16 if(m<mid) ans=m+1; 17 else ans=m-1; 18 } 19 if(ans==0) ans=1; 20 printf("%d\n",ans); 21 return 0; 22 }