zoukankan      html  css  js  c++  java
  • Codeforces Round #316 (Div. 2) B 贪心

    B. Simple Game
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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.

    Examples
    Input
    3 1
    Output
    2
    Input
    4 3
    Output
    2
    Note

    In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0.

    In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less.

    题意:玩游戏  n代表游戏中数字的范围为1~n  m为Misha选择的数   输出一个数a使得Andrew最大概率的游戏获胜  当出现概率相同时 输出最小的a

             游戏规则如下, 对于1~n的任意的c     如果   则Andrew 获胜

    题解:a只能大于m 或者小于m  若a>m 则c取a~n Andrew 获胜  若a<m则c取1~a  Andrew 获胜 

    很容易想到 判断 m与 区间中点的关系(n/2)  无非就是取左还是取右  则相应的c=m-1 ,c=m+1   (与代码中n,m相反  ORZZZ

      特殊数据

     /*

     2  1

     2

     2  2

     1

     1  1

     1

    */

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<queue>
     5 #include<stack>
     6 #include<vector>
     7 #include<map>
     8 #include<algorithm>
     9 #define ll __int64
    10 #define mod 1e9+7
    11 #define PI acos(-1.0)
    12 using namespace std;
    13 int m,n;
    14 int mid;
    15 int main()
    16 {
    17     scanf("%d %d",&m,&n);
    18     if(m==2&&n==1)
    19     {
    20         cout<<"2"<<endl;
    21         return 0;
    22     }
    23     if(m<=2)
    24         {
    25             cout<<"1"<<endl;
    26             return 0;
    27         }
    28     if(m%2==0)
    29     {
    30         if(n<=(m/2))
    31             cout<<n+1<<endl;
    32         else
    33             cout<<n-1<<endl;
    34         return 0;
    35     }
    36     else
    37     {
    38         if(n<(m/2+1))
    39           cout<<n+1<<endl;
    40         else
    41             cout<<n-1<<endl;
    42         return 0;
    43     }
    44     return 0;
    45 }
  • 相关阅读:
    NPM (node package manager) 入门
    win10 环境 gitbash 显示中文乱码问题处理
    javascript中的Array对象 —— 数组的合并、转换、迭代、排序、堆栈
    Javascript 的执行环境(execution context)和作用域(scope)及垃圾回收
    Centos 下 mysql root 密码重置
    执行 $Gulp 时发生了什么 —— 基于 Gulp 的前端集成解决方案(二)
    Java I/O输入输出流详解
    反射---Java高级开发必须懂的
    细说Java多线程之内存可见性
    全面解析java注解
  • 原文地址:https://www.cnblogs.com/hsd-/p/5685328.html
Copyright © 2011-2022 走看看