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

Автор saarang, история, 4 года назад, По-английски

Thank you for participating in our contest! We hope you enjoyed it.

1627A - Not Shading

Hint 1
Hint 2
Solution
Implementation (C++)
Implementation (Java)
Implementation (Python)

1627B - Not Sitting

Hint
Hint Solution
Solution
Implementation (C++)
Implementation (Java)
Implementation (Python)

1627C - Not Assigning

Hint
Solution
Implementation (C++)
Implementation (Java)
Implementation (Python)

1627D - Not Adding

Hint
Solution
Implementation (C++)
Implementation (Java)
Implementation (Python)

1627E - Not Escaping

Hint
Hint 2
Hint 3
Solution
Implementation (C++)
Implementation (Java)
Implementation (Python)

1627F - Not Splitting

Hint 1
Hint 2
Hint 3
Solution
Implementation (C++)
Implementation (Java)
Implementation (Python)
Author Notes
Разбор задач Codeforces Round 766 (Div. 2)
  • Проголосовать: нравится
  • +238
  • Проголосовать: не нравится

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

saarang orz

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

As a rated account on CF, I hope you enjoyed giving this round officially as much as I did testing it!

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

This was an awesome contest with well balanced problem (Difficulty wise). Really enjoyed the problems. Looking forward to your future contests.

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

Editorials with Hints listed first are best :)

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

Can someone share the C++ implementation for problem C?

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

When hacking solutions, how do I filter out submissions that have been made after the contest has finished?

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

Something about E:

Most solutions using SPFA fail on test 31. However, using mcfx optimization I can get AC. (142882998)

Can anyone hack this solution?

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

    First of all, orz orz orz.

    But I don't think the solution you linked uses mcfx optimization. AFAICT, mcfx optimization counts the number of times a node has been pushed into the queue, and if it's in $$$[2; \sqrt V ]$$$ then it push_front's otherwise it push_back's. It speeds up your solution from 1637 ms to 343 ms!

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

To put it in the language of problem setter, this contest was NOT BAD ( ;

»
4 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится
As far as I remember, problem C is a Google online coding challenge problem. (I am not sure though) But I was not able to solve it at that time and i solved it today, soo...
»
4 года назад, скрыть # |
 
Проголосовать: нравится +10 Проголосовать: не нравится

Auto comment: topic has been updated by saarang (previous revision, new revision, compare).

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

Problem B actually has a O(N+M) solution, though it is more complicated. I have spent one hour working out this O(N+M) solution because I misunderstood the hell limits of N and M!!!!!!!!!!!!!! Just didn't notice that sum of all N*M does not exceed 1e5 (it really sucks :( Then I have not enough time to submit problem C and I was almost there...

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

Why in E does a dijkstra not work, for example maintain the state of dp[i]=max health after haveing used ladder i. Are there that much edges, or why does such aporach goes TLE?

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

Isn't the time complexity of D $$$(n + Alog^2(A))$$$. One log due to iterating over multiples of first A numbers and another due to gcd.

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

I want to know whether i can use spfa to solve E, i get Time limit exceeded on test 31 o(╥﹏╥)o

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

Not super exciting because it's silent, but I uploaded a screencast here (I solve all problems & get 15th place): https://www.youtube.com/watch?v=aWAafRRPS2M

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

grid-Forces

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

Nice contest

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

Here are my video solutions for people looking for video format.

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

I did almost the same in D as in editorial, but it gives TLE.

142886157

My solution does $$$(10^6-n)*n*\log_2 10^6$$$, which is at worst $$$40*10^6$$$ operations.

Please help

UPD: got it, nevermind

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

Can't F be solved using DP? I couldn't get any mistake after almost 1 hour.

dp[i][j] -> if we have processed first and last i rows of the grid and the last segment (among k+1 segments at every row) is j assuming that all pieces are considered that don't go out of first i, last i rows, and the vertical pieces in ith and (i+1)th row are to be considered. After that transitions are pretty simple.

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

Where were Anjali when Tina was being so harsh on Rahul.

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

I had used the DP solution for Problem E but I am getting WA. Can someone tell the test case on which it fails or tell me why it is wrong?

Solution

I had used DP for storing (i,j) state result which is in map m2. map m1 I am using for getting ladders for that key row.

NOte: I used the same approach but without storing state (i,j) and got TLE at 4th Test case. Solution

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

In problem E I tried using recursion and travelled from one ladder to another and then tried to memoize it by making a HashMap of (string of row and col). I know this might give TLE but why is it giving wrong answer. If anyone knows please help. My submission 142888967

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

For B i just simulate backward and figure out we will have the max distance to the 4 corners

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

.

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

Became Expert after 111 contests. Thanks for the contest. Happy coding everyone.

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

Seeing as no one has commented about this, we can take advantage of the fact that E has no negative cycles to still run dijkstra on it. We can use the technique of potentials in a graph, see Monogon's blog. We dont need any fancy assignment of potentials based on the edges, the idea that all nodes on floor $$$i$$$ have potential $$$10^{12} - i * 10^6$$$ is enough to get ac.

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

In-contest submission 142874701 using 'map' TLE's but AC's on using 'unordered_map' 142897768 rest of the code exactly same :(

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

In E, I didn't think of DP but instead applied dijkstra with the same idea that we need at most 2k + 2 nodes. but It got TLE but I don't know why exactly. Isn't the complexity for Dijkstra O(V+ELOG(V))? with V up to 2k+2 and E up to 2k. shouldn't it work?

This is my submission: 142877138 (I also tried with vectors instead of sets but it got TLE too)

I know weights of ladders are negative but there can't be any cycles since all ladders are in one direction? is there something I'm missing?

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

So am I the only one who solved B with BFS from corners? Such a shame but at least I got it.

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

Another (a bit more complicated) solution for $$$D$$$:

Iterate from $$$i=10^6$$$ to $$$1$$$, if $$$i$$$ does not exist yet and it is a gcd of $$$2$$$ existing multiples greater than it, mark it as existing and increment the answer.

To check if $$$i$$$ is a gcd of $$$2$$$ greater exising multiples, get all existing multiples of $$$i$$$ after dividing them by $$$i$$$, lets call this group of values $$$grp$$$, if $$$grp$$$ has a co-prime pair then $$$i$$$ is a gcd of $$$2$$$ greater existing multiples.

To check if there is a co-prime pair within $$$grp$$$ we can get the count of all pairs within $$$grp$$$ with any common divisor > $$$1$$$ using inclusion-exclusion and mobius function, let's call this count $$$cnt$$$, a co-prime pair exists if $$$cnt$$$ is less than the count of all possible pairs in $$$grp$$$.

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

    In fact there is no need for mobius (take a look at my submission). The number of pairs with gcd $$$k$$$ is the number of pairs with both elements divisible by $$$k$$$ minus the number of those with gcd $$$2*k$$$, $$$3*k$$$...

    Other parts of your idea still stay the same.

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

      Hey, I looked at your submission for Problem D. And, I have a doubt regarding the same.

      Can you please tell that, why the following loop is being added inside the if condition?

                            if(cnt[i]>=1&&!a[i])
                            {
      			res++,a[i]=1;
      			for(int j=2*i;j<=(int)1e6;j+=i)
                              {
      			  cnt[i]+=a[j];
      			}
      		      }
      

      Thanks in advance!

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

    Another interesting way to check if there is a coprime pair within $$$grp$$$ is to randomly take about 300 pairs of integers in $$$grp$$$ and see if any one of the pairs are coprime. (I don't know how likely it will fail though, at least it passed system tests. Would appreciate a lot if someone can tell the probability of my approach failing)

  • »
    »
    3 года назад, скрыть # ^ |
    Rev. 3  
    Проголосовать: нравится 0 Проголосовать: не нравится
    Soltuion of D:
    
    Brother. I do like
    for i,  let some of its multiples are present  k1*i , k2*i , k3*i ....
            Now, i grp all of them like :  (k1 k2 k3 ....)
            __gcd(k1*i , k2*i) = i,  if __gcd(k1 ,k2) = 1
            Now we need to find if there exist two coprime in it or not
            (Without Proof) i take gcd of this group, 
                            if (gcd==1)  then there exist a co prime pair (kx, ky)
             
    
    Can you Prove this that , gcd==1 only if there is a coprime pair??
    UPD: I got it why my guess work:
         since i have freedom to combine two number
        (k1,k2)->knew by gcd  (knew,k3)->knew by gcd .....  since i know gcd is decreasing
         at last we have a k' . if this is not one then we are not able to make.
        
    
»
4 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

It's a good contest, but E may be too easy. hope to see a more interesting problem next time.

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

I've solved B using bfs, there's a pattern in the distance when you go over k=0,1,2...n*m-1 the minimum distance Rahul can achieve starts at the beginning and then it propagates to it's neighbors increasing by one

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

My solution for Problem D: First I calculate gcd of every pairs in the array using exculsion dp. Then I calculate gcd of every pairs again with the initial array and new gcds (that calculated from the previous step). After doing it 3 times, I found the answer of all the new gcds that can be calculated at most.

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

In problem C Can someone tell me why I am getting a runtime error in test case 2? My logic is completely right. I am not able to figure out why it's giving a run time error. Please Help !!!!!!!!!!!!!!

Code link:https://mirror.codeforces.com/contest/1627/submission/142916147


#include <iostream> using namespace std; #include<bits/stdc++.h> void dfs(ll i ,vector<pair<int,int>>*adj , bool *vis ,ll c, map<ll,ll>&mp ) { vis[i]=true; for(auto it:adj[i]) { if(!vis[it.first]) { mp[it.second] = c; if(c==5) { c=2; } else { c=5; } dfs( it.first ,adj ,vis,c,mp); } } } void solve() { ll n;cin>>n; vector<pair<int,int>>adj[n+1]; map<pair<ll,ll>,ll>p; map<ll,ll>mp; ll deg =0; ll start; for(int i=0;i<n-1;i++) { ll x,y; cin>>x>>y; x--; y--; adj[x].push_back({y,i}); adj[y].push_back({x,i}); if(adj[x].size()>=3 ||adj[y].size()>=3 ) { cout<<-1; return ; } } for(int i=0;i<n-1;i++) { if(adj[i].size()==1) { start =i; break; } } bool vis[n+1]; memset(vis,false,sizeof(vis)); dfs(start ,adj ,vis ,(ll)2,mp); for(int i=0;i<n-1;i++) { cout<<mp[i]<<" "; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll t; cin>>t; while(t--) { solve(); cout<<"\n"; } }
»
4 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

someone please tell me why I am getting TLE on third test case with almost the same solution as in tutorial for problem D my code

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

In problem C What is the meaning of this sentence A path should not visit any vertex twice

Can someone please give me an example where a path visit a vertex twice.

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

can anyone explain me which seats do Tina paint if she plays optimally? I find the solution incomprehensible.

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

Why the problem D's solution takes $$$O(n+AlogA)$$$? We can see that find the multiples of all $$$1\le x\le A$$$ needs $$$O(AlogA)$$$. And gets the gcd of them needs $$$O(logA)$$$ ,too. So it should take $$$O(n+Alog^2A)$$$ in deed.

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

Why is the complexity of problem D O(n + AlogA) and not O(nlogn + AlogA) as the two for loops for finding multiples will take upto nlogn and then AlogA extra factor for gcd computations?

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

In problem E, I use set to save best pairs of {room, cost} for each floor. We iterate over each floor starting from floor 1, and maintain the set by stack-like insertions. 142931555

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

the time complexity of problem B in edutorial is O((mn)*logmn) but i think it should be O(mn+log(mn)).. maybe a typo..

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

Problem D can be solved in $$$ O(n + A log log A) $$$ using multi-demension prefix/suffix sum technique

143170028

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

I use DP for E where each rooms are represented as a single DP state. But seems I got wrong in the implementation part. Any idea what I might be missed?

My implementation : 143189459

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

    UPDATED : It got AC when I try to change them to bottom up. It seems I got the problem :

    1. A room can have more than 1 ladder (which in this submission, I haven't handle the case)

    2. Not sure to proof it, but I think it's a flaw from my DP idea :

    • Assume we can reach room $$$U$$$ and have got the minimum cost by moving from $$$U$$$ to the final room. Assume the distance from $$$U$$$ from final node is $$$D$$$

    • But then, there can be more optimal path that might goes through a path that has a lot of ladders (which in this case, we "gain" more health and "lose" $$$X$$$ points, which I will denote this as $$$-X$$$) and eventually we reached back to node $$$U$$$

    • it can be seen that $$$D-X$$$ might be smaller than $$$D$$$ therefore the DP answer can be updated — which I can't do that in top down because once I visited state $$$U$$$, then I will have $$$dp[U]$$$ fixed and never be updated, while in fact it needs to be updated

    Changed to bottom up like the editorial just did and it seems to solve the problem

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

Problem can also be solved using breadth first search technique in time complexity of O(NM). My solution: 143357988 Thanks

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

Can problem E be solved using djakstra ??

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

1627E - Not Escaping why 143474919 wrong answer 23 ? what's wrong?

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

can someone pls tell me what is wrong in this solution I am unable to understand it. problem — 1627D — Not Adding — https://mirror.codeforces.com/contest/1627/problem/D solution — https://mirror.codeforces.com/contest/1627/submission/143931841 @saarang


vi p(1000005,0); void solve(int t){ int n; cin>>n; int mx=-1; rep(i,1,n){ int tmp; cin>>tmp; p[tmp]=1; mx = max(mx,tmp); } int ans=0; repi(i,mx,1){ if(p[i]) continue; int cnt=0; for(int j=i*2;j<=mx;j+=i){ if(p[j]) cnt++; } if(cnt>=2){ ans++; p[i]=1; } } cout<<ans<<"\n"; }
»
4 года назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone please check why my this solution for problem E using dijkstra gets TLE on test case 8.I have not used negative edges and used dijkstra for each row separately.

Link to my submission:- https://mirror.codeforces.com/contest/1627/submission/144133636

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

Can someone explain the dp for E? It is not clear to me what are the states and how they are updated efficiently.

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

Why my code TLEs for problem E? Please help I have used shortest path approach using dijkstras algo. I have written comments for easy understanding of code. Complexity for my code is O(klogk) imo. 147170375