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 + Fragment 实现类微信界面
在如今的互联网时代,微信已是一个超级App.这篇通过ViewPager + Fragment实现一个类似于微信的界面,之前有用FragmentTabHost实现过类似界面,ViewPager的实现方式 ...
- Android Animation学习 实现 IOS 滤镜退出动画
IOS的用户体验做的很好,其中一点很重要的地方就是动画效果. 最近在学习Android的Animation,简单实现了一个IOS相机滤镜退出的动画: 布局文件:activity_animation_d ...
- java可变参数Varargs
http://www.cnblogs.com/shishm/archive/2012/01/31/2332656.html J2SE 1.5提供了“Varargs”机制.借助这一机制,可以定义能和多个 ...
- 明解C语言,练习13-3,从文件中读入个人信息,按身高排序后显示
#include <stdio.h> #define NUMBER 6 #define F_PATH "D:\\C_C++\\ec13-3\\hw.dat" typed ...
- PHP 用Class构造JSON数据
header('Content-type: appliction/json; charset=shift-JIS'); // error //{ // "result": fals ...
- Mysql group_concat函数被截断的问题
mysql group_concat函数被截断的问题 MySQL的 group_concat 函数默认返回1024个字节长度,超过长度的会被截断.最近程序中就遇到这个问题了. 通过如下命令可以查看 ...
- phpcms V9利用num++实现多样形式列表标签调用
在首页或者频道页调用文章列表的时候,经常会使用到左右对称或者每五行出现一条横线的调用形式. 其实代码很简单,利用num++的循环方式,以及{if}{/if}进行样式判断即可.代码如下: {pc:con ...
- extjs下拉框添加复选框
给ComboBox组件配置listConfig 下拉框代码: var gyslxcm = Ext.create('Ext.form.field.ComboBox',{ id : 'gyslxcm', ...
- python中文字符串前加u
我明明在编码前就加上了# -*- coding: UTF-8 -*-可是运行时还是出错了, # -*- coding: UTF-8 -*- 这句是告诉python程序中的文本是utf-8编码,让pyt ...
- VIM快捷键(转)
VIM快捷键:光标移动:四个方向 kh 0 l j ctrl+f, ctrl+b 向下翻页,向上翻页 ctrl+d, ctrl+u ...