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

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

We hope you enjoyed the contest! Sorry for the late editorial.

1760A - Medium Number

Idea: flamestorm

Tutorial
Solution

1760B - Atilla's Favorite Problem

Idea: SlavicG

Tutorial
Solution

1760C - Advantage

Idea: Errichto

Tutorial
Solution

1760D - Challenging Valleys

Idea: mesanu

Tutorial
Solution

1760E - Binary Inversions

Idea: SlavicG

Tutorial
Solution

1760F - Quests

Idea: flamestorm

Tutorial
Solution

1760G - SlavicG's Favorite Problem

Idea: SlavicG

Tutorial
Solution
Разбор задач Codeforces Round 835 (Div. 4)
  • Проголосовать: нравится
  • +50
  • Проголосовать: не нравится

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

Such a elegant implementation of problem G

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

I believe C can be done in O(n).

  1. Read everything into array a
  2. Find two maximum values — max and pre_max
  3. Do a run:
for (i in a) 
if (i == max) print(i - pre_max) else print(i - max)
  • »
    »
    17 месяцев назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    Yes, this one is work. You can check my solution that use idea of remember max and pre_max: 181961801

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

      can you post it cause it says I'm not allowed to view it

      • »
        »
        »
        »
        6 месяцев назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится
        #include <bits/stdc++.h>
        
        using namespace std;
        #define int long long
        #define uint unsigned long long
        #define endl '\n'
        #define vi vector < int >
        #define pb push_back;
        #define all(a) a.begin(), a.end()
        
        const int INF = 1e9 + 10;
        
        signed main() {
            ios_base::sync_with_stdio(false);
            cin.tie(0), cout.tie(0);
        
            int t;
            cin >> t;
            for(int i = 0; i < t; i++){
                int n;
                cin >> n;
                vi otv(n);
                int maxi = 0;
                int predmaxi = 0;
                int ind_max = 0;
                for(int j = 0; j < n; j++){
                    int s;
                    cin >> s;
                    if(j != 0) {
                        if (s >= predmaxi) {
                            if (s >= maxi) {
                                predmaxi = maxi;
                                maxi = s;
                                ind_max = j;
                            } else {
                                predmaxi = s;
                            }
                        }
                        otv[j] = s;
                    }else{
                        maxi = s;
                        otv[0] = s;
                    }
                }
                for(int h = 0; h < n; h++){
                    if (h != ind_max){
                    cout << otv[h] - maxi << ' ';
                    }else{
                        cout << otv[h] - predmaxi << ' ';
                    }
                }
                cout << endl;
            }
            return 0;
        }
        
»
17 месяцев назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

I had the exact same idea for G as the tutorial, But I screwed up the implementation in so many ways

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

    I find 'screwing up in so many ways' very relatable and funny, maybe this should be my spiritual quote!!

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

Why is this code for problem C, getting TLE? Code

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

Damn the problem ratings are so inflated

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

    I think only G and F are inflated, G should be 1500/1600, but then again, for some 1500s and 1600s I think trees are a new topic, so it is normally going to be inflated, while F is just a binary search with a few more steps, so I think it should be 1300/1400.

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

In problem F, I also sort the array in descending order and then I made a prefix sum of n elements. (Let's call it p) We have 3 situations:

1.If (c + p[1] - 1) / p[1] > d then there's no way that we can get c coins in d days.

2.If there is p[i] (i <= d) such that p[i] >= c then any value k satisfies.

3. Brute-force k from d to 0 as k >= d + 1 in this situation is not possible anymore.

Time complexity is O(n log n + d)

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

    Hi, @TrendBattles, If the sum of the entire array is greater than the c coins needed, will that not result in infinity? In case 2: you wrote only for a particular element, why so.?? What if a particular element is not greater than c but the entire array sum is greater?

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

      Well, I made a prefix sum of n elements and p[i] is the sum of the first i elements.

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

For problem G,unordered_map will give you TLE

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

why does my code get TLE for problem G.

#include <bits/stdc++.h>

using namespace std;

int t;
int n, a, b;





void dfs(int u, int now, int father, vector<vector<pair<int, int>>>& g, unordered_set<int>& st)
{
    if (father != -1)
    {
        st.insert(now);
    }
    for (auto [nxt, w]: g[u])
    {
        if (nxt != father)
        {
            dfs(nxt, now ^ w, u, g, st);
        }
    }
}


bool dfs2(int u, int now, int father, vector<vector<pair<int, int>>>& g, unordered_set<int>& st)
{
    if (st.count(now))
    {
        return true;
    }
    for (auto [nxt, w]: g[u])
    {
        if (nxt != father && nxt != b and dfs2(nxt, now ^ w, u, g, st))
        {
            return true;
        }
    }
    return false;
}



int main()
{
    ios::sync_with_stdio(false);
    cin >> t;
    while (t--)
    {
        cin >> n >> a >> b;
        vector<vector<pair<int, int>>> g(n + 1);
        unordered_set<int> st;
        for (int i = 0; i < n - 1; i++)
        {
            int u, v, e;
            cin >> u >> v >> e;
            g[u].push_back({v, e});
            g[v].push_back({u, e});
        }
        dfs(b, 0, -1, g, st);
        if (dfs2(a, 0, -1, g, st))
        {
            cout << "YES" << endl;
        }
        else
        {
            cout << "NO" << endl;
        }
    }

    return 0;
}
  • »
    »
    17 месяцев назад, # ^ |
      Проголосовать: нравится +3 Проголосовать: не нравится

    I changed from unordered_set to set and it got accepted

    182220698

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

      why unordered_set slower than set......

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

        While it's true that unordered_set is faster than set on average, the worst case time complexity of unordered_set can get to O(n^2) instead of O(log n) in a normal set (specifically on big prime numbers). It's likely that the test you got TLE on was deliberately constructed for solutions like yours

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

How does G solution take care of 2 things?

1.) We may never need to teleport and just reach end node with value 0. In that case inside dfs2, we need to have a condition for that.

2.) I don't see where is the condition to not go beyond end node in dfs2 when we don't get true before reaching end node.

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

    1.) We may teleport from a to a. Consider the following graph Let's say we start at node 1 and need to end at node 3.

    (n) node

    -w- connection with weight w

    (1) -2- (2) -2- (3)
    

    It isn't optimal to teleport to any node however we can compare path from 1 to 2 having XOR value = 2 and path from 3 to 2 having the XOR value equal to 2. In that case we don't teleport but if we had to, we would teleport from node 2 to node 2.

    Alternatively what the functions actually checks is all the possible XOR values starting from a and b. If there is a match answer is yes, else no. In our case we have 2 matches, 0 and 2.

    We get 0 at the start because that's our starting value and 2 by traveling from node 1 to 2. When starting from b we get 0 by traveling from node 1 to 3 and value 2 by traveling from 3 to 2.

    2.) dfs2 starts at node b

        if(dfs2(b, -1, 0)) cout << "YES\n";
        else cout << "NO\n";
    

    It checks if it can hit any optimal values calculated in dfs1. In dfs1 all possible values are calculated where we have the limitation in place.

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

Awesome round.

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

any idea why I'm getting wa2 in e?

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

    It getting WA because of two things:-

    1. You need to handle the case in which you will not do any operations.

    2. Function cal() needs to return long long, because in the worst case ans = (10^10), this will cause overflow of course.

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

someone please explain "Also, we cannot pass b on the path from a→c." in tutorial of G.

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

    It refers to the fact we cannot pass through the node b (end node) when starting dfs from a. Because it's impossible to enter b without our value being 0.

    See the problem statement

    you are allowed to enter node b if and only if after traveling to it, the value of x will become 0.

    In other words, you can travel to node b only by using an edge i such that x XOR wi=0.

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

Can somebody please explain why i am getting WA 182023139 in G . I am storing xor from starting to ending node in a variable then from every node we can go to ending node . and xor will be xor of node from starting to xor of starting to end and then checking if it exists in our set or not.

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

In 1760D - Challenging Valleys you could also insert INF at the start and at the end of the processed array (without successive equal elements) to simplify the job with edges, then check every triplet inside for a[i-1]>a[i]<a[i+1], 182365763

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

Could someone point out where my code fails? ato[i] is xor value of path from a to i. 184338941

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

why does this code fails 185000763 for G.

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

why my solution is failing on test 2 iteration number 53 code

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

Problem G unordered_set gives TLE but set solution Accepted. I also faced similar issues in other problems as well. Can anyone explain me why this is happening ?

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

I have tried stress testing my solution to problem G, and I haven't yet been able to find a test case that breaks my code. Can anyone please take a look at this? 188826168

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

Anyone could help me look at what's the problem of my solution 193477917 for G.

Thanks a lot in advance!

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

202246684 Can anyone tell me why this submission for problem G fails for test 53?

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

why the Problem G can't solve with at most once dfs ?

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

[submission:https://codeforces.com/contest/1760/submission/211710923]

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

For problem D --> why can't I use this method for taking input ? int n; cin >> n; vector a(n); for(int i=0;i<n;i++){ cin >> a[i]; } for this method it doesn't satisfy the sample i/o. exactly why I have to use this one ????--> int n; cin >> n; vector a; for(int i = 0; i < n; i++) { int x; cin >> x; if(i == 0 || x != a.back()) { a.push_back(x); } }

»
9 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
// This is a easiest way to solve this problem
// Time complexity O(N) Space complexity O(1)

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int test, a,b,c, mx, mn; cin >> test;
    while(test --)//Time complexity O(N) and space complexity O(1)
    {
        cin >> a >> b >> c;
        mx = max(a, max(b,c));
        mn = min(a, min(b, c));
        if(a != mx && a != mn) cout << a << endl;
        else if(b != mx && b != mn) cout<< b <<endl;
        else cout<< c <<endl;
    }
    return 0;
}


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

G is the best problem

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

I have used a dfs approach for G,but my code is not working,can someone help me with the debugging the following submission, https://codeforces.com/contest/1760/submission/226432202

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

G :/

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

t = int(input()) for _ in range(t): n = int(input()) s = input() a = "abcdefghijklmnopqrstuvwxyz" l = max(s, key=ord) print(a.index(l)+1)