cf Canada cup A题

时间:2023-02-02 08:53:19

A. Jumping Ball

time limit per test 2 seconds
memory limit per test

256 megabytes

input

standard input

output

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.

Examples
input
4
<<><
output
2
input
5
>>>>>
output
5
input
4
>><<
output
0
Note

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题的更多相关文章

  1. 【Codeforces Round 725】Canada Cup 2016

    模拟Canada Cup 2016,ABC三题,Rank1376 第三题卡住了 Codeforces 725 C 求出两个相同字符的位置,记为x和y. 然后考虑把相同的那个字符放在第一行的什么地方, ...

  2. Canada Cup 2016 D&period; Contest Balloons 好题。优先队列 &plus; 简单贪心

    http://codeforces.com/contest/725/problem/D 这题一看就是贪心的了,w - t最小的那个,肯定是优先打死. 但是一直都不会写,为什么呢,因为这个太像二分答案了 ...

  3. Canada Cup 2016 C&period; Hidden Word 构造模拟题

    http://codeforces.com/contest/725/problem/C Each English letter occurs at least once in s. 注意到题目有这样一 ...

  4. Codeforces Round &num;354 &lpar;Div&period; 2&rpar;-C&period; Vasya and String&comma;区间dp问题,好几次cf都有这种题,看来的好好学学;

    C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard ...

  5. Codeforces Canada Cup 2016

    A. Jumping Ball time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  6. 贪心 CF 332 C 好题 赞

    题目链接: http://codeforces.com/problemset/problem/332/C 题目意思: 有n个命令,要通过p个,某主席要在通过的p个中选择k个接受. 每个任务有两个值ai ...

  7. Canada Cup 2016 C&period; Hidden Word

    C. Hidden Word time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  8. Canada Cup 2016 D&period; Contest Balloons

    最近好弱做什么题目都是做一晚上 这是合肥站炼铜后遗症? 这题就是贪心 我已开始还写了1小时---三分-----. #include<bits/stdc++.h> using namespa ...

  9. Educational Codeforces Round 24 CF 818 A-G 补题

    6月快要结束了 期末也过去大半了 马上就是大三狗了 取消了小学期后20周的学期真心长, 看着各种北方的学校都放假嗨皮了,我们这个在北回归线的学校,还在忍受酷暑. 过年的时候下定决心要拿块ACM的牌子, ...

随机推荐

  1. android aidl 进程间通信需要注意的地方(android&period;os&period;TransactionTooLargeException)

    转自:http://blog.sina.com.cn/s/blog_4e1e357d0102wau9.html 1.bus工程实现通过service实现aidl实体类 2.actor工程通过发起bin ...

  2. 拓展Yii Framework(易框架)

    1.拓展yii 此文针对Yii1.1.15而写,请注意甄别你的Yii Framework 版本. 拓展yii是开发期间常见的代码处理方式.例如,你写一个新的controller(业务控制器),你通过继 ...

  3. 自学 PHP,如何不走弯路?

    1.一本好书至关重要.如果这本书的知识非常深入,那么还是不要看了.对初学者来说只能是打击.因为很多东西都看不懂.一本知识较为浅显,并且说明非常详细,但是能让你上手的基础知识又非常完善的书籍就非常好.( ...

  4. QTableWidget控件总结

    [1]QTableWidget简介 QTableWidget是QT对话框设计中常用的显示数据表格的控件. 学习QTableWidget就要首先看看QTableView控件(控件也是有”家世“的!就像研 ...

  5. VLAN

    VLAN  VLAN技术要点主要有两点: 1.支持VLAN的交换机的内部交换原理: 2.设备之间(交换机之间,交换机与路由器之间,交换机与主机之间)交互时,VLAN TAG的添加和移除. VLAN通信 ...

  6. oracle 11g安装&lpar;转&rpar;

    原文地址:http://www.2cto.com/database/201208/150620.html 一.Oracle 下载 注意Oracle分成两个文件,下载完后,将两个文件解压到同一目录下即可 ...

  7. paper 8:支持向量机系列五:Numerical Optimization —— 简要介绍求解求解 SVM 的数值优化算法。

    作为支持向量机系列的基本篇的最后一篇文章,我在这里打算简单地介绍一下用于优化 dual 问题的 Sequential Minimal Optimization (SMO) 方法.确确实实只是简单介绍一 ...

  8. &ast;&ast;IOS&colon;xib文件解析&lpar;xib和storyboard的比较,一个轻量级一个重量级&rpar;

    使用Xcode做iOS项目,经常会和Xib文件打交道,因为Xib文件直观的展现出运行时视图的外观,所以上手非常容易,使用也很方便,但对于从未用纯代码写过视图的童鞋,多数对Xib的理解有些片面. Xib ...

  9. SCADESuite嵌入式软件基于模型的开发

    SCADE Suite®产品是针对高安全性嵌入式软件的基于模型的开发环境 SCADE Suite是高安全性嵌入式软件的开发标准,其应用领域涵盖航空.国防.轨道交通.能源和重工业.专为最高等级的质量和安 ...

  10. docker WARNING&colon; IPv4 forwarding is disabled 问题解决

    问题: [yuyongxr@localhost ~]$sudo docker run -d --name nginx -p : nginx WARNING: IPv4 forwarding is di ...