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

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

Thank you all for participating in the round.

All the problems were created by BiNARyBeastt during our classes in winter.

A — Simple Palindrome

Solution
Code

B1 — The Strict Teacher (Easy Version), B2 — The Strict Teacher (Hard Version)

Solution
Code

C — Lazy Narek

Solution
Code

D — Alter the GCD (Check this comment or the video editorial for an easier solution).

Solution
Code

E1 — Subtangle Game (Easy Version), E2 — Subtangle Game (Hard Version)

Solution
Code (E1)
Code (E2)
Разбор задач Codeforces Round 972 (Div. 2)
  • Проголосовать: нравится
  • +53
  • Проголосовать: не нравится

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

We trained Tsovak to post this faster than the speed of light, but we failed miserably due to the AI situation.

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

for B, i didnt see that he can choose not to move, and i coded the completely wrong solution for 20 mins, tragic. great problem C, i absolutely loved it.

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

can someone help me why this code gets wrong answer for problem C

submission: https://mirror.codeforces.com/contest/2005/submission/281243184

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

how to train for problems like C?

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

Can someone explain how did I get TLE in C? I thought my code works in $$$O(N * M * 25)$$$: 281197818

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

The contest is amazing!

my only complaint is the protests on problem C could have been better ):

I was going to become a CM for the first time If there were stronger protests.

Thank you for the contest as usual!

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

for problem A

Expected output : aaeiou , palindromes subsequences a , a , e , i , o , u , aa

My output : aeioua , a , e , i , o , u , a , aa

Why my output is considered as wrong answer , can anyone explain?

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

I got TLE at C problem and I think it works in (5 * n * m)
solution
am I missing something ?

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

Anyone else lost question C by initializing dp to -1 instead of -inf? :(

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

Nice round.

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

thanks for the fast editorial!

in the problem $$$B1/B2$$$, C++ upper_bound and lower_bound both should point to element strictly $$$ \gt a[i]$$$ (david's position) since $$$b[i]$$$ only contains teacher's positions. or is it not so? my contest submission using both lower_bound and upper_bound got WA.

my contest submission

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

Bitset helps to solve E2 without any observation. 281264340

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

Why is my submission for C getting TLE on test 11? I think the complexity is $$$O(5*N*M+10*N)$$$.

Submission: 281248462

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

    You're initializing your dp with -1, so if the answer at a certain state was -1 it will call the solve() function again instead of returning the previously calculated answer (which is -1). Instead you can initialize with -INF or create another array to check for visited states.

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

For problem E1, can someone help me understand why Narek wins in this testcase :

6 6 6
1 2 3 4 5 6
1 2 7 7 7 7
7 2 7 7 7 7
7 7 3 7 7 7
7 7 7 4 7 7
7 7 7 7 5 7
7 7 7 7 7 6

This is my submission which is failing for this test : [submission:https://mirror.codeforces.com/contest/2005/submission/281255124]

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

funny how in problem C "problem tags" there is greedy and the first thing editorial says is that greedy approach doesn't work.

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

    It is my bad for not properly stating what I meant by greedy in the hint section. The tag is relating to the fact that you can greedily count the score when you choose one string. I meant that you can't do greedy on each string without considering their connection with each other by dp.

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

can you please share some tests for C, cant debug it :(

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

I solved E1 in O(n*m*l) time complexity. Can anyone uphack this solution?

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

what is wrong in my recursive dp soltuion 281269592 please help me to debug, tried for so long but not able to find

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

Tsovak BiNARyBeastt Kaey TheScrasse

I tried uphacking my solution 281180437 for problem D, and it got an unexpected verdict.
I believe one of the correct marked solutions on polygon also gets TLE on this hack test case.
Can you please take a look at it and fix it?

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

    We were aware of this, and we also know a solution that won’t exceed the time limit. We are currently working on updating the editorial and the main solution accordingly. TheScrasse coded the solution to D just minutes before the start. We took a risk by making the problem harder, asking for the number of ways to get the maximum GCD, so that AI wouldn’t be able to solve it. Thanks for noticing this though!

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

I solved E1(2100 rating on clist) but was not able to submit it because of internet connetion problem.

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

can someone help me with why we cant solve problem c with take not take dp?

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

I tried top down approach for C, but am getting WA on test 2, can somebody tell me what's wrong? 281270159

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

Why this solution is accepted after sorting and not without it

int main() {
    int t;
    cin >> t;
    vector<string> vowels = {"u", "o", "i", "e", "a"};  
    while (t--) {
        int n;
        cin >> n;
        string result = "";
        for (int i = 0; i < n; i++) {
            result += vowels[i % 5];  
        }
        sort(result.begin(),result.end());  
        cout << result << endl;  
     }
    return 0;
}
    
  • »
    »
    20 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Because sorting it puts all the a's together, all the e's together, all the i's together and so on. It doesn't have to be sorted but the occurrences of every vowel need to be grouped together. And sorting is one way to do that. This is an example of a string that's not sorted but still works: eeeoooiiiaauu

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

    Because for example n = 6 Then without sorting it would print: uoieau It also contains the following palindromic subsequences: uou, uiu, etc Whereas after sorting it prints: aeiouu, which doesn't contain these subsequences Basically you were missing the core logic

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

About C:Since you want $$$O(n^2)$$$ algorithm got a TLE, why don't put the data in pretest? $$$n \le 10^3$$$ is very deceptive!

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

I have no idea why "if $$$dp_{k,i,j}$$$ wins, then $$$dp_{k + 2, i, j}$$$ also wins".

In fact, I add an assertion in my code, and it received RE(assertion failed) on E1, test 2.

The submission is here: 281298383

Here is (probably) the hack:

1
6 6 6
1 2 3 4 5 6
1 1 7 7 7 7
7 2 7 7 7 7
7 7 3 7 7 7
7 7 7 4 7 7
7 7 7 7 5 7
7 7 7 7 7 6
»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

rare c d e dp contest

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

/

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

Why in problem C, I let define int long long in my compiler output 0 5 0 7 (correct result of test 1) but in my submission it is 0 5 0 6. After deleting define int long long then it has AC.

My submission

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

A (probably) more simple solution to D.

We will also make use of the fact that there are only $$$O\left(\log(\max(a))\right)$$$ different prefix gcd, but we will use it on all subsegments instead:

  • Iterate $$$l$$$ from $$$1$$$ to $$$n$$$.
  • Imagine that we already have some $$$r \geq l$$$, then we have 3 ranges to consider $$$[1, l)$$$, $$$[l, r]$$$, $$$(r, n]$$$.
  • For $$$[1, l)$$$, we can maintain $$$\tt{prefix}_a$$$ and $$$\tt{prefix}_b$$$ as $$$\gcd(a_1, a_2, ..., a_{l - 1})$$$ and $$$\gcd(b_1, b_2, ..., b_{l - 1})$$$.
  • For $$$[l, r]$$$, realise that there are only $$$O\left(\log(\max(a))\right)$$$ different values of the gcd of both $$$a$$$ and $$$b$$$, and each values will correspond to a continuous range of $$$r$$$.
  • For $$$(r, n]$$$, the same thing apply.
  • So in fact, for each $$$l$$$, there are only $$$O\left(\log(\max(a))\right)$$$ different values of the result, and each value has a continuous range of $$$r$$$.
  • So if we can find all those ranges and iterate through them, we will have $$$O\left(\log(\max(a))^2\right)$$$ per $$$l$$$, assuming $$$\gcd$$$ take $$$O\left(\log(\max(a))\right)$$$.

To find the ranges for $$$(r, n]$$$, we can simply iterate the arrays in reverse and maintain the gcd ranges, leading to $$$O(n\log(\max(a)))$$$.

To find the ranges for $$$[l, r]$$$, build a sparse table with gcd, and keep expanding the divisible ranges to find the points where the continuous gcd changes. This will take $$$O(n\log(n)\log(\max(a)))$$$ to build and $$$O(\log(n)\log(\max(a)))$$$ to find the ranges for each $$$l$$$.

You can see my implementation in contest: 281217872

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

    This solution have since been uphacked by 415411. The test seems to put the algorithm in the worst case and that was slower than model.

    Thanks to the hack, I was able to improve upon the algorithm:

    • Building the gcd sparse table is useless, it allow us to find the $$$[l, r]$$$ ranges as a query in $$$O(\log(n)\log(max(a)))$$$, but this is not necessary.
    • Instead of iterating $$$l$$$ from $$$1$$$ to $$$n$$$, we can do it from $$$n$$$ to $$$1$$$ instead.
    • At $$$l$$$, we calculate all the different $$$r_i \lt r_{i + 1}$$$ such that $$$[l, r']$$$ have the same $$$gcd$$$ for all $$$r_i \leq r' \lt r_{i + 1}$$$.
    • When iterate to $$$l - 1$$$, we append the element at $$$l - 1$$$ to the beginning of these ranges.
    • Realise that this is equivalent to adding the range $$$[l, l]$$$ and updating the gcd of the existing ranges.
    • We also merge the equal ranges, so the amount of range is always $$$O(\log(\max(a)))$$$.

    The above actually improve the algorithm to $$$O(n\log(max(a))$$$, except for the calculating answer part:

    • We need to calculate prefix and suffix gcd for both arrays, this is at first glance $$$O(n\log(\max(a))$$$.
    • Howerver, the complexity of prefix and suffix gcd is actually just $$$O(n + \log(\max(a))$$$.
    Proof

    Applying the same idea as above to the ranges we have:

    • We add a new range $$$[l, l]$$$ $$$n$$$ times.
    • Each of these original range would have the amortized time with $$$\gcd$$$ equal to $$$O(\log(\max(a)))$$$.
    • So it's $$$O(n\log(\max(a))$$$.
    • We also have to merge the equal range, which happen $$$n$$$ times and each time we have at most $$$O(\log(\max(a)))$$$ ranges, so it also $$$O(n\log(\max(a))$$$.

    Finally we have to calculate the answer once we have all the ranges. I can't seem to find a way to improve this to $$$O(n\log(\max(a))$$$, so it's still $$$O(\log(\max(a))^2)$$$

    You can see my new code: 281316893. And the sanity check version where I reversed the array (so I don't take advantage of the fact that most interesting stuffs happen at the end of the hack array): 281316958

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

B2 — The Strict Teacher (Hard Version), Why did he find index through upperbound? Can't we do it by lowerbound? In upperbound case, the index-1 value teacher can be in same cell as studentright?

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

can anybody explain the recursive approach of E1

int fun(vi &res,vvi &arr,int i,int j,int ind){
    if(i>=arr.size() || j>=arr[0].size() || ind==res.size())return 0;
    if(dp[i][j][ind]!=-1)return dp[i][j][ind];
    int ans=fun(res,arr,i+1,j,ind)||(fun(res,arr,i,j+1,ind));
    if(arr[i][j]==res[ind]){
        ans|=fun(res,arr,i+1,j+1,ind+1)==0;
    }
    return dp[i][j][ind]= ans;
}
»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

can somebody help me in figuring out why my E1 solution tles? i'm using minimax technique which uses 1 for the win of Tsokav and -1 otherwise. the time complexity is $$$ O(l * n * m) $$$ which is fine, no?

Submission

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

Can anyone tell me why I am getting a WA? I have reviewed my solution many times and I can't seem to find the problem.

https://mirror.codeforces.com/contest/2005/submission/281266413

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

problem E2

If $$$dp_{k,i,j}$$$ wins, then $$$dp_{k+2,i,j}$$$ also wins.

Why is that true ?

if the grid is

4 2

2 2

and the array is 4 1 3

$$$dp_{1,1,1}$$$ is winning but $$$dp_{3,1,1}$$$ isn't

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

why was there no pretest in C that crush solution O(n^2)

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

E1 can be solved with a complete search using minimax algorithm. It ac's with alpha-beta pruning

Submission

Also, does anyone know how to calculate time complexities of programs like this?

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

Fun Fact: I didn't read the constraints in C carefully and submitted O(n^2) dp solution. It passed pretests but sadly it failed system test. So happy.

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

why for problem A , for n=6 , aeioua is not a right answer ?

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

D can be solved in $$$O(nlog^2(maxA))$$$ with a simple divide-and-conquer formulation. In the merge step, we only consider the $$$log$$$ unique gcds of suffixes in left half and prefixes for right half and store counts in map. Then just brute force the maximum and update counts (taking care to add up counts if maximums are equal). 281328508

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

    This should be official solution. Not only it's several times faster but also, its lot clearer and easier to implement, understand and it doesn't separate small and large testcases (🤢).

    On first it looks like $$$O(nlog^3(maxA))$$$ but when you consider the fact that on lower levels of DnQ you have $$$min(log(maxA),len)$$$ elements in map with simple for loop you can calculate that for $$$n = 200000$$$ in total you have ~16.000.000 operations.

    code to check complexity

    My code: 281365054 (tried to make it as crisp as possible)

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

    This is actually a great way to solve the problem! We changed the problem from only asking for max gcd to asking the amount of ways to get it, so we had a very short time to code two solutions and compare them. With the help of the coordinator, we managed to code something. This is why our official solution is messy and weird. We didn't have enough time to find something good.

    I added your comment and my updated video editorial in the blog to have something that's easier.

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

    in merge step, why are you not considering gcd of suffix or prefix ?

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

    Hi Newtech66 can you please write this again in detail, so that person of my level can also understand. means can you break down the solution in different parts. Thank you

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

      First, you divide the array into two halves and solve the problem of finding mx — the best sum of gcd(Ai) + gcd(Bi) and cnt — the number of pairs (l,r) that achieve this mx, for both halves. Then, you find the best answer (either {mx1,cnt1} or {mx2,cnt2} or {mx1,cnt1+cnt2} if mx1 = mx2).

      This is the best answer you can get if you consider each half independently. But you can also choose a segment [l,r] such that l <= mid <= r. In this case, gcd(Ai) will become a gcd of: {a prefix of array A: [1,l-1], a suffix of the left half of array B: [l,mid] such that these two segments become the new left half of A} and {a prefix of the right half of B: [mid,r] and a suffix of A: [r+1,n] such that these two segments become the new right half of A}. Something similar applies for array B. To find the best way to choose (l,r), you calculate every possible value of the gcd of the first half depending on l i.e. you try every possible value of l form 1 to mid and calculate gcd([1,l-1],[l,mid]) and increase the counter of this value by one (so that you can calculate the number of ways to achieve it), and then you do the same for r. After that you brute force on every possible pair of values of the first and second halves, calculating their gcd and updating the answer mxe. Finally you check whether this mxe is better than what we calculated for every half independently.

      I hope this helps!

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

    Another solution using a DnC approach, but without the need to compute prefix or suffix arrays, is to maintain the possible pairs of GCDs in a map, with 3 associated counts: (1) for no swap; (2) for one-way swap; and (3) for two-way swap. Then, the merge step is a matter of traversing the pairs on both sides and multiplying the counts. In the end, we find the maximum sum and its count, over all pairs in the entire range. 281855060

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

    I realised the number of GCD's we are going to ever obtain are going to be $$$O(log(max(a_1,b_1)))$$$ (ish) and decided to brute force the entire thing.
    Iterating over the indices and maintaining the counts of GCDs obtained by respective swaps.
    My Submission — 282869542

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

Solve A B C E1. Nice dp contest!

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

can i know why i am getting a wrong answer on problem B1 281322308?

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

Submission

can anyone find out what is wrong with this solution for C problem using dp.

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

Why is my submission for D getting TLE on test40? I think my complexity is $$$O(nlog^2n)$$$. And on test37 it needs a long time (over 3000ms) running.

submission 281364038

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

Why My code fails in test case 2 for problem C

link: https://mirror.codeforces.com/contest/2005/submission/281415696

also I found a testcase to which my code gives wrong answer which is

1
3 3
nara
reka
knae

My Answer: 0

Correct Answer: 2

Got it!

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

For Problem C: Another way to do it which does not require thinking about $$$dp_i - 2 \cdot i$$$ is to modify the score: Add a $$$-1$$$ to each occurrence of the letters in "narek" and add a $$$+10$$$ when you complete one instance of the full word "narek", in your $$$dp$$$.

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

plz help me. I can't understand why this code is on time limit of B2. https://mirror.codeforces.com/contest/2005/submission/281483618 I've made a empty set and inserted. I know it is not faster than vector, but anyway, is the time complexity M*O(lgM)?

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

I didn't quite follow the editorial for E, but here's another way to look at it.

As usual for such problems, we can define winning states as those from which you cannot reach a losing state and likewise. Now, let's try to process the game starting from the last element, and at each point, what we want is to mark each cell on the board as a winning or losing position.

It actually turns out, that such a set can be easily marked by maintaining the set of "innermost" positions of numbers (those cells $$$(r,c)$$$, such that no other cell $$$(r',c')$$$ exists with the same value such that $$$r' \gt r$$$ and $$$c' \gt c$$$). Note that this set has size $$$O(n+m)$$$.

As we process, we need to update a set of "dominant" positions $$$S_i$$$. Basically, the next set $$$S_i$$$ for the next value is that subset of "innermost" positions which "dominates" the previous set. That is the subset of "innermost" positions $$$(r,c)$$$ for $$$a_i$$$ such that no element $$$(r',c')$$$ of $$$S_{i+1}$$$ exists with $$$r' \gt r$$$ and $$$c' \gt c$$$.

Updating this set is simple: due to the structure of "innermost" positions, we can find any violations in $$$O(\log(n+m))$$$ time with a binary search. I think, even two pointers can work. Tsovak wins if the dominant set $$$S_1$$$ for $$$a_1$$$ is nonempty.

The overall solution works in $$$O(n \cdot m + l \cdot (n+m) \log(n+m))$$$.

My explanation could greatly benefit with diagrams and care, but here's the submission: 281490495

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

Why in problem E2 we need to store two dp's? Why we can't just do dp[i][j] — min k that dp[k][i][j] wins?

https://mirror.codeforces.com/contest/2005/submission/281529257 — submission with one dp

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define all(v) begin(v),end(v)
int callDp(int i, int ind, vector<string> &str, string &s, vector<vector<int>> &dp){
          if(i==str.size()){
              if(ind!=0){
                  return -ind;
              }
              return 0;
          }
          if(dp[i][ind]!=-1)
              return dp[i][ind];
          int take=0,nottake=0;
          int cntC=0,x=ind;
          for(int j=0;j<str[i].size();j++){
              if(str[i][j]=='n'||str[i][j]=='a'||str[i][j]=='r'||str[i][j]=='e'||str[i][j]=='k'){
                  if(str[i][j]!=s[ind]){
                      cntC++;
                  }
                  else if(str[i][j]==s[ind]){
                      if(ind==4)
                      take+=5;
                      ind=(ind+1)%5;
                  }
              }
          }
          take=take-cntC+callDp(i+1,ind,str,s,dp);
          nottake=callDp(i+1,x,str,s,dp);
          return dp[i][x]=max(take,nottake);
}
void dekhteHain(){
     int n,m;
     cin>>n>>m;
    //  vector<vector<char>> str(n,vector<char>(m));
    vector<string> str(n);
     string s="narek";
     unordered_set<char> st={'n','a','r','e','k'};
     vector<vector<int>> dp(n+1,vector<int>(5,-1));
     int cnt=0;
     for(int i=0;i<n;i++){
        cin>>str[i];
     }
     int ind=0;
     cout<<callDp(0,ind,str,s,dp)<<endl;

}
int32_t main() {
    int t;
    cin>>t;
    while(t--){
        dekhteHain();
    }
}

Can someone please explain why is this code giving TLE, It is code of C problem

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

https://mirror.codeforces.com/contest/2005/submission/281714678 Can someone tell me why my code is giving tle for problem C , my complexity is o(n*m) which should work

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

[submission:281823254]the problem C, can someone tell me what fault in my code? I think we can use the end of the previous string to transfer the following string.

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

E1, just cannot understand why the player would win when choosing that "inside" x, he would also win in that bigger matrix

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

    Your turn to play number x. You have two choices x, either go submatrix A or go submatrix B where B is a submatrix of A. If opponent can win from submatrix B, then he also wins from A (use same strategy as from B). If he cannot win from B then you do not care about A, just go B.

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

For D,I simply found that some prefixes are equivalent(two prefixes are equivalent when them have the same prefix gcd of a[] and b[])and a series of equivalent prefixes form a segment.So the number of these segments is at most 2*log(1e9).Then I do the same thing for suffixes.Calculating the max answer is easy and counting can be solved by enumerating a pair of "segment formed by equivalent prefixes"and"segment formed by equivalent suffixes" and then using two pointers.I think this would be a n*log^3 solution but it's easier to come up with this idea.Your text to link here...

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

For problem D, we have a more direct and simpler implementation method. Note that for any sequence, its prefix GCD (or suffix GCD) can have at most $$$O(\log m)$$$ different values (where $$$ m $$$ is the value range). Thus, we consider enumerating the left endpoint $$$ l $$$ and calculating the contribution of different right endpoints $$$ r $$$ to the answer. Based on this observation, we know that for a fixed $$$ l $$$, the different values of $$$ \gcd_{i=l}^{r} a_i $$$, $$$ \gcd_{i=l}^{r} b_i $$$, $$$ \gcd_{i=r+1}^{n} a_i $$$, and $$$ \gcd_{i=r+1}^{n} b_i $$$ each have $$$ O(\log m) $$$ possibilities. Therefore, we use a monotonic stack and suffix GCD to maintain each of these four values (keeping track of their values and corresponding continuous segments, which are limited to $$$ O(\log m) $$$), achieving a complexity of $$$ O(n \log^2 m) $$$ with a small constant.

Sample solution: https://mirror.codeforces.com/contest/2005/submission/282729163

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

E2:where it is written in the problem statement that there are no duplicates in a?

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

I solved E2 differently from the one in editorial... I just found the pareto optimal points (which are O(n+m) for each element of the grid and brute forced the recursion without any memoization either and it worked loll...

So the recursion works for O(l*(n+m))

Here's the sub : https://mirror.codeforces.com/contest/2005/submission/282989751

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

    can you elaborate

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

      So take any element in the grid. If the element is at two indices (i,j) and (p,q) where i<p and j<q, then the claim is the choosing the index (p,q) will always leave you equal to or better off, than choosing the index (i,j). (This can be easily verified taking an example).

      Now that means... for each element there will be a set of pareto optimal points. Consider the example:

      1 2 3 4

      2 3 3 4

      3 2 4 1

      1 4 2 3

      Here the pareto points for 4 are (4,2), (3,3), (2,4). Basically all those points (i,j) such that you won't find any more 4's in the grid [i+1:n][j+1:n]

      For every value, there will be O(n+m) pareto points at max and when you choose a point, you will lead the opponent to a grid where this element won't exist again. So every state will be visited once (hence the lack of memoizing).

      So you just run the simulation with the revised set of points for each value. (Iterate over all points and check all winning/loosing states).

      The complexity will be O(l*(n+m))

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

        "choosing the index (p,q) will always leave you equal to or better off, than choosing the index (i,j)" can u prove this claim?

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

          So... think about it this way... take the cells A = (1,1) and B = (3,3). We want to prove that choosing B is always a better option.

          Say you won by choosing A. That means the opponent couldn't make a move in the grid starting at (2,2). But that means he would not have won if i had given him the grid (4,4) as well. (Which is choosing B).

          So essentially B is equally as better as A.

          Now say you chose A and lost. So the opponent was able to find a point C in the grid (2,2). But there are chances that he wouldn't have found this point C if you had chosen B, as the grid would then be further reduced.

          Essentially by choosing B, you are reducing the opponents possible moves while not affecting yours. I hope this helps. If not, try going thru the 1st para of E1's solution.