SRM 397(1-250pt)
题意:对于一个长度n的数列(由1-n组成,n <= 8),每次操作可以reverse k个连续的数。问最少多少次操作可以将该数列转化成递增的数列。
解法:就是一个BFS。只是由于最开始学习BFS时写法很复杂,所以直到现在也觉得BFS不好写。然后就在想还有没有别的方法,然后时间就浪费了...最后还是写了个比较冗杂的BFS,150+score。看了官方题解的BFS,真是学习了...一方面是BFS的写法搓,另一方面是不会用C++内置的reverse函数。
tag:BFS
我的代码:
// BEGIN CUT HERE
/*
* Author: plum rain
* score :
*/
/* */
// END CUT HERE
#line 11 "SortingGame.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 ALL(t) t.begin(),t.end()
#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 = ; struct nod{
VI b;
int d;
}; VI b;
int n;
nod an[];
set<VI> st;
VI ans; int BFS(int k)
{
if (b == ans) return ;
st.erase(ALL(st));
int l = , r = ;
VI ttt;
nod tt; tt.d = ;
for (int i = ; i+k <= n; ++ i){
ttt = b;
for (int j = i, t = i+k-; j < t; ++ j, -- t)
swap(ttt[j], ttt[t]);
tt.b = ttt;
if (!st.count(ttt)){
an[r++] = tt;
st.insert(ttt);
}
} while (l < r){
nod tmp = an[l++];
if (tmp.b == ans) return tmp.d;
++ tmp.d;
for (int i = ; i+k <= n; ++ i){
ttt = tmp.b;
for (int j = i, t = i+k-; j < t; ++ j, -- t)
swap(ttt[j], ttt[t]);
if (!st.count(ttt)){
an[r].b = ttt;
an[r++].d = tmp.d;
st.insert(ttt);
}
}
}
return -;
} class SortingGame
{
public:
int fewestMoves(vector <int> B, int k){
b = B; n = b.size();
ans.clear();
for (int i = ; i <= n; ++ i)
ans.PB(i);
return BFS (k);
} // 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(); if ((Case == -) || (Case == )) test_case_4(); }
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() { int Arr0[] = {,,}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arg2 = ; verify_case(, Arg2, fewestMoves(Arg0, Arg1)); }
void test_case_1() { int Arr0[] = {,,}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arg2 = ; verify_case(, Arg2, fewestMoves(Arg0, Arg1)); }
void test_case_2() { int Arr0[] = {,,,,}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arg2 = ; verify_case(, Arg2, fewestMoves(Arg0, Arg1)); }
void test_case_3() { int Arr0[] = {,,,,}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arg2 = -; verify_case(, Arg2, fewestMoves(Arg0, Arg1)); }
void test_case_4() { int Arr0[] = {,,,,,,,}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arg2 = ; verify_case(, Arg2, fewestMoves(Arg0, Arg1)); } // END CUT HERE }; // BEGIN CUT HERE
int main()
{
// freopen( "a.out" , "w" , stdout );
SortingGame ___test;
___test.run_test(-);
return ;
}
// END CUT HERE
官方题解推荐代码:
#include <algorithm>
#include <iostream>
#include <sstream>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
using namespace std;
typedef vector<int> VI; struct SortingGame {
int fewestMoves(vector<int> board, int k) {
map<VI, int> dist;
dist[board] = ; queue<VI> Q;
Q.push(board); int i, N = board.size(); while (!Q.empty()) {
VI u = Q.front(); Q.pop();
int d = dist[u]; for (i = ; i < N; i++)
if (u[i-] > u[i]) break;
if (i == N) return d; for (i = ; i + k <= N; i++) {
VI w = u;
reverse(w.begin() + i, w.begin() + i + k);
if (dist.count(w) == ) {
dist[w] = d + ;
Q.push(w);
}
}
} return -;
}
};
SRM 397(1-250pt)的更多相关文章
- SRM593(1-250pt,500pt)
SRM 593 DIV1 250pt 题意:有如下图所示的平面,每个六边形有坐标.将其中一些六边形染色,要求有边相邻的两个六边形不能染同一种颜色.给定哪些六边形需要染色,问最少需要多少种颜色. 解法: ...
- 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和 ...
- SRM 609(1-250pt, 1-500pt)
嗯....还是应该坚持写题解的好习惯啊... DIV1 250pt 这难度是回到srm 300+的250了嘛...略 // BEGIN CUT HERE /* * Author: plum rain ...
- SRM 442(1-250pt, 1-500pt)
DIV1 250pt 题意:将一个数表示成质因子相乘的形式,若乘式所含数字的个数为质数,则称A为underprime.比如12 = 2*2*3,则含3个数字,是underprime.求A, B之间un ...
- 记第一次TopCoder, 练习SRM 583 div2 250
今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...
- LeetCode 397. Integer Replacement
397. Integer Replacement QuestionEditorial Solution My Submissions Total Accepted: 5878 Total Subm ...
- SRM 513 2 1000CutTheNumbers(状态压缩)
SRM 513 2 1000CutTheNumbers Problem Statement Manao has a board filled with digits represented as St ...
随机推荐
- ViewPager和SwipeRefreshLayout之间嵌套使用时发生"事件"冲突
有时候我们会有一种需求,一个ViewPager有n个页面,每个页面是一个Fragment,在Fragment中使用了具有垂直滑动属性的控件,比如SwipeRefreshLayout!!! 这时二者之间 ...
- Android Parcelable Trans byte[]
思路: http://stackoverflow.com/questions/10898116/make-custom-parcelable-containing-byte-array 谢谢, 这位外 ...
- maven常见命令总结
1.maven vm配置 -Xms512m -Xmx1024m -XX:PermSize=256m 2.启动tomcat clean package -Dpackage.environment=de ...
- 非常不错的ASP操作数据库类,支持多数据库MSSQL,ACCESS,ORACLE,MYSQL等
可同时操作多个不同类型的数据库. 完全不用考虑数据类型的差别,再也不用想字符型字段加不加单引号. 调用非常简单,对数据库的主要操作一般只需要一行代码. 支持mssql事务回滚. 可自动生成和输出sql ...
- JavaScript - 测试 jQuery
测试 JavaScript 框架库 - jQuery 引用 jQuery 如需测试 JavaScript 库,您需要在网页中引用它. 为了引用某个库,请使用 <script> 标签,其 s ...
- Java中实现对象的比较:Comparable接口和Comparator接口
在实际应用中,我们往往有需要比较两个自定义对象大小的地方.而这些自定义对象的比较,就不像简单的整型数据那么简单,它们往往包含有许多的属性,我们一般都是根据这些属性对自定义对象进行比较的.所以Java中 ...
- 使用Qt创建第一个OpenCV的Gui应用
写在前面 学习OpenCV有一些小日子了,发现群里还有很多初学OpenCV的人像我当初一样跌跌撞撞到处找资料,所以在这里把学习笔记分享给大家,希望有志学习OpenCV进行计算机视觉活动的小伙伴们能少走 ...
- spring配置中引入properties
<context:property-placeholder location="classpath*:db.properties" />
- Oracle数据库之PL/SQL包
Oracle数据库之PL/SQL包 1. 简介 包(PACKAGE)是一种数据对象,它是一组相关过程.函数.变量.常量和游标等PL/SQL程序设计元素的组合,作为一个完整的单元存储在数据库中,用名称来 ...
- 谈谈 jQuery 中的防冲突(noConflict)机制
许多的 JS 框架类库都选择使用 $ 符号作为函数或变量名,jQuery 是其中最为典型的一个.在 jQuery 中,$ 符号只是 window.jQuery 对象的一个引用,因此即使 $ 被删除,w ...