Codeforces 570 B.simple Game

Source: Internet
Author: User

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&GT;&GT;N&GT;&GT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.