Nanjing H 19 regional tournament title (thinking)

Nanjing H 19 regional tournament title (thinking)

Jane book links chiefs of reference of this question:
https://www.jianshu.com/p/2f27315f10d8

Subject to the effect: the prince to marry the princess, but the queen wanted to stop. There are three groups of the palace, the princess led
in favor of the wedding, the wedding of the queen led disapproval, eat melon passers no concern. These three people
respectively, a, b, c a, and everyone is in a separate room, where all know, also
know someone else is. And Prince know the number three man. These people were to stay in their
rooms, the prince can ask the following three questions to the people in the room
1) Who are you
2) Who is in the room X
3) princess room in which
everyone will answer questions, but in favor of the wedding will provide the right answer, not in favor of
people would provide the wrong answer, the correct answer may passers Chigua possible errors. Now the king asked
the child to ask a few questions you can at least make sure you find the room where the princess, if not forever then
output NO.

1. Understand the implications of the question

(1) Prince will be inferred where the princess, so not in favor of people will answer wrong answers to disrupt Prince ideas by asking questions, so that the prince can not tell who is right and who is wrong.
(2) Chigua passers answer randomness. To ensure that the prince found the princess, then we must consider the worst case, that is equivalent to a person eat melon passers disapproval.

2. Points of discussion

Can be obtained from the above (2), we need to take melons and passersby who do not agree and as a class. So this time we put these two types of people collectively referred to as the bad guys
(1) if it is 100 (the case easy to overlook)
Prince know how many people in each class, this is only one person princess. and so. . . Also asked what, directly taken away ah (✧◡✧)
(2) if a = b + c
that is in favor of people = bad guys
first person in favor of an affirmative answer is the same , that is
to ask the Princess in which room they will answer y room, then we'll get a answer 1.
So this time, trouble squad on the line, hey hey hey 1 1 Ya Ya ◡
how to prevent it and he found the princess, only the bad guys unified answer a wrong answer, then we'll get (b + c) answer 2. In this case, a = (b + c) , the same number of two answers, then it is judged Prince out the true from false.
(3) if a> b + c
i.e. favor more people than bad.
First consider the worst case, that we are beginning to ask the bad guys. That is, we asked the (b + c) a bad man. At this point all the bad guys have asked over , people did not ask the rest of the people are in favor of. Then we ask (b + c) Personal (these are in favor of the people), and then the result of the case (2) are the same, we will get (b + c) answers 1 and (b + c) th answer 2 . At this time, just you need to ask a person, get (b + c + 1) and an answer (b + c) 2 answers . So the answer to number 1 is greater than the number two answer, the prince will be able to determine the answer to 1 is the correct answer, you can find a princess.
So we have to ask at least 2 * (b + c) +1 times.

Prince know that many people in favor and in favor of people will tell the truth, then the answer is most of the answers is correct.
(4) if a <b + c
that is smaller than those in favor bad.
Then b + c = x * a + y. That is split into x number of bad guys and the same number of people in favor of the collective, the individual may end up remaining y (0 <= y <a) . So long as this x collective answer different answers. Then the prince will definitely get the same number of wrong answers correct answer, then they go to the case (2), the prince can not judge the
prince said: I too difficult ** (; '་. ຶ Д་. ຶ `) * *

3. Code

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int a,b,c,d;
    cin>>a>>c>>d;
    b=c+d;
    if(a==1&&b==0)cout<<"YES"<<endl<<'0';
    else
    {
        if(a==b)cout<<"NO";
        else if(a>b)cout<<"YES"<<endl<<2*b+1;
        else cout<<"NO";
    }
    return 0;
}

Published 17 original articles · won praise 0 · Views 182

Guess you like

Origin blog.csdn.net/weixin_45719073/article/details/103334027