wakanda-forever's blog

By wakanda-forever, 7 days ago, In English

Thanks for participating in the round. We hope you enjoyed the problems!!!

2278A - Fraction and Six

Idea
Implementation
Rate The Problem!

2278B - Stakes and Shapes

Idea
Implementation
Rate The Problem!

2278C - Math and Physics

Idea
Implementation
Rate The Problem!

2278D - Lock and Unlock

Idea
Implementation
Rate The Problem!

2278E - Swap and Negate

Idea
Implementation
Rate The Problem!

2278F - Modulo and Counting

Idea
Implementation
Rate The Problem!

2278G - 0s and 1s

Idea
Implementation
Rate The Problem!
  • Vote: I like it
  • +42
  • Vote: I do not like it

»
5 days ago, hide # |
← Rev. 3  
Vote: I like it +8 Vote: I do not like it

G can also be solved using hashing

»
5 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

F absolutely cooked me ion even deserve 1600

»
5 days ago, hide # |
 
Vote: I like it -8 Vote: I do not like it

bit on the easier side but W round!

»
5 days ago, hide # |
 
Vote: I like it -13 Vote: I do not like it

the person who wrote the statement of problem A deserves 1000+ downvote..

»
5 days ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

As noob, I hate C.

»
5 days ago, hide # |
← Rev. 3  
Vote: I like it +4 Vote: I do not like it

In every prob, 67 in the example saved me from depression

»
5 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Fasteditorialforces

»
5 days ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

pov: 6 7 in every possible sample

»
5 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Fuck E bro it's should've been replaced with H

»
5 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Why is the checker for B Case sensitive :'(

»
5 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Why B simple solution why

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

    B is solved simply because if we take x+y+z times opposite to the most frequent enemy peak, then the enemy will lose at least max(x, y, z) times. After that, he will win x or y or z times, and then there will be a draw x or y or z times. And the opponent is in no way able to achieve a positive score, because we will win max(x,y,z) times, and he is only x or y or z

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

      I know, it's just both funny and annoying how much I overthought it.

»
5 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

2278A - Fraction and Six I'm new to completive programming, and coding in general. It always fascinates me how my extremely long solution could be done so short.

»
5 days ago, hide # |
← Rev. 2  
Vote: I like it +6 Vote: I do not like it

What was wrong with this contest?! A-E was pure greedy and F jumped to a hell (it was really hard) dp

»
5 days ago, hide # |
 
Vote: I like it +18 Vote: I do not like it

Whoever kept Rock,Scissor,Paper in Problem B (instead of Rock,Paper,Scissor) .. is pure evil

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

    Thats true brother. had me the first time. i would have gotten it at first if i just used = signs in the conditions.

»
5 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Did anyone use maps in A?

»
5 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

A, B, C, D and even E being greedy feels so weird

»
5 days ago, hide # |
 
Vote: I like it +25 Vote: I do not like it

Problem D: Why is the editorial so complicated? This is just the classic "min operations to make all elements equal" problem in disguise.

Locking a group and moving the rest is the same as moving that group by +-1 relative to the others, so the cost is just the number of single-element +-1 moves. That's the sum of |a[i] — median|, so sort and take the median. The number of operations is just max — min, since each operation shrinks it by at most 1.

(The editorial's sum over gaps of (b[i+1] — b[i]) * min(i, n-i) is the same number, it's just counting the cost gap by gap.)

Code:

sort(all(a));
ll med = a[n/2], cost = 0;
for (auto x : a) cost += abs(x - med);
cout << cost << " " << a[n-1] - a[0] << "\n";
  • »
    »
    5 days ago, hide # ^ |
     
    Vote: I like it +3 Vote: I do not like it

    dang, why the downvotes — this is exactly how i thought about it

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

    why tf do u have downvotes!, thats how d should be done

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

    Affirmative. I also solved it this way. By the way you can find the median with C++'s nth_element() instead of sorting to achieve a time complexity of O(n).

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

      You're Wrong!You ignore the complexcity of the function!It's not better than O(n)!

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

        What do you mean when you said I ignored the complexity of the function? I am pretty sure that the time complexity of nth_element() is O(n).

        Also, I did not say it was better than O(n), but that nth_element() in O(n) was better than sort() in O(n log n).

        • »
          »
          »
          »
          »
          4 days ago, hide # ^ |
          ← Rev. 2  
          Vote: I like it 0 Vote: I do not like it

          I beg your pardon — I was wrong. I misunderstood the complexity of nth_element() and my earlier reasoning was off. You were right to call it out. Sorry about that and thank you for your patience.

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

    I had same idea and its straight forward to implement.

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

    I thought this way too.This is a beautiful method to solve the problem and your proof is simple and clear.Pretty good!

  • »
    »
    4 days ago, hide # ^ |
    ← Rev. 2  
    Vote: I like it 0 Vote: I do not like it

    i didnt get the intution of median. i solved greedily from prefix or suffix

        sort(v.begin() + 1, v.end());
    
        ll l = 1, r = n, add = 0, cost = 0, ops = 0;
        while(l < r){
            while(l < r and v[l] + add == v[l + 1] + add) l++;
            while(l < r and v[r] + add == v[r - 1] + add) r--;
    
            if(l <= n - r + 1){
                ll x = v[l] + add;
                ll y = v[l + 1] + add;
                ll dif = y - x;
                add -= dif;
                cost += dif * l;
                ops += dif;
                l++;
            }
            else{
                ll y = v[r] + add;
                ll x = v[r - 1] + add;
                ll dif = y - x;
                add += dif;
                cost += dif * (n - r + 1);
                ops += dif;
                r--;
            }
        }
    
  • »
    »
    3 days ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Yes, this idea is more easy to understand. I just do it like this.

»
5 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Anyone who has solved E, how do you come up with the solution given in editorial, or any solution to E for that matter ?! Thank you.

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

    We can argue that the sign that an element can take after any sequence of operations is tied to its initial sign and index parity. For example, an element with value -5 initially at an even index can only have -5 value at any other even index, and +5 at any odd index. The signs for an element are hence alternating (+-+-.. or -+-+...)

    With this we can divide elements into two groups — the elemnts whose sign-map starts with — and those whose sign map starts with +.

    elements in the same group compete for +ve positions, and we assign those positions to the largest elements in that group to maximize the score. There are a few more caveats to the implementation, but this is the basic idea.

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

      yes, and for me, it was the sample -10 1 0 1 -10 which gave away the parody index idea, I was like hmm the answer involves making both 10's positive and both 1's negative, so we can't make everything positive -> what is it about the 10's and 1's -> oh, the 10's are on even indexes, the 1's are on odd indexes

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

    The first observation was: if you move an element an odd number of times, the number is multiplied by -1. If you move it an even number of times, the number remains the same.

    This leads to an observation: a number is multiplied by -1 if and only if initially it was at an odd index and it has moved to an even index, or initially it was at an even index and it has moved to an odd index.

    Now, if we move x elements from odd to even, then we have to also move x elements from even to odd, because for every element you move to the other group, you have to bring one element back from that group.

    Now the problem becomes a bit easier. Create an array of all odd indices and an array of all even indices. Brute force on the number of elements you want to multiply by -1, and change the signs of the smallest elements in each group.

    396819444

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

      Your solution is very intuitive unlike the invariant solution that people are using like saying (-1)^i * ai is constant always.

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

      Why don't authors write solutions like this ?! I mean, they put in so much effort and have the patience to withstand long waits in the queue. And I'm sure they're capable of writing solutions like this. Right ?!

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

      very nice explanation , much appretiated

»
5 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I think you could've made G a bit harder by allowing to perform the operation on subtrees as well. Similar solution should also work for the harder version.

»
5 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it
#include <bits/stdc++.h>
using namespace std;

int main() 
{
   int t;
   cin >> t;
   while(t--){
    long long n, minVal = 1e9 + 1, maxVal = -1e9 -1, score = INT_MAX, cnt = INT_MAX; 
    cin>>n; 
    vector<long long> arr(n), diff(n, 0); 

    for (long long i=0; i<n; i++) {
      cin>>arr[i]; 
      minVal = min(minVal, arr[i]);
      maxVal = max(maxVal, arr[i]); 
    }
    sort(arr.begin(), arr.end());
    for (int i=0; i<n; i++) {
      cout<<arr[i]<<" "; 
    } cout<<endl; 

    if (minVal == maxVal) {
      cout<<0<<" "<<0<<endl; 
      continue; 
    }

    long long currS = 0, currCnt = 0; 

    for (long long i=0; i<n-1; i++) {
      //assume we are moving all elements to arr[0]
      diff[i] = arr[i+1] - arr[i]; 
      if (diff[i]) {
        currCnt += diff[i]; 
        currS += (diff[i] * (i+1)); 
      }
    } 

    score = currS; 
    cnt = currCnt; 

    for (int i=1; i<n; i++) {
      //assume we are moving all elements to arr[i]
      //remove the part where we decreasing elements left of arr[i] 
      currCnt -= (diff[i-1]); 
      currS -= (diff[i-1] * (i)); 

      if (diff[i-1]) {
        //now we are increasing elements left of arr[i] 
        currCnt += diff[i-1]; 
        currS += (diff[i-1] * (n-i)); 
      }

      if (currS < score) {
        score = currS; 
        cnt = currCnt; 
      } else if (currS == score) {
        cnt = min(cnt, currCnt); 
      }
    }

    cout<<score<<" "<<cnt<<endl; 
   
   }
    return 0;
}

Can someone please explain what is wrong here ? I am trying to calculate min cost by making all the elements of array equal to arr[i]

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

    Your idea was to calculate the distance of all numbers from the minimum to the maximum. You asked about an error in the code – take a look at where you’re calculating the distance and operations from all the numbers to the minimum; you’re multiplying by i + 1. Presumably, your logic was that you would cover the elements in this prefix, but the trick is that you can cover the suffix up to i + 1 and get a lower score; I wrote the exact same algorithm at first, but then came to the same realisation.

    code

    btw your idea with diff arr is right

»
5 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

F is really interesting. It's definitely a bit on the harder side compared to other div 3s imo.

  • »
    »
    5 days ago, hide # ^ |
     
    Vote: I like it +3 Vote: I do not like it

    It looks hard until you realise that the crux of the problem was: with this condition on a, the maximum sum you can get is m.

    Now figure out the necessary conditions for the sum to be m and write a DP on it.

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

      After taking the upper bound on An and then proving 1 can only be succeeded by 2 felt a bit non intuitive maybe because I have never seen a similar idea. After that the problem was trivial. Thank you.

»
5 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

greedyforces

»
5 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Crazy difficulty jump from E to F but I ended up on the good side of that difficult jump (solving first 5 within an hour) so I'll take it.

»
4 days ago, hide # |
 
Vote: I like it +6 Vote: I do not like it

In my opinion, F is harder than G

»
4 days ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

The pretests for A is so weak.

»
4 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I feel bad for all the people that are gonna lose their credit for problem A due to WA when final verification happens.

»
4 days ago, hide # |
← Rev. 2  
Vote: I like it 0 Vote: I do not like it

How can someone know to start with forming the array-b in problem E? Is there any clue?

»
4 days ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

As an overthinker, I hate greedy...

»
4 days ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

my 2nd contest and the problems were good ngl I could only solve the a b c but it is what it is

»
4 days ago, hide # |
← Rev. 2  
Vote: I like it 0 Vote: I do not like it

The problem C, I couldn't understand the condition they gave, is it only confusing for me or for you all

There are n nuclear cores arranged in a line, numbered from 1 to n from left to right. The i-th core has an initial mass of ai.

In one operation, you can choose a core i(1≤i≤k, where k is the current number of cores)

What I thought is that i is fixed from the start and k will be current size

like for example if they gave masses -> 1 1 1 5 1 1 1 1 the positions are 1(1) 1(2) 1(3) 5(4) 1(5) 1(6) 1(7) 1(8)

So what I thought is I can only split if (4 <= current size(8), obv every position is >= 1)

then after splitting, I thought the positions would not change but in editorial they just did it so naively, I donno if I couldn't understand and overthought or they couldn't frame question properly

1(1) 1(2) 2(3) 6(5) 1(6) 1(7) 1(8)

now I thought I can only split position 5 one since it's <= current size(7)

1(1) 1(2) 3(3) 7(6) 1(7) 1(8)

and goes on I thought I can only split if it's less than current size

and I wrote a condition that if there are more than 3 last ones then it's -1 impossible to convert into 1 single

»
4 days ago, hide # |
← Rev. 2  
Vote: I like it +1 Vote: I do not like it

The Problem E is good but the editorial is little overwhelming . Here is my idea for it.

If we look at the array a with 3 indices suppose [u,v,w] and try to perform some operations on it. we will see that it can be changed to [w,v,u] and so it concludes that elements order at even and odd indices can be changed to any permutation . If we sort the elements at odd indices and even indices separately and reassign the new array a according to indices, we can see now a more clear picture. This new order of elements will show us that the negative integers are more aligned on left side and positive elements on right side in new array. Till this point we have only changed the permutation but the sum is still the same as the original array.

Now On new array a we have a choice to either perform operation or not. for this we can use DP to do a choice . --> dp[i-2]-a[i]-a[i-1] // operation performed --> dp[i-1] + a[i] // no operation performed will assign max of above 2 in dp[i] .

I believe this dp part code can be done using greedy solution. but DP does not betray.


Code C++ : int main() { int t; cin>>t; while(t--){ int n; cin>>n; vector<ll>a(n+1),dp(n+1),x,y; for(int i=1;i<=n;i++){ int temp; cin>>temp; if(i%2==1){ x.push_back(temp); } else { y.push_back(temp); } } sort(x.begin(),x.end()); sort(y.begin(),y.end()); int l=0,r=0; for(int i=1;i<=n;i++){ if(i%2==1){ a[i]=x[l]; l++; } else { a[i]=y[r]; r++; } } for(int i=1;i<=n;i++){ if(i==1){ dp[i]=a[i]; } else { dp[i]=max({dp[i-2]-a[i]-a[i-1],dp[i-1]+a[i]}); } } cout<<dp[n]<<"\n"; } return 0; }
»
4 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Attention is all me need, but I don't have extremely attention. How To attend the important condition in problem E?

»
4 days ago, hide # |
 
Vote: I like it +4 Vote: I do not like it

Coming back to CP after a while, some similar problems like F would help ramp up faster. Can someone point me in the right direction?

»
4 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

A was accepted in the contest time but now it is showing Wrong Answer .

What the f.....

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

    I experienced the same thing, maybe they have rerun those tests for problem you've solved and now it's WA. Or maybe you just imagined it?

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

      At contest time, there were bad test cases, after contest there is hacking phase right, if people think the test cases are not good enough they'll add better test cases, so if they can find hack(a testcase) which suppose doesn't work with your code they'll add that test case and re run everyone's code kind of

»
4 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Bro I was writing code for problem 'A' for 1 HOUR :sob:, which means — more practice!

»
4 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Difficulty jump from E to F was insane

»
4 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I felt that G was easier than F

»
4 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

The idea I had for E was a bit different (and I think similar to what others have done), but the editorial solution is quite pretty, I have to admit

»
4 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I really liked the 2278E - Swap and Negate. The transformation idea $$$b_i=(−1)^{i−1}a_i$$$ is simply generous! I wish to practice more of this kind. Could anyone find me some similar problems?

»
4 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

first contest let me fumble got cooked by A

»
4 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

D can be done with Ternary Search. It's like killing an ant with a bomb, but who would say killing an ant with a bomb isn't fun?

»
3 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

You can think of problem F as having a score and potential energy. Define the potential to be the last element in the added in our array.

Ex: [10], potential = 10, score = 0 The key idea is that adding the next element, adds the remainder to the score, and the new potential becomes the last element. For example, lets add 7 to the end of our array.

[10, 7], potential = 7, score = 3

Lets add a 4:

[10, 7, 4], potential = 4, score = 6

The observation is that as long as we keep adding elements greater than half the last element. The sum of our score & potential do not change. Which means that if we can keep adding elements greater than last / 2, the potential will be conserved, so we end with a score of the first element.

Two details: 1) Since we cannot add an element less than 1, this means our score will lose 1 unit of potential. 2) It is not possible to add a 1 unless you break the rule of the element being > last / 2. Which means you will lose another 1 unit of potential by either adding a 1, or keeping the last element to be a 2.

This means that if we start at a number x, our score can be atmost x — 2. This implies that the only valid starting is m + 2. And that we must follow the above rules to guarantee that we only lose the 2 points of potential

»
46 hours ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

problem E...how am i even supposed to start thinking in the direction of making a new array

I was stuck just trying to find if i can somehow do something with even and odd positions the whole time.

Please give any advice.