Codeforces Round #378 (Div. 2)题解报告

Posted queuelovestack

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #378 (Div. 2)题解报告相关的知识,希望对你有一定的参考价值。

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

对于一个ACMer来说,做题时最难过的就是感觉自己是对的,但是就是WA,而且你对着数据也找不出你的错在哪,orz...

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

 Problem A-Grasshopper And the String

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

 Problem Description

One day, the Grasshopper was jumping on the lawn and found a piece of paper with a string. Grasshopper became interested what is the minimum jump ability he should have in order to be able to reach the far end of the string, jumping only on vowels of the English alphabet. Jump ability is the maximum possible length of his jump.

Formally, consider that at the begginning the Grasshopper is located directly in front of the leftmost character of the string. His goal is to reach the position right after the rightmost character of the string. In one jump the Grasshopper could jump to the right any distance from 1 to the value of his jump ability.


The picture corresponds to the first example.

The following letters are vowels: 'A', 'E', 'I', 'O', 'U' and 'Y'.

 Input

The first line contains non-empty string consisting of capital English letters. It is guaranteed that the length of the string does not exceed 100.

 Output

Print single integer a — the minimum jump ability of the Grasshopper (in the number of symbols) that is needed to overcome the given string, jumping only on vowels.

 Sample Input

ABABBBACFEYUKOTT
AAA

 Sample Output

4
1

 Problem Idea

解题思路:

【题意】

给你一个长度不超过100的字符串,下标从1开始

有一只蚱蜢,从下标0出发,它能沿着相邻元音字母(A、E、I、O、U、Y)跳跃,直到到达字符串外,跳跃过程如图所示


问跳跃过程中,最大的一次跳跃间隔为多少,上图答案为4,级下标3跳到下标7

【类型】
暴力
【分析】

因为蚱蜢只会相邻元音字母跳跃,所以从左到右遍历一遍,用Max记录最大跳跃间隔就可以得到结果

唯一需要注意的是不要忘记考虑蚱蜢从开始位置(下标0)跳到字符串的第一个元音字母这一段跳跃间隔以及字符串的最后一个元音字母跳到字符串外的这段跳跃间隔即可

【时间复杂度&&优化】
O(strlen(s))

题目链接→Codeforces Problem 733A Grasshopper And the 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<list>
#include<bitset>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define bitnum(a) __builtin_popcount(a)
using namespace std;
const int N = 105;
const int M = 100005;
const int inf = 1000000007;
const int mod = 1000000007;
char s[N];
int main()

    int i,Max=0,k=0;
    scanf("%s",s+1);
    for(i=1;s[i]!='\\0';i++)
        if(s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U'||s[i]=='Y')
            Max=max(Max,i-k),k=i;
    Max=max(Max,i-k);
    printf("%d\\n",Max);
    return 0;

 Problem B-Parade

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

 Problem Description

Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.

There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.

The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.

No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.

Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.

 Input

The first line contains single integer n (1 ≤ n ≤ 10^5) — the number of columns.

The next n lines contain the pairs of integers li and ri (1 ≤ li, ri ≤ 500) — the number of soldiers in the i-th column which start to march from the left or the right leg respectively.

 Output

Print single integer k — the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.

Consider that columns are numbered from 1 to n in the order they are given in the input data.

If there are several answers, print any of them.

 Sample Input

3
5 6
8 9
10 3
2
6 5
5 6
6
5 9
1 3
4 8
4 5
23 54
12 32

 Sample Output

3
1
0

 Hint

In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg — 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.

If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg — 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.

It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.

 Problem Idea

解题思路:

【题意】

有n对数,第i对数用li,ri表示

n对数的美丽值为

在最多交换一对数中的li和ri值的情况下,美丽值最大为多少

【类型】
暴力
【分析】

因为最多交换一对,这意味着可以不交换,所以我们先处理出不交换的情况下n对数的美丽值

然后暴力一遍,交换第i对时,只需减去原先的li加上原先的ri,然后加上后来的li,减去后来的ri即可

描述得有点绕,看看代码消化一下

【时间复杂度&&优化】
O(n)

题目链接→Codeforces Problem 733B Parade

 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<list>
#include<bitset>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define bitnum(a) __builtin_popcount(a)
using namespace std;
const int N = 100005;
const int M = 100005;
const int inf = 1000000007;
const int mod = 1000000007;
int l[N],r[N];
int main()

    int n,ans=0,i;
    __int64 L=0,R=0,Max;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    
        scanf("%d%d",&l[i],&r[i]);
        L+=l[i];R+=r[i];
    
    Max=abs(L-R);
    for(i=1;i<=n;i++)
        if(abs(L-l[i]+r[i]-R+r[i]-l[i])>Max)
            Max=abs(L-l[i]+r[i]-R+r[i]-l[i]),ans=i;
    printf("%d\\n",ans);
    return 0;

 Problem C-Epidemic in Monstropolis

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

 Problem Description

There was an epidemic in Monstropolis and all monsters became sick. To recover, all monsters lined up in queue for an appointment to the only doctor in the city.

Soon, monsters became hungry and began to eat each other.

One monster can eat other monster if its weight is strictly greater than the weight of the monster being eaten, and they stand in the queue next to each other. Monsters eat each other instantly. There are no monsters which are being eaten at the same moment. After the monster A eats the monster B, the weight of the monster A increases by the weight of the eaten monster B. In result of such eating the length of the queue decreases by one, all monsters after the eaten one step forward so that there is no empty places in the queue again. A monster can eat several monsters one after another. Initially there were n monsters in the queue, the i-th of which had weight ai.

For example, if weights are [1, 2, 2, 2, 1, 2] (in order of queue, monsters are numbered from 1 to 6 from left to right) then some of the options are:

  1. the first monster can't eat the second monster because a1 = 1 is not greater than a2 = 2;
  2. the second monster can't eat the third monster because a2 = 2 is not greater than a3 = 2;
  3. the second monster can't eat the fifth monster because they are not neighbors;
  4. the second monster can eat the first monster, the queue will be transformed to [3, 2, 2, 1, 2].

After some time, someone said a good joke and all monsters recovered. At that moment there were k (k ≤ n) monsters in the queue, the j-th of which had weight bj. Both sequences (a and b) contain the weights of the monsters in the order from the first to the last.

You are required to provide one of the possible orders of eating monsters which led to the current queue, or to determine that this could not happen. Assume that the doctor didn't make any appointments while monsters were eating each other.

 Input

The first line contains single integer n (1 ≤ n ≤ 500) — the number of monsters in the initial queue.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 10^6) — the initial weights of the monsters.

The third line contains single integer k (1 ≤ k ≤ n) — the number of monsters in the queue after the joke.

The fourth line contains k integers b1, b2, ..., bk (1 ≤ bj ≤ 5·10^8) — the weights of the monsters after the joke.

Monsters are listed in the order from the beginning of the queue to the end.

 Output

In case if no actions could lead to the final queue, print "NO" (without quotes) in the only line.

Otherwise print "YES" (without quotes) in the first line. In the next n - k lines print actions in the chronological order. In each line print x — the index number of the monster in the current queue which eats and, separated by space, the symbol 'L' if the monster which stays the x-th in the queue eats the monster in front of him, or 'R' if the monster which stays the x-th in the queue eats the monster behind him. After each eating the queue is enumerated again.

When one monster eats another the queue decreases. If there are several answers, print any of them.

 Sample Input

6
1 2 2 2 1 2
2
5 5
5
1 2 3 4 5
1
15
5
1 1 1 3 3
3
2 1 6

 Sample Output

YES
2 L
1 R
4 L
3 L
YES
5 L
4 L
3 L
2 L
NO

 Hint

In the first example, initially there were n = 6 monsters, their weights are [1, 2, 2, 2, 1, 2] (in order of queue from the first monster to the last monster). The final queue should be [5, 5]. The following sequence of eatings leads to the final queue:

  • the second monster eats the monster to the left (i.e. the first monster), queue becomes [3, 2, 2, 1, 2];
  • the first monster (note, it was the second on the previous step) eats the monster to the right (i.e. the second monster), queue becomes [5, 2, 1, 2];
  • the fourth monster eats the mosnter to the left (i.e. the third monster), queue becomes [5, 2, 3];
  • the finally, the third monster eats the monster to the left (i.e. the second monster), queue becomes [5, 5].

Note that for each step the output contains numbers of the monsters in their current order in the queue.

 Problem Idea

解题思路:

【题意】

最初有n只怪物,每只怪物的体重为ai

每一次,某一只怪物可以吃掉与它相邻且体重比它小的怪物,然后使自己的体重增加被吃掉怪物的体重((。^▽^)怪物吸收能力好强,完全吸收)

现在再给你某个时刻剩下怪物的体重,问是否有可能由最初的怪物得到,如果可以,还需输出吃的过程

【类型】
贪心
【分析】

不知道是不是题目的tag立歪了,居然是dp

不过有可能是可以用dp做,不过博主用的方法是贪心

听博主慢慢道来

首先,我们得先判断出不可能的情况有哪些:

①最初时的怪物总体重与某时刻的怪物总体重不相等,样例如下

1
959139
1
470888

②最初时的怪物序列第i段连续部分之和与某时刻第i只怪物体重不相等,样例如下

8
2 5 3 1 4 2 3 4
3
10 6 8

最初是的怪物序列可分三段:2 5 3,1 4,2 3 4或2 5 3,1 4 2,3 4

但是两种分法都无法构成10 6 8,故输出"NO"

③在最初时的怪物序列某段中,不存在任何一只怪物能够吃掉相邻怪物,样例如下

5
1 1 1 3 3
3
2 1 6

显然2需要由1+1构成,但是1,1两只怪物谁都不能吃掉对方

除上述三种情况外,其他情况都是有解的

那么,接下来要做的就是如何找到这个解

考虑到每只怪物只能吃掉与它相邻且体重比它小的怪物,所以对于每一段怪物序列

我们只要提取出最初体重最大,且能够吃掉相邻怪物的那只怪物即可

为什么要选体重最大的呢?因为一开始就是体重最大,那随便吃掉一个只会使得它的体重更大,那剩下的必定都是体重比它小的,它只要朝一个方向吃光,然后再向相反方向吃一遍就可以了,这样也方便我们计算吃掉怪物之后它的位置

而之所以不仅要选择体重最大,还要考虑能够吃掉相邻怪物是因为体重最大的怪物可能不止一个,若某只体重最大的怪物的相邻怪物均是和它体重相等的怪物,那它也是无能为力的

吃怪物时的下标,可以大致介绍一下,若某只怪物向左吃,那每吃掉一只,它的下标会减少1;若某怪物向右吃,则每吃掉一只,它的下标依然保持不变

【时间复杂度&&优化】
O(n)

题目链接→Codeforces Problem 733C Epidemic in Monstropolis

 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<list>
#include<bitset>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define bitnum(a) __builtin_popcount(a)
using namespace std;
const int N = 505;
const int M = 100005;
const int inf = 1000000007;
const int mod = 1000000007;
int a[N],b[N],r[N];
queue<int> q;
void solve(int poi,int k)

    int i;
    if(poi<r[k]&&a[poi]>a[poi+1])
    
        for(i=poi+1;i<=r[k];i++)
            printf("%d R\\n",poi-r[k-1]+k-1);
        for(i=poi-1;i>r[k-1];i--)
            printf("%d L\\n",k+i-r[k-1]);
    
    else
    
        for(i=poi-1;i>r[k-1];i--)
            printf("%d L\\n",k+i-r[k-1]);
        for(i=poi+1;i<=r[k];i++)
            printf("%d R\\n",k);
    

int main()

    int n,k,i,j,p,sum=0,w,Max;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
        scanf("%d",&a[i]),sum+=a[i];
    scanf("%d",&k);
    for(i=1;i<=k;i++)
        scanf("%d",&b[i]),sum-=b[i];
    if(sum)
    
        puts("NO");
        return 0;
    
    for(p=j=i=1;i<=n;i++)
    
        sum+=a[i];
        if(sum>b[j])
        
            puts("NO");
            return 0;
        
        if(sum==b[j])
            sum=0,j++,r[p++]=i;
    
    for(Max=0,j=i=1;i<=n;i++)
    
        if(i>r[j-1]+1&&a[i]>a[i-1]||i<r[j]&&a[i]>a[i+1])
        
            if(a[i]>Max)
                Max=a[w=i];
        
        if(r[j]==i)
        
            if(!Max&&r[j]-r[j-1]>1)
            
                puts("NO");
                return 0;
            
            Max=0;
            if(r[j]-r[j-1]>1)
                q.push(w);
            else
                q.push(0);
            j++;
        
    
    puts("YES");i=1;
    while(!q.empty())
    
        if(q.front())
            solve(q.front(),i);
        i++;
        q.pop();
    
    return 0;

 Problem D-Kostya the Sculptor

Accept: 0    Submit: 0
Time Limit: 3 seconds    Memory Limit : 256 megabytes

 Problem Description

Kostya is a genial sculptor, he has an idea: to carve a marble sculpture in the shape of a sphere. Kostya has a friend Zahar who works at a career. Zahar knows about Kostya's idea and wants to present him a rectangular parallelepiped of marble from which he can carve the sphere.

Zahar has n stones which are rectangular parallelepipeds. The edges sizes of the i-th of them are ai, bi and ci. He can take no more than two stones and present them to Kostya.

If Zahar takes two stones, he should glue them together on one of the faces in order to get a new piece of rectangular parallelepiped of marble. Thus, it is possible to glue a pair of stones together if and only if two faces on which they are glued together match as rectangles. In such gluing it is allowed to rotate and flip the stones in any way.

Help Zahar choose such a present so that Kostya can carve a sphere of the maximum possible volume and present it to Zahar.

 Input

The first line contains the integer n (1 ≤ n ≤ 10^5).

n lines follow, in the i-th of which there are three integers ai, bi and ci (1 ≤ ai, bi, ci ≤ 10^9) — the lengths of edges of the i-th stone. Note, that two stones may have exactly the same sizes, but they still will be considered two different stones.

 Output

In the first line print k (1 ≤ k ≤ 2) the number of stones which Zahar has chosen. In the second line print k distinct integers from 1 to n — the numbers of stones which Zahar needs to choose. Consider that stones are numbered from 1 to n in the order as they are given in the input data.

You can print the stones in arbitrary order. If there are several answers print any of them.

 Sample Input

6
5 5 5
3 2 4
1 4 1
2 1 3
3 2 4
3 3 4
7
10 7 8
5 10 3
4 2 6
5 5 5
10 2 8
4 2 1
7 7 7

 Sample Output

1
1
2
1 5

 Hint

In the first example we can connect the pairs of stones:

  • 2 and 4, the size of the parallelepiped: 3 × 2 × 5, the radius of the inscribed sphere 1
  • 2 and 5, the size of the parallelepiped: 3 × 2 × 8 or 6 × 2 × 4 or 3 × 4 × 4, the radius of the inscribed sphere 1, or 1, or 1.5 respectively.
  • 2 and 6, the size of the parallelepiped: 3 × 5 × 4, the radius of the inscribed sphere 1.5
  • 4 and 5, the size of the parallelepiped: 3 × 2 × 5, the radius of the inscribed sphere 1
  • 5 and 6, the size of the parallelepiped: 3 × 4 × 5, the radius of the inscribed sphere 1.5

Or take only one stone:

  • 1 the size of the parallelepiped: 5 × 5 × 5, the radius of the inscribed sphere 2.5
  • 2 the size of the parallelepiped: 3 × 2 × 4, the radius of the inscribed sphere 1
  • 3 the size of the parallelepiped: 1 × 4 × 1, the radius of the inscribed sphere 0.5
  • 4 the size of the parallelepiped: 2 × 1 × 3, the radius of the inscribed sphere 0.5
  • 5 the size of the parallelepiped: 3 × 2 × 4, the radius of the inscribed sphere 1
  • 6 the size of the parallelepiped: 3 × 3 × 4, the radius of the inscribed sphere 1.5

It is most profitable to take only the first stone.

 Problem Idea

解题思路:

【题意】

有n个长方体

你最多选两个进行拼接(前提是能拼接,能拼接的条件是两个长方体的某个面完全一样,即等长等宽)或只选一个

问要使得得到的新长方体的内切球半径最大,应该选哪个或哪两个长方体

【类型】
贪心
【分析】

貌似有挺多人暴力过的,不过博主用的方法是贪心

显然,对于两个准备拼接的长方体,要拼接肯定是拼接面积最大的面的做法来得更优

比如两个长方体的长宽高均为2,3,4,那么我们肯定是拼接3×4这个面来得更优,因为拼接之后形成的新长方体,剩下的那条边的长度会增加

而长方体内切球的半径取决于长方体的最短边


①2×3面拼接:


内切球半径为1

②2×4面拼接:


内切球半径为1

③3×4面拼接:


内切球半径为1.5(为了避免出现小数,实际编码时取直径)

这更加证实了要拼接肯定拼接大面

于是我们先将每个长方体的三边排个序,取出每个长方体最短边的最大值作为只选一个长方体时最优解

然后将所有的长方体按照最长边、次长边、最短边的优先级排序

那么对于相邻长方体来说,如果能拼接,它们的最长边和次长边至少是相等的

然后拼接之后保留新长方体的最短边(一开始没注意到,取了两拼接边之和,一只错在第7组),更新最大值即可

提供一组数据供参考

INPUT
4
3 7 8
5 7 8
5 6 9
4 6 9
OUTPUT
2
1 2

【时间复杂度&&优化】
O(nlogn)//取决于排序

题目链接→Codeforces Problem 733D Kostya the Sculptor

 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<list>
#include<bitset>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define bitnum(a) __builtin_popcount(a)
using namespace std;
const int N = 100005;
const int M = 100005;
const int inf = 1000000007;
const int mod = 1000000007;
struct node

    int a,b,c,id;
s[N];
int g[3];
bool cmp(node x,node y)

    if(x.c!=y.c)
        return x.c<y.c;
    if(x.b!=y.b)
        return x.b<y.b;
    return x.a<y.a;

int main()

    int n,i,j,ans1=0,ans2=0,Max=0;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    
        for(j=0;j<3;j++)
            scanf("%d",&g[j]);
        sort(g,g+3);
        s[i].a=g[0],s[i].b=g[1],s[i].c=g[2],s[i].id=i;
        if(Max<g[0])
            Max=g[0],ans1=i;
    
    sort(s+1,s+1+n,cmp);
    for(i=1;i<n;i++)
        if(s[i].c==s[i+1].c&&s[i].b==s[i+1].b)
        
            g[0]=s[i].a+s[i+1].a;
            g[1]=s[i].b;
            g[2]=s[i].c;
            sort(g,g+3);
            if(g[0]>Max)
                Max=g[0],ans1=s[i].id,ans2=s[i+1].id;
        
    if(ans2)
        printf("2\\n%d %d\\n",ans1,ans2);
    else
        printf("1\\n%d\\n",ans1);
    return 0;
菜鸟成长记

以上是关于Codeforces Round #378 (Div. 2)题解报告的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #378 (Div. 2)题解报告

Codeforces Round #378 (Div. 2)

Codeforces Round #378 (Div. 2)

Codeforces Round #378 (Div. 2)

Codeforces Round #378 (Div. 2) F - Drivers Dissatisfaction

Codeforces Round #436 E. Fire(背包dp+输出路径)