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?
The first line of input contains two integers nn and kk (1≤n,k≤1001≤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 (1≤ai≤1001≤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.
Print one integer — the maximum number of problems Mishka can solve.
8 4 4 2 3 1 5 1 6 4
5
5 2 3 1 2 1 3
0
5 100 12 34 55 43 21
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; }
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[1…d]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.
The first line of input consists of a single integer nn (1≤n≤1001≤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.
Print a string ss such that the above algorithm results in tt.
10 rocesfedoc
codeforces
16 plmaetwoxesisiht
thisisexampletwo
1 z
z
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; }
思路:先统计每个字母的个数,然后判断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; }