Educational Codeforces Round 32 A B C

A. Local Extrema
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an array a. Some element of this array ai is a local minimum iff it is strictly less than both of its neighbours (that is, ai < ai - 1 and ai < ai + 1). Also the element can be called local maximum iff it is strictly greater than its neighbours (that is, ai > ai - 1 and ai > ai + 1). Since a1 and an have only one neighbour each, they are neither local minima nor local maxima.

An element is called a local extremum iff it is either local maximum or local minimum. Your task is to calculate the number of local extrema in the given array.

Input

The first line contains one integer n (1 ≤ n ≤ 1000) — the number of elements in array a.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000) — the elements of array a.

Output

Print the number of local extrema in the given array.

Examples

Input

31 2 3

Output

0

Input

41 5 2 5

Output

2

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3+66;
int a[maxn];
int mark[maxn];
int main(){
	ios_base::sync_with_stdio(false); cin.tie(0);
	int n;
	memset(mark,0,sizeof(mark));
	cin >> n;
	for( int i = 0 ; i < n ; i++ ){
		cin >> a[i] ;
	}
	int res = 0;
	for( int i = 1 ; i < n - 1 ; i++ ){
		if( !mark[i] && a[i] < a[i-1] && a[i] < a[i+1] ){
			res ++ ;
			mark[i] = 1;
		}
		if( !mark[i] && a[i] > a[i-1] && a[i] > a[i+1] ){
			res ++ ;
			mark[i] = 1;
		}
	}
	cout << res << endl;
	return 0;
}
B. Buggy Robot
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Ivan has a robot which is situated on an infinite grid. Initially the robot is standing in the starting cell (0, 0). The robot can process commands. There are four types of commands it can perform:

  • U — move from the cell (x, y) to (x, y + 1);
  • D — move from (x, y) to (x, y - 1);
  • L — move from (x, y) to (x - 1, y);
  • R — move from (x, y) to (x + 1, y).

Ivan entered a sequence of n commands, and the robot processed it. After this sequence the robot ended up in the starting cell (0, 0), but Ivan doubts that the sequence is such that after performing it correctly the robot ends up in the same cell. He thinks that some commands were ignored by robot. To acknowledge whether the robot is severely bugged, he needs to calculate the maximum possible number of commands that were performed correctly. Help Ivan to do the calculations!

Input

The first line contains one number n — the length of sequence of commands entered by Ivan (1 ≤ n ≤ 100).

The second line contains the sequence itself — a string consisting of n characters. Each character can be U, D, L or R.

Output

Print the maximum possible number of commands from the sequence the robot could perform to end up in the starting cell.

Examples
Input
4
LDUR
Output
4
Input
5
RRRUU
Output
0
Input
6
LLRRRR
Output
4
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e2+66;
char a[maxn];
map<char,int>mp;
int main(){	
	int n;
	cin >> n;
	int res = 0;
	for( int i = 0 ; i < n ; i++ ){
		cin >> a[i] ;
		mp[a[i]] ++;
		if( a[i] == 'U' || a[i] == 'D' ){
			if( mp['D'] && mp['U'] ){
				int tmp = min(mp['U'] , mp['D']);
				res += tmp * 2;
				mp['D'] -= tmp;
				mp['U'] -= tmp;
			} 
		}else if( a[i] == 'R' || a[i] == 'L' ){
			if( mp['R'] && mp['L'] ){
				int tmp = min(mp['R'] , mp['L']);
				res += tmp * 2;
				mp['R'] -= tmp;
				mp['L'] -= tmp;
			} 
		}
	}
	cout << res << endl;
	return 0;
}

C. K-Dominant Character
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a string s consisting of lowercase Latin letters. Character c is called k-dominant iff each substring of s with length at least k contains this character c.

You have to find minimum k such that there exists at least one k-dominant character.

Input

The first line contains string s consisting of lowercase Latin letters (1 ≤ |s| ≤ 100000).

Output

Print one number — the minimum value of k such that there exists at least one k-dominant character.

Examples
Input
abacaba
Output
2
Input
zzzzz
Output
1
Input
abcde
Output
3
思路:取每个字母两两相邻的最大值,然后在出现的字母中取距离最小的那个。
Code:
#include <bits/stdc++.h>
using namespace std;
int a[26];
int x[26];
int main(){
	string s;
	cin >> s;
	int len = s.size();
	memset(x,-1,sizeof(x));
	memset(a,-1,sizeof(a));
	for( int i = 0 ; i < len ; i++ ){
		int tmp = s[i] - 'a';
		if( a[tmp] == -1 ){
			x[tmp] = i + 1;
			a[tmp] = i; continue;
		}else{
			x[tmp] = max( x[tmp] , i - a[tmp] );
			a[tmp] = i;
		}
	}
	for( int i = 0 ; i < 26 ; i++ ){
		x[i] = max( len - a[i] , x[i] );
	}
	int res = 0x3f3f3f;
	for( int i = 0 ; i < 26 ; i ++ ){
		if( a[i] != -1 ){
			if( x[i] != -1 ) res = min(res,x[i]);
		}
	}
	cout << res << endl;
	
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值