Click here ~ ~
B. Simple Game***one DayMisha andAndrew were playing a very simple game. First, each player chooses aninteger inch theRange from 1 toN. Let ' s assume thatMisha chose NumberM andAndrew chose NumberA.then, byUsing a random generator they choose a randomintegerCinch theRangebetween 1 andN (Anyinteger from 1 toN isChosen with theSame probability), Afterwhich theWinner is thePlayerwhose Numberwas closer toC. The boys agreed that ifM andA is located on theSame distance fromC, Misha wins. Andrew wants toWin very much, so he asks you toHelp him. You know the NumberSelected byMisha, and NumberN. You need toDetermine which value ofA Andrew must choose, so that theProbability ofHis victory is theHighest possible. More formally, you need toFind suchintegerA1? ≤?a?≤?n), that theProbability that isMaximal,whereC is theEquiprobably Choseninteger from 1 ton (inclusive). Inputthe FirstLinecontainsintegers n andM (1? ≤?m?≤?n?≤?109) — theRange ofNumbersinch theGame and the NumberSelected byMisha respectively. Outputprint a single Number-such value A, thatProbability thatAndrew wins is theHighest. If There is multiple such values, print theMinimum ofthem. Sample Test (s) input3 1Output2Input4 3Output2
Problem-solving ideas: is to see the relationship between M and the middle number, as detailed in the code:
/*date:2015-8-26 Night Author:itakmotto: Today I want to go beyond yesterday's me, tomorrow I will surpass today's me, to create better code as the goal, constantly surpass oneself. */#include <iostream>using namespace STD;intMain () {intN, M; while(Cin>>N>>M) {intstart = M1;intEnd = m+1;if(m==1&& n==1)cout<<1<<endl;Else{if(M > n/2)cout<<start<<endl;Else cout<<end<<endl; } }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Codeforces 570 B.simple Game