zoukankan      html  css  js  c++  java
  • SRM 393(1-250pt)

    题意:有m个人投票,每个人在心里对所有候选者排了一个序,比如“210”,则他最想投2号,如果2号已经出局他会投1号,最后投0号,否则弃权不投。选举时进行多轮投票,知道选出winner或者所有人均出局。每轮投票以后,得票最高者所得票数如果严格大于该轮投票人数的50%,则他成为winner,游戏结束;若不大于50%,则将得票最低的人淘汰,如果有多人得票相同且最低,则一起淘汰出局,然后进行下一轮投票。

    解法:纯模拟。不过题意很不清晰,有两个地方我都弄了半天猜弄懂。。。就当学习官方题解的代码了。

    tag:simulation

      1 // BEGIN CUT HERE
      2 /*
      3  * Author:  plum rain
      4  * score :
      5  */
      6 /*
      7 
      8  */
      9 // END CUT HERE
     10 #line 11 "InstantRunoffVoting.cpp"
     11 #include <sstream>
     12 #include <stdexcept>
     13 #include <functional>
     14 #include <iomanip>
     15 #include <numeric>
     16 #include <fstream>
     17 #include <cctype>
     18 #include <iostream>
     19 #include <cstdio>
     20 #include <vector>
     21 #include <cstring>
     22 #include <cmath>
     23 #include <algorithm>
     24 #include <cstdlib>
     25 #include <set>
     26 #include <queue>
     27 #include <bitset>
     28 #include <list>
     29 #include <string>
     30 #include <utility>
     31 #include <map>
     32 #include <ctime>
     33 #include <stack>
     34 
     35 using namespace std;
     36 
     37 #define clr0(x) memset(x, 0, sizeof(x))
     38 #define clr1(x) memset(x, -1, sizeof(x))
     39 #define pb push_back
     40 #define mp make_pair
     41 #define sz(v) ((int)(v).size())
     42 #define all(t) t.begin(),t.end()
     43 #define zero(x) (((x)>0?(x):-(x))<eps)
     44 #define out(x) cout<<#x<<":"<<(x)<<endl
     45 #define tst(a) cout<<a<<" "
     46 #define tst1(a) cout<<#a<<endl
     47 #define CINBEQUICKER std::ios::sync_with_stdio(false)
     48 
     49 typedef vector<int> VI;
     50 typedef vector<string> VS;
     51 typedef vector<double> VD;
     52 typedef pair<int, int> pii;
     53 typedef long long int64;
     54 
     55 const double eps = 1e-8;
     56 const double PI = atan(1.0)*4;
     57 const int inf = 2139062143 / 2;
     58 
     59 bool vis[20];
     60 int idx[50], num[20];
     61 pii an[50];
     62 
     63 bool cmp(pii a, pii b)
     64 {
     65     return a.second < b.second;
     66 }
     67 
     68 class InstantRunoffVoting
     69 {
     70     public:
     71         int winner(vector <string> v){
     72             clr0 (vis); clr0 (idx);
     73             int n = sz(v), m = sz(v[0]); 
     74             int cnt = 0;
     75             while (cnt < m){
     76                 int tmp = n;
     77                 clr0 (num);
     78                 for (int i = 0; i < n; ++ i){
     79                     while (idx[i] < m && vis[v[i][idx[i]] - '0']) ++ idx[i];
     80                     if (idx[i] >= m){ 
     81                         -- tmp; continue;
     82                     }
     83                     ++ num[v[i][idx[i]] - '0'];
     84                 }
     85                 int all = 0;
     86                 for (int i = 0; i < m; ++ i)
     87                     if(!vis[i]) an[all++] = mp(i, num[i]);
     88                 sort(an, an+all, cmp);
     89                 if (an[all-1].second * 2 > tmp) return an[all-1].first;
     90 
     91                 for (int i = 0; i < all; ++ i){
     92                     if (an[i].second != an[0].second) break;
     93                     ++ cnt; vis[an[i].first] = 1;
     94                 }
     95             }
     96             return -1;
     97         }
     98         
     99 // BEGIN CUT HERE
    100     public:
    101     void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); }
    102     private:
    103     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(); }
    104     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 << "	Expected: "" << Expected << '"' << endl; cerr << "	Received: "" << Received << '"' << endl; } }
    105     void test_case_0() { string Arr0[] = {"01","10","01","01","10"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 0; verify_case(0, Arg1, winner(Arg0)); }
    106     void test_case_1() { string Arr0[] = {"120","102","210","021","012"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; verify_case(1, Arg1, winner(Arg0)); }
    107     void test_case_2() { string Arr0[] = {"10","01"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = -1; verify_case(2, Arg1, winner(Arg0)); }
    108     void test_case_3() { string Arr0[] = {"3120","3012","1032"
    109 ,"3120","2031","2103"
    110 ,"1230","1230"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = -1; verify_case(3, Arg1, winner(Arg0)); }
    111     void test_case_4() { string Arr0[] = {"24103","30412","32014","21043","30412"
    112 ,"32401","14203","04123","30241","02413"
    113 ,"13042","01432","01342","32401","24301"
    114 ,"12430","41023","02413","42310","12043"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; verify_case(4, Arg1, winner(Arg0)); }
    115     void test_case_5() { string Arr0[] = {"0649853172","2146875039","2671548309"
    116 ,"5897216403","4719823056","7945213860"
    117 ,"9021538647","9286145307","9176403528"
    118 ,"3709486152"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 9; verify_case(5, Arg1, winner(Arg0)); }
    119 
    120 // END CUT HERE
    121 
    122 };
    123 
    124 // BEGIN CUT HERE
    125 int main()
    126 {
    127 //    freopen( "a.out" , "w" , stdout );    
    128     InstantRunoffVoting ___test;
    129     ___test.run_test(-1);
    130        return 0;
    131 }
    132 // END CUT HERE
    View Code
  • 相关阅读:
    Python socket 基础(Server)
    Python socket 基础(Client)
    DOM
    Software Testing Concepts
    coroutine
    这一周~&&html+css的学习感悟
    充实的几天~
    时间不够用的感觉
    论文真痛苦
    焦躁的一周……
  • 原文地址:https://www.cnblogs.com/plumrain/p/srm_393.html
Copyright © 2011-2022 走看看