flamestorm's blog

By flamestorm, 4 hours ago, In English

Thanks for participating. We hope you enjoyed the contest!

1999A - A+B Again?

Idea: flamestorm

Tutorial
Solution

1999B - Card Game

Idea: SlavicG

Tutorial
Solution

1999C - Showering

Idea: SlavicG

Tutorial
Solution

1999D - Slavic's Exam

Idea: SlavicG

Tutorial
Solution

1999E - Triple Operations

Idea: flamestorm

Tutorial
Solution

1999F - Expected Median

Idea: mesanu

Tutorial
Solution

1999G1 - Ruler (easy version)

Idea: flamestorm

Tutorial
Solution

1999G2 - Ruler (hard version)

Idea: flamestorm

Tutorial
Solution
  • Vote: I like it
  • +37
  • Vote: I do not like it

»
4 hours ago, # |
  Vote: I like it +1 Vote: I do not like it

you are so quickly!

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

好快

»
4 hours ago, # |
Rev. 2   Vote: I like it +2 Vote: I do not like it

I will kill myself over B. UPD: Found my error

  • »
    »
    4 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    me too

  • »
    »
    4 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it
    • same :___
  • »
    »
    4 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    us moment

  • »
    »
    4 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    real

  • »
    »
    2 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    me but i found it after one hour :(

  • »
    »
    2 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    you and me both brother

  • »
    »
    48 minutes ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    i also cant understand, where is the mistake in my solution, it has the same meaning, but i get an error in test 2 every time

    • »
      »
      »
      6 minutes ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      i'm a noob, but most probably what you were doing is only taking a subset of all possible combination. like i was not considering cases where first number can be equal and second is greater so win and i also was not considering if first number is greater second even if equal, we still get win.

»
4 hours ago, # |
  Vote: I like it +1 Vote: I do not like it

I spent lot's of time considering 1:0 situations in problem B.

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

fast editorial

»
4 hours ago, # |
  Vote: I like it +8 Vote: I do not like it

Clarification: SlavicG and I actually are best friends, he's just playing hard to get.

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

In problem E, my code was giving me expected/correct output in my local machine, but is giving wrong output during codeforce's testing 274941437

Could any one tell why is that?

  • »
    »
    4 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    try using c++20 and then you will probably get wa on test2

  • »
    »
    4 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Maybe different machine use different implement causing the different output ? Better not use log in this problem I guess, I stuck on that too.

    • »
      »
      »
      4 hours ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      hm, might be. My solution for d also passed test 1 but could not other ones,sad.

      But all in one, I am happy to solve atleast few :D

  • »
    »
    4 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    probably rounding errors with log. safer not to use log when you dont have to

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

    Try not using log

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

Can't believe how much bad this one went

»
4 hours ago, # |
  Vote: I like it +4 Vote: I do not like it

G1<<E or F

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

can anyone help me, for which test case my code didn't work

//this code

#include<bits/stdc++.h>

using namespace std;

int main(){
    int t;
    cin>>t;
    while (t--)
    {
        int a1,a2,b1,b2;
        cin>>a1>>a2>>b1>>b2;
        int x1 = min(a1,a2), x2 = max(a1,a2);
        int y1 = min(b1,b2), y2 = max(b1,b2);

        if(x1==x2 && y1==y2 && x1==y1){
            cout<<0<<endl;
        }

        else if(x1>=y2){
            cout<<4<<endl;
        }

        else if(x2<=y1){
            cout<<0<<endl;
        }

        else{
            cout<<2<<endl;
        }
    }
    
    return 0;
}
  • »
    »
    4 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Bro your code is correct only with the first case

  • »
    »
    64 minutes ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    your code does not work if x1==y1&&x2==y2.

    for example 2 2 3 3.

    you should write this part separately in your code.

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

my idea in D was right but i didn't code it

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

can someone help me with what's wrong with my solution of B?

Spoiler
  • »
    »
    4 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    you forgot about when Suneet wins one round and draws other...The equalities

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

    you forgot to consider the cases like where a1>b1 && a2>=b2 This is also the win case

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

I had the same intuition for F but I got stuck and I had no idea why my code was failing for the last provided test case. Values seem fine, but it keeps dumping to stack trace, isn't it just a O(n) loop with my factorials memoized?

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;

#define debug 1

const ll limit = 2e5;
const ll mod = 1e9+7;

template<typename... Args>
void mycout(const Args&... args) 
{
    #if debug
    ((cout << args << ", "), ...); // Fold expression
    cout << endl;
    #endif
}

unordered_map<ull, ull> factorials;

const ull factorial(const ull n) 
{
    if (n <= 1) 
        return 1;

    if(factorials.contains(n))
        return factorials[n];

    factorials[n] = n * factorial(n-1);

    return factorials[n];
}

const ull pickKFromN(const ull n, const ull k)
{
    if(n <= 0)
        return 0;

    const ull top = factorial(n);
    const ull btm = factorial(k) * factorial(n-k);
    
    return top/btm;
}

int main()
{
    ios::sync_with_stdio(false); 
    cin.tie(0); 
    cout.tie(0);
    
    ll t;
    cin >> t;

    while(t--)
    {
        // k is odd
        ll n,k;
        cin >> n >>k;

        vector<ll> v(n);
        ll sum{};

        
        for(ll i{}; i < n; ++i)
        {
            cin >> v[i];

            sum = (sum + v[i]) % mod;
        }

        if(k == 1)
        {
            cout << sum << endl;
            continue;
        }

        sort(v.begin(),v.end());
        
        // eg 3 will be 2th element so index 1
        ll medianIndex = (k == 1 ? 0 : (k+1)/2 - 1);
        
        if(k == n)
        {
            cout << v[medianIndex] << endl;
            continue;
        }

        ll result{};

        // given median index
        // i can pick index elements on the left
        // then pick k - 2

        /*
            6 3
            1 0 1 0 1 1
            
            medianindex == 1
            0 0 1 1 1
              ^

            1*2*2 == 4
            1*3*1 == 3
            

            0 0 1 1

            i1 : 
        */
        
        for(ll i{medianIndex}; i < n; ++i)
        {
            if(v[i] == 0)
                continue;

            const ull leftElements = i;
            const ull rightElements = n-i-1;

            const ull pickLeft = medianIndex;
            const ull pickRight = k-(medianIndex+1);

            const ull left = pickKFromN(leftElements,pickLeft) % mod;
            const ull right = pickKFromN(rightElements,pickRight) % mod;
            const ull combined = (((v[i] * left) % mod) * right) % mod;
            
            mycout(i,result,combined);
            mycout(leftElements,pickLeft,left);
            mycout(rightElements,pickRight,right);
    
            result = (result + combined) % mod;
        }

        cout << result << endl;
    }

    return 0;
}
»
4 hours ago, # |
  Vote: I like it 0 Vote: I do not like it
from math import *
for _ in range(int(input())):
  l,r=map(int,input().split())
  ans=0
  if l<3:
    ans=1
  else:
    if power(l):
      ans+=ceil(log(l,3))+1
    else:
      ans+=ceil(log(l,3))
  before=ans
  print(ans,"BEFORE")
  for i in range(l+2,r+1):
    if power(i):
      ans+=ceil(log(i,3))+1
    else:
      ans+=ceil(log(i,3))
  print(ans,"AFTER")
  if r>l:
    ans+=ceil(log((3*before)*(l+1),3))+(1 if power((3*before)*(l+1)) else 0)
  print(ans,"ANSWER")

Got the Logic Couldn't make it up.

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

In editorial of G2 , if a < x <= b, area should be a * (b + 1) but given (a+1) * b. May be it's a Typo SlavicG

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

I think G1 and G2 were much easier than E and F... the most basic binary search in both versions

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

Why my code 274894324 is getting WA on E? I did all exactly as in editorial

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

i swear to god B took more time than A,C,D,E

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

Should've been able to solve E. But we move

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

In the code for B, since you are using Python, ans++ should give syntax error. Just a minor detail! Thanks for the quick editorial:D

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

Need to improve my comprehensive understanding of paragraphs , spend a lot of time on B,

missed the point that only one win is enough when the second one is draw (missed this one)

»
4 hours ago, # |
Rev. 2   Vote: I like it +4 Vote: I do not like it

Just got an observation for E:
[1, 3) -> all elements require 1 operations to become 0
[3, 9) -> all elements require 2 operations to become 0
[9, 27) -> all elements require 3 operations to become 0
[27, 81) -> all elements require 4 operations to become 0
[81, 81 * 3) -> all elements require 5 operations to become 0
and so on.

I think this could be optimal.

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

    This is exactly the idea I used in my solution 274869114

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

      I was not left with enough time since stucked in correcting B :(
      Btw your implementation is good.

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

        Thanks! But I couldn't solve B at all, so sad

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

    Yes, this can reach $$$O(\log\log n)$$$ complexity and also feasible to $$$r\le 10^{18}$$$.

    • »
      »
      »
      71 minute(s) ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      How to precompute in log(log(n)) complexity?

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

I love E, F, G2. Brilliant idea!!!

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

Can anyone tell me how to test our code for an interactive problem?

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

    You can write an answer function according to the description of problem. Then use it to simulate this whole process. Check my submission if you know python.

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

      Thanks for the reply!!

      But I did not quite understand it.

      Would be great if I could find a C++ solution for that.

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

For problem E why is this O(n) code getting TLE?(https://codeforces.com/contest/1999/submission/274854161)

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

    i dont think O(n) solutions passed, they used O(n) to precompute and then O(1) for each test case

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

    Sum of $$$r-l$$$ is not bounded by $$$10^5$$$

    For instance, $$$l=1, r=10^5$$$ can be given $$$10^4$$$ times. In that case, your approach will TLE

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

    The problem doesn't guarantee the sum of $$$r-l$$$ is below $$$2\times 10^5$$$

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

In G2, those who didn't understand how we are calculating value of a and b, here is the explanation:

Let, l < a < b < r, where a-l ≈ b-a ≈ r-b ≈ x, consider differences are almost same

So, we can say that r-l = 3x, where x = (r-l)/3, a = l + x, and b = l + 2x.

Hence, a = (2*l + r)/3 and b = (l + 2*r)/3

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

got humbled :'(

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

I'm so used to "Sum of n over all cases doesn't exceed $$$ 2 \times 10^5 $$$ " :)

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

I always thought I didn't need them yet, till maybe Spclist or Xpert. After reading the editorial, now i feel pitiful not learning how to do interactive problems earlier.

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

In which test case my submission 274924034 for E is failing.

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

Can some one tell the failing testcase for problem B for this code


#include<bits/stdc++.h> using namespace std; #define int long long int mod=1000000007; int MAX_INT=2147483647; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin>>t; while(t--){ int a1,a2,b1,b2; cin>>a1>>a2>>b1>>b2; int ans=0; if(a1>b1 && a2>=b2) ans++; if(a2>b1 && a1>=b2) ans++; if(a1>b2 && a2>=b1) ans++; if(a2>b2 && a1>=b1) ans++; cout<<ans<<endl; } return 0; }
  • »
    »
    3 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    8 1 1 1 doesn't work. Should be 4 but your program outputs 2.

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

I dont understand why this gives wrong ans; E

Your code here...
int fun(ll x){
  int count = 0;
  while(x){
    count++;
    x = x/3;
  }
  return count;
}

void solve() {
  ll l, r;
  cin >> l >> r;
  ll ans = 0;
  ans += fun(l);
  ans+= fun(3*ans*(l+1));
  for(int i = l+2; i <= r; i++){
    ans+=fun(i);
  }
  cout << ans << endl;
}
»
3 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

Why is my D solution so slow? 274970418 I am using the same two pointer strategy that the editorial mentions and get TLE every time. I have a single loop iterating through the string and that is it. Am I using any expensive operations without realizing it? Thanks in advance.

  • »
    »
    2 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    In java, adding a letter to the end of a string is an O(n) operation, as it creates an entirely new string.

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

F is a pretty simple combinatorics problem, and G1 is a simple binary search. However I somehow failed to solve those problems in the contest :(

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

Can anyone tell that why in E O(n log n) doesn't work ? To be precise the overall complexity would be 2.5*10^6, which should pass in 1 sec. ? isn't it?

  • »
    »
    2 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    'cuz the complexity is actually $$$O(tn\log n)$$$. Usually the problem guarantees the sum of $$$n$$$, $$$m$$$, etc. doesn't exceed $$$2\times 10^5$$$, but this problem does not.

  • »
    »
    2 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    because sum of r-l over all test cases isnt guaranteed to be under 2*10^5, so your code runs in O(t*n*logn)

»
2 hours ago, # |
Rev. 4   Vote: I like it 0 Vote: I do not like it

I got another simpler setup approach to E, perhaps.

We will make an array of the exponentiation of 3, like lt[0] = 1, lt[1] = 3, lt[2] = 9, lt[3] = 27, ..., lt[15] = 3^15. (Because r <= 2 . 10 ^ 5)

We will make a prefix sum before making the testcase. For each i, call j is the smallest number satisfying a[i] >= lt[j], (1 <= j <= 15).

For example, a[1] = 1, a[2] = 1, a[3] = 2, a[4] = 2, ..., a[8] = 2, a[9] = 3, a[10] = 3, ... After that we will make a prefix sum of a[] called f[]. For instance, f[1] = 1, f[2] = f[1] + a[2] = 2, f[3] = f[2] + a[3] = 4, ...

For each n, m in the testcase, to find the minimum operation needed, while n, m is typed from the keyboard, the answer will be pre[m] — pre[n-1].

Then plus the a[n], as we have to make n become 0 to minimize the operations, the final thing to do is f[m] — f[n-1] + a[n].

Link: My sub

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

Can anyone help me find out the bug in my code for problem D? It passed all testcases, but it got hacked, and I don't know how to find the specific testcase in which it failed at.

https://codeforces.com/contest/1999/submission/274881476

Thanks in advance! If this is against the rules of the contest, please let me know so I can delete this comment.

  • »
    »
    2 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Consider aaaaaaaaaaaaaaaaaaaaab and aaaaaaaaaaac. Your code will compare $$$O(n^2)$$$ times in this case.

»
2 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

You know for the Showering question how did you not get a memory exceeded error??

#include <bits/stdc++.h>

using namespace std;


int main(){
  cin.tie(nullptr)->sync_with_stdio(false);
  int t,n,s,m;
  cin >> t; 
  int start, end;
  vector<int> time; 
  while (t--){
    time.clear();
    cin >> n >> s >> m;// 0 is free and 1 is busy
    while (n--){
      cin >> start >> end;
      while(start < end){
        time.push_back(start);
        start += 1;
      }
    }
    sort(time.begin(), time.end());
    int free_time = time[0];
    for (int x = 0; x<time.size() - 1; x++){
      if (time[x+1] - time[x] > 1 && time[x+1] - time[x] > free_time){
        free_time = time[x+1] - time[x] - 1;
      }
    }
    if (m - time[time.size()-1] - 1 > free_time){
      free_time = m - time[time.size() - 1] - 1;
    }
    if (s <= free_time){
      cout << "YES" << "\n";
    }
    else{
      cout << "NO" << "\n";
    }
  }
  return 0; 
}

I am also using a linked list to store the timings which aren't available. But test case 3 threw a memory exceeded error.

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

in the problem E as the editorial said f(x) = 1 + Math.floor(log(x) in base 3) then why the below code is giving wrong answer

Spoiler
»
2 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

**Can anyone tell me , why its giving Tle , or is it wrong ** ~~~~~ int main() { // startTime = clock(); ios_base::sync_with_stdio(false); cin.tie(NULL);

ifndef ONLINE_JUDGE

freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);

endif

int N = 1e5;
vector<int> dp(2*N + 100);
dp[0] =0, dp[1] =1 , dp[2] =1 , dp[3] =2;
for(int i=4;i< dp.size();i++) dp[i] = 1 + dp[(i/3)];
int test ;
cin >> test;

while (test--) {
    int x,y;
    cin>>x>>y;
    int ans =0;
    ans = 2*(1 + dp[(x/3)]);
    x+=1;
    // for(int i=0;i<=10;i++) cout<<i<<" "<<dp[i]<<"   ";
    while( x<= y){
        ans += (1 + dp[(x/3)]);
        x++;
    }
    cout<<ans<<endl;
}
return 0;

} ~~~~~

»
117 minutes ago, # |
  Vote: I like it 0 Vote: I do not like it

E is good, but isn't the data range a bit small? Many $$$O(Tn)$$$ algorithms can pass this problem, and it's hard to hack them.

»
70 minutes ago, # |
  Vote: I like it 0 Vote: I do not like it

For Problem E, I am running a loop here of size (b-a+1) each time. I tried hacking my solution on max size testcase $$$10000 \newline 1\;200000\newline....\newline....\newline1\;200000\newline$$$ . I don't know this solution got accepted? Can anybody tell the reason for the same or else try to hack it?

Submission link

  • »
    »
    42 minutes ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    the compiler isnt that stupid, it can optimize out obviously unnecessary parts

  • »
    »
    34 minutes ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Firstly, you can't share hacks. Secondly, as the code works in $$$O(r - l)$$$ per test, the max amount of operations is 1e4 * 2e5, which is 2e9 operations, which is just enough to pass.

»
64 minutes ago, # |
  Vote: I like it 0 Vote: I do not like it

Harder Version of F if anyone wants to try. Sum of Medians

»
28 minutes ago, # |
  Vote: I like it 0 Vote: I do not like it

Can any one give me more explain for problem F , i did not Understand tut :(