cry's blog

By cry, 16 months ago, In English
Rating Predictions

2060A - Fibonacciness

Problem Credits: Proof_by_QED
Analysis: larush

Solution 1
Solution 2
Code (C++)
Rate The Problem!

2060B - Farmer John's Card Game

Problem Credits: Lilypad
Analysis: larush

Solution
Code (C++)
Rate The Problem!

2060C - Game of Mathletes

Problem Credits: Friedrich
Analysis: macaquedev

Solution
Code (C++)
Rate The Problem!

2060D - Subtract Min Sort

Problem Credits: Proof_by_QED
Analysis: Proof_by_QED

Solution
Code (C++)
Rate The Problem!

2060E - Graph Composition

Problem Credits: Friedrich
Analysis: DivinePunishment

Solution
Code (C++)
Rate The Problem!

2060F - Multiplicative Arrays

Problem Credits: Proof_by_QED, cry
Analysis: -firefly-

Hint 1
Hint 2
Hint 3
Solution 1
Solution 2
Code (C#)
Rate The Problem!

2060G - Bugged Sort

Problem Credits: chromate00
Analysis: macaquedev

Hint 1
Hint 2
Hint 3
Hint 4
Solution
Code (C++)
Rate The Problem!
  • Vote: I like it
  • +126
  • Vote: I do not like it

| Write comment?
»
15 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

C took me incredibly long. I was completely overcomplicating it thinking that we needed to subtract from the answer when there was an odd number of non-pair numbers.

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

    hahaha, I did the same. I didn't realize the fact that non pair numbers can never be odd up until the contest ended, so silly.

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

    hahahaha me too bro

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

    Same, I didn't read that n is even only :(

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

      How will that drastically change anything? Like all i can think of is by making some modifications like storing remaining useless elemenst' count and then checking its parity would work, no?

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

        It has 2 additional cases:

        If # unpaired is odd and n is odd, Alice gets the last unpaired, so we are good. If # unpaired is odd and n is even, Bob will be forced to disturb 1 pair.

        Certainly not drastic but even is just easier, that's all. I was complicating the problem too much in my mind.

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

    Everyone in my friend group seems to have problem with this.

    But I don't understand how I did it under 10 mins of reading the problem.

    On the other hand, I never have problems implementing solutions. (I did This on my own)

    But I got destroyed trying to implement B.

    And I did not understand D to begin with

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

It was a nice contest! I got stuck on problem E, but I'll learn from it.

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

:)

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

    At first reading your comment, I thought you actually had to take a dump during the contest. I almost felt sorry for you.

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

Why is F so damn hard???

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

Is it me or the B problem was kind of hard to implement ? It took me a while to solve it. Even the total number of users who solved the problem is less than the problem C.

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

As an AKer in 2h29min, I think this is closer to DIV2 ;)

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

Problem G is very good.

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

I solved A, C and D but can't solve B. I don't know what is the logical mistake in my solution.

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

    my approach which might help:

    store in the form of int[n][m] sort all vectors find order by taking first el of all check if order is valid, else -1

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

    I understood the problem statement wrong, and maybe you did the same. I thought that the card stack resets to -1 every time a new round starts. This gives a different problem, with a different solution, but you can't notice it in the sample I/Os. This variation gets WA on test 2.

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

Did anyone use Faulhaber formula to solve F?

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

good contest , bad contest

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

The testcases for E couldve been better. I just thought we have to make both the graphs same but i guess thats what makes me a newbie

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

thank for contest

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

man i dont know why my c is wrong .


#include <bits/stdc++.h> using namespace std; int main(){ int t; cin >> t; while(t--){ int score = 0; int n,k; cin >> n >> k; vector<int> v(n); for(int i = 0; i < n; i++){ cin >> v[i]; } sort(v.begin(),v.end()); for(int i = 0; i < n/2; i++){ if(*min_element(v.begin(),v.end()) + *upper_bound(v.begin(),v.end(),k -1 -*min_element(v.begin(),v.end())) == k){ score++; v.erase(upper_bound(v.begin(),v.end(),k -1 - *min_element(v.begin(),v.end()))); v.erase(min_element(v.begin(),v.end())); }else{ v.erase(min_element(v.begin(),v.end())); v.erase(min_element(v.begin(),v.end())); } } cout << score << endl; } }
»
15 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

why 301896773 gives TLE when submitted in C++20 but accepted when submitted in C++17

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

I'm quite new to graph theory problems, so please pardon any ignorance. However, for the life of me, I cannot understand why in problem E you cannot just count the number of pairs of (u, v) that are in G and not F, and the pairs in F but not G?

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

Can someone explain how for E on the second test of test 2 the # of operations needed is only 2? I'm getting 33.

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

F was awesome

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

Any good resources to learn combinatorics?

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

Self review no one asked for:

so mad that i sold around 50 penalty points because of the misread on a...

i lost a little time on b because of interesting construction

rushed on c, my mind was too disorganized

c-d turnaround was lowkey clean, i hit an instant mindsolve

e i sent two wa because i thought we needed to simply match the edge list, rather than the path. test case 1 seemed to support this. i sent another wa submission because i thought it was overflow

f and g were beyond me

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

    My man, at least you realize it in time.

    When I was realized I read the problem E wrong, the contest is already over.

    It only took me ~ 30 min of implementation so if I could realize I read the problem wrong, I can comeback easily (theoretically)...

    Ready going back to pupils and wait for Div 4.

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

    can I know the extension that you are using to predict the rating changes

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

Why was d at d?

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

    Because we hoped that some people will prove the result instead of resorting to guessing that the obvious strategy works, and the proof is actually not so simple.

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

      Hey can you tell me whethe my proof was correct?

      I started from the first index of array and started taking the mid of first two elements. Than I subtracted it from both. Than we will reduce one element to 0 and the next to whatever was left after subtracting. By the way this was done only when first element was smaller than second as otherwise if this operation is performed there is no way that array can be non decreasing as zero is the minimum element obtainaible and everytime we pair an element with 0 the min of them both will be zero. So if we get something like 1 0 than it's likely impossible but if we have smooth sailing till last index than you are good to go.

      Does it seem satisfactory or you have a better proof? I'd love to hear your take.

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

        This doesn't tell me why it is optimal to decrease on the first element first. Perhaps it is better to do them in a different order. My proof is in editorial

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

There is an easier solution for G without dp and more straightforward to prove(?). Imagine $$$a$$$ and $$$b$$$ stacked on top of each other, draw an arrow pointing downwards from $$$a_i$$$ to $$$b_i$$$ if $$$a_i \gt b_i$$$ otherwise draw an upwards arrow from $$$b_i$$$ to $$$a_i$$$. Now doing an operation on $$$(i,j)$$$ is equivalent to swapping the pairs $$$(a_i,b_i),(a_j,b_j)$$$ then flipping their arrows.

Following from the editorial, the order of the pairs in the final sorted configuration is fixed and we can freely reorder the pairs. The question left is whether we can orient the arrows correctly?

Assume we already fixed the final direction of each arrow in the final state, the number of up arrows in the final and initial state has to have the same parity (parity of up arrows is invariant under swapping). So we now ask what possible parities of up arrows can the final state have?

First sort the pairs by $$$\min(a_i,b_i)$$$. For each $$$i$$$, if $$$\max(a_i,b_i) \gt min(a_{i+1},b_{i+1})$$$ then their arrows must share the same direction, otherwise they can be different or the same. Now we split the pairs into consecutive segments where each all arrows in the same segment must have the same direction. The parity of number of up arrows can be $$$0$$$ or $$$1$$$ iff there exist a segment of odd length because changing the direction of all arrows in an even segment does not change the parity of up arrows. The answer to yes or no reduces to these two conditions:

1) No $$$\max(a_i,b_i) \gt \max(a_{i+1},b_{i+1})$$$ exist

2)There exist an odd segment or the parity of up arrows in the initial state is even

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

    My solution is similar to yours, but I think you're missing one case.

    Even if all the fixed segments are even length, if there exists two consecutive i's where max(ai,bi)>min(ai+1,bi+1) is false, then you can flip the first of those two i's without changing anything else. This means you can always make the total number of flips even.

    (Also you still have to check that after ordering the pairs, one of the flips (or arrow directions) is valid — such as in the first example test case where the middle pair can never be valid.)

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

In the tutorial of B, it says in general,

the i-th cow (where i∈[0,n−1]) must be able to place i,n+i,2n+i,….

but what if the input is

1

3 3

3 4 5

0 1 2

6 7 8

here there is still a possible permutation but not according to the tutorial ??

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

    No player can play consecutive turns, so suppose player 2 plays 0, player 1 plays 3, and player 3 plays 6, then player 2 can't play any of his cards since they are smaller than 6. (If you think that player 2 can play all his cards, then player 1, then player 3, then you might have misread the question) A player will only get his turn after all other players have played, everyone maintains the same turn order throughout the game

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

In problem F solution 1, how are we calculating \begin{align*} \binom{n+1}{j+1} \end{align*}

since n is very large?

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

my downfall was A. I don't know why I was overthinking it so much when it was so obvious. Took me nearly one hour to solve. I really have a lot to learn...

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

For D I used a completely different approach, where the logic (at least if you have to prove it!) is simpler: starting from the end, find the allowed interval for the previous element. Then check that the first element fits in the required interval.

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

Damn... I've used DSU implementation from Algorithmica. And it was wrong all from the start. Spent 30 minutes to find a bug in my solution, and the only bug was my trust in this site.

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

    Algorithmica DSU implementation is fine. The wrong answer for your first several submissions was caused by the if (p[u] != p[*it]): to check if two vertices are in the same component, you should compare their leaders, not their parents.

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

      Yes, you are right here. But I couldn't pass tests anyway because unite function doesn't check that leader(a) == leader(b). So it caused overflowing. I know that you should understand an implementation, that you find in the Internet, in details. And I didn't use DSU for quite a long time. I was just convinced that this is the basic check for the unite function.

      So, yeah, my statement about Algorithmica was not completely honest. Sorry about that.

      Anyway, glad that I got the idea of the problem, and thanks for your reply.

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

I solved G without using dynamic programming and I think it's a bit more intuitive (at least for me).

Using the same insights as mentioned in the hints, my solution builds out a possible answer and checks if it has enough degrees of freedom to end up with an even number of flips, or necessarily has an even number of flips.

Say you've placed the first n pairs (as the editorial mentions, this order is fixed by $$$min(a_i, b_i)$$$), then you take the next smallest pair and decide how it should be flipped. There are 4 cases:

  • it needs to be flipped (meaning the element from $$$a$$$ should end up in $$$b$$$)
    • for example, the previous is $$${1, 3}$$$ and the next to place is $$${4, 2}$$$, the 2 cannot come after the 3, so it has to be flipped first
  • it needs to stay unflipped
    • for example, the previous is $$${1, 3}$$$ and the next to place is $$${2, 4}$$$
  • it can be either orientation
    • for example, the previous is $$${2, 3}$$$ and the next to place is $$${4, 5}$$$
  • or can't be either
    • In this last case, you can early return a "NO". This would be like in the first example case where you place $$${1, 6}$$$ first and then have to place $$${2, 4}$$$, but no matter how you flip $$${2, 4}$$$ the number after the array with 6 will no longer be increasing.

You don't need to track the whole array, just the chosen state of the previous pair, which you can initialize as $$${-1, -1}$$$ (since the first actual pair can have either orientation).

If at any point you have two in a row which can be either orientation, then you can flip or not flip that fist one to make the total number of swaps even, regardless of how the total turns out. From this point on, you will return "YES" as long as you don't find a situation where a pair can't be either orientation.

You will also want to track the number of fixed orientations there are in a row. If you end up with a block of fixed orientations which is of an even count, then you can flip the pair just before this block (which is necessarily one that can be either orientation, otherwise it would be part of this block) which would flip the orientation of all the pairs in this block as well, which will have flipped the orientation of an odd number of pairs, meaning the number of total flips will have gone from even to odd or vice versa. This freedom will mean no matter what the parity of the other fixed blocks are, you can make the total number of flips even or odd.

After going through all pairs, if you found either degree of freedom (either two free pairs in a row, or a block of fixed pairs of even count) or the number of flips applied for the array you built is already even, then it's possible.

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

Why is dp[3][1] = 1 in solution of question G? Shouldn't the dp[4][1] be equal to 1 since we flip the first pair and the total flips is equal to 1?

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

    Editorial author here. Sorry, just a really annoying typo, because I got confused between zero and one-based indexing! It's definitely meant to be dp[4][1].

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

In problem F, why are we only doing (n,j), there can be sequence of repeated factors as well which will reduce the multiple.

i.e. example consider a sequence [2,3,3,4,4,4] and then (n-6) 1's for any n. The multiple should be n!/((n-6)! * 2! * 3!)

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

why am i getting TLE in C? I need help pls

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
 
using namespace std;
int N = 200002;
 
void solve();
 
int main()
{
    IOS

    int t;
    cin >> t;
 
    while(t--)
    {
        solve();
    }

    return 0;
}
 
void solve()
{
    int n, k;
    cin >> n >> k;
 
    vector<int> numbers(n);
    vector<int> counter(N, 0);
 
    for(auto& num: numbers)
    {
        int temp;
        cin >> temp;
        counter[temp] += 1;
        
        num = temp;
    }
 
    int result = 0;
 
    for(int i = 0; i < n; i++)
    {
        if(counter[numbers[i]] > 0 && counter[abs(numbers[i] - k)] > 0 && numbers[i] == abs(numbers[i] - k))
        {
            result += counter[numbers[i]] / 2;
            counter[numbers[i]] = 0;
            counter[abs(numbers[i] - k)] = 0;
        }
        else if(counter[numbers[i]] > 0 && counter[abs(numbers[i] - k)] > 0)
        {
            
            result += min(counter[numbers[i]], counter[abs(numbers[i] - k)]);
            counter[numbers[i]] = 0;
            counter[abs(numbers[i] - k)] = 0;
        }
    }
    
    cout << result << endl;
}
  • »
    »
    15 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    You're declaring an vector of length 200002 for each test case, which is slow and can cause a TLE with many test cases.

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

You can solve problem B in O(nm) time complexity. You don't have to sort cards array, you have to only check is difference between consecutive terms in array is factor of n or not.

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

There is an easier way to prove D, notice that if any set of operations sorts $$$a$$$, then that set + $$$op_1,op_2...op_{n-1}$$$ also sorts.

In the end, we will perform operations on every index at least once, and one can notice that performing $$$op_1$$$ as a first operation is optimal. After operating on index $$$1$$$, induction works for other indices.

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

A kind of brute force based matrix multiplication solution for F: https://mirror.codeforces.com/contest/2060/submission/301857001. This completes the max test

Max test

in ~3.6s. The complexity is $$$O(klog^2(k)log(n))$$$.

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

Learnt something from the editorial.
A new word for me — Horrendous

img

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

Hii!

Can you guys tell me why this solution is getting wrong answer for problem E?

// this is code
#include<bits/stdc++.h>
using namespace std;
 
void soln(){
    int n, f, g;
    cin>>n>>f>>g;
 
    unordered_set<string>st1,st2;
    for(int i = 0; i < f; i++){
 
        int x, y;
        cin>>x>>y;
        int mini = min(x, y);
        int maxi = max(x,y);

        string val = to_string(mini) + to_string(maxi);
        st1.insert(val);       
    }
 
    for(int i = 0; i < g; i++){
 
        int x, y;
        cin>>x>>y;
        int mini = min(x, y);
        int maxi = max(x,y);

        string val = to_string(mini) + to_string(maxi);
        st2.insert(val);       
    }
 
    int ans = 0;
 
    for(auto x: st1){
        if(!st2.count(x)) ans++;
    }
 
    for(auto x: st2){
        if(!st1.count(x)) ans++;
    }
 
    cout<<ans<<endl;
}
 
int main(){
    int t;
    cin>>t;
 
    while(t--){
        soln();
    }
}

I know DSU will be the correct way of solving this, but this is what clicked in my mind.

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

    Although your method itself (calculate the difference between edges) is wrong, your comparing method (concat it to a string) is even wrong. Consider $$$(1,112)$$$ and $$$(11,12)$$$.

    For why your method is wrong, consider:

    $$$F$$$: $$$(1,2)$$$ $$$(1,3)$$$.

    $$$G$$$: $$$(2,3)$$$ $$$(1,3)$$$.

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

    I would also like to mention, to save you from possible hacks in the future, to not use unordered_set and unordered_map, since those have worst case O(n) time complexity for lookups. Instead use the normal set and map

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

      okay!

      Thanks for your suggestion. I am not so active on codeforces. What are hacks?

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

        Essentially, people can make custom test cases to test against another solution, and if you are using unordered_map, they can make a test case that triggers the O(n) time complexity for lookups and give it to your program to make it time out.

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

The Solution2 of F has a typing mistake.

The formula $$$\sum\limits_{i=2}^n f_k(n)-f_k(n-1)$$$ should be corrected to $$$\sum\limits_{i=2}^n f_k(i)-f_k(i-1)$$$.

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

F: How dp from editorial works? Example:

dp[6][1] = 1
dp[9][1] = 1
dp[18][2] = dp[18/2][1] + dp[18/3][1] = 2

But it wrong, correct is 4! Possible arrays: [9 2] [2 9] [6 3] [3 6]

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

    You update dp from all non-1 factors, not just prime. In this case it should be dp[18][2]=dp[18/2][1]+dp[18/3][1]+dp[18/6][1]+dp[18/9][1]+dp[18/18][1]=1+1+1+1+0=4

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

I wish Problem E had been based on a different topic instead of graphs.

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

For problem G, if you have played Rubic's cude, you can find out that we use 2*2 Y perm to contruct E perm.It's very interseting. G is a very good problem.

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

Regarding the solution for G, the key observation part can be proven without contradiction.

Since the final solution keeps the sequences $$$a$$$ and $$$b$$$ in ascending order, we have the following.

$$$a_i \leq a_{i+1}$$$ and $$$b_i \leq b_{i+1}$$$.

Real numbers have a convenient property where $$$x \leq y$$$ and $$$z \leq w$$$ implies $$$min(x,z) \leq min(z,w)$$$, so we can immediately derive the following result.

$$$min(a_i,b_i) \leq min(a_{i+1}, b_{i+1})$$$.

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

For problem F, the solution 1 that is described in the editorial for computing non-unity sequences for length till 16 using dp. You can actually do it using inclusion and exclusion too.

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

Problem F is an advanced version of LeetCode 1735. Count Ways to Make Array With Product. You can try solving the simpler one first.

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

)

»
15 months ago, hide # |
Rev. 2  
Vote: I like it -8 Vote: I do not like it

Can someone tell me why Problem C gives TLE in testcase 17 for this solution in java 8 This uses sorting and two pointers.

    import java.util.*;
    import java.io.*;
     
    import java.io.IOException;
     
    public class Main{
        static class FastReader{
            BufferedReader br;
            StringTokenizer st;
            public FastReader(){
                br=new BufferedReader(new InputStreamReader(System.in));
            }
            String next(){
                while(st==null || !st.hasMoreTokens()){
                    try {
                        st=new StringTokenizer(br.readLine());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return st.nextToken();
            }
            int nextInt(){
                return Integer.parseInt(next());
            }
            long nextLong(){
                return Long.parseLong(next());
            }
            double nextDouble(){
                return Double.parseDouble(next());
            }
            String nextLine(){
                String str="";
                try {
                    str=br.readLine().trim();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return str;
            }
        }
        static class FastWriter {
            private final BufferedWriter bw;
     
            public FastWriter() {
                this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
            }
     
            public void print(Object object) throws IOException {
                bw.append("" + object);
            }
     
            public void println(Object object) throws IOException {
                print(object);
                bw.append("\n");
            }
     
            public void close() throws IOException {
                bw.close();
            }
        }
     
        public static void main(String[] args) {
            FastReader in=new FastReader();
            FastWriter out=new FastWriter();
            try {
                int n = in.nextInt();
                for(int i=0; i<n; i++){
                    int a=in.nextInt();
                    int b=in.nextInt();
                    int[] arr=new int[a];
                    int ans = 0;
                    HashMap<Integer,Integer> map=new HashMap<>();
                    for(int j=0; j<a; j++){
                        arr[j] = in.nextInt();
                    }
                    Arrays.sort(arr);
                    int f = 0, g= a-1;
                    while(f<g){
                        long sum = 1L*arr[f]+arr[g];
                        if(sum==b){
                            ++ans;
                            ++f;
                            --g;
                        }else if(sum<b){
                            ++f;
                        }else --g;
                    }
                    out.println(ans);
                }
            } catch (Exception e) {
                return;
            }finally{
                try{
                    out.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    }
»
15 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

Thanks for the contest, this was such a great problem set; it's been a long time since I've enjoyed one like this.Problem F is definitely on my top 3 list.

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

Can someone comment about the time complexity if E is solved using DFS rather than DSU , I think for DFS , it can get TLE if testcases are not so gentle for the same constraints ?

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

Anyone in community pls help with the following issue of mine:

I had solved first 4 and hadn't got any error in C. But during system testing I was getting runtime error on test 14 (my submission: 301790765). But with same logic another solution of a friend of mine (301906786) was getting accepted. You can see that only difference is that he is accessing first element in multiset and I was accessing last element.

And with my other ID I had submitted exact same to same code as that on which I was getting runtime error on test 14, but to my surprise on this other ID with same code same compiler I was getting runtime error on test 17 (301902846) instead of 14.

So after that I tried exact solution as that of my friend on same compiler C++ 20, but I was getting runtime error on test 4 this time (302179372). So pls help me with this because because of this runtime error I got -99 instead of -35, my official standing slipped from 4000 to 11500.

Pls help cry

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

    OK here's why it's happening

    ll req = *it;
    

    You're setting req equal to the value at *it.

    s.erase(it);
    

    you're deleting the element that the iterator points to

    s.erase(s.find(k-*it));
    

    then you're again trying to access the data behind this iterator that you've deleted, which results in undefined behaviour.

    You were on the right track when you wrote ll req = *it", if you had just written s.erase(s.find(k-req)) instead of s.erase(s.find(k-*it)), you'd have been fine.

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

Am i the only one who understood something wrong on B problem statement? I thought the card stack restets to -1 every time a new round starts. I realised that i was wrong when i saw the solution. Luckily for me i didn't participate in the contest :P

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

I have a few questions regarding Solution 2 for Problem F.
1) "By induction we can prove that both fk(n) and Sk(n) are polynomials." How can we prove this?
2) "From the definition of Sk(n), we have q(k)=p(k)+1."
The definition of Sk(n) is sum of fk(i) for i=1 to n, isn't it? Because of that, Sk(n) is sum of polynomials with degree p(k). Sum of polynomials with degree p(k) should have the same degree (as p(k)). Supposing all of that, q(k) should be p(k). How is q(k)=p(k)+1?

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

I am unable to find the wrong case for my submission on G it's WA on 4. Can someone give some test case for it.

here is my submission:303466389

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

Another solution for F: For a fixed $$$x$$$, let the $$$(\alpha_1, \alpha_2, \dots, \alpha_s)$$$ be the powers of primes in its factorization. Then, for a fixed $$$|a| = m$$$, the number of arrays is $$$\prod\limits_{i=1}^{s}\binom{m+\alpha_i-1}{\alpha_i}$$$ since we choose positions of primes independently for different primes and for a fixed prime we have a kind of "stars and bars" situation. To get the answer for a certain $$$x$$$, we sum the products for $$$m = \overline{1,n}$$$.

Since $$$n$$$ is large, it's impossible to do explicitly. Instead, notice that $$$\prod\limits_{i=1}^{s}\binom{m+\alpha_i-1}{\alpha_i}$$$ is a polynomial of $$$m$$$ with a degree no more than $$$16$$$ (since there are no more than $$$\log_2{(x)} \leq \log_2{(k)} \lt 17$$$ primes in factorization of $$$x$$$). We can write the polynomial as $$$\sum\limits_{j=0}^{t}c_{xj}\cdot m^j$$$, then the answer for $$$x$$$ is \begin{equation} \sum\limits_{m=1}^{n}\sum\limits_{j=0}^{t}c_{xj}\cdot m^j = \sum\limits_{j=0}^{t}c_{xj}\sum\limits_{m=1}^{n}m^j = \sum\limits_{j=0}^{t}c_{xj}\cdot f_j(n), \end{equation} where $$$f_j(n)$$$ is a sum of first $$$n$$$ $$$j$$$-th powers which is actually a polynomial of degree $$$j+1$$$ (Faulhaber's formula: https://en.wikipedia.org/wiki/Faulhaber%27s_formula ).

We can precompute the coefficients of $$$f_j(x)$$$ for $$$j = \overline{1...\lfloor\log_2(k)\rfloor}$$$ by constructing a system of linear equations and Gaussian elimination in $$$O(\log^4(k))$$$. Then we get an answer for a single $$$x$$$ in $$$O(\log^2(k))$$$ time required for multiplication and evaluation of polynomials.

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

why problem B is not run my code 158th test case

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

    probably you aren't checking current top card and comparing with minimum of currently left cards. my idea of solution is following: on every round each cow must place its minimum card, otherwise if on round j it has minimum card k and instead places some m>k, then on any round i>j it can't place that k anymore, since top will be larger. so on each round, every cow removes minimum from its deck. Thus, sort decks for each cows, take minimum cards from each deck (from each cow's m cards, take minimum) -> sort this m cards -> this should be order followed after each round, since order doesn't change between rounds and remains fixed, order to be correct, it must be possible to play with that order on round 0, thus on round 0 (from sorted order of minimums from each deck ) you can determine what order should be followed, so 2 properties should hold on each round: on round j, minimum from each deck of remaining cards should have same order as on round 0 (in order I mean in sorted version if minimum of cow i < minimum of cow j then this should also happened in round 0). also maintain top card on the deck (this will be maximum of minimums from each deck for round j) if on round j, minimum card out of minimums from each deck < top card then answer is -1, otherwise if >top_card and fixed order property is maintained for each round, then answer is yes and order is order determined on round =0, for implementation simply sort card decks initially, then iterate from 0 to m, on each iteration j (which corresponds round) sort column j of row wise sorted grid, and compare to order of initial one. (comparing order means maintain indices of row in sorted version of round ==0 and in every sorted column, row indices should match this one).

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

Rather than using DP, another approach for G:

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

Can someone help me how should I improve , I was able to come up with the approach for E but implementing was difficult . Any suggestions.

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

good contest for practice

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

The solution for problem B can be done in O(n*m) too, I guess. Below is my solution.

/* 2060-B.cpp : Farmer John's Card Game
 * 
 * https://mirror.codeforces.com/problemset/problem/2060/B
 *
 * PROBLEM STATEMENT
 *   We have `n' number of cows and `m' cards distributed to each cow.
 *   Each card is indexed from 0 to `n*m-1'. The cows play a game in
 *   which they stack the cards they have in a way that finally the
 *   stack stands in an ascending order from bottom to top.
 *
 *   The game occurs in rounds. In each round, cows make their moves
 *   one-by-one in order from the first cow to the n-th cow. Our target
 *   is to find a permutation for a successful game. A game is
 *   successful if an ordered stack of cards can be formed by the end of
 *   all rounds. Let's say the i-th cow cannot make the next move
 *   because she lacks the required card. In such a case, we return -1.
 *
 * INTUITION
 *   Set apart the cards and cows in two different sets. The set of
 *   cards is { 0, 1, 3 ... n*m - 1 }. Similarly, the set of cows is
 *   { 1, 2, 3 ... n }. Since each cow has `m' cards, we can create a
 *   uni-directional, many-to-one mapping from the set of cards to the
 *   set of cows.
 *
 *   Since we are only dealing with natural numbers (including zero),
 *   such a mapping can be created with the help of a common array.
 *   Setup an array the same size as the total number of cards, ie
 *   `n*m`. Then if the index of the array is assumed to be the card
 *   number, the value at each cell of the array can be the index of the
 *   cow which possesses it.
 *
 *   Now a simple iteration through our map array simulates the whole
 *   game. If the next card to be played is owned by the same cow which
 *   played the card at the top of the stack, the game cannot proceed
 *   further and we return a -1. If that doesn't happen, we track the
 *   order in which the cows play their cards and return that instead.
 *
 * COMPLEXITY ANALYSIS
 *   The space and time complexity, both, are O(n*m). But this should
 *   not discourage us because the maximum value of n*m is given to be
 *   only 2,000. Worst case scenario is a trivial O(2000).
 */

#include <vector>
#include <iostream>
#include <unordered_set>

void solve(std::vector<int> cards, int cows)
{

  if (cows == 1)
  {
    std::cout << 1 << std::endl;
    return;
  }

  int prev_cow = -1;
  std::vector<int> cow_turns;
  std::unordered_set<int> cow_turn_exists;
  for (int i = 0; i < cards.size(); ++i)
  {
    if (cards[i] == prev_cow)
    {
      std::cout << -1 << std::endl;
      return;
    }

    prev_cow = cards[i];
    if (!cow_turn_exists.count(prev_cow))
    {
      cow_turns.push_back(prev_cow);
      cow_turn_exists.insert(prev_cow);
    }
  }

  for (int cow : cow_turns)
  {
    std::cout << cow << " ";
  }

  std::cout << std::endl;
}

int main()
{
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);

  int T;
  int cows, cards_per_cow, card_no, i, j;
  std::cin >> T;

  while (T--)
  {
    std::cin >> cows >> cards_per_cow;
    std::vector<int> cards(cows * cards_per_cow);

    for (i = 1; i <= cows; ++i)
    {
      for (j = 0; j < cards_per_cow; ++j)
      {
        std::cin >> card_no;
        cards.at(card_no) = i;
      }
    }
    
  
    solve(cards, cows);
  }

  return 0;
}
»
7 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Isn't E brute force?

Why can't you just use sets and if s1 contains a pair that s2 doesn't then ans++ and if s2 contains a pair that s1 doesn't, again we do ans++.

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

hush little boater don't you yap.I'll write the code for you.