【codeforces】Codeforces Round #441(div.2)

来源:互联网 发布:淘宝卖家如何开通微淘 编辑:程序博客网 时间:2024/05/22 07:04

最近打了几场CF英语阅读能力大赛,表示爬了一百来分,然后R440一夜回到解放前。
被英文枪毙的中国选手

好吧,那进入今天的正题,纯属是为了要养成写博客的好习惯来码的。

A. Trip For Meal

【time limit】 per test 1 second
【memory limit】 per test 512 megabytes

Winnie-the-Pooh likes honey very much! That is why he decided to visit his friends. Winnie has got three best friends: Rabbit, Owl and Eeyore, each of them lives in his own house. There are winding paths between each pair of houses. The length of a path between Rabbit’s and Owl’s houses is a meters, between Rabbit’s and Eeyore’s house is b meters, between Owl’s and Eeyore’s house is c meters.

For enjoying his life and singing merry songs Winnie-the-Pooh should have a meal n times a day. Now he is in the Rabbit’s house and has a meal for the first time. Each time when in the friend’s house where Winnie is now the supply of honey is about to end, Winnie leaves that house. If Winnie has not had a meal the required amount of times, he comes out from the house and goes to someone else of his two friends. For this he chooses one of two adjacent paths, arrives to the house on the other end and visits his friend. You may assume that when Winnie is eating in one of his friend’s house, the supply of honey in other friend’s houses recover (most probably, they go to the supply store).

Winnie-the-Pooh does not like physical activity. He wants to have a meal n times, traveling minimum possible distance. Help him to find this distance.

Input
First line contains an integer n (1 ≤ n ≤ 100) — number of visits.

Second line contains an integer a (1 ≤ a ≤ 100) — distance between Rabbit’s and Owl’s houses.

Third line contains an integer b (1 ≤ b ≤ 100) — distance between Rabbit’s and Eeyore’s houses.

Fourth line contains an integer c (1 ≤ c ≤ 100) — distance between Owl’s and Eeyore’s houses.

Output
Output one number — minimum distance in meters Winnie must go through to have a meal n times.

Examples
input
3
2
3
1
output
3

input
1
2
3
5
output
0

Note
In the first test case the optimal path for Winnie is the following: first have a meal in Rabbit’s house, then in Owl’s house, then in Eeyore’s house. Thus he will pass the distance 2 + 1 = 3.

In the second test case Winnie has a meal in Rabbit’s house and that is for him. So he doesn’t have to walk anywhere at all.

【题目大意】
有一个长度为3的环,分别为a、b、c(分别代表它的一个朋友家),现在小熊维尼要从a点开始串门,为了吃蜂蜜 ,维尼每到一个点(包括开始的出发点)就会进去吃蜂蜜,然后就会到下一个相邻的点开始吃蜂蜜。当维尼在吃蜂蜜时,另外两个点的蜂蜜会补满。
现在维尼要吃n次蜂蜜,问最少要走多少路。

【分析】
没什么好分析的,就是n==1和n==2和n>2的情况……
n==1就是0.
n==2就是min(AB,AC)
n>2就是min(AB,AC)+(n-2)*min(AB,AC,BC)

【代码】

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int n,a,b,c;int k,k2,ans;int main(){    freopen("A.in","r",stdin);    freopen("A.out","w",stdout);    scanf("%d%d%d%d",&n,&a,&b,&c);    if(n==1)        printf("0\n");    else    if(n==2)        printf("%d\n",min(a,b));    else    {        k=min(a,b);        k2=min(k,c);        ans=k+(n-2)*k2;        printf("%d\n",ans);    }    return 0;}

B. Divisiblity of Differences

【time limit】 per test 1 second
【memory limit 】per test 512 megabytes

You are given a multiset of n integers. You should select exactly k of them in a such way that the difference between any two of them is divisible by m, or tell that it is impossible.

Numbers can be repeated in the original multiset and in the multiset of selected numbers, but number of occurrences of any number in multiset of selected numbers should not exceed the number of its occurrences in the original multiset.

Input
First line contains three integers n, k and m (2 ≤ k ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — number of integers in the multiset, number of integers you should select and the required divisor of any pair of selected integers.

Second line contains n integers a1, a2, …, an (0 ≤ ai ≤ 10^9) — the numbers in the multiset.

Output
If it is not possible to select k numbers in the desired way, output «No» (without the quotes).

Otherwise, in the first line of output print «Yes» (without the quotes). In the second line print k integers b1, b2, …, bk — the selected numbers. If there are multiple possible solutions, print any of them.

Examples
input
3 2 3
1 8 4
output
Yes
1 4

input
3 3 3
1 8 4
output
No

input
4 3 5
2 7 7 7
output
Yes
2 7 7

【题目大意】
给出n个数,现在要求选出其中的k个数,使得他们%m的值相同。

【分析】
同余定理 ,并没有什么说的啊,直接mod一下就可以啦啦啦。

【代码】

#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<algorithm>using namespace std;const int MAX=1e5+10;int n,k,m,x;int cnt[MAX];vector<int> num[MAX];int main(){    freopen("B.in","r",stdin);    freopen("B.out","w",stdout);    scanf("%d%d%d",&n,&k,&m);    for(int i=0;i<n;++i)    {        scanf("%d",&x);        int tmp=x%m;        ++cnt[tmp];        num[tmp].push_back(x);        if(cnt[tmp]>=k)            break;    }    for(int i=0;i<m;++i)        if(cnt[i]>=k)        {            printf("Yes\n");            for(int j=0;j<k;++j)                printf("%d ",num[i][j]);            printf("\n");            return 0;        }    printf("No\n");    return 0;}

C. Classroom Watch

time limit per test1 second
memory limit per test512 megabytes
inputstandard input
outputstandard output
Eighth-grader Vova is on duty today in the class. After classes, he went into the office to wash the board, and found on it the number n. He asked what is this number and the teacher of mathematics Inna Petrovna answered Vova that n is the answer to the arithmetic task for first-graders. In the textbook, a certain positive integer x was given. The task was to add x to the sum of the digits of the number x written in decimal numeral system.

Since the number n on the board was small, Vova quickly guessed which x could be in the textbook. Now he wants to get a program which will search for arbitrary values of the number n for all suitable values of x or determine that such x does not exist. Write such a program for Vova.

Input
The first line contains integer n (1 ≤ n ≤ 10^9).

Output
In the first line print one integer k — number of different values of x satisfying the condition.

In next k lines print these values in ascending order.

Examples
input
21
output
1
15
input
20
output
0
Note
In the first test case x = 15 there is only one variant: 15 + 1 + 5 = 21.

In the second test case there are no such x.

【题目大意】
定义f(x)=x+x各个数位上的数字之和(如f(12)=12+1+2=15)
问1~n中有多少个数的f(x)等于n

【分析】
一眼看上去貌似好大好大m,O(n)铁定爆炸。
然后我们发现其实并没有这么多有用的数字,因为对于一个10^9以内的数字,它各个数位上的数字之和最多不超过9*9=81,也就是说只要从n开始往下for81个数字,然后每个数字暴力算就行了。
保险起见我算了100个。

【代码】

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespace std ;int ans[10000],n,cnt;int main(){    freopen("C.in","r",stdin);    freopen("C.out","w",stdout);    scanf("%d",&n);    int r=min(n-1,100);    for (int i=n-r;i<=n;i++)    {        int x=i,tmp=i;        while(tmp!=0)        {            x+=tmp%10;            tmp/=10;        }        if(x==n)        {            ans[cnt]=i;            cnt++;        }    }    printf("%d\n",cnt);    for(int i=0;i<cnt;i++)        printf("%d\n",ans[i]);    return 0;}

D. Sorting the Coins

time limit per test 1 second
memory limit per test 512 megabytes

Recently, Dima met with Sasha in a philatelic store, and since then they are collecting coins together. Their favorite occupation is to sort collections of coins. Sasha likes having things in order, that is why he wants his coins to be arranged in a row in such a way that firstly come coins out of circulation, and then come coins still in circulation.

For arranging coins Dima uses the following algorithm. One step of his algorithm looks like the following:

He looks through all the coins from left to right;
If he sees that the i-th coin is still in circulation, and (i + 1)-th coin is already out of circulation, he exchanges these two coins and continues watching coins from (i + 1)-th.
Dima repeats the procedure above until it happens that no two coins were exchanged during this procedure. Dima calls hardness of ordering the number of steps required for him according to the algorithm above to sort the sequence, e.g. the number of times he looks through the coins from the very beginning. For example, for the ordered sequence hardness of ordering equals one.

Today Sasha invited Dima and proposed him a game. First he puts n coins in a row, all of them are out of circulation. Then Sasha chooses one of the coins out of circulation and replaces it with a coin in circulation for n times. During this process Sasha constantly asks Dima what is the hardness of ordering of the sequence.

The task is more complicated because Dima should not touch the coins and he should determine hardness of ordering in his mind. Help Dima with this task.

Input
The first line contains single integer n (1 ≤ n ≤ 300 000) — number of coins that Sasha puts behind Dima.

Second line contains n distinct integers p1, p2, …, pn (1 ≤ pi ≤ n) — positions that Sasha puts coins in circulation to. At first Sasha replaces coin located at position p1, then coin located at position p2 and so on. Coins are numbered from left to right.

Output
Print n + 1 numbers a0, a1, …, an, where a0 is a hardness of ordering at the beginning, a1 is a hardness of ordering after the first replacement and so on.

Examples
input
4
1 3 4 2
output
1 2 3 2 1
input
8
6 8 3 4 7 2 1 5
output
1 2 2 3 4 3 4 5 1
Note

Let’s denote as O coin out of circulation, and as X — coin is circulation.

At the first sample, initially in row there are coins that are not in circulation, so Dima will look through them from left to right and won’t make any exchanges.

After replacement of the first coin with a coin in circulation, Dima will exchange this coin with next three times and after that he will finally look through the coins and finish the process.

XOOO  →  OOOX

After replacement of the third coin, Dima’s actions look this way:

XOXO  →  OXOX  →  OOXX

After replacement of the fourth coin, Dima’s actions look this way:

XOXX  →  OXXX

Finally, after replacement of the second coin, row becomes consisting of coins that are in circulation and Dima will look through coins from left to right without any exchanges.

【题目大意】
真·中国选手屠宰场
一共有n枚硬币,这些硬币有的是可流通在市场上的。现在讲这n枚硬币按顺序放成一行,定义一种检查方式:从左往右看这些硬币,当前看到第i个位置,若第i个位置上的硬币是流通的且第i+1个位置上的硬币是不流通的,那么交换这两枚硬币,并从第i+1个位置开始继续往后看。
从1看到n为一次检查,若一次检查中,没有硬币被交换,则检查完毕。

现在给出n个数,a1~an,第i个数表示a1、a2…ai个位置上的硬币进入流通,而其余不流通,输出此时需要检查多少次(第一个输出的是当所有硬币都不流通时需要检查多少次,显然是1)。

【分析】
神tm鬼畜英文杀手,看题半小时,码题3分钟
这题是一道水题。(一下X代表流通,O代表不流通)
我们考虑,若一次检查中需要交换硬币,那会是什么情况?
显然,我们必然有一个形如XOOX的东西在里面。
然后我们会发现,每一次过程,最多只有一个X会被放到队列的末尾。
这样,这道题就变成了,找出最后一个O的位置……然后算出这个O之前一共有多少个X就是答案了……
当然我们不用傻傻地去扫一遍找答案,只需要维护一下这个O后面有多少个X……详情请见代码(我居然还傻傻地用了堆,明明直接扫…)

【代码】

#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;const int MAXN=3e5+10;int n,flag,a[MAXN];priority_queue<int>q;int main(){//  freopen("D.in","r",stdin);//  freopen("D.out","w",stdout);    scanf("%d",&n);    for(int i=1;i<=n;++i)        scanf("%d",&a[i]);    printf("1 ");    flag=n;    for(int i=1;i<=n;++i)    {        q.push(a[i]);        while(flag==q.top())        {            q.pop();            flag--;        }        printf("%d ",i-(n-flag)+1);    }    return 0;}

E. National Property

You all know that the Library of Bookland is the largest library in the world. There are dozens of thousands of books in the library.

Some long and uninteresting story was removed…

The alphabet of Bookland is so large that its letters are denoted by positive integers. Each letter can be small or large, the large version of a letter x is denoted by x’. BSCII encoding, which is used everywhere in Bookland, is made in that way so that large letters are presented in the order of the numbers they are denoted by, and small letters are presented in the order of the numbers they are denoted by, but all large letters are before all small letters. For example, the following conditions hold: 2 < 3, 2’ < 3’, 3’ < 2.

A word x1, x2, …, xa is not lexicographically greater than y1, y2, …, yb if one of the two following conditions holds:

a ≤ b and x1 = y1, …, xa = ya, i.e. the first word is the prefix of the second word;
there is a position 1 ≤ j ≤ min(a, b), such that x1 = y1, …, xj - 1 = yj - 1 and xj < yj, i.e. at the first position where the words differ the first word has a smaller letter than the second word has.
For example, the word “3’ 7 5” is before the word “2 4’ 6” in lexicographical order. It is said that sequence of words is in lexicographical order if each word is not lexicographically greater than the next word in the sequence.

Denis has a sequence of words consisting of small letters only. He wants to change some letters to large (let’s call this process a capitalization) in such a way that the sequence of words is in lexicographical order. However, he soon realized that for some reason he can’t change a single letter in a single word. He only can choose a letter and change all of its occurrences in all words to large letters. He can perform this operation any number of times with arbitrary letters of Bookland’s alphabet.

Help Denis to choose which letters he needs to capitalize (make large) in order to make the sequence of words lexicographically ordered, or determine that it is impossible.

Note that some words can be equal.

Input
The first line contains two integers n and m (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of words and the number of letters in Bookland’s alphabet, respectively. The letters of Bookland’s alphabet are denoted by integers from 1 to m.

Each of the next n lines contains a description of one word in format li, si, 1, si, 2, …, si, li (1 ≤ li ≤ 100 000, 1 ≤ si, j ≤ m), where li is the length of the word, and si, j is the sequence of letters in the word. The words are given in the order Denis has them in the sequence.

It is guaranteed that the total length of all words is not greater than 100 000.

Output
In the first line print “Yes” (without quotes), if it is possible to capitalize some set of letters in such a way that the sequence of words becomes lexicographically ordered. Otherwise, print “No” (without quotes).

If the required is possible, in the second line print k — the number of letters Denis has to capitalize (make large), and in the third line print k distinct integers — these letters. Note that you don’t need to minimize the value k.

You can print the letters in any order. If there are multiple answers, print any of them.

Examples
input
4 3
1 2
1 1
3 1 3 2
2 1 1
output
Yes
2
2 3

input
6 5
2 1 2
2 1 2
3 1 2 3
2 1 5
2 4 4
2 4 4
output
Yes
0

input
4 3
4 3 2 2 1
3 1 1 3
3 2 3 3
2 3 1
output
No

Note
In the first example after Denis makes letters 2 and 3 large, the sequence looks like the following:

2’
1
1 3’ 2’
1 1
The condition 2’ < 1 holds, so the first word is not lexicographically larger than the second word. The second word is the prefix of the third word, so the are in lexicographical order. As the first letters of the third and the fourth words are the same, and 3’ < 1, then the third word is not lexicographically larger than the fourth word.

In the second example the words are in lexicographical order from the beginning, so Denis can do nothing.

In the third example there is no set of letters such that if Denis capitalizes them, the sequence becomes lexicographically ordered.

【题目大意】
给出n个数字串,每个数字串的数字不超过m。
现在要求将一些数字(比如是q)改变成q-m,使得这些数字串在不交换位置的情况下变成升序排列。
输出要改的数字的个数k以及每个数字是什么,不需要保证k最小。

两个数字串的大小是按照每一位上数字大小进行比较的,但一位上可能是一个多位数,比如数字22>数字3,数字13<数字14 。

【分析】
根据题目输入,我们可以得到一些大小关系,然后?
拓扑排序!
这样就可以解决了啊,但是要注意细节。
如果你不想注意细节,那就判断一下逆序对吧,我们发现,当一组大小关系中出现两个逆序对时,这是不成立的,否则我们就将这组逆序对中逆序对的前一个数字以前的数字(包括这个)全部变小!
比如3<4<1<2<18<17<22是显然不行的。

【代码】

#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<algorithm>using namespace std;const int MAXN=1e5+10;int x,y,n,m;int len[MAXN],cnt[MAXN];bool flag,vis[MAXN];vector<int>num[MAXN],way[MAXN];void dfs(int u){    vis[u]=true;    for(int i=0;i<way[u].size();++i)    {        int v=way[u][i];        if(cnt[v]==2)        {            flag=false;            return;        }        else        {            cnt[v]=1;            if(!vis[v])                dfs(v);        }    }}int main(){    freopen("E.in","r",stdin);    freopen("E.out","w",stdout);    scanf("%d%d",&n,&m);    for(int i=1;i<=n;++i)    {        scanf("%d",&x);        len[i]=x;        for(int j=1;j<=x;++j)        {            scanf("%d",&y);            num[i].push_back(y);        }    }    for(int i=1;i<n;++i)    {        int j=0;        while(j<min(len[i],len[i+1]) && num[i][j]==num[i+1][j])            ++j;//printf("i:%d j:%d\n",i,j);                if(j==min(len[i],len[i+1]))        {            if(len[i]>len[i+1])            {                printf("No\n");                return 0;            }            continue;        }        if(num[i][j]<num[i+1][j])            way[num[i+1][j]].push_back(num[i][j]);        else        {            if(cnt[num[i][j]]==2 || cnt[num[i+1][j]]==1)            {                printf("No\n");                return 0;            }            cnt[num[i][j]]=1;cnt[num[i+1][j]]=2;//1==big,2==small        }    }    flag=true;    memset(vis,false,sizeof(vis));    for(int i=1;i<=m;++i)    {        if(cnt[i]==1)            dfs(i);    }    if(!flag)        printf("No\n");    else    {        printf("%Yes\n");        int tot=0;        for(int i=1;i<=m;++i)            if(cnt[i]==1)                ++tot;        printf("%d\n",tot);        for(int i=1;i<=m;++i)            if(cnt[i]==1)                printf("%d ",i);    }    return 0;}

F. High Cry

time limit per test 1 second
memory limit per test 512 megabytes

Disclaimer: there are lots of untranslateable puns in the Russian version of the statement, so there is one more reason for you to learn Russian :)

Rick and Morty like to go to the ridge High Cry for crying loudly — there is an extraordinary echo. Recently they discovered an interesting acoustic characteristic of this ridge: if Rick and Morty begin crying simultaneously from different mountains, their cry would be heard between these mountains up to the height equal the bitwise OR of mountains they’ve climbed and all the mountains between them.

Bitwise OR is a binary operation which is determined the following way. Consider representation of numbers x and y in binary numeric system (probably with leading zeroes) x = xk… x1x0 and y = yk… y1y0. Then z = x | y is defined following way: z = zk… z1z0, where zi = 1, if xi = 1 or yi = 1, and zi = 0 otherwise. In the other words, digit of bitwise OR of two numbers equals zero if and only if digits at corresponding positions is both numbers equals zero. For example bitwise OR of numbers 10 = 10102 and 9 = 10012 equals 11 = 10112. In programming languages C/C++/Java/Python this operation is defined as «|», and in Pascal as «or».

Help Rick and Morty calculate the number of ways they can select two mountains in such a way that if they start crying from these mountains their cry will be heard above these mountains and all mountains between them. More formally you should find number of pairs l and r (1 ≤ l < r ≤ n) such that bitwise OR of heights of all mountains between l and r (inclusive) is larger than the height of any mountain at this interval.

Input
The first line contains integer n (1 ≤ n ≤ 200 000), the number of mountains in the ridge.

Second line contains n integers ai (0 ≤ ai ≤ 109), the heights of mountains in order they are located in the ridge.

Output
Print the only integer, the number of ways to choose two different mountains.

Examples
input
5
3 2 1 6 5
output
8

input
4
3 3 3 3
output
0

Note
In the first test case all the ways are pairs of mountains with the numbers (numbering from one):

(1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)
In the second test case there are no such pairs because for any pair of mountains the height of cry from them is 3, and this height is equal to the height of any mountain.

【题目大意】
一共有n座山,现在A和B两个人各在一座山上向对方喊话(A在l,B在r),声音的大小等于这些山的高度的“或”的值(c++的’|’)(包括l和r)。两人要互相听到当且仅当声音大小大于这些山的高度(包括l和r)。问一共有多少种不同的选择方式,使得他们能听到对方的声音。

【分析】
正着算很难算,于是我们考虑反着来。
那么用所有可能的选择情况减去不合理的情况就行了。
由于我有点累了我要写作业!,详情看代码吧。
注意long long

【代码】

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespace std;typedef long long LL;const int MAXN=2e5+10;LL ans;int n,a[MAXN],l[MAXN],r[MAXN];int main(){    freopen("F.in","r",stdin);    freopen("F.out","w",stdout);    scanf("%d",&n);    ans=(1ll*n*(n+1))/(1ll*2);    a[0]=a[n+1]=-1;    for(int i=1;i<=n;++i)        scanf("%d",&a[i]);    for(int i=1;i<=n;++i)    {        l[i]=i-1;        while((a[i]|a[l[i]]) == a[i])            l[i]=l[l[i]];    }    for(int i=n;i>=1;--i)    {        r[i]=i+1;        while((a[i]|a[r[i]]) == a[i] && a[i]>a[r[i]])//pay attention            r[i]=r[r[i]];    }    for(int i=1;i<=n;++i)        ans-=1ll*(i-l[i])*(r[i]-i);    printf("%I64d\n",ans);    return 0;}

完结撒花

原创粉丝点击