Jamshed11's blog

By Jamshed11, 3 months ago, In English

2192A — String Rotation Game

Hint 1
Solution

Code: 363907571

2192B — Flipping Binary String

Hint 1
Hint 2
Solution

Code: 363909084

2192C — All-in-one Gun

Hint 1
Hint 2
Solution

Code: 363909342

2192D — Cost of Tree

Hint 1
Hint 2
Hint 3
Solution

Code: 363909632

2192E — Swap to Rearrange

Hint 1
Hint 2
Solution

Code: 363911188

2192F — Fish Fight

Hint 1
Hint 2
Solution

Code: 363912044

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

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

bro the time limit of problem F is too strict, I got TLE on #58 using the same method.

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

I solved problem B with the same idea as the solution, probably some minor mistakes. Can anyone help point it out? Thanks.

(Python btw)

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

    u should handle the -1 case in the else statement and the else one in the if , otherwise if there are odd no of ones and odd no of zeroes , it will return -1 , whereas it should have been the third case and zeroes are to be printed

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

    Ones — odd, Zeroes — odd : Do operations on all zeroes. Ones — odd, Zeroes — even : Not possible. Ones — even, Zeroes — odd : Do operations on all zeroes. Ones — even, Zeroes — even : Do operations on all ones.

    Your cases are actually wrong

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

      Does "Ones — even, Zeroes — odd: Do operations on all zeroes" case really needed? Doesn't matter if we do the operations on all zeroes or on all ones

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

        yeah we can do both ways. I was just pointing out to one way to do ops

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

    Try for n = 4 and s = 1000 and n = 6 and s = 111000. we can make all 1's to 0, ur code prints -1

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

      Hmm, I see... I swapped my first condition's position with the last condition. It works. Thanks

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

    3rd condition should be 1st and 1st should br 3rd. In the if else code

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

    if len(ones) % 2 == 1: print(-1) You don't need this case, because of this it is failing. Look at this example: 1110 The count of 1's is 3 but here we can make all 0's with the 0.

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

Really liked Problem D! Thanks for the contest :)

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

    I confused root-to-vertex and longest-vertex-to-leaf distance and I figured out what happened with like 4 minutes left :(

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

When someone read problem D it looks more difficult then E but in reality......

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

The problem D overloaded my brain.

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

Sadly E can easily be solved with Chat GPT 5.2

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

C was so easy. Idk why I was thinking that swapping will add the difference to all the elements upto i, hence I was not able to come up with optimal swap strategy for each index i. Pretty messed up contest for me.

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

    can you tell me what's wrong in my solution ???

    void solve()
    {
        ll n, h, k;
        cin >> n >> h >> k;
    
        vll a(n);
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
        }
    
        ll sum = accumulate(all(a), 0ll);
    
        ll multiple = h / sum;
        ll res = (k + n) * multiple;
        // cout << "multiple : " << multiple << endl;
        vll suffix_max(n + 1, 0);
        // suffix_max[n] = 0;
        for (int i = n - 1; i >= 0; i--)
        {
            /* code */
            suffix_max[i] = max(suffix_max[i + 1], a[i]);
        }
    
        ll cursum = multiple * sum;
        if (cursum >= h)
        {
            cout << res - k << endl;
            return;
        }
    
        ll range_min = 1e10;
        for (int i = 0; i < n; i++)
        {
            res += 1;
            range_min = min(range_min, a[i]);
            cursum += a[i];
            if (cursum + max(0ll, suffix_max[i + 1] - range_min) >= h)
            {
                cout << res << endl;
                return;
            }
        }
    }
    
    
    • »
      »
      »
      3 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      Your code is correct. I don't see any issue in it

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

        thx for reply bro i found the issue. In contest i submitted code with ll sum = accumulate(all(a), 0); instead of using 0ll i used 0 but thought this would not be problem and didn't resubmit

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

thx for round, very good problems with fresh ideas. Take some of this for own problems.

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

For E, one doesn't even need to find Euler’s cycle. We can try to pair up all the numbers and turn every problem into another easier problem which every number shows up twice in the combined array of a and b. This can be done by simply sequentially assigning a new number to each pair.

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

E was straight up euler circuit langauage anyone who has practice cses advanced graoh type wouldve easily solved today

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

Solved and coded D in 40 minutes, with 45 minutes till the end, but recursion limit of 200_000 apparently takes up too much memory... I was so close to having refactored it in time, too! Ugh

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

I wasted so much time since I misunderstood the statement for D. :( I thought "a tree with root $$$r$$$" in the formula refers to "the given tree when rerooted at $$$r$$$", instead of "the subtree at $$$r$$$ of the given tree with root $$$1$$$". I only realised this was not the case when noticing that in the sample outputs all leaves have cost zero.

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

    I understood the same. That's why I skipped D in the contest. Then, when I started to discuss the problem after contest with my friends, they told me I had misread it. T.T

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

This hurts

https://mirror.codeforces.com/contest/2192/submission/363904297

https://mirror.codeforces.com/contest/2192/submission/363906924

I found the mistake right away, but I, sadly, do not have inhuman speed to be able to do it in 2 seconds

»
3 months ago, hide # |
 
Vote: I like it -9 Vote: I do not like it

B was easier than normal div2s

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

I was able to solve till C. D is a different beast compared to the others. If possible, please add in voting for the problem (excellent, good, etc.).

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

c number can be made harder. For example, before reloading, you can swap at most one element.

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

isnt E wayyy more easier than D? But somehow D got x2 more AC than D? Anyone feel the same way?

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

E is too classical.

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

Someone plz hack my solution for E during the contest. just make n = 1e6, a = {1, 2, 3, ..., n — 1, n — 1}, b = {1, 2, 3, ..., n, n}, and I believe the solution will get WA or RE.363902719

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

for problem D are operations independent or dependent? like does tree get changed after the operation or not?

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

For 2192C - All-in-one Gun,

My solution: Array a will be sorted in non increasing order in the start, let's say, till the index i, the largest i elements of array a are present in non increasing order. After that the order breaks (at index i + 1).

Let the largest, farthest (from index i + 1) element present at index j. So, my claim is that, if we try to swap all indices (from i + 1 -> j — 1) with the index j (always one of the swapped indices is j), then, we can get the answer.

What's the issue with my approach, getting wrong answer?

Code: 363889728

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

GOOD (and god hah) contest. I like it. Spent a lot of time for D, and didnt read and solved E and F, while E was easy. My bad

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

why my code is getting wa for c problem pls can anyone tell ?

~~~~~~ //this is code

void solve()

{

int n, health, k;

cin >> n >> health >> k;

vector<int> a(n);

 multiset<pair<int,int>> ms;

int sum = 0;

for (int i = 0; i < n; i++)

{

    cin >> a[i];

    ms.insert({a[i],i});

    sum += a[i];

}

vector<int> pref(n+1,0);

for(int i=0;i<n;i++){

    pref[i+1]=pref[i]+a[i];

}

int ans=0,add=n;

// for(auto it : ms){

//     cout << it.ff << " " << it.ss << endl;

// }

// cout << endl;

// for(auto it : pref){

//     cout << it << " ";

// }

// cout << endl;

int step=health/sum;

if(health%sum==0){

    ans=step*n+(step-1)*k;

    cout << ans;

    return;

}

else{

    int rem=health%sum;

     ans=(step)*n + (step)*k;

    auto it = lower_bound(pref.begin(),pref.end(),rem);

    int dist=it-pref.begin();

    add=min(add,dist);

    for(int i=0;i<n-1;i++){

        ms.erase({a[i],i});

        // cout << a[i] << "  " << i << endl;

        auto v=*ms.rbegin();

        // cout << (v.ff) << " " << (v.ss) << endl;

        int diff=v.ff-a[i];

        if(diff<=0)continue;

        int l=0,r=n;

        int found=-1;

        while(l<=r){

            int mid=l+(r-l)/2;

            int find=pref[mid];

            // cout << find << endl;

            if(mid<=v.ss && mid>=(i+1)){

                find+=diff;

            }

            if(find>=rem){

                found=mid;
                r=mid-1;
            }
            else{
                l=mid+1;
            }
        }
        // cout << "found" << " " << found << endl;
        if(found!=-1){
            add=min(add,found);
        }
        // cout << add << endl;
    }
}
// dbg(add);
// dbg(ans);
cout << ans+add;

}

~~~~~

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

D is quite easy logic-wise, but I messed up the implementation too badly. I maintained vectors of some 4 things for each child to calculate stuff for the parent. If anyone has any suggestions on where could I have saved redundant computation in my code I'll love that, thanks!

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

atmostone forces !!

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

Got stuck on C

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

In ques. 2192D - Cost of Tree,

My solution: Consider a node nd (we want to calc. answer for nd). If u and v are in different child subtrees of nd, I claim that u must be immediate child of nd, and v must be the farthest node from nd.

If u and v are in same subtree (let's say in subtree of immediate child x), x would have calc. answer for itself, nd can use his answer for its answer calc.

But, getting wrong answer. Someone please help.

Code: 363920169

Accepted now, using the same approach.

Accepted code: 363923515

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

    I didn't read your code but this observation is not always correct. if node u has one child, then you can't do an operation on its child. My initial observation was similar to yours but i noticed the problem while implementing.

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

      I don't think this observation even holds when you can do operations on children of node u. i think i can come up with tests where doing the operations on grandchildren of node u is optimal. but too lazy to do so xD.

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

Is it only me, who find B quite hard compared to the normal div2B :(

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

I solved E kind of fast, but couldn't prove how to do the minimum number of operations. Unless, after the contest I saw that I DON'T need the minimum number of them!!! It is because in Russian there's only a small non-bold word "not" in the problem statement, that I missed. And in English there are two words "do not", that are harder to miss...

UPD: good problems btw

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

I got -1 rating change; made 5 wrong sub in C :(

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

There are another approach to solve E by using 2sat

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

Problem D bonus:

What if the operation doesn't refrain from choosing a node $$$u$$$ outside of the subtree of node $$$r$$$?

What node should we consider?

Answer

How can we do this?

Hint
Answer
»
3 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

In question D editorial solution line 20-21, can anyone explain what this line means “then it will always be optimal to choose parent of u as node u” ? it may be a typo or i am not understanding it.

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

    Lets say node 1 is parent of node 2, and currently you are thinking about choosing node 2 as u. "choose parent of u as node u" means instead of choosing node 2 as u, choose node 1 as u.

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

can someone help me check my C :v Bruh i really dont see any issue T_T wrong in test 2 :V in test case 3252

edit: i just relize my wrong :V fixed

// this is code
#include<bits/stdc++.h>
using namespace std; 
typedef long long ll;
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr); 
    int t; 
    cin >> t;
    while(t--) {
        int n,h,k; 
        cin >> n >> h >> k; 
        ll a[n+5];
        ll q[n+5];
        ll b[n+5]; 
        cin >> a[1];
        q[1] = a[1]; 
        for (int i = 2; i <= n; i++) {
            cin >> a[i]; 
            q[i] = q[i-1] + a[i];
        }
        b[n] = a[n];
        for (int i = n-1; i >= 1; i--) b[i] = max(b[i+1], a[i]); 
        ll t = h/q[n]; 
        ll r = h - t*q[n]; 
        int u = 0;
        if(b[1] >= r) u = 1;
        else {
            for (int i = 2; i <= n; i++) {
                if (q[i-1] + b[i] >= r) {
                    u = i; 
                    break; 
                }
            }
        }
        long long ans; 
        if (t == 0) ans = u; 
        else {
            ans = t*n + (t-1)*k; 
            if (r > 0) ans+=(k+u); 
        }
        cout << ans << '\n'; 
    }
    return 0; 
}
»
3 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

problem D is very beautiful, but I didn't have time to submit the code

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

is it just me or can anyone else not see the code submissions as well, it just shows N/A

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

Can someone please suggest where I might be going wrong with my code? Thanks!

363959696

  • »
    »
    3 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it
    if(cnt == 1) {
            for(ll v: adj[u]) { 
                if(v == pu) continue;
                dp[u] = max(c[u], c[u] + (dp[v] - c[v])); // !!!
            }
        }
        else if(cnt > 1) {
            for(ll v: adj[u]) {
                if(v == pu) continue;
                if(d[v] == maxDepth) {
                    dp[u] = max(dp[u], c[u] + (secMaxDepth - depth)*sum[v]);
                }
                else {
                    dp[u] = max(dp[u], c[u] + (maxDepth - depth)*sum[v]);
                }
                dp[u] = max(c[u] + (dp[v] - c[v]), dp[u]); // !!!
            }
        }
    
    
»
3 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

hello i have a different way of solving b that thought was interesting:

let the number of 1's & 0's in the original string be x and y, in each turn/move you can go from (x,y) to (y-1,x+1) and (y+1,x-1) if x,y>0, and you want then value of either x or y to become zero(by decreasing it 1 by 1)

so you can set it on the number of 1's but there is a catch since everytime your doing the move you are flipping the spots of x & y at the end the one that you choose to decrease by one each turn might end up on the number of 0's ,

how you can calculate this is if x mod 2 = 0 the end value of x can end up on the number of 1's or if y mod 2 = 1 then the end value of y can end up on the number of 1's if both are true you choose the minimum and if none are true the output is -1

i know i could've explained better but as a beginner i did the best i could if a better programmer sees this and would like to refine it i would love to answer any questions they have

anyways here is the code i wrote during the contest:

#include <bits/stdc++.h>
using namespace std;


int main() {
    int t;
    cin >> t;
    while(t--) {
        int n;
        string s;
        vector<int> ap;
        vector<int> bp;
        cin >> n;
        cin >> s;
        
        for (int i=0;i<n;i++) {
            if (s[i]=='1') {ap.push_back(i);}
            else {bp.push_back(i);}
        }
        int a=ap.size();
        int b=bp.size();
        if (b%2!=1 && a%2!=0) {
            cout << -1 ;
        }
        else if (b%2==1 && a%2!=0) {
            cout << b << endl;
            for (int i=0;i<b;i++) {
                cout << bp[i]+1 << ' ';
            }
        }
        else if (b%2!=1 && a%2==0) {
            cout << a << endl;
            for (int i=0;i<a;i++) {
                cout << ap[i]+1 << ' ';
            }
        }
        else if (b%2==1 && a%2==0) {
            int x=min(a,b);
            cout << x << endl;
            if (x==a) {
                for (int i=0;i<a;i++) {
                    cout << ap[i]+1 << ' ';
                }
            }
            else {
                for (int i=0;i<b;i++) {
                    cout << bp[i]+1 << ' ';
                }
            }
        }
        cout << endl;
    }
}


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

what a clean editorial

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

I like problem D much

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

Here are my notes and core understanding of Problem E: The inherent even-degree property of undirected Euler circuits is what guarantees the element balance between the two arrays. In the ideal case, the circuit forms a closed ring where every node has a degree of exactly 2, with edges running either a[i]→b[i] or the reverse. In practice, when building the graph, some nodes may have edges in the opposite direction from the ideal, resulting in a non-ideal state. But as long as we track these nodes, we can construct a valid ideal Euler circuit by simply choosing a consistent ideal direction for the edges. Elements can flow freely within the circuit, and thanks to the in-out conservation brought by the even-degree property, they will always end up in a balanced state. My implementation: 363982694

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

Good round!

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

in div2 C I couldn't understand the solution so made a prefix sum array while replacing minimum element till with maximum element available after that point. It is accepted but can it be simplified?

// this is code

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        ll n , h , k;
        cin>>n>>h>>k;

        vector<ll> a(n);
        vector<ll> pre(n);
        vector<ll> most(n);

        ll sum = 0;
        ll mx = 0;
        ll idx = 0;
        for(ll i = 0 ; i < n ; i++){
            cin>>a[i];
            sum+=a[i];
            if(a[i] > mx){
                mx = a[i];
                idx= i;
            }
        }
        ll temp = a[n-1];
        for(int i = n-1 ; i >= 0 ; i--){
            most[i] = temp;
            temp = max(temp , a[i]);
        }

        ll presum = 0;
        for(int i = 0 ; i < n ; i++){
            temp = min(temp , a[i]);
            presum += a[i];
            if(temp > most[i] || i == n-1){
                pre[i] = presum;
                continue;
            }
            pre[i] = presum - temp + most[i];
        }

        ll time = (h/sum)*n;
        if(h/sum > 0){
            time += ((h/sum))*k;
        }
        
        if(h%sum == 0){
            cout<<time-k<<'\n';
            continue;
        }

        h = h%sum;
        if(h <= mx){
            cout<<time+1<<'\n';
            continue;
        }

        for(int i = 0 ; i < n ; i++){
            ll o = h-pre[i];
            ++time;
            if(o <= 0){
                cout<<time<<'\n';
                break;
            }
        }
        
    }

    return 0;
}

Thank You

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

Man i wasted much time in B