Codeforces Round #354 (Div. 2)(A && B && C)

来源:互联网 发布:p2p网贷数据库 编辑:程序博客网 时间:2024/05/29 12:20

传送门


A. Nicholas and Permutation
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Nicholas has an array a that contains n distinct integers from 1 to n. In other words, Nicholas has a permutation of size n.

Nicholas want the minimum element (integer 1) and the maximum element (integer n) to be as far as possible from each other. He wants to perform exactly one swap in order to maximize the distance between the minimum and the maximum elements. The distance between two elements is considered to be equal to the absolute difference between their positions.

Input

The first line of the input contains a single integer n (2 ≤ n ≤ 100) — the size of the permutation.

The second line of the input contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n), where ai is equal to the element at the i-th position.

Output

Print a single integer — the maximum possible distance between the minimum and the maximum elements Nicholas can achieve by performing exactly one swap.

Examples
input
54 5 1 3 2
output
3
input
71 6 5 3 4 7 2
output
6
input
66 5 4 3 2 1
output
5
Note

In the first sample, one may obtain the optimal answer by swapping elements 1 and 2.

In the second sample, the minimum and the maximum elements will be located in the opposite ends of the array if we swap 7 and 2.

In the third sample, the distance between the minimum and the maximum elements is already maximum possible, so we just perform some unnecessary swap, for example, one can swap 5 and 2.


题目大意:
给定有 n 个数,然后每次只能移动最大的数 n 和最小的数 1之中的一个,让你求的就是如何移动,使移动完之后的最大的数和最小的数的下标和最大

解题思路:

这就是一个水题,其实有5种情况,然后最后排一下序就行了,分别是不动,把最大值移动到开头和结尾,把最小值移动到开头和结尾,然后就OK了,

My Code:

<span style="font-size:18px;">#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>using namespace std;const int MAXN = 1e2+5;int a[MAXN];int b[MAXN];int main(){    int n;    while(cin>>n)    {        int Max=-1, Min=105, Max_k, Min_k;        for(int i=1; i<=n; i++)        {            cin>>a[i];            if(Max < a[i])            {                Max = a[i];                Max_k = i;            }            if(Min > a[i])            {                Min = a[i];                Min_k = i;            }        }        b[0] = abs(Max_k-Min_k);        b[1] = abs(Max_k-n);        b[2] = abs(Max_k-1);        b[3] = abs(Min_k-n);        b[4] = abs(Min_k-1);        sort(b, b+5);        cout<<b[4]<<endl;    }    return 0;}</span>


B. Pyramid of Glasses
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Mary has just graduated from one well-known University and is now attending celebration party. Students like to dream of a beautiful life, so they used champagne glasses to construct a small pyramid. The height of the pyramid is n. The top level consists of only 1 glass, that stands on 2 glasses on the second level (counting from the top), then 3 glasses on the third level and so on.The bottom level consists of n glasses.

Vlad has seen in the movies many times how the champagne beautifully flows from top levels to bottom ones, filling all the glasses simultaneously. So he took a bottle and started to pour it in the glass located at the top of the pyramid.

Each second, Vlad pours to the top glass the amount of champagne equal to the size of exactly one glass. If the glass is already full, but there is some champagne flowing in it, then it pours over the edge of the glass and is equally distributed over two glasses standing under. If the overflowed glass is at the bottom level, then the champagne pours on the table. For the purpose of this problem we consider that champagne is distributed among pyramid glasses immediately. Vlad is interested in the number of completely full glasses if he stops pouring champagne in t seconds.

Pictures below illustrate the pyramid consisting of three levels.

Input

The only line of the input contains two integers n and t (1 ≤ n ≤ 10, 0 ≤ t ≤ 10 000) — the height of the pyramid and the number of seconds Vlad will be pouring champagne from the bottle.

Output

Print the single integer — the number of completely full glasses after t seconds.

Examples
input
3 5
output
4
input
4 8
output
6
Note

In the first sample, the glasses full after 5 seconds are: the top glass, both glasses on the second level and the middle glass at the bottom level. Left and right glasses of the bottom level will be half-empty.


题目大意:

就是有 n 行杯子(形状如图所示),我们要往里面倒酒,每次都是从第一行的第一个杯子开始倒,每隔一个时间单位就往第一个杯子里倒满水,求的是在 t 个时间单位里,满着的杯子的个数有多少个?

解题思路:
其实,这个题我们发现还是挺有规律的, 因为每个杯子都与它上面的两个杯子有关系,准确的说,每个杯子都是上一个杯子的一半,这个其实可以参考一下杨辉三角(我当初的灵感就是这么来的),那么我们设c[i][j],表示的是第 i 行 第 j 个杯子酒的容量(如果满了 我们就让它等于1),那么我们首先最外层一个for循环,从1-t,然后统计一下就行了

My Code:

<span style="font-size:18px;">#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>using namespace std;const double eps = 1e-9;const int MAXN = 15;double c[MAXN][MAXN];int n, t;void Init(){    for(int i=0; i<MAXN; i++)        for(int j=0; j<MAXN; j++)            c[i][j] = 0;    for(int k=0; k<t; k++)    {        c[1][1] += 1.0;        for(int i=1; i<=n; i++)        {            for(int j=1; j<=i; j++)            {                if(c[i][j]-1.0 > -eps)                {                    c[i+1][j] += (c[i][j]-1.0)/2;                    c[i+1][j+1] += (c[i][j]-1.0)/2;                    c[i][j] = 1.0;                }            }        }    }}int main(){    while(cin>>n>>t)    {        Init();        int sum = 0;        for(int i=1; i<=n; i++)            for(int j=1; j<=i; j++)                if(c[i][j]-1.0 > -eps)                    sum++;        cout<<sum<<endl;    }    return 0;}</span>


C. Vasya and String
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

High school student Vasya got a string of length n as a birthday present. This string consists of letters 'a' and 'b' only. Vasya denotesbeauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equal letters.

Vasya can change no more than k characters of the original string. What is the maximum beauty of the string he can achieve?

Input

The first line of the input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n) — the length of the string and the maximum number of characters to change.

The second line contains the string, consisting of letters 'a' and 'b' only.

Output

Print the only integer — the maximum beauty of the string Vasya can achieve by changing no more than k characters.

Examples
input
4 2abba
output
4
input
8 1aabaabaa
output
5
Note

In the first sample, Vasya can obtain both strings "aaaa" and "bbbb".

In the second sample, the optimal answer is obtained with the string "aaaaabaa" or with the string "aabaaaaa".


题目大意:

给你一个只有字母 'a' 和 'b' 的字符串,长度为n已知,然后让你最多操作 k 次,使得相同字母的长度最多,输出字母相同最长的长度

解题思路:
我用尺取做的,复杂度 O(n*log(n)) 的正好可以过,我们首先第一次尺取a,也就是找 a 相同连续最多的,因为不超过 k,遇到相同的 a ,r就向右走,否则cnt++,cnt不超过k,当找完了之后l++第二次就是找b了,然后比较就行了。


My Code:

<span style="font-size:18px;">#include <iostream>using namespace std;int main(){    int n, k;    string s;    while(cin>>n>>k>>s)    {        int l=0, r=0, cnt=0, ans=-1;        while(l<n && r<n)        {            while((cnt<k||s[r]=='a') && r<n)            {                if(s[r] != 'a')                    cnt++;                r++;            }            ans = max(ans, r-l);            ///cout<<ans<<endl;            while(s[l]=='a' && l<=r)                l++;            l++;            cnt--;        }        l=0, r=0, cnt=0;        while(l<n && r<n)        {            while((cnt<k||s[r]=='b') && r<n)            {                if(s[r] != 'b')                    cnt++;                r++;            }            ans = max(ans, r-l);            while(s[l]=='b' && l<=r)                l++;            l++;            cnt--;        }        cout<<ans<<endl;    }    return 0;}</span>


,

0 0
原创粉丝点击