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

7422 ワード

今日このAは4つの问题を出して、ハハハ、テーマの水の駄目なことができますが、やはりとても楽しくて、その上1つの问题は最后の1分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 a1, a2, ..., 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
#include
#include
#include
#include
#include
#include
#include
#include
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;ia[i-1]&&a[i]>a[i+1])
            ans++;
        else if(a[i]

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
#include
#include
#include
#include
#include
#include
#include
#include
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

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
#include
#include
#include
#include
#include
#include
#include
#include
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

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 ≤ 1000, 1 ≤ 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
#include
#include
#include
#include
#include
#include
#include
#include
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