Educational Codeforces Round 48 (Rated for Div. 2)

A. Death Note
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You received a notebook which is called Death Note. This notebook has infinite number of pages. A rule is written on the last page (huh) of this notebook. It says: “You have to write names in this notebook during n consecutive days. During the i-th day you have to write exactly ai names.”. You got scared (of course you got scared, who wouldn’t get scared if he just receive a notebook which is named Death Note with a some strange rule written in it?).

Of course, you decided to follow this rule. When you calmed down, you came up with a strategy how you will write names in the notebook. You have calculated that each page of the notebook can contain exactly m names. You will start writing names from the first page. You will write names on the current page as long as the limit on the number of names on this page is not exceeded. When the current page is over, you turn the page. Note that you always turn the page when it ends, it doesn’t matter if it is the last day or not. If after some day the current page still can hold at least one name, during the next day you will continue writing the names from the current page.

Now you are interested in the following question: how many times will you turn the page during each day? You are interested in the number of pages you will turn each day from 1 to n.

Input
The first line of the input contains two integers n, m (1≤n≤2⋅105, 1≤m≤109) — the number of days you will write names in the notebook and the number of names which can be written on each page of the notebook.

The second line contains n integers a1,a2,…,an (1≤ai≤109), where ai means the number of names you will write in the notebook during the i-th day.

Output
Print exactly n integers t1,t2,…,tn, where ti is the number of times you will turn the page during the i-th day.

Examples
inputCopy
3 5
3 7 9
outputCopy
0 2 1
inputCopy
4 20
10 9 19 2
outputCopy
0 0 1 1
inputCopy
1 100
99
outputCopy
0
Note
In the first example pages of the Death Note will look like this [1,1,1,2,2],[2,2,2,2,2],[3,3,3,3,3],[3,3,3,3]. Each number of the array describes during which day name on the corresponding position will be written. It is easy to see that you should turn the first and the second page during the second day and the third page during the third day.

题意:给出每天需要写的字符数ai以及每页纸能容纳的字符数,求出每天需要翻动的页数
题解:模拟即可

#include<bits/stdc++.h>
using namespace std;
#define debug(x) cout<<#x<<" is "<<x<<endl;
typedef long long ll;
ll a[200005];
int main(){
    ll n,m;
    scanf("%lld%lld",&n,&m);
    for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
    ll sum=0;
    for(int i=1;i<=n;i++){
        sum+=a[i];
        printf("%lld",sum/m);
        char ch=(i==n)?'\n':' ';
        printf("%c",ch);
        sum%=m;
    }
    return 0;
}

B. Segment Occurrences
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given two strings s and t, both consisting only of lowercase Latin letters.

The substring s[l…r] is the string which is obtained by taking characters sl,sl+1,…,sr without changing the order.

Each of the occurrences of string a in a string b is a position i (1≤i≤|b|−|a|+1) such that b[i…i+|a|−1]=a (|a| is the length of string a).

You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[li…ri].

Input
The first line contains three integer numbers n, m and q (1≤n,m≤103, 1≤q≤105) — the length of string s, the length of string t and the number of queries, respectively.

The second line is a string s (|s|=n), consisting only of lowercase Latin letters.

The third line is a string t (|t|=m), consisting only of lowercase Latin letters.

Each of the next q lines contains two integer numbers li and ri (1≤li≤ri≤n) — the arguments for the i-th query.

Output
Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[li…ri].

Examples
inputCopy
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
outputCopy
0
1
0
1
inputCopy
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
outputCopy
4
0
3
inputCopy
3 5 2
aaa
baaab
1 3
1 1
outputCopy
0
0
Note
In the first example the queries are substrings: “cod”, “deforces”, “fo” and “for”, respectively.

题意:给出两个字符串s和t,有q次询问,每次询问一个区间[L,R],求s串中[L,R]内子串==t串的个数
题解:显然不能通过前缀和解决,因为不存在区间可加减性(因为左端点可能截断一部分子串,前缀和不能有效减掉这部分子串),开两个数组a[maxn],b[maxn]分别存储s串中子串=t串的头位置和尾位置,然后通过lower_bound找出区间[L,R]内第一个子串的头所在数组a的位置pos1和最后一个子串的尾所在数组b的位置pos2,个数就是pos2-pos1+1

#include<bits/stdc++.h>
using namespace std;
#define debug(x) cout<<#x<<" is "<<x<<endl;
typedef long long ll;
int pos1[1005],pos2[1005];
char ch[1005],ch2[1005];
int main(){
    int n,m,q;
    scanf("%d%d%d",&n,&m,&q);
    scanf("%s",ch+1);
    scanf("%s",ch2+1);
    int tot=0;
    for(int i=1;i<=n;i++){
        if(n-i+1<m)continue;
        int f=1;
        for(int j=1;j<=m;j++){
            if(ch[i+j-1]!=ch2[j]){
                f=0;
                break;
            }
        }
        if(f){
            pos1[++tot]=i;
            pos2[tot]=i+m-1;
        }
    }
    while(q--){
        int l,r;
        scanf("%d%d",&l,&r);
        int pos3=lower_bound(pos1+1,pos1+1+tot,l)-pos1;
        int pos4=lower_bound(pos2+1,pos2+1+tot,r)-pos2;
        if(pos4>tot||pos2[pos4]>r)pos4--;
        if(pos3>tot){
            printf("0\n");
            continue;
        }
        if(pos3==0||pos4==0)printf("0\n");
        else printf("%d\n",max(pos4-pos3+1,0));
    }
    return 0;
}

C. Vasya And The Mushrooms
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vasya’s house is situated in a forest, and there is a mushroom glade near it. The glade consists of two rows, each of which can be divided into n consecutive cells. For each cell Vasya knows how fast the mushrooms grow in this cell (more formally, how many grams of mushrooms grow in this cell each minute). Vasya spends exactly one minute to move to some adjacent cell. Vasya cannot leave the glade. Two cells are considered adjacent if they share a common side. When Vasya enters some cell, he instantly collects all the mushrooms growing there.

Vasya begins his journey in the left upper cell. Every minute Vasya must move to some adjacent cell, he cannot wait for the mushrooms to grow. He wants to visit all the cells exactly once and maximize the total weight of the collected mushrooms. Initially, all mushrooms have a weight of 0. Note that Vasya doesn’t need to return to the starting cell.

Help Vasya! Calculate the maximum total weight of mushrooms he can collect.

Input
The first line contains the number n (1 ≤ n ≤ 3·105) — the length of the glade.

The second line contains n numbers a1, a2, …, an (1 ≤ ai ≤ 106) — the growth rate of mushrooms in the first row of the glade.

The third line contains n numbers b1, b2, …, bn (1 ≤ bi ≤ 106) is the growth rate of mushrooms in the second row of the glade.

Output
Output one number — the maximum total weight of mushrooms that Vasya can collect by choosing the optimal route. Pay attention that Vasya must visit every cell of the glade exactly once.

Examples
inputCopy
3
1 2 3
6 5 4
outputCopy
70
inputCopy
3
1 1000 10000
10 100 100000
outputCopy
543210
Note
In the first test case, the optimal route is as follows:

Thus, the collected weight of mushrooms will be 0·1 + 1·2 + 2·3 + 3·4 + 4·5 + 5·6 = 70.
In the second test case, the optimal route is as follows:

Thus, the collected weight of mushrooms will be 0·1 + 1·10 + 2·100 + 3·1000 + 4·10000 + 5·100000 = 543210.

题意:有一个2*n的土地,每个土地初始的粮食数是0,给出每块土地粮食的生长速率,起始位置在左上角,要求每块土地必须走且只能走一次,求能得到的最大粮食数

题解:由于是2*n的土地并且必须走且只能走一次,注意到如果当前列两块土地只走了一块而这时选择向右移动的话,那么为了能走完所有土地,就必须一直向右走到尽头然后往回走(如上图样例一),所以总的移动方式只有:(1)当前列不向上或向下移动而选择直接往右走,那么就只能一直向右到尽头再回头。(2)向上或者向下。所以只需要枚举在哪一列进行了(1)操作即可

#include<bits/stdc++.h>
using namespace std;
#define debug(x) cout<<#x<<" is "<<x<<endl;
typedef long long ll;
ll ans[300005],a[300005],b[300005],sum[300005],sum2[300005],sum3[300005],sum4[300005],sum5[300005],sum6[300005];
int main(){
    ll n;
    scanf("%lld",&n);
    for(int i=1;i<=n;i++){
        scanf("%lld",&a[i]);
        sum[i]=sum[i-1]+a[i]*(i-1);
        sum3[i]=sum3[i-1]+a[i];
        //sum5[i]
    }
   // for(int i=n;i>=)
    for(int i=1;i<=n;i++){
        scanf("%lld",&b[i]);
        sum2[i]=sum2[i-1]+b[i]*(i-1);
        sum4[i]=sum4[i-1]+b[i];
    }
    for(int i=n;i>=1;i--){
        sum5[i]=sum5[i+1]+a[i]*(n-i);
        sum6[i]=sum6[i+1]+b[i]*(n-i);
    }
    //memset(dp,-1,sizeof(dp));
    ll ak=0;
    ll ans=0;
    for(int i=1;i<=n;i++){
        if(i%2)ak=max(ak,ans+sum[n]-sum[i-1]+((i-1)*(sum3[n]-sum3[i-1]))+sum6[i]-sum6[n+1]+(n+i-1)*(sum4[n]-sum4[i-1]));
        else ak=max(ak,ans+sum2[n]-sum2[i-1]+((i-1)*(sum4[n]-sum4[i-1]))+sum5[i]-sum5[n+1]+(n+i-1)*(sum3[n]-sum3[i-1]));
        if(i%2){
            ans+=2ll*(i-1)*a[i]+(2ll*(i-1)+1)*b[i];
            //0 3 4 7 8
        }
        else{
            ans+=2ll*(i-1)*b[i]+(2ll*(i-1)+1)*a[i];
        }
    }
    printf("%lld\n",ak);
    return 0;
}

D. Vasya And The Matrix
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Now Vasya is taking an exam in mathematics. In order to get a good mark, Vasya needs to guess the matrix that the teacher has constructed!

Vasya knows that the matrix consists of n rows and m columns. For each row, he knows the xor (bitwise excluding or) of the elements in this row. The sequence a1, a2, …, an denotes the xor of elements in rows with indices 1, 2, …, n, respectively. Similarly, for each column, he knows the xor of the elements in this column. The sequence b1, b2, …, bm denotes the xor of elements in columns with indices 1, 2, …, m, respectively.

Help Vasya! Find a matrix satisfying the given constraints or tell him that there is no suitable matrix.

Input
The first line contains two numbers n and m (2 ≤ n, m ≤ 100) — the dimensions of the matrix.

The second line contains n numbers a1, a2, …, an (0 ≤ ai ≤ 109), where ai is the xor of all elements in row i.

The third line contains m numbers b1, b2, …, bm (0 ≤ bi ≤ 109), where bi is the xor of all elements in column i.

Output
If there is no matrix satisfying the given constraints in the first line, output “NO”.

Otherwise, on the first line output “YES”, and then n rows of m numbers in each ci1, ci2, … , cim (0 ≤ cij ≤ 2·109) — the description of the matrix.

If there are several suitable matrices, it is allowed to print any of them.

Examples
inputCopy
2 3
2 9
5 3 13
outputCopy
YES
3 4 5
6 7 8
inputCopy
3 3
1 7 6
2 15 12
outputCopy
NO

题意:给出了n*m的矩阵每行的异或值ai和每列的异或值bi,还原矩阵或者输出不存在
题解:前n-1行,m-1列随便填,最后一行一列根据要求填,最后检查是否全部行和列都满足要求即可。

#include<bits/stdc++.h>
using namespace std;
#define debug(x) cout<<#x<<" is "<<x<<endl;
typedef long long ll;
ll a[105],b[105],c[105][105];
int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
    for(int i=1;i<=m;i++)scanf("%lld",&b[i]);
    for(int i=1;i<n;i++){
        for(int j=1;j<m;j++){
            c[i][j]=1;
        }
    }
    for(int i=1;i<n;i++){
        if(m%2)c[i][m]=a[i];
        else c[i][m]=a[i]^1;
    }
    ll xx=0;
    for(int i=1;i<m;i++){
        if(n%2)c[n][i]=b[i];
        else c[n][i]=b[i]^1;
        xx^=c[n][i];
    }
    xx^=a[n];
    c[n][m]=xx;
    xx=0;
    for(int i=1;i<=n;i++){
        xx^=c[i][m];
    }
    if(xx==b[m]){
        printf("YES\n");
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                printf("%lld",c[i][j]);
                char ch=(j==m)?'\n':' ';
                printf("%c",ch);
            }
        }
    }
    else{
        printf("NO\n");
    }
    return 0;
}

E. Rest In The Shades
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
There is a light source on the plane. This source is so small that it can be represented as point. The light source is moving from point (a,sy) to the (b,sy) (sy<0) with speed equal to 1 unit per second. The trajectory of this light source is a straight segment connecting these two points.

There is also a fence on OX axis represented as n segments (li,ri) (so the actual coordinates of endpoints of each segment are (li,0) and (ri,0)). The point (x,y) is in the shade if segment connecting (x,y) and the current position of the light source intersects or touches with any segment of the fence.

You are given q points. For each point calculate total time of this point being in the shade, while the light source is moving from (a,sy) to the (b,sy).

Input
First line contains three space separated integers sy, a and b (−109≤sy<0, 1≤a<b≤109) — corresponding coordinates of the light source.

Second line contains single integer n (1≤n≤2⋅105) — number of segments in the fence.

Next n lines contain two integers per line: li and ri (1≤li<ri≤109, ri−1<li) — segments in the fence in increasing order. Segments don’t intersect or touch each other.

Next line contains single integer q (1≤q≤2⋅105) — number of points to check.

Next q lines contain two integers per line: xi and yi (1≤xi,yi≤109) — points to process.

Output
Print q lines. The i-th line should contain one real number — total time of the i-th point being in the shade, while the light source is moving from (a,sy) to the (b,sy). The answer is considered as correct if its absolute of relative error doesn’t exceed 10−6.

Example
inputCopy
-3 1 6
2
2 4
6 7
5
3 1
1 3
6 1
6 4
7 6
outputCopy
5.000000000000000
3.000000000000000
0.000000000000000
1.500000000000000
2.000000000000000
Note
The 1-st point is always in the shade;
the 2-nd point is in the shade while light source is moving from (3,−3) to (6,−3);
the 3-rd point is in the shade while light source is at point (6,−3).
the 4-th point is in the shade while light source is moving from (1,−3) to (2.5,−3) and at point (6,−3);
the 5-th point is in the shade while light source is moving from (1,−3) to (2.5,−3) and from (5.5,−3) to (6,−3);
待补
F. Road Projects
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
There are n cities in the country of Berland. Some of them are connected by bidirectional roads in such a way that there exists exactly one path, which visits each road no more than once, between every pair of cities. Each road has its own length. Cities are numbered from 1 to n.

The travelling time between some cities v and u is the total length of the roads on the shortest path from v to u.

The two most important cities in Berland are cities 1 and n.

The Berland Ministry of Transport decided to build a single new road to decrease the traffic between the most important cities. However, lots of people are used to the current travelling time between the most important cities, so the new road shouldn’t change it too much.

The new road can only be built between such cities v and u that v≠u and v and u aren’t already connected by some road.

They came up with m possible projects. Each project is just the length x of the new road.

Polycarp works as a head analyst at the Berland Ministry of Transport and it’s his job to deal with all those m projects. For the i-th project he is required to choose some cities v and u to build the new road of length xi between such that the travelling time between the most important cities is maximal possible.

Unfortunately, Polycarp is not a programmer and no analyst in the world is capable to process all projects using only pen and paper.

Thus, he asks you to help him to calculate the maximal possible travelling time between the most important cities for each project. Note that the choice of v and u can differ for different projects.

Input
The first line contains two integers n and m (3≤n≤3⋅105, 1≤m≤3⋅105) — the number of cities and the number of projects, respectively.

Each of the next n−1 lines contains three integers vi, ui and wi (1≤vi,ui≤n, 1≤wi≤109) — the description of the i-th road. It is guaranteed that there exists exactly one path, which visits each road no more than once, between every pair of cities.

Each of the next m lines contains a single integer xj (1≤xj≤109) — the length of the road for the j-th project.

Output
Print m lines, the j-th line should contain a single integer — the maximal possible travelling time between the most important cities for the j-th project.

Example
inputCopy
7 2
1 2 18
2 3 22
3 4 24
4 7 24
2 6 4
3 5 12
1
100
outputCopy
83
88
Note
The road network from the first example:

You can build the road with length 1 between cities 5 and 6 to get 83 as the travelling time between 1 and 7 (1→2→6→5→3→4→7 = 18+4+1+12+24+24=83). Other possible pairs of cities will give answers less or equal to 83.
待补
G. Appropriate Team
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Since next season are coming, you’d like to form a team from two or three participants. There are n candidates, the i-th candidate has rank ai. But you have weird requirements for your teammates: if you have rank v and have chosen the i-th and j-th candidate, then GCD(v,ai)=X and LCM(v,aj)=Y must be met.

You are very experienced, so you can change your rank to any non-negative integer but X and Y are tied with your birthdate, so they are fixed.

Now you want to know, how many are there pairs (i,j) such that there exists an integer v meeting the following constraints: GCD(v,ai)=X and LCM(v,aj)=Y. It’s possible that i=j and you form a team of two.

GCD is the greatest common divisor of two number, LCM — the least common multiple.

Input
First line contains three integers n, X and Y (1≤n≤2⋅105, 1≤X≤Y≤1018) — the number of candidates and corresponding constants.

Second line contains n space separated integers a1,a2,…,an (1≤ai≤1018) — ranks of candidates.

Output
Print the only integer — the number of pairs (i,j) such that there exists an integer v meeting the following constraints: GCD(v,ai)=X and LCM(v,aj)=Y. It’s possible that i=j.

Examples
inputCopy
12 2 2
1 2 3 4 5 6 7 8 9 10 11 12
outputCopy
12
inputCopy
12 1 6
1 3 5 7 9 11 12 10 8 6 4 2
outputCopy
30
Note
In the first example next pairs are valid: aj=1 and ai=[2,4,6,8,10,12] or aj=2 and ai=[2,4,6,8,10,12]. The v in both cases can be equal to 2.

In the second example next pairs are valid:

aj=1 and ai=[1,5,7,11];
aj=2 and ai=[1,5,7,11,10,8,4,2];
aj=3 and ai=[1,3,5,7,9,11];
aj=6 and ai=[1,3,5,7,9,11,12,10,8,6,4,2].
待补

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值