摘要: 题目链接题目大意:给定一个只含1,2,3的数列,求排序的最小交换次数。这题说不出需要用什么算法,如果有的话,应该是贪心的思想。我的做法是,先统计1,2,3的个数,然后就知道了1,2,3应该排在哪些区间,首先将错位的两两交换(例如1在2的区间,2在1的区间),然后三个之间交换(例如1在2的区间,2在3的区间,3在1的区间)。View Code 1 #include <stdio.h> 2 #include <memory.h> 3 #define MIN(a,b) ((a)<(b)?(a):(b)) 4 #define N 1000 5 int a[N]; 6 in 阅读全文
posted @ 2012-04-25 20:39 BeatLJ 阅读(242) 评论(0) 推荐(0) 编辑
摘要: 题目链接二分查找树的模拟。不能用数组模拟,那样内存会爆掉。View Code 1 #include <stdio.h> 2 #define N 1000 3 struct node 4 { 5 int x; 6 struct node *left,*right; 7 }node[N]; 8 char f; 9 void Insert(struct node *r,struct node *p)10 {11 if(!r) return;12 if(r->x>p->x)13 {14 if(r->left) Insert(r->left,p);15 else 阅读全文
posted @ 2012-04-25 19:59 BeatLJ 阅读(242) 评论(0) 推荐(0) 编辑
摘要: 题目链接典型的DFS题,骑士周游问题。这题的关键在于字典序输出路径,要处理好搜索的顺序,另外需要注意的是,字母表示行,数字表示列。View Code 1 #include <stdio.h> 2 #include <memory.h> 3 #define N 26 4 int dx[8]={-2,-2,-1,-1,1,1,2,2}; 5 int dy[8]={-1,1,-2,2,-2,2,-1,1}; 6 char vis[N][N],ok; 7 int n,m; 8 int ans[N]; 9 void dfs(int i,int j,int cnt)10 {11 i 阅读全文
posted @ 2012-04-25 17:15 BeatLJ 阅读(297) 评论(0) 推荐(0) 编辑
摘要: 题目链接宽度优先搜索。View Code 1 #include <stdio.h> 2 #include <memory.h> 3 #include <queue> 4 #define N 300 5 using namespace std; 6 typedef pair<int,int> node; 7 queue<node> Q; 8 int dx[8]={1,1,-1,-1,2,2,-2,-2}; 9 int dy[8]={2,-2,2,-2,1,-1,1,-1};10 int n;11 int step[N][N];12 in 阅读全文
posted @ 2012-04-25 14:27 BeatLJ 阅读(199) 评论(0) 推荐(0) 编辑
摘要: 题目链接动态规划题。题目大意:给定一个二维数组,数组中每个数代表一个高度,每次只能向相邻且高度下降的方向移动,求最长的移动距离。View Code 1 #include <stdio.h> 2 #include <memory.h> 3 #define MAX(a,b) ((a)>(b)?(a):(b)) 4 #define N 100 5 int dx[4]={0,0,1,-1}; 6 int dy[4]={1,-1,0,0}; 7 int h[N][N],n,m; 8 int c[N][N]; 9 int dp(int i,int j)10 {11 int n 阅读全文
posted @ 2012-04-25 13:52 BeatLJ 阅读(159) 评论(0) 推荐(0) 编辑
摘要: 题目链接优先队列的题。进行队列弹出操作时要注意判空,如果有多case,记得清空队列。View Code 1 #include <stdio.h> 2 #include <queue> 3 #define N 30000 4 using namespace std; 5 6 priority_queue<int,vector<int>,greater<int> >qmin; 7 priority_queue<int,vector<int>,less<int> >qmax; 8 9 int a[N];1 阅读全文
posted @ 2012-04-25 09:42 BeatLJ 阅读(215) 评论(0) 推荐(0) 编辑