atcoder_official's blog

By atcoder_official, history, 10 months ago, In English

We will hold AtCoder Beginner Contest 410.

We are looking forward to your participation!

  • Vote: I like it
  • +58
  • Vote: I do not like it

»
10 months ago, hide # |
 
Vote: I like it +10 Vote: I do not like it

500 Internal Server Error showing

»
10 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it
int rp = 0;
while (contest.end != true) rp++;
cout << rp << endl;
»
10 months ago, hide # |
 
Vote: I like it -56 Vote: I do not like it

How about D? How to do it?

»
10 months ago, hide # |
 
Vote: I like it -11 Vote: I do not like it

Problem C: This is some text.

// this is code
void inc(int pos, int d) {
    for (; pos < n; pos |= pos + 1)
        f[pos] += d;
}

The rest of the text.

»
10 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

F is so cooked?

Anybody got some hints? I don't see any observation that makes it simpler?

  • »
    »
    10 months ago, hide # ^ |
    Rev. 2  
    Vote: I like it +5 Vote: I do not like it
    Spoiler1
    Spoiler2
    • »
      »
      »
      10 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      I saw that part, but I don't know how to find the number of rectangles between two column/rows.

      • »
        »
        »
        »
        10 months ago, hide # ^ |
        Rev. 3  
        Vote: I like it +5 Vote: I do not like it

        List the first and last rows of the rectangle, calculate the sum of the elements in each column between them, and then the problem becomes finding how many intervals there are so that their sum is $$$0$$$. It can be solve easily using "Prefix sum" algo and an array.

    • »
      »
      »
      10 months ago, hide # ^ |
      Rev. 2  
      Vote: I like it 0 Vote: I do not like it
      Spoiler
    • »
      »
      »
      10 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      Nice Bro , how you got this trick ? have u done similar problems before .

»
10 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it
»
10 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

How to construct the sequence of operations for problem G, second sample input?

»
10 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I think E is easier than before. I usually cannot solve E in previous ABCs, but this time I got accepted in 52 minutes.

»
10 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

E is very easy.I solve A,B,CandE.I use Dynamic Programming to solve E.

»
10 months ago, hide # |
 
Vote: I like it -8 Vote: I do not like it

Can't we solve D by having a path xor of 1 to n (let's call this xn) and then using xn = min(xn, xn^b) for all b where b is one of the basis of xor of all the cycles in the graph connected with the path 1 to n?

  • »
    »
    10 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    My submission: submission

    I would like to know for which type of cases this will fail.

  • »
    »
    10 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    In theory, yes. In practice, I'm not sure. Take a look at this problem. Its solution is just like what you mentioned. It is the same question as D except for two differences:

    1- The constraint of D are smaller.

    2- The graph in D is directed, while this problem has an undirected graph.

    I tried to follow the same approach but I could not get it to work on a directed graph. But maybe due to the lower constraints, someone could figure out a way of doing it.

  • »
    »
    10 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    People who are downvoting me, it would be good if you also give some hint about why this approach fails.

»
10 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

haven't participated any coding contest for a year, and I just come back here. however i solved E but didn't come up with a solution with D... didn't realize it was just a bfs...

»
10 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can anyone give me an idea for problem D? I just can't come up with a solution for the cyclic graph.

»
10 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

very nice and elegant soln for E. Might be easy for some, but I couldn't get ahold of the DP idea till the very end.

»
10 months ago, hide # |
Rev. 2  
Vote: I like it +5 Vote: I do not like it

does someone how to cheeze the time limit for E with an $$$O(n.H.M)$$$ solution?

Edit: I asked GPT to solve it and this is what it gave. Can someone verify if it is what the editorial meant in their OTHER SOLUTION?

  • »
    »
    10 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    may be bitset can help...it helps reducing t.c of o(n) to o(n/32)

    Refer to this blog : (https://mirror.codeforces.com/blog/entry/73558)

  • »
    »
    10 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    You was going right direction but it will not pass so what we can do is state reduction.

    Initially my dp state was depending on three state , now with the optimization shown in the picture I can convert it to two state ..

    How??

    each time my dp state is depending upon the previous values of h and m so either we can eliminate h or m

    1--> eliminating h will result dp[m] --> what is the maximum health I can restore till I use max possible money in this level . If I cannot make even 0 health left it means I was forced to use my health (assumption) even though it was impossible for me so I can't go further break and return i

    2--> similarly I can eliminate my magic or money .

    dp[h] what is the maximum magic that I can save in this level .

    I will use two dp , dp[h] --> for current magic and nxt[h] --> maximum magic that can saved for next state if the health at the very next state is h . Two transitions are possible I am taking the 2nd case

    a--> either I can use my health against monster if(arr[i]<=h) then I will go to

    nxt[h-arr[i]]=max(dp[h] -- means dont fight , nxt[h-arr[i]] --> fight and have h-arr[i] health for next state with m magic)

    b--> or I can use my magic

    if(dp[h]>=brr[i] )

    nxt[h]=max(dp[h]-brr[i] --> use magic in this state , nxt[h]--> dont use )

    Initialize nxt with -1 at starting of all health choices If after all the choices if my maximum element in nxt is <0 it means I cannot have any valid choice return the cnt or value of level (i) .

    Here is the code

    for (int i = 0; i < N; ++i) {
        fill(nxt.begin(), nxt.end(), NEG);
        for (int h = 0; h <= H; ++h) if (dp[h] != NEG) {
            if (h >= A[i])
                nxt[h - A[i]] = max(nxt[h - A[i]], dp[h]);
            if (dp[h] >= B[i])
                nxt[h] = max(nxt[h], dp[h] - B[i]);
        }
        if (*max_element(nxt.begin(), nxt.end()) == NEG) break;
        dp.swap(nxt);
        ++defeated;
    }
    
    
    

    This is for the first time I am explaining in such huge length of words . Sorry If I failed

»
10 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

Rapidly solve A-F then stuck at G be like:

»
10 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can anyone link their solution for E in Top Down Approach ? I was trying to solve it but failing in memoisation part.

»
10 months ago, hide # |
Rev. 4  
Vote: I like it +9 Vote: I do not like it

I thought in problem E, you can skip the monster, then, BOOM !!!!

The most correct thing I did is abandon E, then do F in last 25 minutes of contest and finally I get AC in F in the last few minutes. If the ranking is calculated in ICPC rules (which means each problem weight equally), then I will totally be finished even if I solve F at last.

»
10 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Does anyone have an idea how we could solve E if we were allowed to reorder the monsters before starting the game?

  • »
    »
    10 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Note that reordering will be the same as if we allow to skip monsters. The state this time will dp[pos][magic left] = { monsters killed, max health remaining}

    • »
      »
      »
      10 months ago, hide # ^ |
      Rev. 4  
      Vote: I like it 0 Vote: I do not like it

      If I understood correctly, $$$dp[pos][magic]$$$ is a pair, and since we are looking for the maximum, when taking the maximum between pairs, the first element is prioritized, so for example, $$$(1, 0) \gt (0, 10)$$$.

      Consider the following input:

      3 10 1

      10 2

      1 2

      1 2

      Here we would not be able to use magic at all, so we would actually be taking a greedy approach that does not work. Since $$$dp[1][1] = max((1, 0), (0, 10)) = (1, 0)$$$, and afterwards we will not be able to fight anymore, so $$$dp[3][1] = dp[2][1] = dp[1][1] = (1, 0)$$$. This is not optimal, since we could fight the second and third monsters with health, and get an answer of 2.

      If understood your dp state correctly, then I don't think it has optimal substructure.

    • »
      »
      »
      10 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      Having more health could be better than number of killed monsters, sorting monsters by ascending order of a[i] is needed

»
10 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Is problem E solvable assuming you could choose the monsters in any order?

»
10 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

This F is harder than before I think.

»
10 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Could someone tell me why the Bellman-Ford algorithm cannot be used for Problem D?

  • »
    »
    10 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    The correctness of algorithms like Bellman-Ford and Dijkstra relies on the principle of optimal substructure. For additive paths, this means that a shortest path from A to C that passes through B must contain the shortest path from A to B.

    This principle does not hold for XOR sums.

    • »
      »
      »
      10 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      well said

    • »
      »
      »
      10 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      bhaiya what do you mean by optimal substructure. can you please elaborate and can you please tell me why djikstra fails here as it also tries all the paths from source to end which will have the xor always min the previous or equal

  • »
    »
    10 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Why did u use it in the very first place? Maybe to detect Negative Cycle, but was that really needed? Meanwhile if you have used it to find the shortest valued XOR for all the intermediate path, that might not be enough to yield the minimum valued path from 1 to N. Moreover 2 greatest values path xor might yield to a minimum valued path.

    Example
»
10 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can someone explain why my solution using Dijkstra's for D is incorrect? My submission

  • »
    »
    10 months ago, hide # ^ |
     
    Vote: I like it +1 Vote: I do not like it

    bro dijikstra is wrong

    let say the there there is only one edge from n and that is n and n-1 with weight x+1 and the path to reach from 1 to n-1 can be x, x+1 as per dijikstra you will take x as the minimum path but that is wrong if you take x+1 as a path then it x+1^x+1 which will end up giving 0 as path cost

    • »
      »
      »
      10 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      bro can you explain me in more , like i had take a example of graph 4 4 1 2 3 1 3 8 3 4 8 2 4 8 still the same result i am getting from Djikstra , can you please explain why normal djikstra fails here as it also storing the min xor from source node to the end node , i have confusion can you please clear

    • »
      »
      »
      10 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      ok bro i got it 5 5 1 2 0 2 3 8 1 4 0 4 3 7 3 5 8 this was the test case where djikstra fails

»
10 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

any recursive dp solution for problem E (without binary search O(n*m))

»
10 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

What I did for question E reducing state

Initially my dp state was depending on three state , now with the optimization shown in the picture I can convert it to two state ..

How??

each time my dp state is depending upon the previous values of h and m so either we can eliminate h or m

1--> eliminating h will result dp[m] --> what is the maximum health I can restore till I use max possible money in this level . If I cannot make even 0 health left it means I was forced to use my health (assumption) even though it was impossible for me so I can't go further break and return i

2--> similarly I can eliminate my magic or money .

dp[h] what is the maximum magic that I can save in this level .

I will use two dp , dp[h] --> for current magic and nxt[h] --> maximum magic that can saved for next state if the health at the very next state is h . Two transitions are possible I am taking the 2nd case

a--> either I can use my health against monster if(arr[i]<=h) then I will go to

nxt[h-arr[i]]=max(dp[h] -- means dont fight , nxt[h-arr[i]] --> fight and have h-arr[i] health for next state with m magic)

b--> or I can use my magic

if(dp[h]>=brr[i] )

nxt[h]=max(dp[h]-brr[i] --> use magic in this state , nxt[h]--> dont use )

Initialize nxt with -1 at starting of all health choices If after all the choices if my maximum element in nxt is <0 it means I cannot have any valid choice return the cnt or value of level (i) .

Here is the code

for (int i = 0; i < N; ++i) {
    fill(nxt.begin(), nxt.end(), NEG);
    for (int h = 0; h <= H; ++h) if (dp[h] != NEG) {
        if (h >= A[i])
            nxt[h - A[i]] = max(nxt[h - A[i]], dp[h]);
        if (dp[h] >= B[i])
            nxt[h] = max(nxt[h], dp[h] - B[i]);
    }
    if (*max_element(nxt.begin(), nxt.end()) == NEG) break;
    dp.swap(nxt);
    ++defeated;
}

This is for the first time I am explaining in such huge length of words . Sorry If I failed

»
10 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

in D, why won't dp[v] = std::min(dp[v], dp[u] ^ w) work. We do it every time even if a node is visited since cycles can be present. Please explain.

Spoiler
  • »
    »
    10 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Because XOR doesn't work in that way.

    let us suppose you are getting 3 , 8 and you select 8

    on the next stage let again the wt be 8

    no if you would have choose 3 you'll end up having 11 where as if you would have choose 8 then you might end up having 0 as ans. which is actually correct.

    So taking min wont help .

»
10 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

can anyone help to figure out the mistake in ques 3


#include <bits/stdc++.h> using namespace std ; void sitaram(){ int n , q ; cin >> n >> q; vector<int>v(n+1); for(int i =0;i<=n;i++)v[i]=i; int r = 0 , l = 0 ; while(q--){ int t ; cin >> t; if(t==1){ int p , x ; cin >> p >> x ; if((n+p-l)%n==0)v[n]=x ; else v[(n+p-l)%n]=x ; } else if(t==2){ int p ; cin >> p ; if((n+p-l)%n==0)cout << v[n] << endl; else cout << v[(n+p-l)%n] << endl; } else{ int k ; cin >> k ; r+=k ; l = n - r%n ; } } } int main() { sitaram(); return 0; }
  • »
    »
    10 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Note that in operation 3, $$$k\le 10^9$$$. So $$$r$$$, which memorized the sum of $$$k$$$ s, had exceeded the range of int. You should use long long int.

»
10 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Are we possible to solve F in a complexity of less than $$$O(N \sqrt N)$$$?