Codeforces Round #354 (Div. 2)

来源:互联网 发布:安居客网络经纪人app 编辑:程序博客网 时间:2024/05/02 23:50

此文章可以使用目录功能哟↑(点击上方[+])

感觉这次cf代码打得都比较挫,但没想到都能过终测,等了好久的cf,这次时间提早终于也让我水了一次...

链接→Codeforces Round #354 (Div. 2)

 Problem A Nicholas and Permutation

Accept: 0    Submit: 0
Time Limit: 1 second    Memory Limit : 256 megabytes

 Problem Description

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.

 Sample Input

5
4 5 1 3 2
7
1 6 5 3 4 7 2
6
6 5 4 3 2 1

 Sample Output

3
6
5

 Problem Idea

解题思路:给你n个不同的元素,选择任意两数交换一次,使得n个数中的最大值与最小值距离(下标之差的绝对值)最大,问距离为多少

着实不太好意思讲这道题,毕竟自己都觉得这道题的代码敲得非常挫

个人做法是,分别将最大值与最小值和数组第一个元素与最后一个元素对调,取最大距离


题目链接→Codeforces 676A Nicholas and Permutation

 Source Code

/*Sherlock and Watson and Adler*/#pragma comment(linker, "/STACK:1024000000,1024000000")#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>#include<stack>#include<math.h>#include<vector>#include<map>#include<set>#include<cmath>#include<complex>#include<string>#include<algorithm>#include<iostream>#define exp 1e-10using namespace std;const int N = 105;const int M = 40;const int inf = 100000000;const int mod = 2009;int s[N];int main(){    int n,Max=0,Min=inf,l,r,ans,i;    scanf("%d",&n);    for(i=0;i<n;i++)    {        scanf("%d",&s[i]);        if(Max<s[i])            Max=s[l=i];        if(Min>s[i])            Min=s[r=i];        ans=abs(l-r);    }    swap(s[0],s[l]);    for(i=0;i<n;i++)        if(s[i]==Min)            ans=max(ans,i);    swap(s[0],s[l]);    swap(s[n-1],s[l]);    for(i=0;i<n;i++)        if(s[i]==Min)            ans=max(ans,n-1-i);    swap(s[n-1],s[l]);    swap(s[0],s[r]);    for(i=0;i<n;i++)        if(s[i]==Max)            ans=max(ans,i);    swap(s[0],s[r]);    swap(s[n-1],s[r]);    for(i=0;i<n;i++)        if(s[i]==Max)            ans=max(ans,n-1-i);    printf("%d\n",ans);    return 0;}

 Problem B Pyramid of Glasses

Accept: 0    Submit: 0
Time Limit: 1 second   Memory Limit : 256 megabytes

 Problem Description

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.

 Sample Input

Test 1
3 5
Test 2
4 8

 Sample Output

Test 1
4
Test 2
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.

 Problem Idea

解题思路:酒杯叠成的金字塔,从上往下,第一层1个酒杯,第二层2个酒杯...第n层n个酒杯

现在从最顶层往下倒酒,以每秒能够倒满最上面那个杯酒的量倒酒,问第t秒能倒满金字塔中几个杯子的酒,如上图

这道题如果要自己去一个个判断每个酒杯在第几秒能被倒满,着实有点复杂,因为越下层,情况越多,比如从第三层开始,中间的酒杯能接受到的量要比两边来得多

故,此题可以直接采取暴力的做法

对于每一秒,我们给最顶层的酒杯内加上单位1的酒,然后从上往下处理每个酒杯,若酒杯里的酒超过了单位1,那我们就把超过的部分平均分成两份,加到它正下方的两个酒杯里

最终,我们只要判断有几个杯子里的酒是单位1的就可以了

题目链接→Codeforces 676B Pyramid of Glasses

 Source Code

/*Sherlock and Watson and Adler*/#pragma comment(linker, "/STACK:1024000000,1024000000")#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>#include<stack>#include<math.h>#include<vector>#include<map>#include<set>#include<cmath>#include<complex>#include<string>#include<algorithm>#include<iostream>#define exp 1e-10using namespace std;const int N = 100005;const int M = 40;const int inf = 100000000;const int mod = 2009;double s[12][12];int main(){    int n,t,i,j,k,ans=0;    scanf("%d%d",&n,&t);    for(i=1;i<=t;i++)    {        s[1][1]+=1.0;        for(j=1;j<=n;j++)            for(k=1;k<=j;k++)                if(s[j][k]>1.0)                {                    s[j+1][k]+=(s[j][k]-1.0)/2;                    s[j+1][k+1]+=(s[j][k]-1.0)/2;                    s[j][k]=1.0;                }    }    for(j=1;j<=n;j++)        for(k=1;k<=j;k++)        {            //printf("%lf\n",s[j][k]);            if(s[j][k]==1.0)                ans++;        }    printf("%d\n",ans);    return 0;}

 Problem C Vasya and String

Accept: 0    Submit: 0
Time Limit: 1 second    Memory Limit : 256 megabytes

 Problem Description

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.

 Sample Input

Test 1
4 2
abba
Test 2
8 1
aabaabaa

 Sample Output

Test 1
4
Test 2
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".

 Problem Idea

解题思路:给你一个只含有'a'和'b'的字符串,要求至多修改(a->b或b->a)k个字符,使得含有相同字符的连续子串长度最长

如样例1,你可以修改2个字符,故我把2个a变成b,或把2个b变成a,都可以使得最终长度为4

此题可以采用尺取法,除了k=0的情况,长度已经固定,其他情况下,我用两个下标,分别指向区间左端和右端,从第一个字符开始,往后延伸,直到k次修改完之后又遇到了要修改的字符,此时移动左端下标至又可以修改字符为止,这样就能取到最大长度

题目链接→Codeforces 676C Vasya and String

 Source Code

/*Sherlock and Watson and Adler*/#pragma comment(linker, "/STACK:1024000000,1024000000")#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>#include<stack>#include<math.h>#include<vector>#include<map>#include<set>#include<cmath>#include<complex>#include<string>#include<algorithm>#include<iostream>#define exp 1e-10using namespace std;const int N = 100005;const int M = 40;const int inf = 100000000;const int mod = 2009;char s[N];int main(){    int n,k,p,i,j,ans=1,sum=0;    scanf("%d%d",&n,&k);    scanf("%s",s);p=k;    if(k==0)    {        for(sum=i=1;i<n;i++)            if(s[i]!=s[i-1])            {                ans=max(ans,sum);                sum=1;            }            else                sum++;        ans=max(ans,sum);        printf("%d\n",ans);        return 0;    }    for(j=i=0;i<n;i++)    {        if(s[i]=='a')            sum++;        else        {            if(k>0)                sum++,k--;            else            {                i--;                while(!k)                {                    if(s[j]!='a')                        k++;                    j++,sum--;                }            }        }        ans=max(ans,sum);    }    for(k=p,sum=j=i=0;i<n;i++)    {        if(s[i]=='b')            sum++;        else        {            if(k>0)                sum++,k--;            else            {                i--;                while(!k)                {                    if(s[j]!='b')                        k++;                    j++,sum--;                }            }        }        ans=max(ans,sum);    }    printf("%d\n",ans);    return 0;}

菜鸟成长记

1 0
原创粉丝点击