您的位置:首页 > 其它

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");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: