cf Canada cup A题
A. Jumping Ball
256 megabytes
standard input
standard output
In a new version of the famous Pinball game, one of the most important parts of the game field is a sequence of n bumpers. The bumpers are numbered with integers from 1 to n from left to right. There are two types of bumpers. They are denoted by the characters '<' and '>'. When the ball hits the bumper at position i it goes one position to the right (to the position i + 1) if the type of this bumper is '>', or one position to the left (to i - 1) if the type of the bumper at position i is '<'. If there is no such position, in other words if i - 1 < 1 ori + 1 > n, the ball falls from the game field.
Depending on the ball's starting position, the ball may eventually fall from the game field or it may stay there forever. You are given a string representing the bumpers' types. Calculate the number of positions such that the ball will eventually fall from the game field if it starts at that position.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the length of the sequence of bumpers. The second line contains the string, which consists of the characters '<' and '>'. The character at the i-th position of this string corresponds to the type of the i-th bumper.
Output
Print one integer — the number of positions in the sequence such that the ball will eventually fall from the game field if it starts at that position.
4
<<><
2
5
>>>>>
5
4
>><<
0
In the first sample, the ball will fall from the field if starts at position 1 or position 2.
In the second sample, any starting position will result in the ball falling from the field.
================================================================================================
题目大意:输入有n个字符组成的字符串,每个字符要么是">"要么是"<";当为前者时,位置i向右移动一位变成位置i+1(然后继续看i+1上的字符);当为后者时,位置i向左移动一位变成了位置i-1(然后继续看i-1上的字符).如果位置i移动后i+1>n或者i-1<1,则为出界。输出所有会造成出界的初始位置的数目。
题解:这题一开始没读懂题意,卡了一会(实际上水题啊);
思路一:观察每个字符串会发现只有"><"出现时,>前边的所有连续的>和<后边所有连续的<都不会出界。所以计算出字符串中所有的这种格式的字符数目sum,最后用n-sum即结果。
思路二:如果用法一,会发现处理起来有些麻烦;可以考虑它(法一)的对立面,从左边开始,连续的<一定会出界;从右边开始,连续的>一定会出界;所以,这两边续的总数,即最终结果.
代码如下:
#include <stdio.h>
#include <string.h>
#define maxn 200005
char str[maxn];
int cnt = 0;
int main()
{
int n;
int i,l = 0,r = 0;
scanf("%d",&n);
scanf("%s",str);
for(i = 0; i < n; i++)
if(str[i] =='<')
l++;
else
break;
for(i = n-1; i >= 0; i--)
if(str[i] =='>')
r++;
else
break;
printf("%d\n",l+r); }
cf Canada cup A题的更多相关文章
- 【Codeforces Round 725】Canada Cup 2016
		
模拟Canada Cup 2016,ABC三题,Rank1376 第三题卡住了 Codeforces 725 C 求出两个相同字符的位置,记为x和y. 然后考虑把相同的那个字符放在第一行的什么地方, ...
 - Canada Cup 2016 D. Contest Balloons  好题。优先队列 + 简单贪心
		
http://codeforces.com/contest/725/problem/D 这题一看就是贪心的了,w - t最小的那个,肯定是优先打死. 但是一直都不会写,为什么呢,因为这个太像二分答案了 ...
 - Canada Cup 2016 C. Hidden Word 构造模拟题
		
http://codeforces.com/contest/725/problem/C Each English letter occurs at least once in s. 注意到题目有这样一 ...
 - Codeforces Round #354 (Div. 2)-C. Vasya and String,区间dp问题,好几次cf都有这种题,看来的好好学学;
		
C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard ...
 - Codeforces Canada Cup 2016
		
A. Jumping Ball time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
 - 贪心 CF 332 C 好题 赞
		
题目链接: http://codeforces.com/problemset/problem/332/C 题目意思: 有n个命令,要通过p个,某主席要在通过的p个中选择k个接受. 每个任务有两个值ai ...
 - Canada Cup 2016 C. Hidden Word
		
C. Hidden Word time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
 - Canada Cup 2016 D. Contest Balloons
		
最近好弱做什么题目都是做一晚上 这是合肥站炼铜后遗症? 这题就是贪心 我已开始还写了1小时---三分-----. #include<bits/stdc++.h> using namespa ...
 - Educational Codeforces Round 24 CF 818 A-G 补题
		
6月快要结束了 期末也过去大半了 马上就是大三狗了 取消了小学期后20周的学期真心长, 看着各种北方的学校都放假嗨皮了,我们这个在北回归线的学校,还在忍受酷暑. 过年的时候下定决心要拿块ACM的牌子, ...
 
随机推荐
- 内存管理 - MEMORY POOL
			
内存池优势: 效率高,频繁的new和delete效率低下 减少内存碎片,反复向系统申请和释放内存会产生大量内存碎片 防止内存泄露 内存池设计思路: 内存池可以根据实际需要,设计成不同的样子.下面是针对 ...
 - struts2 配置拦截器
			
第一步:继承MethodFilterInterceptor写自己的自定义拦截器 import org.apache.struts2.ServletActionContext; import com.o ...
 - 【转】关于编写性能高效的javascript事件的技术
			
原文转自:http://blog.jobbole.com/80170/ 如何能做出高效的web前端程序是我每次做前端开发都会不自觉去考虑的问题.几年前雅虎里牛逼的前端工程师们出了一本关于提升web前端 ...
 - Newtonsoft.Json 处理多态类型的反序列化
			
Newtonsoft.Json的序列化和反序列化很成熟也很好用, 最近在处理多态类型的反序列化中遇到了问题, 反序列化后只能到基类,而得不到也不能转换到子类.从网上查询了一番后,需要写一个创建类型的C ...
 - Xcode6中如何使用自定义的类模板
			
说到IOS类的模板,有些人感觉很陌生,但是只要有开发过IOS程序的人,其实都用过类的模板,只不过是用的系统自带的类的模板. 例如创建一个ClassTemplateVC继承于UIViewControll ...
 - iOS 线性滚动
			
在这里,给大家带来简单的滚动实现,首先看一下实现效果. 通过观察不难发现,有很多地方并不是那么容易想出来的,对于篇随笔,感兴趣可以查查相关资料,我就不尽行过多说明,(主要是开考文字,不好说明
 - Twitter API 申请key
			
最近听了一下coursera的python课(https://www.coursera.org/learn/python-network-data/home/welcome),讲的挺简单也挺有意思.其 ...
 - Javascript 右移0位的作用
			
Javascript 中右移0位可以用来快速去掉小数,关于位移运算的定义: 右移运算是将一个二进制位的操作数按指定移动的位数向右移动,移出位被丢弃,左边移出的空位或者一律补0,或者补符号位. 实际看下 ...
 - 【转】Alchemy的使用和多项式批量计算的优化
			
原文:http://www.cnblogs.com/flash3d/archive/2012/01/30/2332158.html ================================== ...
 - c语言数据结构之 堆排序
			
算法:先生成随机数,赋值到数组,将数组第一个元素a[0]设置为哨兵,函数调用数组和随机数个数n,再设定n/2的根结点与孩子结点进行比较操作,若右孩子存在,则选出三个数里最小的数赋值给根节点,如果右孩子 ...