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

Автор flamestorm, 2 года назад, По-английски

We hope you enjoyed the contest!

1915A - Odd One Out

Idea: flamestorm

Tutorial
Solution

1915B - Not Quite Latin Square

Idea: flamestorm

Tutorial
Solution

1915C - Can I Square?

Idea: SlavicG

Tutorial
Solution

1915D - Unnatural Language Processing

Idea: flamestorm

Tutorial
Solution

1915E - Romantic Glasses

Idea: flamestorm

Tutorial
Solution

1915F - Greetings

Idea: mesanu

Tutorial
Solution

1915G - Bicycles

Idea: SlavicG

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

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

This was a standard div 4, I believe previous div 4s were harder than this. Good contest regardless!

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

For anyone who wants to know more about similar problems like G.

Here is a great blog on shortest path modelling:

https://mirror.codeforces.com/blog/entry/45897

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится
for _ in range(int(input())):
    n=int(input())
    arr=list(map(int,input().split()))
    newarr=[]
    sl=0
    sd=0
    for i in range(n):
        if i%2==0:sl+=arr[i];newarr.append(sl)
        else:sd+=arr[i];newarr.append(sd)
    
    # print('newarr',newarr)
    
    d={0,-arr[0]}
    f=False
    for i in range(n-1):
        if i%2==0:
            diff=newarr[i+1]-newarr[i]
            if diff in d:f=True;break
            else:d.add(diff)

        else:
            diff=newarr[i]-newarr[i+1]
            if diff in d:f=True;break
            else:d.add(diff)

        if f:break
    if f:print("YES")
    else:print("NO")

        

why does this give TLE in E?anyone

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

int the third question if i put r = sum(the sum of all number) instead of 1e9 then it fails in testcase 3. why is that happening?

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

I kept my promise, I can die with a clear conscience.

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

Can someone please tell where i am going wrong in this solution wirtten by me for G. 239395920

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

why my soln of F fails for this test case:

from bisect import * I=lambda:map(int,input().split()) rr=range for _ in rr(int(input())): n,=I() an=0 l=[list(I()) for _ in rr(n)] l.sort() k=[] for i in rr(n): ind=bisect(k,l[i][1]) an+=len(k)-ind k.insert(ind,l[i][1]) print(an)

test case: 1 200000 -999999999 999999999 -999999998 999999998 -999999997 999999997 -999999996 999999996 -999999995 999999995 -999999994 999999994 -999999993 999999993 -999999992 999999992 -999999991 999999991 -999999990 999999990 -999999989 999999989 -999999988 999999988 -999999987 999999987 -999999986 999999986 -999999985 999999985 -999999984 999999984 -999999983 999999983 -999999982 999999982 -999999981 999999981 -999999980 999999980 -999999979 999999979 -999999978 999999978

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

here is my code for f using binary index tree. https://mirror.codeforces.com/contest/1915/submission/239413594

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

Why my E's solution got hacked for this testcase? Both the editorial's and my solution is giving "NO" as output.

int main(){
    cout<<"1\n200000\n";
    for (int i = 0; i < 100000; ++i)
    {
        if (i != 0) cout << ' ';
        cout << "1 107898";
    }
    cout <<endl;

    return 0;
}

My code:

int main() {
    tc {
        ll n; cin >> n;
        vector<ll> v(n);
        for(int i = 0; i < n; i++) cin >> v[i];
        if(n == 1) {
            cout << "NO" << endl;
            continue;
        }
        bool flag = false;
        for(int i = 0; i < n - 1; i++) {
            if(v[i] == v[i + 1]) {
                flag = true;
                break;
            }
        }
        if(flag) {
            cout << "YES" << endl;
            continue;
        }
        flag = true;
        unordered_map<ll, int> mp;
        ll pre = 0;
        for(int i = 0; i < n; i++) {
            if(i & 1) pre += v[i];
            else pre -= v[i];
            if(pre == 0) {
                cout << "YES" << endl;
                flag = false;
                break;
            }
            mp[pre]++;
        }
        if(flag) {
            for(auto it : mp) {
                if(it.second > 1) {
                    cout << "YES" << endl;
                    flag = false;
                    break;
                }
            }
            if(flag) cout << "NO" << endl;
        } 
    }
    return 0;
}
»
2 года назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone give the reasoning, on why a normal dijkstra won't work for problem G ? We are asked to find the path that takes minimum time from node 1 to node n.

can't we just update the distance to a node based on this? d[v] > d[u] + min(k, s[u]) * w[u][v]

why are we storing d[v][s] ? why do we need 2d array to calculate shortest distance to node v using slowness s

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

Can anyone help in question 6. How is this standard problem

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

    Basically we just want to count how many pair (i, j) that a[i] > a[j] and b[i] < b[j]. Because this is the necessary and sufficient condition when people i and people j meet at the same point.

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

e can also be solved easily by creating prefix sum array for even and odd indices we need to find even[j]-even[i]=odd[j]-odd[i] which is equivalent to even[j]-odd[j]=even[i]-odd[i] so just store the difference array of even and odd prefix sum if any value repeats answer is "yes" else "no"

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

Can anyone please explain why the below solution fails for problem G:

Approach: A slight modification of the Dijkstra's Algorithm. We calculate DIS [ i ] [ j ] to be the shortest path to reach from node 1 to node ' i ' with bike of slowness factor ' j ' . Update the DIS array when a new path having shorter distance is encountered with bike of same slowness factor.

Submission link 239402842

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

    You are updating mins. But when going a -> b where mins at a was 10 and b would become 5, you are calculating time using 5 * edge weight. Note that to reach b you should use 10 * edge weight, use updated mins after that.

    Edit: code here's mine with the same idea

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

I can't understand the sentence in the E problem, " Be careful about using hash tables, as they can be hacked.". Could anyone explain this?

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

Can someone show me how to solve F without ordered_set? Maybe with Fenwick tree or something else?

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

Why this code for problem c is not working? (https://mirror.codeforces.com/contest/1915/submission/239353207)

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

Can someone hack this solution for problem G? 239328319

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

If I get a input for which like everyone gets hacked due to TLE, i can only apply in a contest to a limited people but whosoever i put they get hacked,

Shouldn't there be system testing for all the successful hacks afterwards with those inputs on all users???

Ain't it unfair?

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

good contest but some solutions for g should not be accepted due to overflow

look at this one

https://mirror.codeforces.com/contest/1915/submission/239403382

curr is int and it should be long long upd: didn't know system testing hadn't been finished

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

Can anyone explain why me solution got AC? it has a time complexity of n^3 https://mirror.codeforces.com/contest/1915/submission/239328701

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

in this Submission,the verdict says wrong answer and the reason is wrong output on the testcase

ABC ?CA CAB (4th case of test 2)

However , my program works exactly well with this testcase as well .

Is there any error from codeforces side?

  • »
    »
    2 года назад, скрыть # ^ |
    Rev. 4  
    Проголосовать: нравится +3 Проголосовать: не нравится
    Code where error exists
    Hint
    The error
    Solution
»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Damn, my solution for the E got hacked because I was using the unordered_map :( I know how it can be fixed just so funny that I was getting caught on this again

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

[1st submission][2nd submission]

these are my two submissions to problem F , the only difference between these two submissions is that one has (#define int long long) and in other submission I commented that line , the one with (#define int long long ) got TLE whereas the submission without this got AC, why so and how can we avoid this TLE?

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

Can we use DP in any way to find the solution for problem G?

I know we can go with some modification of dijkstra's algorithm, but was wondering if any one has done it differently.

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

can anyone explain how can i find number of pairs of segment such that one completely lies inside anohter , inorder to solve F

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

For problem F, after sorting, its actually just Kendall tau distance, which can be solved in O(nlogn) with segment/fenwick trees or with some divide and conquer techniques, related to merge sort and quick sort

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

Can someone explain to me why do I get TLE when I use unordered_set in PROBLEM G? When I changed it to set, I got AC.


void solve() { std::cin >> n; std::vector<int> a(n); for (int i = 0; i < n; i++) std::cin >> a[i]; long long s = 0; std::unordered_set<ll> st; st.insert(0); for (int i = 0; i < n; i++) { a[i] *= ((i % 2) ? -1 : 1); s += a[i]; if (st.find(s) != st.end()) { std::cout << "YES\n"; return; } st.insert(s); } std::cout<<"NO\n"; }
»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

We can solve problem F by using merge sort algorithm.

You can count the number that smaller the current number and before it in the second element of pair after sort it by nondecreasing sort.

This is the code: https://mirror.codeforces.com/contest/1915/submission/239384276

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

Problem-F can also be solved using Fenwick tree? If yes, then can someone please explain the solution using Fenwick tree.

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

    i assume that you know that in problem can be simplified to counting the inversion in an array to do this you can assign each element A[i] to it's index in sorted order like A = {5, 7 3, 2, 9} sorted A = {2, 3, 5, 7, 9} now you assign each element to it's relative index like (5 => 3), (7 => 4) and so on now A becomes A = {3, 4, 2, 1, 5} notice that all the elements in A are now between 1 and n. now assume you have visited array vis and a an array B which stores the prefix sum of vis array now iterate on A in reverse order and add B[A[i]] to you'r ans. why? it's because if you see carefully B[i] stores no. of elements < A[i] that are already visited and update the vis array vis[A[i]] = 1 and recalculate the prefix sum array B. the complexity of this O(n^2) now you can do the same thing in fenwick tree using point updates so it will be O(n*long(n)). you can learn fenwick tree from here hope it helps and sorry for my bad english.

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

please dont freeze the contest need to upsolve it not able to submit code now

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

Problem C, why sqrt doesn't get TLE?

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

IN problem E. Romantic Glasses TEST case 6: 2 5 10 4 4 9 6 7 8

why answer "YES"

there is no possiblity of equal subarray from (l,r)???????????????????

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

In problem E, can someone explain why using a hashtable leads to a TLE/hack?

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

has anyone solved F without using segement or fenwick tree in python? using bisect insort to keep a sorted list gives TLE on 11

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

Need help with C. Took inputs as long long, added them. Take l=0, r=total, k=0. While(l<=r){ m = l+(r-l)/2; if(m*m <= total) k=m, l=m+1; else r=m-1; } At the end if(k*k != total) print No else print Yes

I am getting Wrong Answer at case 3. Please, point out what I did wrong. Submission

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

Can someone please look at the following code snippet (for problem 1915-F), and tell me why it gives TLE on test case #3: (And please bear with my excessive usage of macros, i hope you understand what the code is doing.)

bool comp(pair<int64, int64> a, pair<int64, int64> b) {
	return a.first < b.first;
}
 
void solve()
{
	inp(n);
	multiset<int64> mst;
 
	vector<pair<int64, int64>> ab(n);
 
	lp(i, n) {
		inpp(ab[i].first, ab[i].second);
	}
 
	sort(all(ab), comp);
 
	uint64 ans = 0;
 
	lp(i, n) {
 
		ans += (uint64)distance(mst.upper_bound(ab[i].second), mst.end());
		dbg2(i, ans);
		mst.insert(ab[i].second);
 
	}
 
	out(ans);
	return;
}
»
2 года назад, скрыть # |
 
Проголосовать: нравится +33 Проголосовать: не нравится

First of all thanks for the nice problemsetting. SlavicG, mesanu, flamestorm

I have a slightly different solution for problem G, here is how it goes:

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

In the tutorial of F, the explanation says "for each of them add the number of segments that we have already passed that have an a value larger than the one of the current segment." Shouldn't it be smaller instead of larger as we have B sorted in increasing order ? Edit: I got it

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

In Problem E can anybody tell why solution gives TLE when I use unordered map and gets accepted when I use map

void solution(){ ll n; cin>>n; vector a(n); unordered_map<ll,ll> mpe; ll sume = 0,sumo = 0; for(auto &it:a) cin>>it; for(ll i = 0; i < n; i++){ if(i % 2 == 0){ sume += a[i]; }else{ sumo += a[i]; }

    ll diff = sumo - sume;
    if(diff == 0 or (mpe.find(diff) != mpe.end())){
       cout<<"YES"<<endl;
       return;
    }

    mpe[diff] = i;
}

cout<<"NO"<<endl;

}

int main(){ ll t; cin>>t; while(t--)solution(); return 0; }

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

A more efficient (and prolly simpler) code for G as it does not require a 2D array: https://mirror.codeforces.com/contest/1915/submission/239576580

It's just Dijkstra with time as the parameter, but when I push cities into the priority queue, I also store the minimum slowness with which I will leave the city, once I visit it (the minimum between the slowness with which I entered the city and the slowness of that city's bike). Instead of a "visited" array, I made an array which stores the minimum slowness with which I have left the city, if I have visited it. So when I visit a city again, I carry on with that iteration only if the slowness with which I am entering the city is strictly lesser than the minimum slowness with which I have exited the city before, otherwise I continue with the next iteration.

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

Can someoone teach me G in a little more detail I don't know much about creating a new graph with state data.

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

My G did not withstand system test.

https://mirror.codeforces.com/contest/1915/submission/239379244

Subtle bug in Djikstra implementation, try to find it if you like. (Note: I know what it is and I think it was a great way to learn something through the contest, won't make the same mistake again now.)

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

why is unordered_map not working but normal map working?

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

Can anyone help me to find what's wrong with my solution please? (My code for 1915E — Romantic Glasses). It's getting wrong answer on test case 3. My code is in JAVA. I've tried my best but can't find anything :(

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

    In your last for loop, compare in the following way because on comparing def.get(i)==def.get(i-1), it is comparing the references not the value.

    for(int i=1;i<=n;i++){
       long a=def.get(i);
       long b=def.get(i-1);
       if(a==b){
       ans = "YES";
       break;
     }
    }
    
»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone explain why Dijkstra in G problem is not getting TLE verdict? We have n^2 vertices, for each vertex we traverse m edges, which should be at least O(n^2 * m) yet author's solution's passing with no probs. Where my logic gone wrong? Or I guess don't understand Dijkstra at all LOL

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

    Dijsktra's time complexity, when using priority queue is O(E + VlogV), you can refer to the : proof here

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

    This is a good question, and flamestorm or SlavicG should've presented a more elaborate complexity analysis in the editorial.

    We have $$$V = 1000n$$$ vertices, but we do not traverse all $$$m$$$ edges for each vertex. Instead, each edge $$$(u, v, w)$$$ is traversed at most $$$2000$$$ times: once from each of the vertices $$$(u, 1), (u, 2), \ldots, (u, 1000)$$$ and $$$(v, 1), (v, 2), \ldots, (v, 1000)$$$. Hence, the total number of edges is $$$E \le 2000m$$$.

    The time complexity of Dijkstra's algorithm as implemented in the editorial is $$$O((E + V) \log V)$$$, which for $$$n = m = 1000$$$ has an upper bound of roughly $$$60 \cdot 10^6$$$.

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

In tutorial of 1915E what does the author mean by hash map can be hacked?

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

In problem F how can we actually solve it without using order_of_key, with some implementations I did try to use lower bound it is still TLE. Here is my code https://mirror.codeforces.com/contest/1915/submission/261686634

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

My approach for problem E was to create a presum[] which stores the value of both till that index then I can find the is any difference has appeared before then ans is yes else no but I dont know why it is wrong . please help me to find the flaw in my logic .

include<bits/stdc++.h>

using namespace std ; void solve(){ int n ; cin>>n; vector nums(n); map<int,int> mpp; mpp[0] = 1; vector pre(n); cin>>nums[0]; if(n == 1){ cout<<"NO\n"; return ; } cin>>nums[1]; pre[0] = nums[0] ; pre[1] = nums[1]; bool flag = false; mpp[nums[1]-nums[0]] = 1; mpp[-1*nums[0]] = 1; if(nums[0] == nums[1]){ flag = true; } for(int i = 2;i<n;i++){ cin>>nums[i] ; pre[i] = nums[i] + pre[i-2]; if(mpp.find(pre[i]-pre[i-1]) != mpp.end()){ flag = true; } mpp[pre[i]-pre[i-1]] = 1; } // for(int i=0;i<n;i++) cout<<pre[i]<<" "; //cout<<"\n"; if(flag) cout<<"YES\n"; else cout<<"NO\n"; } int main(){ int tt; cin>>tt; while(tt--) solve(); }` ~~~~~ Your code here... ~~~~~

`

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

The tutorial has the solution, but where can we find the proof of correctness, why can't the solution TLE, meaning why don't we need to compute for all the times a node is reached with all the bikes, so it would be

Times (can be from minimum 0 to maxTime, with slowest bike from 0 to n) * Nodes* bicycles (slowness factors), like how can we prove we are not eventually calculating all states, and pruning works correctly because we pick fastest bike,

I am just asking how to prove the solution to G problem, apart form intuition, is there any mathematical way possible.

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

I am a little confused about 1915F - Greetings. Why should a segment fully contain another one?

Lets consider this case

1
2 6
8 4

Both the first person and the second one will be at index 5 in the 3rd second, so the answer must be 1. But the given solution says 0. Can someone explain if I'm understanding it the wrong way?

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

can someone tell me more about ordered_set st; specifically about this ~~~~~

include <ext/pb_ds/assoc_container.hpp>

include <ext/pb_ds/tree_policy.hpp>

typedef __gnu_pbds::tree<int, __gnu_pbds::null_type, less, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> ordered_set; ~~~~~

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

For F. Greetings , This is the solution without using the policy based data structure and also manual coordinate compression using map.

This uses inversion Counting.

371322868 -> Fenwick tree

371326004 -> Segment tree