Codeforces Round #490 (Div. 3)--Mishka and Contest,Reversing Encryption,Alphabetic Removals

A. Mishka and Contest
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Mishka started participating in a programming contest. There are nn problems in the contest. Mishka's problem-solving skill is equal to kk.

Mishka arranges all problems from the contest into a list. Because of his weird principles, Mishka only solves problems from one of the ends of the list. Every time, he chooses which end (left or right) he will solve the next problem from. Thus, each problem Mishka solves is either the leftmost or the rightmost problem in the list.

Mishka cannot solve a problem with difficulty greater than kk. When Mishka solves the problem, it disappears from the list, so the length of the list decreases by 11. Mishka stops when he is unable to solve any problem from any end of the list.

How many problems can Mishka solve?

Input

The first line of input contains two integers nn and kk (1n,k1001≤n,k≤100) — the number of problems in the contest and Mishka's problem-solving skill.

The second line of input contains nn integers a1,a2,,ana1,a2,…,an (1ai1001≤ai≤100), where aiai is the difficulty of the ii-th problem. The problems are given in order from the leftmost to the rightmost in the list.

Output

Print one integer — the maximum number of problems Mishka can solve.

Examples
input
Copy
8 4
4 2 3 1 5 1 6 4
output
Copy
5
input
Copy
5 2
3 1 2 1 3
output
Copy
0
input
Copy
5 100
12 34 55 43 21
output
Copy
5
Note

In the first example, Mishka can solve problems in the following order: 
[4,2,3,1,5,1,6,4][2,3,1,5,1,6,4][2,3,1,5,1,6][3,1,5,1,6][1,5,1,6][5,1,6][4,2,3,1,5,1,6,4]→[2,3,1,5,1,6,4]→[2,3,1,5,1,6]→[3,1,5,1,6]→[1,5,1,6]→[5,1,6]
, so the number of solved problems will be equal to  5
5

In the second example, Mishka can't solve any problem because the difficulties of problems from both ends are greater than kk.

In the third example, Mishka's solving skill is so amazing that he can solve all the problems.

思路:只需要两边分别判断即可,然后循环。AC代码:

#include<stdio.h>
int n,k;
int a[110];
int main()
{
	scanf("%d%d",&n,&k);
	for(int i=0;i<n;i++) scanf("%d",&a[i]);
	int i=0,j=n-1;
	int sum=0;
	while(i<=j)
	{
		if(a[i]<=k) sum++,i++;
		else if(a[j]<=k) sum++,j--;
		else if(a[i]>k && a[j]>k) break;
	}
	printf("%d\n",sum);
	return 0;
}

B. Reversing Encryption
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A string ss of length nn can be encrypted by the following algorithm:

  • iterate over all divisors of nn in decreasing order (i.e. from nn to 11),
  • for each divisor dd, reverse the substring s[1d]s[1…d] (i.e. the substring which starts at position 11 and ends at position dd).

For example, the above algorithm applied to the string ss="codeforces" leads to the following changes: "codeforces "secrofedoc "orcesfedoc "rocesfedoc "rocesfedoc" (obviously, the last reverse operation doesn't change the string because d=1d=1).

You are given the encrypted string tt. Your task is to decrypt this string, i.e., to find a string ss such that the above algorithm results in string tt. It can be proven that this string ss always exists and is unique.

Input

The first line of input consists of a single integer nn (1n1001≤n≤100) — the length of the string tt. The second line of input consists of the string tt. The length of tt is nn, and it consists only of lowercase Latin letters.

Output

Print a string ss such that the above algorithm results in tt.

Examples
input
Copy
10
rocesfedoc
output
Copy
codeforces
input
Copy
16
plmaetwoxesisiht
output
Copy
thisisexampletwo
input
Copy
1
z
output
Copy
z
Note

The first example is described in the problem statement.

思路:按照题目的意思模拟即可,每次判断当前数是否可以整除n。AC代码:

#include<stdio.h>
#include<string.h>
int n;
char s[110];
int a[10];
void f(int i,int j)
{
	while(i<j)
	{
		char c=s[i];
		s[i]=s[j];
		s[j]=c;
		i++;
		j--;
	}
}
int main()
{
	scanf("%d",&n);
	scanf("%s",s+1);
	for(int i=1;i<=n;i++)
	{
		if(n%i) continue;
		f(1,i);
	}
	printf("%s\n",s+1);
	return 0;
}
C. Alphabetic Removals
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a string ss consisting of nn lowercase Latin letters. Polycarp wants to remove exactly kk characters (knk≤n) from the string ss. Polycarp uses the following algorithm kk times:

  • if there is at least one letter 'a', remove the leftmost occurrence and stop the algorithm, otherwise go to next item;
  • if there is at least one letter 'b', remove the leftmost occurrence and stop the algorithm, otherwise go to next item;
  • ...
  • remove the leftmost occurrence of the letter 'z' and stop the algorithm.

This algorithm removes a single letter from the string. Polycarp performs this algorithm exactly kk times, thus removing exactly kk characters.

Help Polycarp find the resulting string.

Input

The first line of input contains two integers nn and kk (1kn41051≤k≤n≤4⋅105) — the length of the string and the number of letters Polycarp will remove.

The second line contains the string ss consisting of nn lowercase Latin letters.

Output

Print the string that will be obtained from ss after Polycarp removes exactly kk letters using the above algorithm kk times.

If the resulting string is empty, print nothing. It is allowed to print nothing or an empty line (line break).

Examples
input
Copy
15 3
cccaabababaccbc
output
Copy
cccbbabaccbc
input
Copy
15 9
cccaabababaccbc
output
Copy
cccccc
input
Copy
1 1
u
output
Copy
 
        

思路:先统计每个字母的个数,然后判断k在哪个字母的位置即可完成删减。AC代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
int n,k;
string s;
int a[27];
int main()
{
	cin>>n>>k;
	cin>>s;
	memset(a,0,sizeof(a));
	for(int i=0;i<s.size();i++)
	{
		int t=s[i]-96;
		a[t]++;
	}
	int x=-1;
	for(int i=1;i<27;i++)
	{
		if(k<=a[i])
		{
			x=i;
			break;
		}
		k-=a[i];
	}
	for(int i=0;i<s.size();i++)
	{
		int t=s[i]-96;
		if(t<x) continue;
		if(t==x && k)
		{
			k--;
			continue;
		}
		cout<<s[i];
	}
	cout<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/zhengxiangmaomao/article/details/80778353