Codeforces Round #162 (Div. 2) A~D 题解

A. Colorful Stones (Simplified Edition)

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

There is a sequence of colorful stones. The color of each stone is one of red, green, or blue. You are given a string s. The i-th (1-based) character of s represents the color of the i-th stone. If the character is "R", "G", or "B", the color of the corresponding stone is red, green, or blue, respectively.

Initially Squirrel Liss is standing on the first stone. You perform instructions one or more times.

Each instruction is one of the three types: "RED", "GREEN", or "BLUE". After an instruction c, if Liss is standing on a stone whose colors is c, Liss will move one stone forward, else she will not move.

You are given a string t. The number of instructions is equal to the length of t, and the i-th character of t represents the i-th instruction.

Calculate the final position of Liss (the number of the stone she is going to stand on in the end) after performing all the instructions, and print its 1-based position. It is guaranteed that Liss don‘t move out of the sequence.

Input

The input contains two lines. The first line contains the string s (1?≤?|s|?≤?50). The second line contains the string t (1?≤?|t|?≤?50). The characters of each string will be one of "R", "G", or "B". It is guaranteed that Liss don‘t move out of the sequence.

Output

Print the final 1-based position of Liss in a single line.

Examples

Input

Copy

RGBRRR

Output

Copy

2

Input

Copy

RRRBGBRBBBBBBRR

Output

Copy

3

Input

Copy

BRRBGBRGRBGRGRRGGBGBGBRGBRGRGGGRBRRRBRBBBGRRRGGBBBBBRBGGRGRGBBBRBGRBRBBBBRBRRRBGBBGBBRRBBGGRBRRBRGRB

Output

Copy

15模拟;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 200005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-5
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;

inline int rd() {
	int x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == ‘-‘) f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; }

/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) {
		x = 1; y = 0; return a;
	}
	ans = exgcd(b, a%b, x, y);
	ll t = x; x = y; y = t - a / b * y;
	return ans;
}
*/
string s, t;

int main()
{
	ios::sync_with_stdio(0);
	cin >> s >> t;
	int lens = s.length();
	int lent = t.length();
	int pos = 0;
	int st = 0;
	while (st < lent) {
		if (t[st] == s[pos]) {
			pos++; st++;
		}
		else st++;
	}
	printf("%d\n", pos + 1);
	return 0;
}

B. Roadside Trees (Simplified Edition)

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Squirrel Liss loves nuts. There are n trees (numbered 1 to n from west to east) along a street and there is a delicious nut on the top of each tree. The height of the tree i is hi. Liss wants to eat all nuts.

Now Liss is on the root of the tree with the number 1. In one second Liss can perform one of the following actions:

  • Walk up or down one unit on a tree.
  • Eat a nut on the top of the current tree.
  • Jump to the next tree. In this action the height of Liss doesn‘t change. More formally, when Liss is at height h of the tree i (1?≤?i?≤?n?-?1), she jumps to height h of the tree i?+?1. This action can‘t be performed if h?>?hi?+?1.

Compute the minimal time (in seconds) required to eat all nuts.

Input

The first line contains an integer n (1??≤??n?≤?105) — the number of trees.

Next n lines contains the height of trees: i-th line contains an integer hi (1?≤?hi?≤?104) — the height of the tree with the number i.

Output

Print a single integer — the minimal time required to eat all nuts in seconds.

Examples

Input

Copy

212

Output

Copy

5

Input

Copy

521211

Output

Copy

14
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 200005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-5
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;

inline int rd() {
	int x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == ‘-‘) f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; }

/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) {
		x = 1; y = 0; return a;
	}
	ans = exgcd(b, a%b, x, y);
	ll t = x; x = y; y = t - a / b * y;
	return ans;
}
*/
int n;
int h[maxn];
int df[maxn];
int main()
{
//	ios::sync_with_stdio(0);
	n = rd();
	for (int i = 1; i <= n; i++) {
		h[i] = rd();
	}
	ll ans = 0;
	for (int i = 1; i < n; i++)df[i] = h[i + 1] - h[i];
	ans += 1ll*(h[1] + 1);
	for (int i = 2; i <= n; i++) {
		if (h[i] >= h[i - 1]) {
			ans += 1ll*(1 + 1 + h[i] - h[i - 1]);
		}
		else {
			ans += 1ll*(h[i - 1] - h[i] + 1 + 1);
		}
	}
	printf("%lld\n", ans);
	return 0;
}

C. Escape from Stones

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Squirrel Liss lived in a forest peacefully, but unexpected trouble happens. Stones fall from a mountain. Initially Squirrel Liss occupies an interval [0,?1]. Next, n stones will fall and Liss will escape from the stones. The stones are numbered from 1 to n in order.

The stones always fall to the center of Liss‘s interval. When Liss occupies the interval [k?-?d,?k?+?d] and a stone falls to k, she will escape to the left or to the right. If she escapes to the left, her new interval will be [k?-?d,?k]. If she escapes to the right, her new interval will be [k,?k?+?d].

You are given a string s of length n. If the i-th character of s is "l" or "r", when the i-th stone falls Liss will escape to the left or to the right, respectively. Find the sequence of stones‘ numbers from left to right after all the n stones falls.

Input

The input consists of only one line. The only line contains the string s (1?≤?|s|?≤?106). Each character in s will be either "l" or "r".

Output

Output n lines — on the i-th line you should print the i-th stone‘s number from the left.

Examples

Input

Copy

llrlr

Output

Copy

35421

Input

Copy

rrlll

Output

Copy

12543

Input

Copy

lrlrr

Output

Copy

24531

Note

In the first example, the positions of stones 1, 2, 3, 4, 5 will be , respectively. So you should print the sequence: 3, 5, 4, 2, 1.

偏思维一点,找到规律就行了;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 1000005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-5
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;

inline int rd() {
	int x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == ‘-‘) f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; }

/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) {
		x = 1; y = 0; return a;
	}
	ans = exgcd(b, a%b, x, y);
	ll t = x; x = y; y = t - a / b * y;
	return ans;
}
*/

string s;
int pos[maxn];
int main()
{
//	ios::sync_with_stdio(0);
	cin >> s;
	int lens = s.length();
	int ed = lens - 1;
	int st = 0;
	int tot = 0;
	for (int i = 0; i < lens; i++) {
		if (s[i] == ‘l‘) {
			pos[ed] = (i + 1); ed--;
		}
		else {
			pos[st] = (i + 1); st++;
		}
	}
	for (int i = 0; i < lens; i++) {
		printf("%d\n", pos[i]);
	}
	return 0;
}

D. Good Sequences

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Squirrel Liss is interested in sequences. She also has preferences of integers. She thinks n integers a1,?a2,?...,?an are good.

Now she is interested in good sequences. A sequence x1,?x2,?...,?xk is called good if it satisfies the following three conditions:

  • The sequence is strictly increasing, i.e. xi?<?xi?+?1 for each i (1?≤?i?≤?k?-?1).
  • No two adjacent elements are coprime, i.e. gcd(xi,?xi?+?1)?>?1 for each i (1?≤?i?≤?k?-?1) (where gcd(p,?q) denotes the greatest common divisor of the integers p and q).
  • All elements of the sequence are good integers.

Find the length of the longest good sequence.

Input

The input consists of two lines. The first line contains a single integer n (1?≤?n?≤?105) — the number of good integers. The second line contains a single-space separated list of good integers a1,?a2,?...,?an in strictly increasing order (1?≤?ai?≤?105ai?<?ai?+?1).

Output

Print a single integer — the length of the longest good sequence.

Examples

Input

Copy

52 3 4 6 9

Output

Copy

4

Input

Copy

91 2 3 5 6 7 8 9 10

Output

Copy

4

Note

In the first example, the following sequences are examples of good sequences: [2; 4; 6; 9], [2; 4; 6], [3; 9], [6]. The length of the longest good sequence is 4.

有趣的dp题目;

考虑枚举因数;

用dp[i]表示以因数i结尾时的最大值;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 1000005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-5
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;

inline int rd() {
	int x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == ‘-‘) f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; }

/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) {
		x = 1; y = 0; return a;
	}
	ans = exgcd(b, a%b, x, y);
	ll t = x; x = y; y = t - a / b * y;
	return ans;
}
*/

int n;
int a[maxn];
int dp[maxn];
int ans;

void sol(int x) {
	int maxx = -inf;
	for (int i = 2; i <= sqrt(x); i++) {
		if (x%i == 0) {
			maxx = max(max(maxx, dp[i]), dp[x / i]);
		}
	}
	maxx = max(maxx, dp[x]);
	for (int i = 2; i <= sqrt(x); i++) {
		if (x%i == 0) {
			dp[i] = maxx + 1; dp[x / i] = maxx + 1;
			while (x%i == 0)x /= i;
		}
	}
	dp[x] = maxx + 1; ans = max(ans, maxx + 1);
}

int main()
{
//	ios::sync_with_stdio(0);
	n = rd();
	for (int i = 1; i <= n; i++) {
		a[i] = rd();
	}
	for (int i = 1; i <= n; i++)sol(a[i]);
	printf("%d\n", ans);
	return 0;
}


原文地址:https://www.cnblogs.com/zxyqzy/p/10353212.html

时间: 2024-11-05 02:40:17

Codeforces Round #162 (Div. 2) A~D 题解的相关文章

Codeforces Round #198 (Div. 2)A,B题解

Codeforces Round #198 (Div. 2) 昨天看到奋斗群的群赛,好奇的去做了一下, 大概花了3个小时Ak,我大概可以退役了吧 那下面来稍微总结一下 A. The Wall Iahub and his friend Floyd have started painting a wall. Iahub is painting the wall red and Floyd is painting it pink. You can consider the wall being mad

Codeforces Round #246 (Div. 2) (ABCD详细题解)

比赛链接:http://codeforces.com/contest/432 A. Choosing Teams time limit per test:1 second memory limit per test:256 megabytes The Saratov State University Olympiad Programmers Training Center (SSU OPTC) has n students. For each student you know the numbe

Codeforces Round #162 (Div. 1) B. Good Sequences (dp+分解素数)

题目:http://codeforces.com/problemset/problem/264/B 题意:给你一个递增序列,然后找出满足两点要求的最长子序列 第一点是a[i]>a[i-1] 第二点 gcd(a[i],a[i-1])>1 也就是说两个数不能互质 找出最长的子序列长度 思路:首先想互质问题,如果两个数互质说明两个数之间没有素因子相同,我们想以每个素因子结尾的最大长度是多少 然后比如样例 2 3 4 6 9 第一个数 2      2结尾 1 第二个数 3      3结尾 1 第三

Codeforces Round #609 (Div. 2) A-E简要题解

contest链接:https://codeforces.com/contest/1269 A. Equation 题意:输入一个整数,找到一个a,一个b,使得a-b=n,切a,b都是合数 思路:合数非常多,从1开始枚举b,a就是b+n,每次check一下a,b是否是合数,是的话直接输出,break即可 AC代码: 1 #include<iostream> 2 #include<string> 3 #include<vector> 4 #include<cstri

Codeforces Round #615 (Div. 3) A-F简要题解

contest链接:https://codeforces.com/contest/1294 A. 给出a.b.c三个数,从n中分配给a.b.c,问能否使得a = b = c.计算a,b,c三个数的差值之和,n对其取余,判断是否为0即可. AC代码: 1 #include<iostream> 2 #include<vector> 3 #include<algorithm> 4 #include<cmath> 5 #include<cstring>

Codeforces Round #162 (Div. 1) B dp

//存入所有数的素数因数 //若两个数不互质,那么他们之间必然有素数因数 //dp[i][0]表示第i个数不选前i个数中能得到的最长序列 //dp[i][1]表示选了第i个数 //dp[i][0] = max(dp[i-1][0] , dp[i-1][1]) //dp[i][1] = max(dp[pos][1] + 1 ,dp[i][1] ); //pos位第i个数的质数因子出现的最后一个位置 #include<cstdio> #include<cstring> #include

Codeforces Round #162 (Div. 1) C Choosing Balls dp

//dp[i] 表示以颜色为i结尾的最大值 //dp[i] = max(dp[i] , dp[i] + a*v[i] ,other_max + b*v[i]) ; //为除颜色i以外的其它颜色的最大值 #include<cstdio> #include<cstring> #include<iostream> using namespace std ; const int maxn = 100010 ; const __int64 inf = 0x7fffffffffff

Codeforces Round #162 (Div. 1) A. Escape from Stones

A. Escape from Stones time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Squirrel Liss lived in a forest peacefully, but unexpected trouble happens. Stones fall from a mountain. Initially Squ

Codeforces Round #175 (Div. 2) A~D 题解

A.Slightly Decreasing Permutations Permutation p is an ordered set of integers p1,??p2,??...,??pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n th