Codeforces Round #441 (Div. 2, by Moscow Team Olympiad)

来源:互联网 发布:青少年犯罪率数据2016 编辑:程序博客网 时间:2024/05/16 01:47

A. Trip For Meal
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

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
3231
output
3
input
1235
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.


题目大意:


在图上走n-1次,每次都走最短的一条边,问总共走了多少长度的路径。


思路:模拟一下即可;


Ac代码:

#include<stdio.h>#include<string.h>#include<vector>#include<algorithm>using namespace std;int a,b,c;int n;pair<int,int>get(int x){    int y;    if(x==1)    {        if(a<b)return make_pair(a,2);        else return make_pair(b,3);    }    if(x==2)    {        if(a<c)return make_pair(a,1);        else return make_pair(c,3);    }    if(x==3)    {        if(b<c)return make_pair(b,1);        else return make_pair(c,2);    }}int main(){    while(~scanf("%d%d%d%d",&n,&a,&b,&c))    {        int now=1;        int ans=0;        while(--n)        {            pair<int,int>temp=get(now);            ans+=temp.first;            now=temp.second;        }        printf("%d\n",ans);    }}

B. Divisiblity of Differences
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

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 nk and m (2 ≤ k ≤ n ≤ 100 0001 ≤ 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 ≤ 109) — 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 31 8 4
output
Yes1 4 
input
3 3 31 8 4
output
No
input
4 3 52 7 7 7
output
Yes2 7 7 


题目大意:

给出N个数,现在让我们从中取K个数,使得取出的数字中任取两个出来的差都是m的倍数。


思路:


跟倍数有关,其实就跟模数有关,我们直接统计a【i】%m结果每个数出现的个数即可。


Ac代码:


#include<stdio.h>#include<string.h>#include<map>using namespace std;int a[160000];int b[160000];int main(){    int n,k,m;    while(~scanf("%d%d%d",&n,&k,&m))    {        int flag=0;        map<int,int>have;        for(int i=1;i<=n;i++)scanf("%d",&a[i]),b[i]=a[i],a[i]%=m;        for(int i=1;i<=n;i++)        {            have[a[i]]++;            if(have[a[i]]>=k)            {                flag=i;            }        }        if(flag==0)printf("No\n");        else        {            printf("Yes\n");            for(int i=1;i<=n;i++)            {                if(a[i]==a[flag]&&k>=1)                {                    printf("%d ",b[i]);                    k--;                }            }        }        printf("\n");    }}


C. Classroom Watch
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard 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 ≤ 109).

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
115
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.


题目大意:

让我们找有多少个数x,使得其计算结果为n,Cal(x)=x+数字x在十进制表示下每个数的加和(185=185+1+8+5);


思路:


因为每个数字x能够加上的数字是有限制的(最多也就加上几个9而已),所以我们暴力去做就行了。


Ac代码:

#include<stdio.h>#include<string.h>#include<vector>#include<algorithm>using namespace std;vector<int>ans;int main(){    int n;    while(~scanf("%d",&n))    {        ans.clear();        for(int i=0;i<=100000;i++)        {            if(n-i>=0)            {                int temp=n-i;                int sum=temp;                while(temp)                {                    sum+=temp%10;                    temp/=10;                }                if(sum==n)ans.push_back(n-i);            }        }        printf("%d\n",ans.size());        sort(ans.begin(),ans.end());        for(int i=0;i<ans.size();i++)        {            printf("%d ",ans[i]);        }        printf("\n");    }}

D. Sorting the Coins
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

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:

  1. He looks through all the coins from left to right;
  2. 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
41 3 4 2
output
1 2 3 2 1
input
86 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个操作,每一次将一个位子上的硬币变成不流通的,然后进行操作,询问操作轮回次数:

一次轮回的操作是:

找到一个不流通的货币和右边的流通货币(必须相邻,如果不相邻不能交换)进行交换,然后依次做下去。

每一次将一个硬币变成不流通的,然后问这样的一个序列进行的轮回次数。


思路:


如果将一个货币变成不流通的时候,我们不难发现,如果他和最后一个位子不能形成联通,那么其对答案的贡献为+1,否则如果能够构成连通,我们考虑将连通的整块部分在答案中减去即可。


Ac代码:

#include<stdio.h>#include<string.h>using namespace std;int vis[1450000];int f[1450000];int sum[1450000];int a[1450000];int find(int a){    int r=a;    while(f[r]!=r)    r=f[r];    int i=a;    int j;    while(i!=r)    {        j=f[i];        f[i]=r;        i=j;    }    return r;}void merge(int a,int b){    int A,B;    A=find(a);    B=find(b);    if(A!=B)    f[B]=A;}int main(){    int n;    while(~scanf("%d",&n))    {        int ans=1;        int now=n;        memset(vis,0,sizeof(vis));        for(int i=1;i<=n;i++)scanf("%d",&a[i]),f[i]=i,sum[i]=1;        printf("1 ");        for(int i=1;i<=n;i++)        {            vis[a[i]]=1;            ans++;            while(now>=1&&vis[now]==1)now--,ans--;            printf("%d ",ans);        }        printf("\n");    }}

E.我是萌萌哒E题题解

F.我是萌萌哒F题题解


阅读全文
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苹果手机5c屏幕没有反应怎么办 玩穿越火线屏幕两边是黑的怎么办? 8g内存只有2g可用怎么办? 三星7e微信分身打不开怎么办? 光猫的网口1不亮怎么办 两年前的发票发现名头有错误怎么办 苹果5s手机通话声音小怎么办 华为全网通手机电信卡打不了怎么办 合约机移动违约不返话费我该怎么办 电信手机卡合约套餐要到期了怎么办 苹果6s联通4g网速慢怎么办 营业厅买到的不是全网通手机怎么办 全网通手机联通卡被禁用怎么办 红米5手机关机充电自动开机怎么办 华为平板怎么解锁密码忘了怎么办 华为荣耀手机开锁密码忘记了怎么办 畅玩7x密码忘了怎么办 过了时的手机没有刷机包怎么办? 刷了个刷机包游戏玩不了了怎么办? 华为麦芒5手机外放声音小怎么办 微信显示存储卡已拔出怎么办 储存卡已拔出微信头像不可用怎么办 智能手机的电话卡取不出来了怎么办 换了苹果手机通讯录没了怎么办 手机玻璃膜一角翘起来了怎么办 华为畅玩7x耗电快怎么办 魅蓝5s充电器死机了怎么办 苹果手机乐动力不计步数怎么办 意大利居留按手印时间过了怎么办 酷派t1手机解析包出现问题怎么办 p新买的手机壳有味怎么办 门锁钥匙口竖着钥匙放不进去怎么办 摩拜单车被别人骑走了怎么办 捡到苹果8p手机怎么办才能自己用 用力按压导致玻尿酸变形移位怎么办 华为麦芒5应用锁密码忘了怎么办 华为麦芒6应用锁密码忘了怎么办 华为手机的设置不在桌面了怎么办 华为手机所有应用都不在桌面怎么办 华为麦芒5设置页面不显示怎么办 华为麦芒5主屏页面不显示怎么办