zoukankan      html  css  js  c++  java
  • Codeforces Round #441 (Div. 2, by Moscow Team Olympiad)(A B C D)

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

    题意本身不难,就看能不能全面的想到了。

     1 #include <bits/stdc++.h>
     2 #define ll long long int
     3 #define N 100000
     4 #define mem(a) memset(a,0,sizeof(a))
     5 using namespace std;
     6 int main(){
     7   int n;
     8   int a,b,c;
     9   cin>>n>>a>>b>>c;
    10   if(n==1){
    11     cout<<0<<endl;
    12     return 0;
    13   }
    14   int m=min(a,min(b,c));
    15   int sum=min(a,b)+(n-2)*m;
    16   cout<<sum<<endl;
    17   return 0;
    18 }

     

    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 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 ≤ 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 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

     乍一看没啥思路,仔细想下就是求模。

    直接上代码。

     1 #include <bits/stdc++.h>
     2 #define ll long long int
     3 #define N 100000
     4 #define mem(a) memset(a,0,sizeof(a))
     5 using namespace std;
     6 int s[N],a[N];
     7 vector<int> v[N];
     8 int main(){
     9   int n,m,k;
    10   cin>>n>>m>>k;
    11   for(int i=0;i<n;i++){
    12     cin>>a[i];
    13     s[a[i]%k]++;
    14     v[a[i]%k].push_back(a[i]);
    15   }
    16   for(int i=0;i<k;i++){
    17     if(s[i]>=m){
    18       cout<<"Yes"<<endl;
    19       for(int j=0;j<m;j++){
    20         cout<<v[i][j]<<" ";
    21       }
    22       cout<<endl;
    23       return 0;
    24     }
    25   }
    26   cout<<"No"<<endl;
    27   return 0;
    28 }
    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
    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.

     这题看上去挺简单,一般直接写就会TLE,其实这题不能用暴力,只要仔细想一下,就算所有的位数都取最大的那也只是多一个81,

    所以干脆直接从它的前100位开始,这样就减少好多数据量了。

     1 #include <bits/stdc++.h>
     2 #define ll long long int
     3 #define N 100000
     4 #define mem(a) memset(a,0,sizeof(a))
     5 using namespace std;
     6 int n;
     7 vector<int> v;
     8 int main(){
     9   cin>>n;
    10   if(n<1000){
    11     for(int i=0;i<n;i++){
    12       int a=i,cnt=i;
    13       while(a){
    14         cnt+=a%10;
    15         a/=10;
    16       }
    17       if(cnt==n){
    18         v.push_back(i);
    19       }
    20     }
    21   }else{
    22     for(int i=n-100;i<n;i++){
    23       int a=i,cnt=i;
    24       while(a){
    25         cnt+=a%10;
    26         a/=10;
    27       }
    28       if(cnt==n){
    29         v.push_back(i);
    30       }
    31     }
    32   }
    33   int ans=v.size();
    34   if(ans){
    35     cout<<ans<<endl;
    36     for(int i=0;i<ans;i++){
    37       cout<<v[i]<<endl;
    38     }
    39   }else{
    40     cout<<0<<endl;
    41   }
    42   return 0;
    43 }
    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
    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.

     

    这题一开始没看懂题意,结果比赛的时候没写出来,

    这题也不能暴力,两层循环需要优化

    其实就是找最右边的零。

     1 #include <bits/stdc++.h>
     2   #define ll long long int
     3   #define N 300005
     4   #define mem(a) memset(a,0,sizeof(a))
     5   using namespace std;
     6   int k[N];
     7   int n;
     8   int main(){
     9     cin>>n;
    10     int m=1;
    11     int ans=n;
    12     printf("1 ");
    13     for(int i=1;i<=n;i++){
    14       int x;
    15       scanf("%d",&x);
    16       m++;
    17       k[x]=1;
    18       while(k[ans]&&ans>=1){
    19         ans--;
    20         m--;
    21       }
    22       cout<<m<<" ";
    23     }
    24     printf("
    ");
    25     return 0;
    26   }
  • 相关阅读:
    【leetcode】N叉树的前序遍历
    【leetcode】第 N 个泰波那契数
    【leetcode】下一个更大元素 I
    【leetcode】Nim 游戏
    【leetcode】非递减数列
    053-208
    053-211
    053-56
    053-53
    053-236
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/7694137.html
Copyright © 2011-2022 走看看