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 ...
随机推荐
- Array,ArrayList 和 List<T>的选择和性能比较.
Array Class Provides methods for creating, manipulating, searching, and sorting arrays, thereby serv ...
- JavaScript HTML DOM EventListener
JavaScript HTML DOM EventListener addEventListener() 方法 实例 点用户点击按钮时触发监听事件: document.getElementById(& ...
- SpringMVC4+thymeleaf3的一个简单实例(篇二:springMVC与thymeleaf的整合)
延续前篇内容. 开始之前,我们首先要准备以下12个jar文件:spring-aop-4.3.3.RELEASE.jarspring-beans-4.3.3.RELEASE.jarspring-cont ...
- php中文乱码
一. 首先是PHP网页的编码 1. php文件本身的编码与网页的编码应匹配 a. 如果欲使用gb2312编码,那么php要输出头:header(“Content-Typ ...
- c 连接数据库 mysql
sudo apt-get install mysql-server mysql-client 再装开发包代码:sudo apt-get install libmysqlclient15-dev 安装完 ...
- 谷歌地图实现车辆轨迹移动播放(google map api)
开发技术:jquery,js baidu map api,json,ajax QQ1310651206 谷歌地图(google map api)实现车辆轨迹移动播放(google map api)
- java之两个字符串的比较
compareTo() 的返回值是int, 它是先比较对应字符的大小(ASCII码顺序)1.如果字符串相等返回值02.如果第一个字符和参数的第一个字符不等,结束比较,返回他们之间的差值(ascii码值 ...
- .net程序员必须知道的知识
A while back, I posted a list of ASP.NET Interview Questions. Conventional wisdom was split, with ab ...
- 一个基于nodejs,支持http/https的中间人(MITM)代理,便于渗透测试和开发调试。
源码地址:https://github.com/wuchangming/node-mitmproxy node-mitmproxy node-mitmproxy是一个基于nodejs,支持http/h ...
- linux下Rtree的安装
1. 首先安装依赖libspatialindexhttp://libspatialindex.github.io/ sudo ./configure sudo make sudo make insta ...