soj 1939. 煎饼堆
2013-03-21 11:24
274 查看
题意:有一堆煎饼,没个煎饼有直径,你能从最上面的到某个位置将这些煎饼倒转过来,问经过怎样的操作能使这些煎饼是按照直径从下到上递减的顺序排列。题目中的例子给的很详细,要求输出这样的操作序列。
思路:每次都将最大的翻转到最底下,再递归求解问题。
思路:每次都将最大的翻转到最底下,再递归求解问题。
#include <iostream> #include <cstdio> using namespace std; int t, n, v[35], maxn, maxp; void reserve(int p1, int p2) { for (int i = (p1+p2+1)/2; i <= p2; ++ i) swap(v[i], v[p1+p2-i]); } void work(int s) { if (s == n-1) return ; maxn = v[s]; maxp = s; for (int i = s+1; i < n; ++ i) { if (v[i] > maxn) { maxn = v[i]; maxp = i; } } if (maxp == s) ; else if (maxp == n-1) { printf("%d ", s+1); reserve(s,n-1); } else { printf("%d %d ", maxp+1, s+1); reserve(maxp,n-1); reserve(s,n-1); } work(s+1); } int main() { scanf("%d",&t); while (t --) { scanf("%d",&n); for (int i = 0; i < n; ++ i) scanf("%d",&v[n-i-1]); work(0); printf("0\n"); } }
相关文章推荐
- soj上一个fuckable的题目
- UVa Problem 120 Stacks of Flapjacks (煎饼堆)
- 【线段树】soj 3019 Count Color
- SOJ 3137: Simple Computing _容斥原理
- SOJ 4164 贪心
- SOJ Binary Numbers dp + 逼近思想
- soj 1176. Two Ends
- soj 1702. Robot Roll Call -- Cambot...Servo...Gypsy...Croooow
- soj 1237. Paint Mix
- soj 2503 最长字符串
- soj 2013 weekly-5 暨校赛模拟III 7966-7974
- Sicily 1308 Dependencies among J (SOJ 1308) 【BFS 广度优先搜索】
- SOJ 4299: component
- ural 1939 First Seal
- soj dsa extra 1002 sudoku
- poj1939 water
- 3D打印煎饼七十二变 早餐吃出新花样
- SOJ 2309: In the Army Now 树状数组求逆序对
- soj 11598. XOR
- soj 4390 电梯问题