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

Автор Jamshed11, 3 месяца назад, По-английски

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

Разбор задач Codeforces Round 1081 (Div. 2)
  • Проголосовать: нравится
  • +100
  • Проголосовать: не нравится

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

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

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

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 месяца назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

Really liked Problem D! Thanks for the contest :)

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

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

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

The problem D overloaded my brain.

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

Sadly E can easily be solved with Chat GPT 5.2

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

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 месяца назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    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 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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

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

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 месяца назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

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

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

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 месяца назад, скрыть # |
 
Проголосовать: нравится +14 Проголосовать: не нравится

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 месяца назад, скрыть # |
 
Проголосовать: нравится +2 Проголосовать: не нравится

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 месяца назад, скрыть # |
 
Проголосовать: нравится -9 Проголосовать: не нравится

B was easier than normal div2s

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

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 месяца назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

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

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

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

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

E is too classical.

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

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 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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

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

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 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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 месяца назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

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 месяца назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

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 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

atmostone forces !!

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

Got stuck on C

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

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 месяца назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    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 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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

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

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 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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

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

There are another approach to solve E by using 2sat

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

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 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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 месяца назад, скрыть # |
Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

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 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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

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

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

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

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

363959696

  • »
    »
    3 месяца назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится
    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 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

what a clean editorial

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

I like problem D much

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

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 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Good round!

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

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 недели назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Man i wasted much time in B