• SRM 402(1-250pt, 1-500pt)


    DIV1 250pt

    题意:对于任意一个由1~n组成的数列,其原始顺序为1,2,3..n。给出1~n的一个排列a[n],要通过swp操作将其变回原始顺序。当i < j且a[i] > a[j]时,可以进行swp操作,即swap(a[i], a[j])。问要将给定排列变回原始顺序,所需要做的swp操作的次数期望。 n <= 8。

    解法:概率dp。能用概率dp的原因是i < j 且 a[i] > a[j]时才能进行swp操作,这样就保证了不会出现环。

       这道题用递归的写法比较好写,但是用递归的同时一定要注意记忆化,记忆化以后时间复杂度就是O(8!)。否则会超时。

    tag:概率dp

      1 // BEGIN CUT HERE
      2 /*
      3  * Author:  plum rain
      4  * score :
      5  */
      6 /*
      7 
      8  */
      9 // END CUT HERE
     10 #line 11 "RandomSort.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 CLR(x) memset(x, 0, sizeof(x))
     38 #define CLR1(x) memset(x, -1, sizeof(x))
     39 #define PB push_back
     40 #define SZ(v) ((int)(v).size())
     41 #define zero(x) (((x)>0?(x):-(x))<eps)
     42 #define out(x) cout<<#x<<":"<<(x)<<endl
     43 #define tst(a) cout<<#a<<endl
     44 #define CINBEQUICKER std::ios::sync_with_stdio(false)
     45 
     46 typedef vector<int> VI;
     47 typedef vector<string> VS;
     48 typedef vector<double> VD;
     49 typedef pair<int, int> pii;
     50 typedef long long int64;
     51 
     52 const double eps = 1e-8;
     53 const double PI = atan(1.0)*4;
     54 const int maxint = 2139062143;
     55 
     56 map<VI, double> mp;
     57 int a[20], sz;
     58 
     59 bool ok()
     60 {
     61     for (int i = 0; i < sz; ++ i)
     62         if (a[i] != i+1) return 0;
     63     return 1;
     64 }
     65 
     66 double dfs()
     67 {
     68     if (ok()) return 0;
     69     VI tmp; tmp.clear();
     70     for (int i = 0; i < sz; ++ i)
     71         tmp.PB (a[i]);
     72     if (mp.count(tmp)) return mp[tmp];
     73 
     74     int num = 0;
     75     for (int i = 0; i < sz; ++ i)
     76         for (int j = i+1; j < sz; ++ j)
     77             if (a[i] > a[j]) ++ num;
     78 
     79     double ret = 0;
     80     for (int i = 0; i < sz; ++ i)
     81         for (int j = i+1; j < sz; ++ j){
     82             if (a[i] < a[j]) continue; 
     83 
     84             swap(a[i], a[j]);
     85             ret += (dfs()+1) / num;
     86             swap(a[i], a[j]);
     87         }
     88     mp[tmp] = ret;
     89     return ret;
     90 }
     91 
     92 class RandomSort
     93 {
     94     public:
     95         double getExpected(vector <int> p){
     96             sz = p.size();
     97             mp.clear();
     98             for (int i = 0; i < sz; ++ i)
     99                 a[i] = p[i];
    100             return dfs();
    101         }
    102         
    103 // BEGIN CUT HERE
    104     public:
    105     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(); }
    106     private:
    107     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(); }
    108     void verify_case(int Case, const double &Expected, const double &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "	Expected: "" << Expected << '"' << endl; cerr << "	Received: "" << Received << '"' << endl; } }
    109     void test_case_0() { int Arr0[] = {7, 2, 5, 4, 1, 3, 8, 6}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); double Arg1 = 1.0; verify_case(0, Arg1, getExpected(Arg0)); }
    110     void test_case_1() { int Arr0[] = {4,3,2,1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); double Arg1 = 4.066666666666666; verify_case(1, Arg1, getExpected(Arg0)); }
    111     void test_case_2() { int Arr0[] = {1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); double Arg1 = 0.0; verify_case(2, Arg1, getExpected(Arg0)); }
    112     void test_case_3() { int Arr0[] = {2,5,1,6,3,4}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); double Arg1 = 5.666666666666666; verify_case(3, Arg1, getExpected(Arg0)); }
    113 
    114 // END CUT HERE
    115 
    116 };
    117 
    118 // BEGIN CUT HERE
    119 int main()
    120 {
    121 //    freopen( "a.out" , "w" , stdout );    
    122     RandomSort ___test;
    123     ___test.run_test(-1);
    124        return 0;
    125 }
    126 // END CUT HERE
    View Code

    DIV1 450pt

    题意:对于一个由.和X组成的环形字符串(即该字符串首尾相接),连续的X(或者1个)组成blocks,连续的.(或者1个)组成gaps。比如..X.XX..由1个长度为4的gap(因为首尾相接),1个长度为1的gap,1个长度为1的block,1个长度为2的block组成。定义gap array an[]为将所有gap的长度放到an[]中,从大到小做一个排序得到的数组即为gap array。现在,去掉某一个block,要使得到的an字典序最大,应该去掉哪个block?返回该block中下标最小的点的值,比如XX...X....XX.X,去掉长度为3的block,返回下标1,若去掉长度为2的block,返回10。若去掉某两个block后gap array相同,则返回下标最小值小的那一个。

       字符串长度 <= 2500。

    解法:题意好复杂...

       还好这道题是可以暴力过掉的.....n*n*logn的复杂度能接受。但是一方面由于我编码能力太差,另一方面由于我对STL很多函数的不熟悉,导致我没有暴力出来......忧伤...看了官方题解提供的代码。点击打开

       当然,这道题除了能暴力直接做,也是有线性解法的。若要比较去掉某两个block后生成的gap array的大小,设与第一个block相邻的两个gap长度分别为a,b,与第二个block相邻的gao长度分别为c,d,则只需要比较a+b和c+d,max(a,b)和max(c,d),min(a,b)和min(c,d)即可。

       至于这个的原因嘛,想一下,很简单的- -....

    tag:think

      1 // BEGIN CUT HERE
      2 /*
      3  * Author:  plum rain
      4  * score :
      5  */
      6 /*
      7 
      8  */
      9 // END CUT HERE
     10 #line 11 "LargestGap.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 CLR(x) memset(x, 0, sizeof(x))
     38 #define CLR1(x) memset(x, -1, sizeof(x))
     39 #define PB push_back
     40 #define SZ(v) ((int)(v).size())
     41 #define zero(x) (((x)>0?(x):-(x))<eps)
     42 #define out(x) cout<<#x<<":"<<(x)<<endl
     43 #define tst(a) cout<<#a<<endl
     44 #define CINBEQUICKER std::ios::sync_with_stdio(false)
     45 
     46 typedef vector<int> VI;
     47 typedef vector<string> VS;
     48 typedef vector<double> VD;
     49 typedef pair<int, int> pii;
     50 typedef long long int64;
     51 
     52 const double eps = 1e-8;
     53 const double PI = atan(1.0)*4;
     54 const int maxint = 2139062143;
     55 
     56 class LargestGap
     57 {
     58     public:
     59         int getLargest(vector <string> b){
     60             VI best, cur; int pos;
     61             string s = accumulate(b.begin(), b.end(), string(""));
     62             for (int i = 0; i < (int)s.size(); ++ i) if (s[i] == 'X'){
     63                 string s2 = s.substr(i) + s.substr(0,i) + "X";
     64                 cur.clear();
     65                 int len = 0;
     66                 for (int j = 0; j < (int)s2.size(); ++ j){
     67                     if (s2[j] == '.') ++ len;
     68                     else if (len) cur.PB (len), len = 0;
     69                 }
     70                 if (cur.size() > 1) cur[0] += cur.back(), cur.pop_back();
     71                 sort(cur.begin(), cur.end(), greater<int>());
     72                 if (cur > best) best = cur, pos = i;
     73             }
     74             return pos;
     75         }
     76         
     77 // BEGIN CUT HERE
     78     public:
     79     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();}
     80     private:
     81     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(); }
     82     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; } }
     83     //void test_case_0() { string Arr0[] = {; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 5; verify_case(0, Arg1, getLargest(Arg0)); }
     84     void test_case_0() { string Arr0[] = {"XXXX","....","XXXX","....","XXXX","...."}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 0; verify_case(1, Arg1, getLargest(Arg0)); }
     85     void test_case_1() { string Arr0[] = {"XXX.........XX...........XX..X"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 12; verify_case(2, Arg1, getLargest(Arg0)); }
     86     void test_case_2() { string Arr0[] = {"XXX","X.....","....XX..XXXXXX","X........X..",".XXX."}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 32; verify_case(3, Arg1, getLargest(Arg0)); }
     87 
     88 // END CUT HERE
     89 
     90 };
     91 
     92 // BEGIN CUT HERE
     93 int main()
     94 {
     95 //    freopen( "a.out" , "w" , stdout );    
     96     LargestGap ___test;
     97     ___test.run_test(-1);
     98        return 0;
     99 }
    100 // END CUT HERE
    View Code

       

    ------------------------------------------------------------------
    现在的你,在干什么呢?
    你是不是还记得,你说你想成为岩哥那样的人。
  • 相关阅读:
    6000?
    基本把“主页大巴”做完了
    myDesk
    挂出了
    如何在网页中添加flash文件
    oracle的SQl语句
    打开PHP 提示下载
    实时计字数提醒的文本框
    wampserver无法打开http://localhost
    “ASP.adduser_aspx.GetTypeHashCode()”: 没有找到适合的方法来重写
  • 原文地址:https://www.cnblogs.com/plumrain/p/srm_402.html
Copyright © 2020-2023  润新知