题意:有m个人投票,每个人在心里对所有候选者排了一个序,比如“210”,则他最想投2号,如果2号已经出局他会投1号,最后投0号,否则弃权不投。选举时进行多轮投票,知道选出winner或者所有人均出局。每轮投票以后,得票最高者所得票数如果严格大于该轮投票人数的50%,则他成为winner,游戏结束;若不大于50%,则将得票最低的人淘汰,如果有多人得票相同且最低,则一起淘汰出局,然后进行下一轮投票。
解法:纯模拟。不过题意很不清晰,有两个地方我都弄了半天猜弄懂。。。就当学习官方题解的代码了。
tag:simulation
// BEGIN CUT HERE
/*
* Author: plum rain
* score :
*/
/* */
// END CUT HERE
#line 11 "InstantRunoffVoting.cpp"
#include <sstream>
#include <stdexcept>
#include <functional>
#include <iomanip>
#include <numeric>
#include <fstream>
#include <cctype>
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <set>
#include <queue>
#include <bitset>
#include <list>
#include <string>
#include <utility>
#include <map>
#include <ctime>
#include <stack> using namespace std; #define clr0(x) memset(x, 0, sizeof(x))
#define clr1(x) memset(x, -1, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz(v) ((int)(v).size())
#define all(t) t.begin(),t.end()
#define zero(x) (((x)>0?(x):-(x))<eps)
#define out(x) cout<<#x<<":"<<(x)<<endl
#define tst(a) cout<<a<<" "
#define tst1(a) cout<<#a<<endl
#define CINBEQUICKER std::ios::sync_with_stdio(false) typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef pair<int, int> pii;
typedef long long int64; const double eps = 1e-;
const double PI = atan(1.0)*;
const int inf = / ; bool vis[];
int idx[], num[];
pii an[]; bool cmp(pii a, pii b)
{
return a.second < b.second;
} class InstantRunoffVoting
{
public:
int winner(vector <string> v){
clr0 (vis); clr0 (idx);
int n = sz(v), m = sz(v[]);
int cnt = ;
while (cnt < m){
int tmp = n;
clr0 (num);
for (int i = ; i < n; ++ i){
while (idx[i] < m && vis[v[i][idx[i]] - '']) ++ idx[i];
if (idx[i] >= m){
-- tmp; continue;
}
++ num[v[i][idx[i]] - ''];
}
int all = ;
for (int i = ; i < m; ++ i)
if(!vis[i]) an[all++] = mp(i, num[i]);
sort(an, an+all, cmp);
if (an[all-].second * > tmp) return an[all-].first; for (int i = ; i < all; ++ i){
if (an[i].second != an[].second) break;
++ cnt; vis[an[i].first] = ;
}
}
return -;
} // BEGIN CUT HERE
public:
void run_test(int Case) { if ((Case == -) || (Case == )) test_case_0(); if ((Case == -) || (Case == )) test_case_1(); if ((Case == -) || (Case == )) test_case_2(); if ((Case == -) || (Case == )) test_case_3(); if ((Case == -) || (Case == )) test_case_4(); if ((Case == -) || (Case == )) test_case_5(); }
private:
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
void test_case_0() { string Arr0[] = {"","","","",""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, winner(Arg0)); }
void test_case_1() { string Arr0[] = {"","","","",""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, winner(Arg0)); }
void test_case_2() { string Arr0[] = {"",""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = -; verify_case(, Arg1, winner(Arg0)); }
void test_case_3() { string Arr0[] = {"","",""
,"","",""
,"",""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = -; verify_case(, Arg1, winner(Arg0)); }
void test_case_4() { string Arr0[] = {"","","","",""
,"","","","",""
,"","","","",""
,"","","","",""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, winner(Arg0)); }
void test_case_5() { string Arr0[] = {"","",""
,"","",""
,"","",""
,""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, winner(Arg0)); } // END CUT HERE }; // BEGIN CUT HERE
int main()
{
// freopen( "a.out" , "w" , stdout );
InstantRunoffVoting ___test;
___test.run_test(-);
return ;
}
// END CUT HERE