Блог пользователя BledDest

Автор BledDest, история, 9 лет назад, По-русски

Приносим свои извинения за серьёзные неполадки с тестирующей системой в самом начале контеста. Мы надеемся, такого не повторится.

Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
Разбор задач Educational Codeforces Round 23
  • Проголосовать: нравится
  • +93
  • Проголосовать: не нравится

»
9 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

По поводу задачи B... Можно решить проще: и по памяти, и немного по времени (вроде бы). Можно одним проходом по массиву найти 3 наименьших элемента и их количество, а потом 3-4 условия на то, что выводить (случаи, если наименьшего больше трех раз встретилось, и парочку, если наоборот).

  • »
    »
    9 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Можно, конечно. По времени то же должно получаться. Решение из разбора разве что объяснять проще.

»
9 лет назад, скрыть # |
 
Проголосовать: нравится +16 Проголосовать: не нравится

Thanks for fast editorial! :D An easy trick for D: Once you have calculated the sum of minimum in all subarrays, you can just make all the numbers negative ( multiply each of them by -1) and then again calculate the sum of minimum in all subarrays. The sum of maximum will just be the negative of that.

»
9 лет назад, скрыть # |
 
Проголосовать: нравится +6 Проголосовать: не нравится

C problem also solvable with DP (27810451) works in 183 * 92 * 2.
D problem also solvable with D&C approach(27810683) which works in N * log(N).

»
9 лет назад, скрыть # |
Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

Альтернативное решение C:

Заметим, что все числа, меньшие s — не действительно большие. Рассмотрим число x >= s. Очевидно, x — сумма_цифр(x) >= x — максимальная_сумма_цифр = x — 9*18 = x — 162. Тогда, если x >= s + 162: x — сумма_цифр(x) >= s + 162 — 162 = s, то есть число действительно большое.

Значит, количество НЕ действительно больших чисел от 1 до n равно:

X = кол(<s) + кол(>=s) = min(s-1, n) + (кол-во не действительно больших чисел на отрезке [s, min(s+161, n)]).

Ответ на задачу равен n — X.

»
9 лет назад, скрыть # |
 
Проголосовать: нравится +70 Проголосовать: не нравится

There is also another solution for problem C.

Since we know that the maximum value for n is 1018, then the maximum sum of digits we can get is for n = 999999999999999999, which is 162. Using this observation, and the proof mentioned in the editorial, we can be sure that all integers in the interval [s + 162, n] are really big numbers. So, what is left now is to check the interval [s, s + 161] with a simple for-loop.

Source Code
  • »
    »
    9 лет назад, скрыть # ^ |
    Rev. 2  
    Проголосовать: нравится -45 Проголосовать: не нравится

    #include <bits/stdc++.h>

    define ll long long int

    using namespace std;

    int main() {

    ll n,s;
    cin>>n>>s;
    float y=log10(s);
    
    int k=ceil(y);
    if(s==1)
    {
        k=1;
    }
    //cout<<k<<endl;
    if(n<=s)
    {
        cout<<0;
        return 0;
    }
    ll ans=n-s-(9*k);
    ll l=0;
    ans=max(ans,l);
    ll p=(9*k)+s;
    for(ll i=s;i<=p;i++)
    {
        if(i>n)
        {
            break;
        }
        else
        {
            ll h=i;
            ll sum=0;
            while(h!=0)
            {
                sum+=h%10;
                h=h/10;
            }
    
            if((i-sum)>=s)
            {
                ans++;
            }
        }
    }
    cout<<ans;
    return 0;
    

    }

  • »
    »
    9 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +1 Проголосовать: не нравится

    Amazing observation :D

  • »
    »
    5 дней назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    My god,you are the shit!

»
9 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

Can i know the difficulty difference between an educational round and a normal round.How does difficulty of each problem in Normal round map to educational round?

»
9 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I did not understand the editorial to problem B. please explain. thanks :)

  • »
    »
    9 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    A very simple approach to problem B: Sort the array containing the numbers. Pick the first three. Now, we can prove that the minimum product will always contain only these three elements and nothing else. Thus, we next count occurrences of these first three letters in the whole sequence. Let the first, second and third numbers be a, b and c. And their repetition in the whole sequence be countA, countB, countC, and let they all be different. Then, ans is countA*countB*countC. But, if b repeats, then b is equal to c (sorted order). Then ans is countA* (ways of choosing 2 elements from countB elements). Similarily if a is repeated two times. Ans is countB only(any other repetition of a will cause it to be equal to c). If all three are same. Then ans is (no. of ways of choosing 3 elements out of countA elements). Here, the no. of ways of choosing 3 elements out of n elements = n*(n-1)*(n-2)/6 Here, the no. of ways of choosing 2 elements out of n elements = n*(n-1)/2

»
9 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I am not getting problem A ,I mean why does not (x2-x1)%x==0 and (y2-y1)%y==0 work?Please explain.

»
9 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

How to approach a problem like 'B'? I mean how does the right technique strike? Are there any pointers(in the limits or similarity to a classic problem) to the right way? Any help would be appreciated.

  • »
    »
    9 лет назад, скрыть # ^ |
    Rev. 3  
    Проголосовать: нравится 0 Проголосовать: не нравится

    Hmm. I don't really know :) I used the approach from the editorial in couple of other problems, though I can't remember any in particular. I guess that if this one doesn't seem intuitive then you can always try the other one. Where you just check some cases on the amount of the elements less than the third minimum of the array. Most people implemented this solution.

    UPD: the author of this task told me that the similar approach is used here

  • »
    »
    9 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    I solved it differently. First sort array a. Then take three smallest values, let them be a,b,c in this order. Also, while doing input of array, keep array cnt[x], which tells how many times x appeared in input.

    1. case a!=b!=c Then number of ways is cnt[a] * cnt[b] * cnt[c] (simple combinatorics)
    2. case a == b but b!= c (if b!=c then a!=c as well, because a,b,c are sorted) then there are cnt[a] * (cnt[b] — 1) /2 ways to choose pair (a,a) and cnt[c] to choose c, so to get total multiply these two.
    3. case a != b, b == c Similar to 2. case 4.case a == b == c then answer is cnt[a] * (cnt[b] — 1) * ( cnt[c] — 2) / 6 (again simple combinatorics)

    That covers all cases.

»
9 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Hello, In problem D I don't know why did I get a Wrong answer on test 12. Can you please help me?

27814997

»
9 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

How to solve the problem D when imbalance is defined as (max-min)^2 or (max-min)^k with k>=2 in better than O(N^2)?

»
9 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +5 Проголосовать: не нравится

WHat is the movement of

source: 2 -5 destination: -8 8

move: 2 1

. in Problem A ? How this is YES ?

»
9 лет назад, скрыть # |
Rev. 4  
Проголосовать: нравится 0 Проголосовать: не нравится

Why my MEX queries does not work? Here's my code:

Solution
»
9 лет назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

aren't NlogN solutions supposed to pass for D ?? Mine got TLE because it uses insert and lower_bound in a set n times ! and I know their complexity is logN http://mirror.codeforces.com/contest/817/submission/27808174

»
9 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

I solved Problem C by searching for the first 'Really Big Number' greater than 's' and less than or equal to n by running a loop.I considered the fact that if a number suppose i=25 doesn't satisfy the 'Really Big Number' condition then no number from 20-29 will satisfy the condition and thus I can jump from i=25 to i=30.By following this, if i found the first valid number less than or equal to n then our answer will be 'n-i+1' otherwise '0'. My question is whether my approach is correct or not because i got accepted but i couldn't proved why my approach is correct. Here is my accepted code.

  • »
    »
    9 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +9 Проголосовать: не нравится

    Your approach is absolutely correct since you have used binary_search.

    As you can observe from the value of num-sumD(num), it is always increasing.

    • For num = 1 to 9 : num-sumd(num) = 0
    • For num = 10 to 19 : num-sumD(num) = 9
    • For num = 20 to 29 : num-sumD(num) = 18
    • .
    • .
    • .
    • For num = 999999999999999990 to 999999999999999999(18 times) : num-sumd(num) = 999999999999999872

    And since the value of num — sumD(num) is strictly non-decreasing, take the lower limit to be 1, higher to be n + 1 and search for the appropriate num such that for values less than num (num — sumd(num)) is less than s and for values greater than or equal to num (num — sumd(num)) is greater than or equal to s.

    So your approach is alright. For more clarification you can see my code : 27802867

»
9 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится
#include<iostream>
using namespace std;
int main()
{
	long long a,s,q;
	cin>>a>>s;
	if(s%9!=0)
	q=s/9+1;
	else
	q=s/9;
	if (a-10*q>=0)
	cout<<a-10*q+1;
	else
	cout<<"0";
}

I don't know where I went wrong in problem C please help I don't know much about dp and stuff I used the fact that difference of any number and its sum of digits is equal to nearest multiple of 9 of that number.

»
9 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone help me with sqrt decomposition solution for 817F - MEX Queries (According to editorial its possible).

I can't figure how this approach can be used to solve Problem F.

  • »
    »
    9 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +4 Проголосовать: не нравится

    It's not much different from the segment tree one. Numbers are also compressed and the queries are the same.

    You process assigning on a segment and xor of the segment by dividing original array into sqrt blocks, for each block you maintain its sum. Lazy propagation is used too. Just every query works in instead of .

    We decided not to force participants to use solutions.

»
9 лет назад, скрыть # |
 
Проголосовать: нравится +4 Проголосовать: не нравится

Similar problem as D are :

http://practice.geeksforgeeks.org/problems/maximum-of-minimum-for-every-window-size/0

http://agc005.contest.atcoder.jp/tasks/agc005_b

Same approach can be use in these problem , next smaller , next greater using stack .

»
9 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone please tell why my code is getting WA ??? Link .Thanks in advance !!!

  • »
    »
    9 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +3 Проголосовать: не нравится

    your code is giving wrong answer because suppose you are standing on a node where xbit=1 and ybit=0 then there xor is 1 and you are adding all the numbers that have that bit zero. but this is wrong because this does not gaurantee all the number in this group to be smaller then your leadership(l).this group till now are only equal, not became less. so you can't add them. if xbit=0 and ybit=1 then your case is correct. there may be case some of these number can latter become equal to or greater than leadership(l). link this is my code you can see . this might help you visualising what i am saying.

»
9 лет назад, скрыть # |
Rev. 5  
Проголосовать: нравится 0 Проголосовать: не нравится

Problem B In the tutorial "Let's also store set of three elements which give minimal product of the array". In c++ how do you calculate the product if all the numbers are greater than 10^9?

»
9 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Скажите, а что делать, если в задаче Е нет числа p[i] ^ l[i]?

»
9 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Problem B In the tutorial "Let's also store set of three elements which give minimal product of the array". In c++ how do you calculate the product if all the numbers are greater than 10^9?

»
9 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

There is also one more solution of problem C.

All numbers bigger than s+9*18 and not more than n are really big. So we can check numbers from s to s+9*18 and add max(0, n — s-9*18) to answer. Sorry for my English

»
9 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +12 Проголосовать: не нравится

Problem F in such constraints can also be solved with coordinates compression and bitmasks in obvious way.

BTW, it's fastest solution at this moment. (171 ms without fastIO). 27832302

  • »
    »
    9 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    can you explain your approach . it is diificult to understand from code

    • »
      »
      »
      9 лет назад, скрыть # ^ |
       
      Проголосовать: нравится +1 Проголосовать: не нравится

      Global idea as it is in the editorial:

      The first two types of queries are translated to "assign value 1 or 0 on a segment" (set the number on some position is either present or not). The third is "for each i in segment [l, r] assign xi to xi^1" (this will inverse the segment as described in statement).

      Let's store this array of zeros and ones as bitset, it means that each byte of array will represent 8 values in 8 bit. Then you can perform first two queries on 32 values at once as simple assignment of int variable a[i] = 0 or a[i] = -1, and third query as a[i] = ~a[i]. To find the answer after each query you should find first value in array not equal -1 and find first zero bit index in it.

»
9 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone explain me D&C solution for D? Because solution with stacks looks awful. I was thinking D&C on contest but couldnt solve it. Edit: got it. this solution helped me http://mirror.codeforces.com/contest/817/submission/27832753

»
9 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Detailed approach for D???

  • »
    »
    9 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +12 Проголосовать: не нравится

    Considering finding the sum of minimum values for all subsegments, the solution tries to know for each index i how many times a[i] will be the answer. If x is the number of subsegments that contain index i and a[i] is the minimum value, then ans[i] = x·a[i]. The final answer will be .

    For the correctness of the algorithm, if a subsegment have more than one cell with the same value, we will consider the answer to be the leftmost cell.

    For index i, Let:

    • L[i] be the rightmost index j < i and a[j] ≤ a[i]
    • R[i] be the leftmost index j > i and a[j] < a[i]

    Clearly, All cells in the range [L[i] + 1, i - 1] have values greater than a[i] and all cells in the range [i, R[i] - 1] have values greater than or equal to a[i]. Now, we can say that index i can be the answer to any subsegment whose startpoint is in the range [L[i] + 1, i] and endpoint is in the range [i, R[i] - 1]. So, ans[i] = (i - L[i])·(R[i] - i)

    How to compute the array L? Keep a stack with you and go from left to right. The stack is initially empty and will contain indices. Now, we are at index i:

    • If the stack is empty, L[i] =  - 1.
    • If top of the stack is j and a[j] ≤ a[i], then L[i] = j
    • If top of the stack is j and a[j] > a[i], then pop this element and do the same check for the new stack top.

    If j is popped from the stack, then for k > i, can L[k] = j? The answer is no, because a[j] > a[i] and i is closer to k from j, so if a[i] ≤ a[k], then L[k] = i. Otherwise, a[j] > a[i] > a[k]. That's why we remove j from the stack because it will never be used again.

    After finding L[i], push i onto the stack.

    You can compute the array R in a similar way from right to left.

»
9 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can any one tell me why we are doing mod 2 in problem A.

»
9 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone explain Problem D?? I read the solution many times but not getting it.

»
9 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Is there an online solution for F ?

I thought of having a sorted set of ranges . Insertion and deletion can be done in O(qlog(q)) amortised time . But I'm not sure how to implement flipping efficiently .

  • »
    »
    9 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    You can use Dynamic Segment Tree.

    I've tried to do it, but got Memory Limit Exceeded. I think my implementation is still not good enough. If you want some idea on how to code, this is my code: 27989735

    I think that I would receive Accepted if memory limit was higher.

    • »
      »
      »
      9 лет назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      Can you please briefly explain your approach.

      • »
        »
        »
        »
        9 лет назад, скрыть # ^ |
         
        Проголосовать: нравится +1 Проголосовать: не нравится

        Imagine that you will solve this problem with a standard segment tree with range update (10^18 nodes and lazy propagation).

        The only thing that change in the dynamic segment tree is that you do not declare an static array with size 4 * 10^18, you just create those nodes that you really need to, because there will not exist 10^18 queries.

        Here is an example, if you do not have any query in range [10^5, 10^10], simply don't create those nodes.

        And if you want to flip some interval [l, r], just do this:

        new_value = (r - l + 1) - previous_value;
        
»
9 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

How is the complexity for B (as per the solution given in editorial) is O(n)? How will you find the 3 smallest no.s in O(n), i.e without sorting( which would take O(nlogn) )?

»
9 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

Can someone please tell why my code is getting WA at test 12? Link Thanks in Advance!

»
9 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Anyone there who can help me with my previous post. Please help me. Don't know what is the problem with my code. Why my code gives tle in 43th test case.

  • »
    »
    9 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    I'm not sure but this can probably be Java anti-sort test. Arrays.sort over an array of primitive types uses quicksort and may work in O(n2). Try replacing it either with Collections.sort or with array if Integer values.

»
9 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Что измениться в задаче Е если будет ограничение pi^pj <= l а не pi^pj < l ???

»
9 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

I am getting WA on test case 7. Can anyone tell me which case I am missing??? The method I used to solve this problem is almost identical to the editorial... I just put the values L and R+1 in a set and then constructed segment tree from there... Any help is appreciated. :)

My Code
»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Chtholly Tree used to solve F: 88862104

»
4 года назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

You can solve the 817B using one traversal instead of two. Simply store the numbers inside a std::map, whose keys are by default sorted ascending. Then there are only a few cases:

Assuming the frequency of the smallest 3 elements are respectively a, b, c:

if a>=3, the answer is comb(a, 3)

if a==2, the answer is b

if a==1 && b==1, the answer is c

if a==1 && b>=2, the answer is comb(b, 2)

»
3 года назад, скрыть # |
 
Проголосовать: нравится -10 Проголосовать: не нравится

Don't understand editorial for E, can someone explain it more detailedly?

Thanks a lot in advance.