`
hellobin
  • 浏览: 65449 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

UVA 514 - Rails

    博客分类:
  • UVA
 
阅读更多

 

There is a famous railway station in PopPush City. Country there is incredibly hilly. The station was built in last century. Unfortunately, funds were extremely limited that time. It was possible to establish only a surface track. Moreover, it turned out that the station could be only a dead-end one (see picture) and due to lack of available space it could have only one track.

 


\begin{picture}(6774,3429)(0,-10)\put(1789.500,1357.500){\arc{3645.278}{4.7247}......tFigFont{14}{16.8}{\rmdefault}{\mddefault}{\updefault}Station}}}}}\end{picture}

The local tradition is that every train arriving from the direction A continues in the direction B with coaches reorganized in some way. Assume that the train arriving from the direction A has$N \leŸ 1000$coaches numbered in increasing order$1, 2, \dots, N$. The chief for train reorganizations must know whether it is possible to marshal coaches continuing in the direction B so that their order will be$a_1. a_2, \dots, a_N$. Help him and write a program that decides whether it is possible to get the required order of coaches. You can assume that single coaches can be disconnected from the train before they enter the station and that they can move themselves until they are on the track in the direction B. You can also suppose that at any time there can be located as many coaches as necessary in the station. But once a coach has entered the station it cannot return to the track in the direction A and also once it has left the station in the direction B it cannot return back to the station.

 

Input

The input file consists of blocks of lines. Each block except the last describes one train and possibly more requirements for its reorganization. In the first line of the block there is the integerNdescribed above. In each of the next lines of the block there is a permutation of$1, 2, \dots, N$The last line of the block contains just 0.

The last block consists of just one line containing 0.

 

Output

The output file contains the lines corresponding to the lines with permutations in the input file. A line of the output file containsYesif it is possible to marshal the coaches in the order required on the corresponding line of the input file. Otherwise it containsNo. In addition, there is one empty line after the lines corresponding to one block of the input file. There is no line in the output file corresponding to the last ``null'' block of the input file.

 

Sample Input

 

5
1 2 3 4 5
5 4 1 2 3
0
6
6 5 4 3 2 1
0
0

 

Sample Output

 

Yes
No

Yes

 

思路:

先用手模拟一遍

如1-2-3-4-5 --> 2-3-1-5-4

要成功实现转换必需:

push(1), push(2), pop(2), push(3), pop(3), pop(1), push(4), push(5), pop(5), pop(4)

具体实现可以用两个指针A和B分别指向(1-2-3-4-5)序列和(2-3-1-5-4)序列,依次移动。

规律就是当A与B指向的元素相等时,立即做push-pop(可省略),把两个指针都向后移一位(表示当前检验元素找到了匹配,进行下一个元素的检验)。

否则就检查栈顶元素是否与B所指向的元素相等,若相等,立刻pop并把B指针向后移一位。

再否则就把A指针指向的元素压入栈内,以待以后可能的匹配。

如果以上情况都不满足,就表示不能实现题目的需求。

 

Solution1:

 

//#define RUN
#ifdef RUN

#include<stdio.h>
const int MAXN = 1000 + 10;
int n, target[MAXN];

int main() {

#ifndef ONLINE_JUDGE
	freopen("514.in", "r", stdin);
	freopen("514.out", "w", stdout); 
#endif

	while(scanf("%d", &n)==1 && n!=0) {
		while(true){
			scanf("%d", &target[1]);
			if(target[1] == 0){
				printf("\n");
				break;
			}
			for(int i = 2; i <= n; i++)
				scanf("%d", &target[i]);

			int stack[MAXN], top = 0;
			int A = 1, B = 1;

			int ok = 1;

			while(B <= n) {
				if(A == target[B]){ A++; B++; }
				else if(top && stack[top] == target[B]){ top--; B++; }
				else if(A <= n) stack[++top] = A++;
				else { ok = 0; break; }
			}

			printf("%s\n", ok ? "Yes" : "No");
		}

	}
	return 0;
}

#endif



Solution2:

 

#define RUN
#ifdef RUN

#include<cstdio>
#include<stack>
using namespace std;
const int MAXN = 1000 + 10;

int n, target[MAXN];

int main() {

#ifndef ONLINE_JUDGE
	freopen("514.in", "r", stdin);
	freopen("514.out", "w", stdout); 
#endif

  while(scanf("%d", &n)==1 && n!=0) {

	while(true){
		scanf("%d", &target[1]);
		if(target[1] == 0){
			printf("\n");
			break;
		}

		for(int i = 2; i <= n; i++)
			scanf("%d", &target[i]);

		stack<int> s;
		int A = 1, B = 1;

		int ok = 1;
		while(B <= n) {
			if(A == target[B]){ A++; B++; }
			else if(!s.empty() && s.top() == target[B]){ s.pop(); B++; }
			else if(A <= n) s.push(A++);
			else { ok = 0; break; }
		}
		printf("%s\n", ok ? "Yes" : "No");
	}
    
  }
  return 0;
}
#endif



 

 

分享到:
评论

相关推荐

    基于java的开发源码-Rails3消息队列系统 Sidekiq.zip

    基于java的开发源码-Rails3消息队列系统 Sidekiq.zip 基于java的开发源码-Rails3消息队列系统 Sidekiq.zip 基于java的开发源码-Rails3消息队列系统 Sidekiq.zip 基于java的开发源码-Rails3消息队列系统 Sidekiq.zip ...

    jquery-datatables-rails, 用于 Rails的jquery数据表 gem.zip

    jquery-datatables-rails, 用于 Rails的jquery数据表 gem jquery-datatables-rails 这个 gem 为 jQuery DataTables插件提供了方便,以便与 Rails 资产pipleine结合使用。 它提供所有基本的datatable文件,以及一些...

    turbo-sprockets-rails3, 加速你的Rails 3资产.zip

    turbo-sprockets-rails3, 加速你的Rails 3资产 用于 Rails 3.2.x的涡轮链轮 通过只根据源文件的哈希来重新编译已经更改的资产,从而加快 Rails 3 rake assets:precompile的速度只编译一次以生成指纹和非打印的资产...

    关于rails 3.1 cucumber-rails 1.2.0

    Rails 3.1 和 Cucumber-Rails 1.2.0 是两个在Web开发领域非常重要的工具,尤其对于Ruby on Rails框架的测试和自动化流程。本文将深入探讨这两个组件,以及它们如何协同工作来增强软件开发的效率和质量。 首先,...

    Angle-3.4-rails

    "Angle-3.4-rails"是一个专门针对Rails框架的项目,其包含了"backend-rails"和"backend-rails-seed"两个关键部分,旨在为开发者提供一个强大的后端开发环境和数据初始化工具。 一、Rails框架介绍 Rails是由David ...

    projectile-rails, 基于弹丸的Rails 模式.zip

    projectile-rails, 基于弹丸的Rails 模式 弹 Rails 概要弹 Rails 是在 GNU Emacs中使用 Ruby on Rails 应用程序和引擎的次要模式。 在内部,它是基于弹 。这意味着你可以在 greping ( 或者 acking ) 文件。运行测试...

    influxdb-rails-源码.rar

    《InfluxDB与Rails集成深度解析——以influxdb-rails源码为例》 InfluxDB,作为一款专为时间序列数据设计的高性能数据库,被广泛应用于监控、物联网、大数据分析等领域。Rails,作为Ruby on Rails框架的核心部分,...

    Ruby-Rails的Clojurescript集成类似于webpackrails

    Ruby on Rails 是一个广受欢迎的Web开发框架,它以其生产力和灵活性著称。在现代Web开发中,前端JavaScript的处理和打包变得越来越重要,而ClojureScript是一种基于Clojure语言的JavaScript编译器,它提供了丰富的...

    jquery-ui+jquery-ui-rails

    在这个案例中,我们看到`jquery-ui-rails-4.2.1.gem`,这是该gem的一个特定版本。这个gem负责将jQuery UI的库文件打包并整合到Rails的asset pipeline中,使得在Rails项目中使用jQuery UI变得简单。 要使用`jquery-...

    jquery-fileupload-rails, 用于 Rails的jQuery文件上传集成.zip

    jquery-fileupload-rails, 用于 Rails的jQuery文件上传集成 Rails 文件上传jQuery-File-Plugin 是一个文件上传插件,由的Tschan 。 jQuery文件上传功能多文件选择。drag&拖放支持。进度栏和jQuery预览图像。 支持...

    jquery-ui-rails:Rails资产管道的jQuery UI

    jquery-ui-rails 这个gem为Rails打包了jQuery UI资产(JavaScript,样式表和图像),因此您不必再通过下载自定义软件包。 请参阅以查看哪些版本的jquery-ui-rails捆绑了哪些版本的jQuery UI。 警告:此gem与3.0.0...

    minitest-rails, Rails的Minitest集成.zip

    minitest-rails, Rails的Minitest集成 minitestRails 5的Minitest集成 安装gem install minitest-rails这将安装以下宝石:minitest配置创建一个新的Rail

    grape-swagger-rails, Swagger UI作为葡萄 Swagger gem的Rails 引擎.zip

    grape-swagger-rails, Swagger UI作为葡萄 Swagger gem的Rails 引擎 GrapeSwaggerRails Swagger UI作为葡萄 Swagger gem的Rails 引擎。安装将此行添加到你的应用程序的Gemfile中:gem 'grape-swagger-rails'

    Ruby-on-Rails-rails.zip

    Ruby_on_Rails_rails.zip Ruby_on_Rails_rails.zip Ruby_on_Rails_rails.zip Ruby_on_Rails_rails.zipRuby_on_Rails_rails.zip Ruby_on_Rails_rails.zip Ruby_on_Rails_rails.zip Ruby_on_Rails_rails.zipRuby_on_...

    to_xls-rails:将Rails ActiveRecord或Mongid数据导出到Excel文件

    这个简单的插件使您能够调用to_xls到Rails的数组集合。 数组元素支持对象:ActiveRecord,Mongid,哈希。 在您的Gemfile中: gem 'to_xls-rails' # Last officially released gem # gem "to_xls-rails", :git =&gt; ...

    webpack-rails, 将 web pack与你的Ruby on Rails 应用程序集成.zip

    webpack-rails, 将 web pack与你的Ruby on Rails 应用程序集成 不再维护webpack-rails 不再被维护。 有关详细信息,请参阅 #90. web pack-railsweb pack 为你提供了将 web pack集成到现有的Ruby on Rails 应用程序中...

    sclo-ror42-rubygem-rails-html-sanitizer-1.0.3-1.el7.noarch.rpm

    官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装

    foundation-rails:Rails基础

    基金会::铁路 Foundation :: Rails是一颗宝石,可以在即将到来的Rails项目中非常轻松地使用Foundation。安装将这些行添加到应用程序的Gemfile中: gem ' foundation-rails 'gem ' autoprefixer-rails ' 然后执行: ...

    jquery-validation-rails, 对 Rails 资产管道的jQuery验证.zip

    jquery-validation-rails, 对 Rails 资产管道的jQuery验证 :: 验证:: rails针对 Rails 资产管道的验证 。安装这里 gem将以下行添加到项目的Gemfile 中:gem 'jquery-validation-rails'在你的终端中运行以下命令:cd...

    rubocop-rails:专注于执行 Rails 最佳实践和编码约定的 RuboCop 扩展

    扩展专注于执行 Rails 最佳实践和编码约定。 注意:此存储库管理 rubocop-rails gem (&gt;= 2.0.0)。 rubocop-rails gem (&lt;= 1.5.0) 已重命名为 gem。 安装 只需安装rubocop-rails gem gem install rubocop-rails...

Global site tag (gtag.js) - Google Analytics