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

Автор chokudai, история, 4 года назад, По-английски

We will hold AtCoder Beginner Contest 166.

The point values will be 100-200-300-400-500-600.

We are looking forward to your participation!

  • Проголосовать: нравится
  • +77
  • Проголосовать: не нравится

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

Thank you atcoder for the wonderful weekend !!

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

I wish it will be a good round!

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

Why so easy A,B,C,D,E?

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

Why so much hate in this world cry

Get AC on A, B, C, D, E and WA only on the last of the 60 tests of the F...

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

![ ]()

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

How to solve F!

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

    Keep alive this round, try to alive next round(Be careful when there are two 1, and one 0).

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

    Always increase a smaller variable and decrease a greater one in a pair, except for the one case: when two variables you need to change are $$$1, 1$$$ and the third one is $$$0$$$, in this situation you have to choose what variable to increase depending on the next request in order to avoid a case like where you have $$$2, 0, 0$$$ and the next request is $$$BC$$$.

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

    Here's my solution.

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

How to solve problem E?

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

    You need to use the frequency map, In first loop perform f[ i — a[i] ]++, and in second loop perform count += f[ a[i] + i ]. that's it

  • »
    »
    4 года назад, # ^ |
    Rev. 2   Проголосовать: нравится +3 Проголосовать: не нравится

    For two attendees to pair i-j = hi + hj (Assume i>j) should hold where i and j are indices of the attendees. Move both the i terms on i side and j terms on the other then it reduces to i-hi = hj + j. Which is equivalent to finding number of comment elements in two arrays by making arrays for both i-hi and the other as hj+j.

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

    E is pretty easy if you make the observation that for any j > i following condition has to holds, j — A[j] = A[i] + i. so keep a map to count the occurrence of A[i] + i observed till now. add the count of j — A[j] into the answer.

    solution

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

    You need to find number of pair of indices such that |j-i| = A[i] + A[j] . Assume j > i, then, you need to check which (i,j) satisfy j — A[j] = i + A[i].

    You can start iterating from left, keep a count of i+A[i] and for every new index j, do, ans+=count[j-A[j]]

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

    We are considering j > i so we need to find all i and j such that

    j - i = a[j] + a[i] which is a[j] - j = - a[i] - i

    so for each j we need find count all of those whose value - a[i] - i is equal to a[j] -j. We can use a map to store values of - a[i] - i and add it to our answer in each step for value a[j] - j.

    Code

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

    Just use the fact that A[i]+A[j]=abs(i-j) expands to i-A[i]=A[j]+j (atleast in positive value of abs sign). For the other one it'll cover cases by symmetry. No compute the two arrays with entries i-A[i] and j+A[j], store the counts of elements in a frequency map and return the answer.

    A simple solution is here

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

    An easy code to understand

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

why greedy for F fails? anyone Please?

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

    What do you do when both A and B are equal and next event is AB?

    For example: A = 1, B = 1 and C = 0 and event is AB.

    You need to check if next event is BC or AC and work accordingly. Can't just make anyone of it +1 and other -1.

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

      Yeah for this I did offline thing. At every stage I checked if both are equal then which is having more query remaining. So the one with higher no of query remaining I add to it(give it priority)

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

        You should give priority to the next closest move, not to the most frequent among remaining. For example A = 1, B = 1, C = 0, and the sequence of moves is AB, AC, BC, BC, BC. On move AB you should add 1 to A, to make move AC possible, not to B, even though BC is more frequent.

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

this F is not very hard,i think,but has a number of details to deal with.

i finally got accepted after 5 tries with a loooooooooooooong code.

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

Got 457 lines of code in F)

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

E anyone?

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

I had a lot of TLEs with my solutions for this contest. For example, here is my code for problem E (C++):

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;

int main(){
		int N, count=0;
		cin>>N;
		int att[N];
		
		for(int i=0; i<N; i++){
			cin>>att[i];
		}
		
		for(int i=0; i<N-1; i++){
			
			for(int j=i+1; j<N; j++){

				if(abs((i+1)-(j+1))==(att[i]+att[j])){
					count++;
				}
			}
		}
		
		cout<<count;
	}
	

I got AC for half of the test cases, and TLE for the other half. Can anyone tell me why?

Also, if someone can tell me what might generally be the case when AtCoder grading returns a TLE, please let me know. Thanks in advance!

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

    Your code has a complexity of O(N^2) and N is 1e5. So that is why you are getting TLE.

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

    TLE = Time limit exceeded.

    Checking every possible pair (complexity O(N^2)) is not efficient enough for such a big value of n as 2*10^5.

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

What is the approach for F?

I tried defining each of the 8 choice possibilities (For AB choose A first or B if both remain, similary for AC and BC) at the start and check if it works. Do you have to do something like this at each step and maintain an 8 * n DP or something like that?

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

    My solution was to choose such an option that I will survive current and also next round. If both choices are survivable then go greedy.

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

    You could just run a DFS and check if you can reach a path of length n. Start with state a,b,c and then at each point proceed to a 2 posibble state given by choice[i].

    Here is my submission: https://atcoder.jp/contests/abc166/submissions/12776155

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

    I tried with 27*n dp but it failed

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

      Any one passed using dp?

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

      i did a 64*n dp which got AC.

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

        can you share your approach. Also explain your dp states

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

          I have similar (maybe even the same approach) — you can notice that we don't care what a value is if it's bigger than 3, so we can have states (position, a, b, c), and after each step a = min(a, 3) and so on. You have 64 * n states and two transitions from each state. Here's my code

          https://atcoder.jp/contests/abc166/submissions/12752950

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

            we don't care what a value is if it's bigger than 3. I think we can make a=min(a,2), with 27*n states is also acceptable. my submission

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

            Can you share your intuition behind why don't we care for values that are greater than 3 (or 2)?

            Why just taking 3 would do? I understand by looking at examples but am not able to wrap my head around it. It would help if you could share your intuition perhaps.

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

              This whole idea came to me when I noticed that configuration "1 1 1" is always correct if we choose greedy approach (add 1 to smaller of two). That led me to conclusion, that if sum is bigger or equal to 3 (unless it's 3 0 0 and operation is BC) is always correct as well. Connect this with pretty natural DP solution and you get my solution. If you have more questions feel free to ask

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

    Greedy approach can get AC, You only need to check a couple of things first :

    • If one of them is 0 you have to add it +1.
    • If both of them 1, you need to check which one comes next first, for example if the order is AB then BC ( A = 1, B = 1, C = 0 ) you have to add +1 to B otherwise you add it to the other.
    • If you're not in any of the other cases you can just add +1 to the minimum of them.
    • Keep checking if one of them is less than 0 return No

    Solution : https://atcoder.jp/contests/abc166/submissions/12771073

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

      why does this work

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

        It can be easily shown if one value is greater than the other, it's safe to increase the min value. Our only concern is when both values are equal to 1, that way one of them will become a zero, we need to know which one to increase. The choice is based on which value will be used again first. Same example as the last comments : A = 1, B = 1 and C = 0 it will depend on the order.

        • Suppose the order is : AB then BC, if we increase A our solution will fail.

        • And if the order is : AB then AC then if we increase B our solution will fail too.

        It is clear that we need to increment the value that comes next first in that case.

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

      UPD : Deleted

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

For F I did complete search using recursion and it passed. Can anybody explain why?

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

can someone help me in problem D ?

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

    D is quite easy. You just need to use brute force and compute all fifth powers of numbers from -1000 to 1000, and then you can use an O(n^2) algorithm to see if x is equal to the difference of any two. Code

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

      pff i feel so stupid !! thanks bro

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

      In my first atempt i used 100 instead of 1000, and it failed. But the 1000 was only a lucky guess.

      How would we find the correct minimum number?

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

        Find the smallest number x for which (x^5-(x-1)^5) > 1e9.

        I think it comes around 120.

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

        I guess look at $$$b=a-1$$$ for boundary condition because that minimise $$$x$$$ for given $$$a$$$. I tried $$$100$$$ because I misread number of digits in $$$100^5-99^5$$$. Then I used $$$200$$$ and it passed.

      • »
        »
        »
        »
        4 года назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится
        • for (i in range 0 -120 )
        • for (j in range -64 i-1)
        • if (i^5 -j^5==x) — done You can find detail here
  • »
    »
    4 года назад, # ^ |
    Rev. 2   Проголосовать: нравится -6 Проголосовать: не нравится

    I don't have a strong proof as to why will there always be a pair less than |a| , |b| <= 10^3 but had some intuitive feeling and it ACed

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

      x^5 — (x-1)^5 is an increasing function for postive integers. The first time its value crosses 10^9 is when x is around 10^12.

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

English Editorail of F

Three Variables Game • When A + B + C = 0, N is 1 or more, so the answer is No. • When A + B + C = 1, at least one of the two variables is zero at any time, so the action on each turn is uniquely determined. • When A + B + C> = 2, the answer is No if the variables added and subtracted in the first turn are both zero, otherwise the answer is Yes. In the latter case, you can keep one of the variables being added positive by taking the following strategy each turn. {When one of the variables to add and subtract is zero and the other is positive, add one to the one that is zero and subtract one from the other. (A + B + C = 2, the variables to add and subtract are both 1, and when the character string of the next turn and the character string of the current turn are different, not the last turn, the character addition and subtraction of the next turn is performed. Add 1 to the variable you plan to do and subtract 1 from the other. {Otherwise, select appropriately

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

What wrong in my code for the problem D

x=int(input())
value=0
index=0
hashmap=dict()
while value<int(1e9)+1:
    value=pow(index,5)
    hashmap[value]=index
    index+=1
#print(hashmap)    

for i in range(1,64):
    b1=x+int(pow(i,5))
    b2=x+int(pow(-i,5))
    if hashmap.get(b1)!=None:
        print(hashmap[b1],i)
        break
    if hashmap.get(abs(b2))!=None:
        print(hashmap[abs(b2)],-i)
        break     
»
4 года назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

the language of D and E was so clear still couldn't solve it, got WA on last test case of E and TLE on D. I like atcoder's format easy language difficult task .makes these problems more approachable. Thanks Atcoder

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

Please help me with problem E and F

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

Yay I full solved all 6 problems with 0 incorrect attempts!

Timing was slow though :(

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

I thought the problem statement on B was not clearly defined. Otherwise, it was such a great contest! got WA on both D and E because I did not take long. Feel like killing myself :( Gonna upsolve F now.

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

I solved D by expressing A^5 — B^5 as (a − b)(a^4 + a^3*b + a^2*b^2 + a*b^3 + b^4).

At this point, I iterated over all possible divisors. Since a-b < (the term consisting of all positives), I only had to check the divisors less than sqrt(X). Then, once a-b was set, I binary searched to find the value of b (if any) to satisfy the expression.

My final runtime: optimized O(sqrt(X)*log(X))

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

    You can also check if A^5-B^5 equals to x for all a and b from -200 to 200. It's faster.

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

    can you share your code

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

      This code is easier
      Code
      just check from -1000 to +1000
      because if a = 1001 the minimum positive result is b = -1000 and the result is 1.0004901e+20 and it's bigger than x

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

    Yeah I also did first part of your method, but I think it was overkill when I looked at $$$100^5-99^5$$$. So I wrote the easy brute force.

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

In codeforces https://codeforces.com/contest/1345/problems we get list of all problems, how to do the same in atcoder, like all the task they appear in the same page. If anyone knows please comment the link of that page where all task appear on one page.

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

I overkilled F with dp, followed by 14-15 RTE's :(

PS: Atcoder could really work to differentiate between MLE and RTE.

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

Solution in simple words:

A. Just check the input string.

B. Compute the food each person has. One can use a frequency array to do this.

C. Iterate through all paths. Each path makes one observatory non-peak (or two, if they have same height).

D. Since $$$a^5 - b^5 = X > 0$$$, we have $$$a > b$$$, that is $$$b \leq a - 1$$$. So $$$a^5 - (a - 1)^5 \geq a^5 - b^5 = X$$$. Obviously $$$f(x) = x^5 - (x - 1)^5$$$ is increasing when $$$x > 1$$$ and decreasing when $$$x < 0$$$, so we can find the upper and lower bound of $$$a$$$ from $$$X$$$. Simply enumerate all possible $$$a$$$'s and check if $$$a^5 - X$$$ is the fifth power of some integer. This can be done with precomputing all fifth powers in the potential range. In practice we can use a looser bound to estimate the range of $$$a$$$.

E. Consider the number of pairs $$$(i, j)$$$ satisfying $$$i < j$$$ and $$$A_i + A_j = j - i$$$. Fix $$$j$$$, the number of $$$i$$$'s pairing with $$$j$$$ satisfying $$$A_i + i = j - A_j$$$. Just iterate from left to right and keep the frequencies of all $$$A_i + i$$$.

F. In each operation we can greedily take $$$1$$$ from the greater number and add $$$1$$$ to the smaller one. The only exception is there being two $$$1$$$'s and one $$$0$$$ and we are going to operate on the $$$1$$$'s. We have to create another $$$0$$$ in this operation, so we need to consider the next operation in order to make it doable. The answer is No if any operation creates a negative number.

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

My python solution12793022

I am getting WA for one test case (killer_05.txt). Can somebody please tell me what am I missing? Thank you.

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

https://atcoder.jp/contests/abc166/submissions/12803726 i am getting wrong ans on killer test 5 Can somebody help me out. It is for problem F atcoder 166

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

    I read it a bit fast but I think your mistake is in this case: "if(a[id1]==a[id2] && a[id1]==1 && a[id3]==0)".

    You are not taking into consideration the other special case where a[id2]=1 and a[id3]=0. Also the way you are searching for the future cases is wrong because you assign a char variable to a string one.

    Generally, I think that you only need to check for equality and increase the a[id], which is the one that will participate in a future choice sooner than the other.

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

For D — detail explanation and reduced range of searching with code Here

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

For problem E, I found an interesting solution. Let's imagine there are n persons standing on n consecutive cells in a row, the question ask for how many pairs of them can reach each other if they lay down to the left or to the right, formally it 's number of pairs which have sum of their heights equal to their distance. So we just let each person lay down to the left and to the right and mark these points. Then we iterate all marked points and add $$$from\_left[point]*from\_right[point]$$$ to the answer, check my code