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矩阵,找出来面积最大的“类似国际象棋棋盘”的子矩阵. 解法:枚举矩阵宽(水平方向)的起点和终点,然后利用尺取法来找到每个固定宽度下的最大矩阵,不断更新 ...
随机推荐
- Nginx配置同一个域名http与https两种方式都可访问
##配置 http://test.pay.joyhj.com https://test.pay.joyhj.com 两者都可访问 # vim /usr/local/nginx/conf/vhost/t ...
- CI 笔记5 (CI3.0 默认控制器,多目录)
在ci3.x中,不支持多级子目录的默认控制器设置, 解决方法如下: 在index.php中,添加 $routing['directory'] = 'admin';然后在默认的router.php的默 ...
- 使用highlight.js高亮你的代码
在逛别人的博客的时候,看见别人的代码的例子使用了高亮的语法,无论是java,js还是php等等语言,都会自动的对关键字进行高亮. 于是在前几天自己写了一个博客,遇到code时,自然就想到了别人网站如何 ...
- 分享一个nodejs写的小论坛
引言:作为一个前端小菜鸟,最近迷上了node,于是乎空闲时间,为了练练手写了一个node的小社区,关于微信小程序的,欢迎大家批评指导. 项目架构部分 一.前端架构 作为一个写样式也得无聊的前端狮,我偷 ...
- latex引用多篇参考文献
1.如何使连续的参考文献能够中间用破折号连起来?比如[6,7,8,9]变成[6-9]? 方法:在文档开始前加上下面的语句命令 \usepackage[numbers,sort&compress ...
- printf 格式化最常用用法
printf 操作符的参数包括”格式字符串“及”要输出的数据列表". 格式字符串好像用来填空的模版,代表你想要的输出格式: printf "Hello,%s;your passwo ...
- C#快速导入海量XML数据至SQL Server数据库
#region 将Xml中的数据读到Dataset中,然后用SqlBulkCopy类把数据copy到目的表中using (XmlTextReader xmlReader = new XmlTextRe ...
- excel poi 文件导出,支持多sheet、多列自动合并。
参考博客: http://www.oschina.net/code/snippet_565430_15074 增加了多sheet,多列的自动合并. 修改了部分过时方法和导出逻辑. 优化了标题,导出信息 ...
- pojo和JavaBean的区别
javabean可以处理业务,pojo不可以. pojo就是get 和set 例如: Student{ id; name; get();... set();...} javabean可以实现业务逻辑 ...
- AJAX编程模板
AJAX一直以来没怎么接触,主要是做JSON数据在服务器和客户端之间传递的时候,被玩坏了,对它莫名的不可爱,最近心理阴影小了,于是又来看看它....... AJAX即“Asynchronous Jav ...