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

时间:2022-04-19 08:29:27

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

 // BEGIN CUT HERE
/*
* Author: plum rain
* score :
*/
/* */
// END CUT HERE
#line 11 "RandomSort.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 CLR(x) memset(x, 0, sizeof(x))
#define CLR1(x) memset(x, -1, sizeof(x))
#define PB push_back
#define SZ(v) ((int)(v).size())
#define zero(x) (((x)>0?(x):-(x))<eps)
#define out(x) cout<<#x<<":"<<(x)<<endl
#define tst(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 maxint = ; map<VI, double> mp;
int a[], sz; bool ok()
{
for (int i = ; i < sz; ++ i)
if (a[i] != i+) return ;
return ;
} double dfs()
{
if (ok()) return ;
VI tmp; tmp.clear();
for (int i = ; i < sz; ++ i)
tmp.PB (a[i]);
if (mp.count(tmp)) return mp[tmp]; int num = ;
for (int i = ; i < sz; ++ i)
for (int j = i+; j < sz; ++ j)
if (a[i] > a[j]) ++ num; double ret = ;
for (int i = ; i < sz; ++ i)
for (int j = i+; j < sz; ++ j){
if (a[i] < a[j]) continue; swap(a[i], a[j]);
ret += (dfs()+) / num;
swap(a[i], a[j]);
}
mp[tmp] = ret;
return ret;
} class RandomSort
{
public:
double getExpected(vector <int> p){
sz = p.size();
mp.clear();
for (int i = ; i < sz; ++ i)
a[i] = p[i];
return dfs();
} // 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(); }
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 double &Expected, const double &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() { int Arr0[] = {, , , , , , , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); double Arg1 = 1.0; verify_case(, Arg1, getExpected(Arg0)); }
void test_case_1() { int Arr0[] = {,,,}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); double Arg1 = 4.066666666666666; verify_case(, Arg1, getExpected(Arg0)); }
void test_case_2() { int Arr0[] = {}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); double Arg1 = 0.0; verify_case(, Arg1, getExpected(Arg0)); }
void test_case_3() { int Arr0[] = {,,,,,}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); double Arg1 = 5.666666666666666; verify_case(, Arg1, getExpected(Arg0)); } // END CUT HERE }; // BEGIN CUT HERE
int main()
{
// freopen( "a.out" , "w" , stdout );
RandomSort ___test;
___test.run_test(-);
return ;
}
// END CUT HERE

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

 // BEGIN CUT HERE
/*
* Author: plum rain
* score :
*/
/* */
// END CUT HERE
#line 11 "LargestGap.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 CLR(x) memset(x, 0, sizeof(x))
#define CLR1(x) memset(x, -1, sizeof(x))
#define PB push_back
#define SZ(v) ((int)(v).size())
#define zero(x) (((x)>0?(x):-(x))<eps)
#define out(x) cout<<#x<<":"<<(x)<<endl
#define tst(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 maxint = ; class LargestGap
{
public:
int getLargest(vector <string> b){
VI best, cur; int pos;
string s = accumulate(b.begin(), b.end(), string(""));
for (int i = ; i < (int)s.size(); ++ i) if (s[i] == 'X'){
string s2 = s.substr(i) + s.substr(,i) + "X";
cur.clear();
int len = ;
for (int j = ; j < (int)s2.size(); ++ j){
if (s2[j] == '.') ++ len;
else if (len) cur.PB (len), len = ;
}
if (cur.size() > ) cur[] += cur.back(), cur.pop_back();
sort(cur.begin(), cur.end(), greater<int>());
if (cur > best) best = cur, pos = i;
}
return pos;
} // 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();}
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[0]))); int Arg1 = 5; verify_case(0, Arg1, getLargest(Arg0)); }
void test_case_0() { string Arr0[] = {"XXXX","....","XXXX","....","XXXX","...."}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, getLargest(Arg0)); }
void test_case_1() { string Arr0[] = {"XXX.........XX...........XX..X"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, getLargest(Arg0)); }
void test_case_2() { string Arr0[] = {"XXX","X.....","....XX..XXXXXX","X........X..",".XXX."}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, getLargest(Arg0)); } // END CUT HERE }; // BEGIN CUT HERE
int main()
{
// freopen( "a.out" , "w" , stdout );
LargestGap ___test;
___test.run_test(-);
return ;
}
// END CUT HERE