Codeforces-Educational Codeforces Round 32-(A,B,C,D)

今天这场A了4道题,哈哈哈哈,虽然题目水的不行,还是挺开心的,而且有一道题是最后一分钟AC。。。先贴代码


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 - 1and 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 a1a2, ..., an (1 ≤ ai ≤ 1000) — the elements of array a.

Output

Print the number of local extrema in the given array.

Examples
input
3
1 2 3
output
0
input
4
1 5 2 5
output
2

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define M 1005

int n;
int a[M];

int main()
{
    int i;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
        scanf("%d",&a[i]);

    int ans=0;
    for(i=2;i<n;i++)
    {
        if(a[i]>a[i-1]&&a[i]>a[i+1])
            ans++;
        else if(a[i]<a[i-1]&&a[i]<a[i+1])
            ans++;
    }
    printf("%d\n",ans);
    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 UDL 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<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define M 105

int n,ans;
int num[5];
char s[M];

int main()
{
    int i;
    scanf("%d",&n);
    scanf("%s",s);
    for(i=0;i<n;i++)
    {
        if(s[i]=='L')
            num[1]++;
        else if(s[i]=='R')
            num[2]++;
        else if(s[i]=='U')
            num[3]++;
        else if(s[i]=='D')
            num[4]++;
    }

    ans=min(num[1],num[2])*2+min(num[3],num[4])*2;
    printf("%d\n",ans);
    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 kcontains 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

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define M 100005
const int maxval=10000000000;

int len,ans;
int num[28];
int pre[28];
char s[M];

int main()
{
    int i,j;
    char ch='a';
    scanf("%s",s);
    len=strlen(s);
    for(i=0;i<26;i++)
    {
        pre[i]=-1;
        num[i]=-1;
    }

    for(i=0;i<len;i++)
    {
        int it=s[i]-'a';
        if(pre[it]!=-1)
            num[it]=max(num[it],i-pre[it]);
        else
            num[it]=i+1;
        pre[it]=i;
    }

    for(i=0;i<26;i++)
    {
        num[i]=max(num[i],len-pre[i]);
    }

    ans=maxval;
    for(i=0;i<26;i++)
    {
        if(num[i]!=0)
            ans=min(ans,num[i]);
    }

    printf("%d\n",ans);

    return 0;
}

D. Almost Identity Permutations
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A permutation p of size n is an array such that every integer from 1 to n occurs exactly once in this array.

Let's call a permutation an almost identity permutation iff there exist at least n - k indices i (1 ≤ i ≤ n) such that pi = i.

Your task is to count the number of almost identity permutations for given numbers n and k.

Input

The first line contains two integers n and k (4 ≤ n ≤ 10001 ≤ k ≤ 4).

Output

Print the number of almost identity permutations for given n and k.

Examples
input
4 1
output
1
input
4 2
output
7
input
5 3
output
31
input
5 4
output
76

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define M 100005

ll ans;
ll v[6]={0,0,1,2,9};
ll p[1005][5];
void init(ll i,ll t)
{
    ll j,k,temp1,temp2;
    for(j=1;j<=t;j++)
    {
        temp1=temp2=1;
        for(k=0;k<j;k++)
            temp1=temp1*(i-k);
        for(k=1;k<=j;k++)
            temp2=temp2*k;
        p[i][j]=temp1/temp2;
    }
}

int main()
{
    ll i,n,k;
    ll ans=0;
    scanf("%I64d",&n);
    scanf("%I64d",&k);
    init(n,k);
    ans=1;
    for(i=2;i<=k;i++)
    ans+=p[n][i]*v[i];
    printf("%I64d\n",ans);

    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值