【思维】Stacks of Flapjacks

[UVa120] Stacks of Flapjacks

算法入门经典第8章8-1 (P236)

题目大意:有一个序列,可以翻转[1,k],构造一种方案使得序列升序排列。

试题分析:从插入排序即可找到思路。每次我们优先地将没有到自己位置上的、最大的数挪到自己的位置上。

     为什么可以这样做呢?难道不会改变已经排好序的么。

     不会,因为我们从大往小处理,翻转的是前面的一段,而排好序的是后面一段,因此肯定不会打乱后面的。

     对于每一个数,设其下标为pos,已经排好序的有x个,那么我们先将pos其变为1,即翻转[1,pos],然后将其翻转到N-x那里去,所以再翻转[1,N-x]。

代码:

#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;

#define LL long long

inline int read(){
	int x=0,f=1;char c=getchar();
	for(;!isdigit(c);c=getchar()) if(c==‘-‘) f=-1;
	for(;isdigit(c);c=getchar()) x=x*10+c-‘0‘;
	return x*f;
}
const int INF=9999999;
const int MAXN=100000;

int a[MAXN+1];
int cnt; int N,M;
char str[MAXN+1];
int b[MAXN+1];

bool cmp(int a,int b){
	return a>b;
}
int ret;
void ref(int k){
	for(int i=1;i<=k/2;i++)
		swap(a[i],a[k-i+1]);
	return ;
}
int ans[MAXN+1];

int main(){
	while(scanf("%d",&a[1])!=EOF){
		ret=0;
		gets(str); cnt=1;
		int len=strlen(str);
		for(int i=0;i<len;i++){
			if(str[i]==‘ ‘) continue;
			else {
				int p=i+1;
				int sum=str[i]-‘0‘;
				while(isdigit(str[p])){
					sum=sum*10+str[p]-‘0‘;
					++p;
				}
				i=p;
				a[++cnt]=sum;
			}
		}
		N=cnt;
		for(int i=1;i<=N;i++) b[i]=a[i];
		for(int i=1;i<N;i++) printf("%d ",b[i]);
		printf("%d\n",b[N]);
		sort(b+1,b+N+1,cmp);
		for(int i=1;i<=N;i++){
			int tmp;
			for(int j=1;j<=N;j++){
				if(a[j]==b[i]) {
					tmp=j; break;
				}
			}
			if(tmp==b[i]) continue;
			if(tmp!=1){
				ans[++ret]=N-tmp+1;
				ref(tmp);
			}
			if(b[i]!=1){
				ans[++ret]=i;
				ref(N-i+1);
			}
		}
		for(int i=1;i<=ret;i++) printf("%d ",ans[i]);
		puts("0");
	}
	return 0;
}

  

时间: 2024-08-04 13:57:29

【思维】Stacks of Flapjacks的相关文章

[2016-03-03][UVA][120][Stacks of Flapjacks]

[2016-03-03][UVA][120][Stacks of Flapjacks] UVA - 120 Stacks of Flapjacks Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Background Stacks and Queues are often considered the bread and butter of data s

Stacks of Flapjacks

Stacks of Flapjacks Background Stacks and Queues are often considered the bread and butter of data structures and find use in architecture, parsing, operating systems, and discrete event simulation. Stacks are also important in the theory of formal l

uva 120 stacks of flapjacks ——yhx

 Stacks of Flapjacks  Background Stacks and Queues are often considered the bread and butter of data structures and find use in architecture, parsing, operating systems, and discrete event simulation. Stacks are also important in the theory of formal

UVA 120 Stacks of Flapjacks(翻煎饼,啦啦啦啦~)

Problem A : Stacks of Flapjacks Background Stacks and Queues are often considered the bread and butter of data structures and find use in architecture, parsing, operating systems, and discrete event simulation. Stacks are also important in the theory

Stacks of Flapjacks UVA 120

说明:这道题初读有点绕....开始我以为只是将一个序列的最大值放到最右边,把最小值放到最左边...所以就怎么也通不过啦.后来终于理解题意了,其实就是要你排个序,从左到右为从大到小.但是改变序列顺序的规则有些特殊,即只能将从最左边开始到某一位置构成的连续子序列进行对称翻转.如有序列:  2,4,9,5,7,6.我选择的位置是5所在的位置,翻转后序列就变为5,9,4,2,7,6.然后用类似于冒泡排序的方法.通过最多两次翻转可以得到最大值,然后放到右边.不断重复上述操作,即可得到一个有序序列. 题目:

uva Stacks of Flapjacks

                                                 Stacks of Flapjacks  题目链接:Click Here~ 题目描写叙述:     给你n个数.要你得到的最后结果是从下到大排序.可是给出的序列不一定是有序你.要通过你的调整.问:要经过哪几个位置上的数的调整? 算法分析:     一開始,我的思路是直接模拟一边消除逆序数就能够了,由于我看数据非常小,仅仅有30.可是提交之后却TEL了. 后来上网查了一下.看到有一个人的思路还是非

UVa 120 Stacks of Flapjacks

题意:给一摞随意摞的煎饼,要按直径从上到下递增的顺序排列,求翻的顺序. 思路:贪心. 1,读取煎饼数据,新建一个数组,复制数据,排序,作为参考. 2,从最大直径(maxNum)的煎饼开始,看其是否在底层,是的话就寻找上一层最大的. 3,假如不在底层,看其在不在顶层,在顶层就把全部煎饼翻过来,使当前需要操作的最大的直径煎饼放在最下. 4,假如不在底层也不在顶层,就找到它,先翻到顶层,然后步骤同3. 直到maxNum代表数据为最小直径的煎饼. 代码: #include <iostream> #in

UVa120 - Stacks of Flapjacks (STL)

Background 背景 Stacks and Queues are often considered the bread and butter of data structures and find use in architecture, parsing, operating systems, and discrete event simulation. Stacks are also important in the theory of formal languages. 栈和队列常常被

B - Stacks of Flapjacks UVA - 120

BackgroundStacks and Queues are often considered the bread and butter of data structures and find use in architecture, parsing, operating systems, and discrete event simulation. Stacks are also important in the theory of formal languages. This proble