### wrg0ababd's blog

By wrg0ababd, history, 4 years ago, translation,

Simple implementation of solution described higher: http://ideone.com/IoSts1

• +145

 » 4 years ago, # |   +6 Auto comment: topic has been updated by alkurmtl (previous revision, new revision, compare).
 » 4 years ago, # | ← Rev. 2 →   0 I think Problem D constraints are very bad, because O((n + m) * n ^ 2 * k) solutions passed, but there is O((n + m) * n * k) solution...
 » 4 years ago, # |   +45 and using intervals instead of segments What is the difference between intervals and segments?
•  » » 4 years ago, # ^ |   +5 I think segment contains it's borders, interval doesn't
•  » » 4 years ago, # ^ |   -35 segments are empty if l > rbut intervals are empty if l >= r
 » 4 years ago, # |   +11 For Problem C, making the value of eps more precise changes verdict from WA to AC. But, aren't we supposed to take any difference < 1e-6 as zero, as it is mentioned in the problem statement as well, that even the judge will compare the answers with error limit = 1e-6?
•  » » 4 years ago, # ^ |   +18 It's not fair, i think, but in statement it says that it compares answers, but not anything else, printing -1 should be very precise :/
•  » » 4 years ago, # ^ |   +3 The problem is there was a case (40) where using a large epsilon would give -1 but the answer was actually 0.99998999989999903804, so the output is completely off from the answer.
 » 4 years ago, # |   +25 I thought that there has to be an equal number of vertices instead of leaves in E. The renaming of "leaves" and "vertices not being leaves" to "offices" and "departments" was strange. ;_;
•  » » 4 years ago, # ^ |   +7 Problems were confusing to be honest, I fell for it too.
•  » » 4 years ago, # ^ | ← Rev. 2 →   0 Same! I get that they try to wrap the problem in something so that it doesn't seem boring, but clarity should be the #1 priority always.It's a shame as well, I really liked the problem otherwise.
 » 4 years ago, # |   +5 I tried the following for problem C: Find a time Ti for each mice i in which the mice will be in the maze, using ternary search. Use binary search to find the lower and upper bound of the time, using Ti Do the intersection of the bounds and take the smallest time. But I got WA on test 30 because of the precision error. Can you find where I did the mistake? and How to find a good EPS which will meet the requirement.
 » 4 years ago, # | ← Rev. 2 →   0 here is my Solution for B i used almost exactly what you mentioned first (naive recursive). passed pretests but got TLE on test 10.adding the below code passes test 10 but TLE on 11. any help please?if(turns==2 && (dir=='u' || dir=='d') && y!=sj) return; if(turns==2 && (dir=='l' || dir=='r') && x!=si) return;
•  » » 4 years ago, # ^ |   0 26615391 : same solution. TLE on test 10.
•  » » » 4 years ago, # ^ |   0 Solved, i dunno why i had a for loop in my recursive method, thats what i thought id use just after reading the problem statement and i left it there even after changing my approach, my bad, i wish they had more difficult test cases in pretests, but still my bad :D
•  » » 4 years ago, # ^ | ← Rev. 4 →   0 My problem was TLE, and it's solved by using a boolean visiting 4D array to mark the position I have already checked with the direction and number of turns that I have checked the position with.
•  » » » 4 years ago, # ^ |   0 Your solution is 90% identical to mine what a coincidence :D however i didnt use the visited array and still it worked in less time even thought im using java, i dont think u need the visited array maybe u have to recheck ur base case conditions or the recursive function , or maybe stop it when u have done already 2 turns and ure not the same line or column of the destination, cause in that case ull need one more turn to get to dest, so stop it from the beginning when turns=2 and Tx!=x and Ty!=Y
•  » » » » 4 years ago, # ^ | ← Rev. 2 →   0 Here is my code 26655398. I have already put these return cases you mentioned. However, I'll back to this problem soon, thank you:D. Edit: please look back to my previous comment, direction instead of destination, sorry for this mistake.
 » 4 years ago, # | ← Rev. 2 →   0 It seems to me C is in something easier to solve this way(sorry if someone have alredy written about this): We used bisection method for minimal time calculate. Let start with L=0, R=10^11. Let us now be at time M = (L + R) / 2. Then it is easy to calculate for every mouse her coordinates Xi and Yi. Obviously when Yi >= Y2 and her y-speed >= 0, we must move the right border, since she will never be inside a rectangle. Similarly examine the three other cases:Yi <= Y1 and y-speed <= 0Xi <= X1 and x-speed <= 0Xi >= X2 and x-speed >= 0And we must move the right border, if all mouses are in the rectangle. If these conditions are not met, move the left border. When we have done all iterations, we have right border as an answer. We will get it, if all mouses were in the rectangle at the same time. Else deduce -1
 » 4 years ago, # |   +8 B has easier solution. Paint crosses on S and T with different colors (say 's' and 't'), then examine each line and each column if any of them contains /S\.*T/ or /T\.*S/ (c++26615732).
•  » » 4 years ago, # ^ |   0 Awesome idea! Thank for your share!
•  » » » 4 years ago, # ^ |   0 Similar idea: 1. Paint 's' to the left and to the right from 'S'. Same with 'T'. 2. Rotate whole field. 3. Repeat step "1". 4. Try to find S+dots+T or T+dots+S (ignoring case). 5. Repeat step "2". 6. Repeat step "4". Implementation in perl (26638162).
•  » » » » 4 years ago, # ^ |   0 Thank you for sharing the idea, although I can't read Perl. :)
 » 4 years ago, # |   +28 Why do you give him so many downvotes?After all he prepared the round and posted the editorial!
 » 4 years ago, # |   +17 The post is hidden because of too negative feedback, click here to view it
 » 4 years ago, # |   0 Same code shows two different verdict why?
•  » » 4 years ago, # ^ |   +1 The code works fine with C++14 but not with C++11 but I don't know why, maybe it's a precision-related issue.
•  » » » 4 years ago, # ^ |   0 Hmm ,THis is problem with Language c++11 and c++14
 » 4 years ago, # | ← Rev. 3 →   0 For problem B, I got WA on test 62. Here is my submission: 26615111Can anybody help me ? What's wrong in my code ?Update: I have found my bug. I was using visited array as vis[row][col][mov], using visvis[row][col][dir][mov] got AC. Here is my AC code: 26632674
 » 4 years ago, # |   0 I have written dfs solution for Problem B but it has got WA for test case 24. My Code#include using namespace std; string s[1000]; bool vis[1000][1000][4]; int dx[]={0,1,0,-1}; int dy[]={1,0,-1,0}; int n,m,sx,sy; bool is_turn(int px,int py,int tx,int ty){ if(abs(px-tx)==1&&abs(py-ty)==1) return true; return false; } bool is_valid(int x,int y,int i){ if(x==n||y==m||y<0||x<0) return false; if((s[x][y]=='.'||s[x][y]=='T')&&!vis[x][y][i]) return true; return false; } bool dfs(int px,int py,int x,int y,int t,int in){ if(t>2){ return false; } if(s[x][y]=='T') return true; vis[x][y][in]=true; bool tr=false; for(int i=0;i<4;i++){ int tx=x+dx[i]; int ty=y+dy[i]; if(is_valid(tx,ty,i)){ if(is_turn(px,py,tx,ty)) tr|=dfs(x,y,tx,ty,t+1,i); else tr|=dfs(x,y,tx,ty,t,i); } } return tr; } int main(){ scanf("%d%d",&n,&m); for(int i=0;i>s[i]; } for(int i=0;i
•  » » 4 years ago, # ^ |   +5 you can visit cell before but with more turns, you need to add number of turns in used. 26637049.
•  » » » 4 years ago, # ^ |   0 thank you so much :)
 » 4 years ago, # |   -18 I don't like this round...
 » 4 years ago, # |   -11 In problem B, I used DP to get the minimum number of direction changes to reach 'T' from a given cell.The DP state is: (row, column, current direction).If the starting cell can reach 'T' in 2 direction changes or less, then the answer is YES.This code gives me WA.Is there a logical problem with my method?
•  » » 4 years ago, # ^ |   +9 You essentially do a DFS instead of a BFS, so when you find a path, it is not necessarily the shortest one.In other words, such a dynamic programming solution should care about the order of visiting the cells, and yours doesn't.
 » 4 years ago, # |   0 How can problem B be solved just using 3(currx,curry,dir) states in case of bfs whereas for recursive dfs I need 4 states (currx,curry,dir,turn). I couldn't understand this part can someone explain it to me . Thank you
•  » » 4 years ago, # ^ |   0 Using 0 - 1bfs, a bfs on a graph where the edges weights are 0 or 1. This BFS instead of using a queue, uses a deque (double ended queue). If you change the direction, then this edge has weight 1, otherwise it has weight 0. If you go through an edge with weight 1, push it to the end of the deque, otherwise push it to the front.While doing the BFS, always pop the front of the deque
 » 4 years ago, # |   +8 For Problem G,could anyone please tell me the detailed way to cut the field into O(n) rectangles without deleted cells? I can't understand the way mentioned in the editorial.Thanks!
•  » » 4 years ago, # ^ |   +11 In my solution, I did a scan line, maintaining a partition of (currently) non-deleted rows into ranges in a map. There will be two types of events: Some deleted rectangle appears. In this case, we need to remove all the ranges that overlap with this rectangle's range (and output a new non-deleted rectangle) and create up to 2 new non-deleted ranges to the left and to the right of deleted range. For example, partition before: [.1.][.2.]....[.3.]...[..4..][.5.] new deleted range: .......[................]......... partition after: [...][]..................[..][...] The algorithm will create non-deleted rectangles for ranges 2, 3 and 4 and create two new ranges as shown above. Some deleted rectangle disappears. In this case, we simply create a new non-deleted range. For example, partition before: [...][]..................[..][...] old deleted range: .......[................]......... partition after: [...][][................][..][...] My solution for reference (submit).
•  » » » 4 years ago, # ^ |   0 Thanks a lot!
 » 4 years ago, # |   0 Can E be solved for N ≤ 106? It's possible to calculate knapsack for this constrains.
•  » » 4 years ago, # ^ |   +23 Yes, should be doable in O(n*(sqrt(n)+log^2(n))) with FFT convolutions.
•  » » » 4 years ago, # ^ |   0 How ?
•  » » » » 4 years ago, # ^ | ← Rev. 3 →   +5 Here is the simpler version in and small constant (/64 with bit-compression): Let's split the weights into two groups: and w < k. In the first group, there are at most values (because their sum doesn't exceed n). In the second group, there are at most k distinct values, and each group of equal weight w can be replaced with O(logn) equivalent (for knapsack) weights: if there are m weights equal to w, let's replace them with w, 2w, 4w, ..., , . This way, we can reduce the size problem to at most weights, which can be solved with regular knapsack dp (possibly with bit-compression)
•  » » » » » 3 years ago, # ^ | ← Rev. 2 →   +5 Let's replace w,w,w by 2w,w while we can (it my be done in O(n)), after that it will be only different weights by the same bound.So extra log is not needed
•  » » » » » » 3 years ago, # ^ |   0 Nice trick, thank you!
 » 4 years ago, # |   +1 Can someone give more detailed explanation of problem D ?Thanks
•  » » 4 years ago, # ^ |   0 Ok, I'll try explain my solutionOleg can't go near offices he visited, so if now he is in office v, he visited q, q > v, he can't visit office k, where k > q. That means if now he's in office v, he can only visit office q, l < v < r, l is rightmost visited office which position is less than v and r is leftmost visited office which position is more than v. So we have to solve our problem for state [v][l][r][s], where s count of steps we made. We have 2 options. First is go to left and second is go to right. If we go left, we are not interested what is r, and if we go right we are not interested what is l. So our state now is [left border][right border][direction][s]. State [v][l][r][s] becomes 2: [v][r][right][s] and [l][v][left][s]. We can exactly identify our current position. If we go left it's right border, and if we go right it's left border. Now, when we are in state [l][r][direction][s] and now we are in office v, we have to just find all offices q, l<=q<=r and c[v][q] != 0 and solve our problem for [l][q][right][s+1] and [q][r][left][s+1] and take minimum. For it we can use recursion. We can start from any office, so we have to solve problem for [0][v][left][0] and [v][n+1][right][0] and take minimum.We have n^2*2*k states. In each state we check make not more than n operations. So algo works in O(n^3*k)
•  » » » 3 years ago, # ^ |   0 How is the time complexity O((n+m)*n*k). I didn't even understand how the naive algorithm works in O((n+m)*n^2*k)
 » 4 years ago, # |   +28 Problem G can be solved using Hopcroft-Karp algorithm, if you optimize it hard enough: 26669894.
 » 3 years ago, # | ← Rev. 2 →   0 I think problem B has a better solution with brute force and a little dp firstly, initialize 2 arrays h and v so that h[i,j]=number of cell with road work from (i,1) -> (i,j) and v[i,j]=number of cell with road work from (1,j)->(i,j) since igor can only make 2 turns so his way must be one of these: --- — ---- | ^ | | | | | | <-- --> | V ---- so we just need brute force and calculate the number of cell with road work on the path (get it from h and v cause he can only go up, down, left or right)
 » 3 years ago, # |   +13 Problem F has a tag "divide and conquer". Is there a solution with O((n + m + q)polylog(n + m + q)) time complexity?
•  » » 3 years ago, # ^ |   +33 Yes. Check submission of niyaznigmatul: 26621779.
•  » » » 3 years ago, # ^ |   +13 Thanks! For other people who are trying to understand this code: The setmin function sets every element of the interval to be the minimum of its value and val. The getmin function returns the first index in the interval whose value is larger than val.
 » 3 years ago, # |   0 I have a solution for problem F with time complexity O((N+M)logN) using segment tree.
•  » » 3 years ago, # ^ |   0 Would you be nice to explain your solution? thanks in advance
 » 3 years ago, # | ← Rev. 3 →   -16 Just in case someone is looking for a proof for problem A (793A - Oleg and shares)We can't increase the values of any number, so we have to make bigger numbers equal to smaller numbers by subtracting k (zero or more times) of them.Suppose the array is sorted.At some point we'll have to make numbers arr[i] and arr[0] equal, and the only way is by subtracting k from arr[i] some number (q) of times and subtracting k from arr[0] some number (p) of times so we have: (a = arr[0], b = arr[i] for simplicity, 0 < i < n)a — pk = b — qk(b — a) = k (q — p)We don't know (and we don't need to know) values p and q, but now it's certain that (b — a) must be divisible by k.Now the key step is that if k divides (b — a), then we can make b equal to a by subtracting k (b — a) / k times because the only criteria to make a = b — qk is that k divides (b — a). This is optimal.
 » 5 months ago, # |   0 Getting W.A. for problem B......Can anyone provide a test case for which this fails?
 » 3 days ago, # |   0 I did B by doing dfs from s towards t and keeping in check whenever I am changing direction. Idk why I'm getting WA ON TEST CASE 12. Can anyone help?