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
- // 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
SRM 402(1-250pt, 1-500pt)的更多相关文章
- SRM475 - SRM479(1-250pt,500pt)
SRM 475 DIV1 300pt 题意:玩游戏.给一个棋盘,它有1×n(1行n列,每列标号分别为0,1,2..n-1)的格子,每个格子里面可以放一个棋子,并且给定一个只含三个字母WBR,长度为n的 ...
- SRM468 - SRM469(1-250pt, 500pt)
SRM 468 DIV1 250pt 题意:给出字典,按照一定要求进行查找. 解法:模拟题,暴力即可. tag:water score: 0.... 这是第一次AC的代码: /* * Author: ...
- SRM470 - SRM474(1-250pt,500pt)(471-500pt为最短路,474-500pt未做)
SRM 470 DIV1 250pt 题意:有n个房间排成一排,相邻两个房间之间有一扇关闭着的门(共n-1扇),每个门上都标有‘A’-‘P’的大写字母.给定一个数n,表示第n个房间.有两个人John和 ...
- SRM593(1-250pt,500pt)
SRM 593 DIV1 250pt 题意:有如下图所示的平面,每个六边形有坐标.将其中一些六边形染色,要求有边相邻的两个六边形不能染同一种颜色.给定哪些六边形需要染色,问最少需要多少种颜色. 解法: ...
- topcoder srm 553
div1 250pt: 题意:... 解法:先假设空出来的位置是0,然后模拟一次看看是不是满足,如果不行的话,我们只需要关心最后栈顶的元素取值是不是受空白处的影响,于是还是模拟一下. // BEGIN ...
- topcoder srm 552
div1 250pt: 题意:用RGB三种颜色的球摆N层的三角形,要求相邻的不同色,给出RGB的数量,问最多能摆几个 解法:三种颜色的数量要么是全一样,要么是两个一样,另外一个比他们多一个,于是可以分 ...
- topcoder srm 551
div1 250pt 题意:一个长度最多50的字符串,每次操作可以交换相邻的两个字符,问,经过最多MaxSwaps次交换之后,最多能让多少个相同的字符连起来 解法:对于每种字符,枚举一个“集结点”,让 ...
- topcoder srm 550
div1 250pt: 题意:有个机器人,从某一点出发,他只有碰到地形边缘或者碰到走过的点时才会改变运动方向,然后接着走,现在给出他的运动轨迹,判断他的运动是否合法,如果合法的话,那么整个地形的最小面 ...
- topcoder srm 610
div1 250pt: 题意:100*100的01矩阵,找出来面积最大的“类似国际象棋棋盘”的子矩阵. 解法:枚举矩阵宽(水平方向)的起点和终点,然后利用尺取法来找到每个固定宽度下的最大矩阵,不断更新 ...
随机推荐
- tableView尾部多处一部分空白高度
问题出现的原因:创建tableView时使用的style是UITableViewStylePlain 解决办法: 在创建tableView时,self.automaticallyAdjustsScro ...
- asp.net尽量不在js里写<%%>
asp.net尽量不在js里写<%%> eg: <script type="text/javascript"> var rootsid="&quo ...
- Oracle通过主键id删除记录很慢
问题描述: Oracle通过主键id删除2000条记录很慢,需要花费十二分钟. 解决过程: 1.首先查看SQL的执行计划,执行计划正常,cost只有4,用到了主键索引. 2.查看等待事件, selec ...
- java 全角、半角字符串转换
转自:http://www.cnblogs.com/modou/articles/2679815.html 加入了空字符串的验证 半角转全角的方法: /** * @Title: ToSBC * ...
- ubuntu install opengrok
总结: 1. 安装jdk和tomcat 2. 安装ctags 3. 解压opengrok.tar.gz包, 然后将source.war复制到tomcat/webapp下面 sudo cp -R ope ...
- OpenGrok的安装
http://opengrok.github.io/OpenGrok/ Ubuntu环境下OpenGrok的安装及使用 http://www.linuxidc.com/Linux/2013-05/84 ...
- Android 中文 API (29) —— CompoundButton
前言 本章内容是android.widget.CompoundButton,翻译来自德罗德,再次感谢德罗德 !期待你一起参与Android API 的中文翻译,联系我over140@gmail.com ...
- python 时间戳
import timeprint time.time()输出的结果是(单位:s):1395421406.39 x = time.localtime(x) x = time.strftime('%Y-% ...
- c#解析xml字符串 分析 EntityName 时出错
因为xml字符串中的特殊html字符被转义了,怎么防止转义呢,可以在xml内加上<![CDATA[返回内容]] 这样可以防止特殊字符被转义,就好像微信公共平台消息传递也都是xml格式他们也都加& ...
- bzoj2734: [HNOI2012]集合选数
Description <集合论与图论>这门课程有一道作业题,要求同学们求出{1, 2, 3, 4, 5}的所有满足以 下条件的子集:若 x 在该子集中,则 2x 和 3x 不能在该子集中 ...