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

Автор Vladosiya, история, 23 месяца назад, перевод, По-русски

1974A - Рабочий стол телефона

Идея: Aksenov239

Разбор
Решение

1974B - Симметричное кодирование

Идея: MikeMirzayanov

Разбор
Решение

1974C - Красивые пары троек

Идея: MikeMirzayanov

Разбор
Решение

1974D - Ingenuity-2

Идея: MaxBuzz

Разбор
Решение

1974E - Деньги покупают счастье

Идея: RobinFromTheHood

Разбор
Решение

1974F - Игра с отрезанием

Идея: MikeMirzayanov

Разбор
Решение

1974G - Деньги теперь покупают меньше счастья

Идея: RobinFromTheHood, izban, Aksenov239

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

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

Thanks for the contest ! Good Problems !

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

Thank you for the excellent coordination and organization! I joked about physicists because I am one :)
Please encourage all physicists to code!

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

Thanks for the good contest Graet Problems

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

Problem C was quite tough than average!! Acceptance rate is less than 50%. Anyways another good round by Vladosiya

»
23 месяца назад, скрыть # |
Rev. 3  
Проголосовать: нравится +18 Проголосовать: не нравится
Simplest Implementation for Problem D
»
23 месяца назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

can someone explain Q3 solution?

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

    For a more functional explanation: to avoid redundant counting, we'll iterate once through every triplets, count every triplet to the left of it that would make a beautiful pair.

    In notation, denoting $$$cnt(x)$$$ is the current counter for triplet $$$x$$$, then for each triplet $$$(a_i, a_{i+1}, a_{i+2})$$$ being iterated:

    • Add $$$cnt((0, a_{i+1}, a_{i+2})) + cnt((a_i, 0, a_{i+2})) + cnt((a_i, a_{i+1}, 0))$$$ to the final answer. This is the process of pairing the current triplet with the ones before it that have at most one error.
    • Still, we need the two triplets to have precisely one error actually. So the completely identical pair(s) must be excluded. Therefore, subtract $$$3 \times cnt((a_i, a_{i+1}, a_{i+2}))$$$ from the final answer. The constant factor $$$3$$$ was because if there were any such element, they would have been counted $$$3$$$ times at the previous steps.
    • Add $$$1$$$ to $$$cnt((a_i, a_{i+1}, a_{i+2}))$$$, $$$cnt((0, a_{i+1}, a_{i+2}))$$$, $$$cnt((a_i, 0, a_{i+2}))$$$ and $$$cnt((a_i, a_{i+1}, 0))$$$. This is to include the current triplet to be counted at further iterations.

    Take the 8th test in the sample:

    5
    2 1 1 1 1
    

    We'd have three triplets, $$$(2, 1, 1)$$$, $$$(1, 1, 1)$$$ and $$$(1, 1, 1)$$$, and initially $$$ans = 0$$$.

    • Iterate through the first. It's obvious that answer remains at $$$0$$$ because there's nothing to the left of it to pair with. Still, after this, $$$cnt((2,1,1))=cnt((0,1,1))=cnt((2,0,1))=cnt((2,1,0))=1$$$.
    • Iterate through the second. We can add $$$cnt((0,1,1))+cnt((1,0,1))+cnt((1,1,0))=1+0+0=1$$$ to the final answer, thus now $$$ans = 1$$$. Also, now $$$cnt((2,1,1))=1$$$, $$$cnt((1,1,1))=1$$$, $$$cnt((0,1,1))=2$$$, $$$cnt((2,0,1))=cnt((2,1,0))=cnt((1,0,1))=cnt((1,1,0))=1$$$.
    • Iterate through the third. We add $$$cnt((0,1,1))+cnt((1,0,1))+cnt((1,1,0))=2+1+1=4$$$, yet we have to subtract $$$3 \times cnt((1,1,1)) = 3 \times 1 = 3$$$, therefore final answer is $$$2$$$.
»
23 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For Task 5, my recursive code works fine, but on memoizing, it gives a different answer on the last sample case, Why is this happening, and how to rectify it?

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

    There are 3 changing states in your code i,e curr_month , happiness , salary but the dp you have created is consisting of only 2 variables.

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

    I was struggling with the same issue for over a couple of hours, I recommend 261999415, there's a minor constraint that u need to consider.

    If for a particular happiness value, If we have more amount with me in some other recursive call, i need not implement my current recursive call with a lower amount left with me, because that shall cause an issue in the future.

    Because we have 3 parameters to consider, we have to consider also the amount that we spend for a particular index with particular happiness value, for which i have used the sp matrix.

    Hope that helps !

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

I wonder how submissions increases rapidly in the last half hour

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

In my opinion, questions were really good, but quite implementation based (at least in C++).

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

Here's an alternate solution for problem C using only sorting: solution (C++)

The solution is not so fast since I have used sorting 3 times, but it is nonetheless O(n logn)

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

    Sorting 3 times is fast (proof: 261990288), passing vector of vectors to calc is slow

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

    we can also apply principle of exclusion and inclusion by help of nested maps but that can be space inefficient solution but here it worked.

        #define int long long
        int n;
        cin >> n;
        vector<int> nums(n, 0);
        for (int i = 0; i < n; i++) cin >> nums[i];
        int ans = 0;
        map<int, map<int, int>> mp1,mp2,mp3;
        map<int, map<int, map<int, int>>> mp;
        for (int i = 0; i < n - 2; i++)
        {
            ans +=   mp1[nums[i]][nums[i + 1]] 
                    + mp2[nums[i]][nums[i + 2]] 
                    + mp3[nums[i + 1]][nums[i + 2]] 
                    - 3*mp[nums[i]][nums[i + 1]][nums[i + 2]];
            mp1[nums[i]][nums[i + 1]]++;
            mp2[nums[i]][nums[i + 2]]++;
            mp3[nums[i + 1]][nums[i + 2]]++;
            mp[nums[i]][nums[i + 1]][nums[i + 2]]++;
        }
        cout<<ans<<endl;
    
    • »
      »
      »
      23 месяца назад, скрыть # ^ |
      Rev. 2  
      Проголосовать: нравится +4 Проголосовать: не нравится

      tuple can be used instead of in nested map mp, storing the exact same triple: map<tuple<int, int, int>, int> similar;

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

        Thank you so much! Tuple make my code much simpler. I don't know why my first implement of my idea using map<string, int> wrong answer on test 7. Then I implement the same idea with map<tuple<int, int, int>, int> and got accept. I'm really confused. Here is two version of my code 262693385 and 262688887, Could someone please point out my mistake in version of the code that uses map<string, int>?

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

In question C , it is given 4 seconds , as per what i know one second has 10^8 iterations , so 4 sec should have 10^32 iterations and max n value is 2*10^5 , so n^2 soln at worst has 4*10^10 iterations and max testcases are 10^4 so an overall T.C of 4*10^14 which is approx 2 sec , but why n^2 solns got failed , im just curious , i did using map ,but got tle in bruteforce , please correct me if i am wrong .

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

Problem G has to be Antimatter Dimensions-inspired. Which one of you writers plays the game?

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

my solution to d which was the most troll solution I've ever written. I just brute-forced while trying to have device 2 follow the path of device 1. I was very unsure of it passing but it somehow got AC. Can someone please help in formal proof (not mathematical) of why this works? submission: 261894801

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

Can someone explain to me, why my code for Problem C is giving WA on test case 8, it got accepted during the contest, but failed in System testing.

261837444

  • »
    »
    23 месяца назад, скрыть # ^ |
    Rev. 3  
    Проголосовать: нравится 0 Проголосовать: не нравится
    ans-=j.second*j.second;
    

    Those lines in your code seem to be very questionable overflows (as the map key/values are all pair<int, int>, tested with C++17 32bit to make sure int is 32bit). Resubmitting with pair<ll, ll> passed that test.

    I don't really know how it escaped the original testset.

    Quick fix without changing data type:

    ans-=1LL*j.second*j.second;
    
»
23 месяца назад, скрыть # |
 
Проголосовать: нравится +12 Проголосовать: не нравится

Anyone else used Segment Trees on problem G?

It's a bit of an overkill, but it was my first idea and it worked.

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

problem E is exactly same as Knapsack -2 from Atcoder dp contest. Just add the extra condition: on any given month , if minimum cost required to come up with happiness h should be less than the money earned till the previous month.

Check out my submission here : 261998965.

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

One of the concise and clear implementation of problem D-

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

5th question me DP itna aasaan tha ki me agle Janam me bhi naa kar paun(laughing emoji-2)

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

Easiest G ever:)

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

In question F, my approach was to keep track of every y coordinate containing a chip for each row(same for column) in vectors, and maintain two pointers for rows and columns. And if there is a row being erased(similarly for column), I applied binary search to get the index of smallest and largest indices within the range bound by the two pointers for columns, to add to the scores.

But the submission is giving memory-limit-exceeded. 262010194

Can anyone identify the reason, I think the total space I am using is in order the O(n).

  • »
    »
    23 месяца назад, скрыть # ^ |
    Rev. 3  
    Проголосовать: нравится +3 Проголосовать: не нравится
    row[x1].size()>0
    

    Be extra careful at such lines. If x1 isn't yet in the map, the map will allocate a new "block" of memory for that key, and thus as time goes on, it'll keep populating until MLE.

    That doesn't yet consider the fact that for(int j=0; j<sh; j++) will TLE eventually (as the sum of sh in each test can reach $$$2 \cdot 10^9 - 2$$$ at maximum), but usually MLE will come first if possible anyway.

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

Hey Vladosiya

It appears there's an issue in the tutorial where the directions for how N and S affect the variable y are incorrectly stated. The current explanation mentions that N decreases y by 1 and S increases y by 1. However, N should increase y by 1 and S should decrease y by 1.

Thanks.

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

    Actually it does not really matter, as long as everything is consistent in the code. Swapping the directions just mirrors the picture, but does not change whether a certain answer is correct or not.

    But surely the NS-polarity does not agree with the attached solution (while the tutorial is supposedly an adaptation of my original analysis, the solution is not mine), so, Vladosiya, it is definitely better to make these the same.

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

You can observe that it is similar to classic knapsack but the constraints are different. here the maximum salary you can get is (m-1)*x which is nearly 1e8 range..so you can't store it as state. So now you have to see the problem in another way.. i.e. you know what is the maximum happiness you can get and i.e equal to sum of all happiness now you have to start from this maximum value of happiness till zero see for which happiness you got cost <=((m-1)*x)..and return it as answer so here your states will be f(index,happiness)

Caution : 

Don't use memoization.. you will get TLE.. try to use tabulation method For reference you can check my submission 262090006 For reference video you can check out this (but in hindi language) — video

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

I got MLE in problem E. I'm using memoization to calculate the max happiness. Can anyone help me in optimizing the Memory in my code?

262108607

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

good contest!

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

I am confused with Task-C and Kotlin.

Don't understand why this is TL https://mirror.codeforces.com/contest/1974/submission/262119536

and this is OK. https://mirror.codeforces.com/contest/1974/submission/262120101

The only difference is in keys of map. In the first case it is data class(x,y,z), in the seconde it is String "$x_$y_$z". For some reason solution with data class works very slow. Any guru of kotlin for help?

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

For problem E, Why does this code 262139086 TLE on test 14 ? It uses the same idea as in the editorial and has the same time complexity.

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

For those stuck in G, here's the same idea 105123E - Powerhouse of the Cell?

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

someone please suggest the sources you practise problem e. and similar ones for practising dp.im a newbie here want to learn dsa first.

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

hey MikeMirzayanov what is this partial behaviour towards flagging solutions.

This user -> looneyd_noob is out of competition for the solution: https://mirror.codeforces.com/contest/1974/submission/261894175 but i also noticed the solution of this user -> Shaxx19 who is not out of the solution even having a very similar solution just variable name changed and some comments added and even submitted 10 minutes later; this is his solution https://mirror.codeforces.com/contest/1974/submission/261900757 the entire nested for loop is same with same indentation. Man he needs to get out of contest, you are doing very wrong and people's trust will reduce from this platform. Please get him out of competition.

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

    I noticed that someone has raised concerns regarding the similarity between my solution and another user's solution, and is requesting my disqualification from the competition.

    I would like to clarify that my solution is indeed my own work. Any similarities to other submissions are purely coincidental, as the nature of coding often leads to similar logic and structures when solving the same problem. I have added proper comments to demonstrate my understanding of the code, which the other user hadn't.

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

F was a simple binary search problem if you are aware of 2D coordinates to 1D conversion trick.Submission

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

Task F huge plus plus Mann

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

Could anyone explain me how to implement Problem G using segment trees? I did'nt understand that part in the editorial

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

why could my knapsack solution be failing on E? My solution

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

Can someone write a solution in C++ for Qo C There is no suitable hash function in unordered map for a tuple in C++ !

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

    You can just use vector instead tuple and it will work without coding a hash function. In fact you can just use a pair for this problem so it exists a hash function in the STL :)

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

solution of c blowed my mind ,,never though of using map this way

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

isn't the time complexity of solution of f given is O(m*n) ?

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

can anyone help me in writing the recursive dp code for the problem E? its similar to knapsack 2 of atcoder but the thing that is troubling me is in knapsack problem we have a constant capacity w but here w is the salary that is getting changed continously.So, unable to handle it. any kind of help is welcomed.

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

In the fifth question, I have implemented a somewhat different dynamic programming technique than suggested using an unordered_map and set. I have iterated through months 1 to n and during each iteration, I have updated the dp table using the following idea: Define following variables:
money: Total amount earned till that month (m-1)*x
k: happiness obtained at a cost of less than or equals to money
spent: the amount spent in obtaining a hapiness of k
Now, if cost[i]<money,
define k1: (happiness obtained in ith month) + (maximum happiness obtained at a cost of less than or equals to money- cost[i])
if(k1>k), we update spent to cost[i] + money spent in obtaining (maximum happiness obtained at a cost of less than or equals to money- cost[i]), and update dp[spent]=max(k,k1).
Now to locate the indices in the dp table, we can store the spent values in a set and use the lower_bound() function. Since lower bound function returns the minimum value in the set greater than equals to, and we need a spent index less than equals to some integer, the spent values can be stored in the set after multiplying with -1.

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int mod=1000000007;

void solve(){
    int m,x;
    cin>>m>>x;
    vector<int> cost(m,0),hap(m,0);
    for(int i=0;i<m;i++){
        cin>>cost[i]>>hap[i];
    }
    unordered_map<int,int> mp;
    mp[0]=0;
    set<int> s;
    s.insert(0);
    for(int i=0;i<m;i++){
        int money=x*i;
        int k=mp[-1*(*s.lower_bound(-1*money))];
        int spent=-1*(*s.lower_bound(-1*money));
        if(cost[i]<=money){
            int k1=hap[i]+mp[-1*(*s.lower_bound(-1*(money-cost[i])))];
            if(k1>k){
                spent=-1*(*s.lower_bound(-1*(money-cost[i])))+cost[i];
            }
            k=max(k,k1);
        }
        mp[spent]=k;
        s.insert(-1*spent);
    }
    cout<<mp[-1*(*s.lower_bound(-1*(x*(m-1))))]<<endl;
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    int t=1;
    cin>>t;
    while(t>0){
        solve();
        t--;
    }
}

265689316

This is failing in some 104th case of test-2. What can be the mistake in my approach ?

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

OG problems!!

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

PLEASE Help me in submitting my solution in C problem[problem:1974C]. I am following the same approach as many have done in their solutions, which might be the only way to solve the problem. I am just creating a map to store all the triplets in form of a string, just with a slight change that where I get bi not equal to ci I am putting '0' instead of it. Here's the link to my submission: submission Please have a look Vladosiya.