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

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

Разбор задач контеста. Если что будет непонятно, смело пишите в комментариях! :)

Tutorial is loading...
Код
Tutorial is loading...
Код
Tutorial is loading...
Код
Tutorial is loading...
Код
Tutorial is loading...
Код
Tutorial is loading...
Код
Разбор задач Codeforces Round 379 (Div. 2)
  • Проголосовать: нравится
  • +82
  • Проголосовать: не нравится

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

Автокомментарий: текст был обновлен пользователем gepardo (предыдущая версия, новая версия, сравнить).

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

Why contest announcement blog is missing? Edit: now it's back!

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

When should we use it = st.lower_bound(tmp) and it = lower_bound(st.begin(),st.end(),tmp)?

I know first one has logarithmic complexity and second one 'maybe' linear. But in some cases, we can use only second one. I have read this. What exactly is meant by non-random-access iterators?

AC using first

TLE using second

So I was expecting maybe this will give TLE. But it didn't.

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

    it = lower_bound(st.begin(),st.end(),tmp)

    for std::vector it is logarithmic complexity

  • »
    »
    10 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +13 Проголосовать: не нравится
    it = lower_bound(st.begin(),st.end(),tmp)

    This is regular operations for vectors, arrays, etc. It only works in log N if you can "jump" to a random place in O(1). For example I can look at the x-th element of an array by using array[x-1] in O(1).

    it = st.lower_bound(tmp)

    This is special function for sets, multisets, maps, etc. It uses a special walk through the tree which C++ constructs for you which has log N complexity. If you use the first method, it will get TLE because you can't "jump" to the x-th element of the set in O(1).

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

Why in A is O(1)? I thought it is O(N), isn't it?

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

for problem E, it is possible that you can't decrease the diameter by 2, no matter how you paint the nodes.

Example:
14
0 1 1 1 1 0 0 0 0 0 0 1 1 1
1 2
1 3
1 4
1 5
2 6
2 7
2 8
3 9
4 10
5 11
6 12
7 13
8 14

the diameter of the tree is 5, and no matter which node you paint, the diameter can't be less than 4.

I think the answer is the radius of the tree and each operation can decrease the radius by 1.

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

In problem D, I am getting MLE. Can someone help? code

Thanks!

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

I solved C using BIT by iterating over all second type of spells. BIT maintains the lowest value I can achieve to make potion using cost  ≤  some vale. code

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

Has anyone solved problem E,using dp on compressed tree ?

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

pretests of Problem F were a little bit weak... I even passed them all without checking the correctness.Sad story.

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

Why is the distance in Q4 defined as :

inline int dist(int x1, int y1, int x2, int y2)
{
	return max(abs(x1 - x2), abs(y1 - y2));
}

Instead of the normal eucliden distance function sqrt(pow(x1-x2,2) + pow(y1-y2,2)); ? I got wrong answer since I used eucliden distance and got an overflow. Why does this distance function work (the one defined in editorial : Q4 : dist function above)

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

In problem E, I still don't understand the meaning of "changes the color of all vertices u such that all vertices on the shortest path from v to u have the same color". Could someone please explain how this operation works for me?

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

    To paint node u, let it be of color white. Then do dfs from u and cover all those nodes that are white and reachable from u. Do not dfs to black nodes. Finally as you traverse nodes, paint them to black.

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

      i did the same thing but by doing it , i get ans = 4 for test case 3 whereas it is 3, can u explain how is it 3?

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

        I ran into the same result at first. For me, it turned out that it is not enough just to count the connected components with the same color. You also have to account for solutions where a component is repainted multiple times, such that the component grows with each repainting. The tutorial is actually pretty good on this. Hope this helps.

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

    I got confused too and had to basically ignore this whole problem.

    The pitfall is, while the “such that” clause is intended to describe the selection of u, non-native speakers may confuse “such that” with “so that”, and interpret the clause as the result of the operation. Compare:

    • Change the color of all vertices u, where each u is a vertex such that all vertices on the shortest path from v to u have the same color.
    • Change the color of all vertices u, so that after the operation, all vertices on the shortest path from v to u have the same color.
»
10 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For problem C: http://codeforces.me/contest/734/submission/22247048 Can someone help me understand why this solution fails ?

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

Thank u everyone..!

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

I really appreciate problem E, thanks for such a good problem!

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

In Problem E, please explain this " This operation changes the color of all vertices u such that all vertices on the shortest path from v to u have the same color (including v and u) ". thanks

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

In the last problem how do you go from "from where ..." To "Now it's not hard to find ai: ..."

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

In (Problem C — Anton and Making Potions, what if we do not use the upper_bound function?

What if I instead sort the first array based on points (m log m), and use two pointers? (order m + k).

I felt this should also work, since the complexity is O(m log m + k), but it is giving me TLE.

Could someone please help me understand why this isn't working?

My Code — CODE

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

Why is my solution giving WA for, it uses the same binary search logic as in the editorial ? http://codeforces.me/contest/734/submission/22262919

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

Кто-нибудь, расскажите, пожалуйста, нормальное решение задачи E.

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

Very well written editorial. Thanks!

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

For F, why is complexity ? We have a loop over the digits, which takes . However, can someone explain why val is O(n)?

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

Did anybody do E with dp on tree??

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

Had to say — a perfect implementation of question C , learned a lot .thanks alex256

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

any idea what does test 43 in D do ? I keep getting WA in it

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

D. Anton and Chess when I run code in my codeblocks the answer is right for test 2. But when I submit the same code to the system. the system is run different result from me. Can someone help me? http://codeforces.me/contest/734/submission/22280387

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

For problem C: http://codeforces.me/contest/734/submission/22284393 Can someone help me understand why this solution WA ?

using greedy & two pointer

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

Help! Can someone help me to know why this submission (22316916) give wrong answer!

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

Hi! Can someone help me to know why this submission give wrong answer! Problem D — http://codeforces.me/contest/734/submission/22316916

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

Hi! Can someone help me to know why this submission give wrong answer! Problem E — http://codeforces.me/contest/734/submission/22295869

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

How the complexity of problem c code is O(m.logK + k) ?

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

In D, why answer of test3 is 3? I found it is 4, isn't it?

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

Could someone explain to me what the paint operation does in the problem E (Anton and Tree)? In particular, I don't understand this: We denote it as paint(v), where v is some vertex of the tree. This operation changes the color of all vertices u such that all vertices on the shortest path from v to u have the same color (including v and u). Does the color of v always have to change? In the example tree, why does after paint(3), all but node 6 get painted black? Take the shortest part from 3 to 6, is it also valid to make 5 white?

Thanks in advance!

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

Regarding Problem 379F — Anton and School, Why can't the correctness be checked directly by generating b & c using;

bi = (ai and a1) + (ai and a2) + ... + (ai and an) ci = (ai or a1) + (ai or a2) + ... + (ai or an)

If one doesn't derive the correctness formula as in the editorial, then does this naive approach result in time limit exceeded?

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

Regarding Problem 379E — Anton and Tree, the editorial shows a proof for upper limit on number of operations needed i.e. (d+1)/2, but which step of the proof states that this is exactly the number of operations needed?

Could anyone arrive at an iterative solution to the DFS instead of the recursive ones shown in the editorial?

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

Thanks for the help, I was stuck on C.

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

for (int i = 0; i < 31; i++) for (int j = 0; j < n; j++) if ((a[j] & (1LL << i)) == 0) bits[i][j] = 0; else bits[i][j] = 1;

Can someone explain me those lines for me from problem F??

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

For problem F, why is it not sufficient that summation of d(i) be divisible by 2n and a(i) be divisible by n and non-negative i.e. why do we have to check for correctness ?

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

Problem F: Is there a intuitive/combinatorial way to see that (a & b) + (a | b) = a + b ?

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

Hi, could somebody help me figure out the problem with this submission for problem C: Anton and Making Potions.

Algorithm:

  1. Try minimum for only first spell — O(m)
  2. Try minimum for only second spell — O(k)
  3. Try minimum for both spells using a greedy search — O(m log k)

Here is the submission number 22677789.

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

Hi! My code for problem E is getting RTE in test case 2. But the code runs successfully on my pc, including test case 2. Can anyone help me find out what's the problem? Code: http://codeforces.me/contest/734/submission/22926048

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

F good problem!

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

In problem C. Anton and Making Potions

this line gets WA in case 16

 int j=lower_bound(d.begin(),d.end(),s-b[i])-d.begin();

between editing it to

 int j=lower_bound(d.begin(),d.end(),s+1-b[i])-d.begin();

gets AC..why ?

also here the full submissions:

http://codeforces.me/contest/734/submission/23527242 (WA)

http://codeforces.me/contest/734/submission/23527311 (AC)

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

    Its because lower_bound gives the first occurrence of the number to be found. It may be a case where there are multiple c[i] possible for the same money_left.

    We have to find the last occurrence for this money_left.

    Let's say the array c and d are:

    c=[1, 2, 3, 4, 5];

    d=[10, 10, 11, 12, 13];

    Now if the money left is 10 , then the max value in c will be 2.

    Using int j=lower_bound(d.begin(),d.end(),s-b[i])-d.begin(); will give 1.

    So to get the last occurrence we have to add 1 to the money_left which will return the last occurrence of the required answer.

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

in problem E Can't we just count number of unconnected white color nodes and unconnected black nodes and print minimum of them ....