1. 程式人生 > >浙大pat | 浙大pat 牛客網PAT頂級(Top Level)練習題 1003 博弈論

浙大pat | 浙大pat 牛客網PAT頂級(Top Level)練習題 1003 博弈論

You class are planning for a spring outing. N people are votingfor a

destination out of K candidate places.

 The voting progress isbelow:

 First the class vote forthe first candidate place. If more than half

of the class agreed on the place, the place is selected. Thevoting ends.

 Otherwise they vote forthe second candidate place. If more than half

of the class agreed on the place, the place is selected. Thevoting ends.

 Otherwise they vote forthe third candidate place in the same way and

go on.

 If no place is selectedat last there will be no spring outing and

everybody stays at home.

 Before the voting, theChief Entertainment Officer did a survey, found

out every one's preference which can be represented as apermutation of

0, 1, ... K. (0 is for staying at home.) For example, when K=3,

preference "1, 0, 2, 3" means that the first place ishis

first choice, staying at home is the second choice, the secondplace is

the third choice and the third place is the last choice.

 The Chief EntertainmentOfficer sends the survey results to the class.

So everybody knows the others' preferences. Everybody wants hismore

prefered place to be selected. And they are very smart, theyalways

choose the optimal strategy in the voting progress to achievehis goal.

 Can you predict whichplace will be selected?

輸入描述:

The first line contains two integers, N and K, the number ofpeople in your class and the number of candidate places.
The next N lines each contain a permutation of 0~K, representing someone'spreference.
For 40% of the data, 1 <= N, K <= 10
For 100% of the data, 1 <= N, K <= 1000

輸出描述:

Output the selected place. Or "otaku" without quotesif no place is selected.

In the sample case, if the second peoson vote against the first place, no placewould be selected finally because the first person must vote against the secondplace for his own interest. Considering staying at home is a worse choice thanthe first place, the second person's optimal strategy is voting for the firstplace. So the first place will be selected

輸入例子:

2 2
1 0 2
2 1 0

輸出例子:

1

這是一道博弈論方面的題目,不知道叫做什麼博弈,姑且叫他階段博弈吧。

這種博弈的特徵就是每一個都按照自己最大優勢的方式來選擇,並且每一個人都按照這種最大優勢的方式來選擇,每一個人都知道其他人會按照這種方式選擇,因此之後的所有可能都是可以預測的,每個人的選擇方式的內容是當所有人都按照最大優勢選擇的時候,這個人選擇所有預測結果中最好的一種,

說起來有點繞口,假如沒有相關經驗,我覺得是很難做出來的,這種博弈的特點就是從後往前推,最後一步的情況是可以預測的,選擇投票的必然是最後一個地點比家裡蹲的優先順序要高,選擇投反對票的必然是家裡蹲的優先順序比最後一個點的要高,然後可以得到倒數第二步的結果,這樣一直往前推,就能夠得到第一步到最後一步的結果,就能夠知道哪個地點最終被選中

這一題比較坑的地方就是題目中並沒有說有多少組資料,這個時候一般是預設題目中有一組資料,但是這樣就出錯了,這一題實際上是不停輸入,直到遇到EOF,所以以後在做這樣的題目的時候注意一下

#include<iostream>

#include<vector>

#include<unordered_map>

using namespacestd;

int main()

{

      //0 2 4 5 3 1

      int N, K,tmp;

      while (cin >> N >> K)

      {

            vector<unordered_map<int,int>> theLike(N);

            for (int i = 0; i < N; i++)

            {

                  for (int j = 0; j < K + 1;j++)

                  {

                       cin >> tmp;

                       theLike[i][tmp] = j;

                  }

            }

            int theWinNum = 0;

            for (int i = K; i >= 1; i--)

            {

                  int count = 0;

                  for (int j = 0; j < N; j++)

                  {

                       if (theLike[j][i] <theLike[j][theWinNum])count++;

                  }

                  if (count > N / 2)

                  {

                       theWinNum = i;

                  }

            }

            if (theWinNum > 0)

                  cout <<theWinNum<<endl;

            else

                  cout <<"otaku"<<endl;

      }

      return 0;

}