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 or i + 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.
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.
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.
题目大意: 一个只包含'<', '>'的数组, 任意选择一个开始的地方如果是'<' 就向左移动一位, 是'>'就向右移动一位, 问有几个开始位置可以使小球最终掉落到地面?
题目分析: 从反面考虑, 只有"><"会使小球永远不掉到地上, 同时'>'左面的连续的'>'开始也不会掉, 同理右边。 这样问题就是求'><'右面和左面的连续的'>' 和 '<'最后拿n 减 ,还有些细节需要注意。
题目代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#include#include #include #include #include using namespace std;char a[200005];int idx[200005];int main() {// freopen( "in.txt", "r", stdin ); int n; int count; int ans; while( cin >> n && n ) { ans = 0; count = 0; memset( a, 0, sizeof( a ) ); memset( idx, 0, sizeof( idx ) ); for( int i = 1; i <= n; i++ ) { cin >> a[i]; if( a[i] == '<' && a[i-1] == '>' ) { idx[count++] = i; ans += 2; } } for( int i = 0; i < count; i++ ) { int left = idx[i] - 1; int right = idx[i]; while( 1 ) { if( a[--left] == '>' ) { ans++; } else break; } while( 1 ) { if( a[++right] == '<' ) { ans++; } else break; } } cout << n - ans << endl; } return 0;}
A new airplane SuperPuperJet has an infinite number of rows, numbered with positive integers starting with 1 from cockpit to tail. There are six seats in each row, denoted with letters from 'a' to 'f'. Seats 'a', 'b' and 'c' are located to the left of an aisle (if one looks in the direction of the cockpit), while seats 'd', 'e' and 'f' are located to the right. Seats 'a' and 'f' are located near the windows, while seats 'c' and 'd' are located near the aisle.
![](http://codeforces.com/predownloaded/ea/33/ea33130a9c92fe0622a46c8d9840bf807e6aab55.png)
It's lunch time and two flight attendants have just started to serve food. They move from the first rows to the tail, always maintaining a distance of two rows from each other because of the food trolley. Thus, at the beginning the first attendant serves row 1 while the second attendant serves row 3. When both rows are done they move one row forward: the first attendant serves row 2 while the second attendant serves row 4. Then they move three rows forward and the first attendant serves row 5 while the second attendant serves row 7. Then they move one row forward again and so on.
Flight attendants work with the same speed: it takes exactly 1 second to serve one passenger and 1 second to move one row forward. Each attendant first serves the passengers on the seats to the right of the aisle and then serves passengers on the seats to the left of the aisle (if one looks in the direction of the cockpit). Moreover, they always serve passengers in order from the window to the aisle. Thus, the first passenger to receive food in each row is located in seat 'f', and the last one — in seat 'c'. Assume that all seats are occupied.
Vasya has seat s in row n and wants to know how many seconds will pass before he gets his lunch.
The only line of input contains a description of Vasya's seat in the format ns, where n (1 ≤ n ≤ 1018) is the index of the row and s is the seat in this row, denoted as letter from 'a' to 'f'. The index of the row and the seat are not separated by a space.
Print one integer — the number of seconds Vasya has to wait until he gets his lunch.
if( ( n - 1 ) % 4 == 0 || ( n - 3 ) % 4 == 0 ) { ans += 16 * ( ( n - 1 ) / 4 ); } else if ( ( n - 2 ) % 4 == 0 || n % 4 == 0 ) { ans += 16 * ( ( n - 2 ) / 4 ) + 7; }
附上代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#include#include #include #include