igdor99's blog

By igdor99, 4 years ago, translation, In English,

580A - Kefa and First Steps

Note, that if the array has two intersecting continuous non-decreasing subsequence, they can be combined into one. Therefore, you can just pass the array from left to right. If the current subsequence can be continued using the i-th element, then we do it, otherwise we start a new one. The answer is the maximum subsequence of all the found ones.

Asymptotics — O(n).

Solution

580B - Kefa and Company

At first we sort all friends in money ascending order. Now the answer is some array subsegment. Next, we use the method of two pointers for finding the required subsegment.

Asymptotics — O(n log n).

Solution

580C - Kefa and Park

Let's go down the tree from the root, supporting additional parameter k — the number of vertices in a row met with cats. If k exceeds m, then leave. Then the answer is the number of leaves, which we were able to reach.

Asymptotics — O(n).

Solution

580D - Kefa and Dishes

A two-dimensional DP will be used to solve the problem. The first dimention is the mask of already taken dishes, and the second — the number of the last taken dish. We will go through all the zero bits of the current mask for the transitions. We will try to put the one in them, and then update the answer for a new mask. The answer will consist of the answer of the old mask, a dish value, which conforms to the added bit and the rule, that can be used. The final answer is the maximum of all the values of DP, where mask contains exactly m ones.

Asymptotics — O(2n * n2).

Solution

580E - Kefa and Watch

At first, we calculate the hash for all line elements depending on their positions. That is, the hash of the number k, standing on the i-th position will be equal to gi * k, where g is the base of the hash. We construct the segment tree of sums, which support a group modification, for all hashes. Thus, we can perform queries for modification in O(log n). It remains to deal with the queries of the second type. Let us assume, that we want to process the query 2 l r d. Obviously, the substring from l to r have a d-period, if a substring from l + d to r is equal to substring from l to r - d. We can find out the sum of hashes at the subsegment with the help of the sums tree, so we can compare the two strings in O(log n).

Asymptotics — O(m log n).

Solution

 
 
 
 
  • Vote: I like it
  • +34
  • Vote: I do not like it

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

Use __builtin_popcount(int) in C++ for counting non-zero bits instead of new function

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

I think you wanted to say l+d r and l r-d.But you forgot a case.

For example 12312312,123123 can be solved using that method because is periodic,but the part that remains (12 in our example) has to be compared to the prefix of the same length.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    12312312 is periodic, because two bold parts are equal: 12312312 and 123 12312. Everything is OK.

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

    Sorry, we had mistake. Thanks for your attentiveness! =)

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

In Problem B:

How the inner loop (inside the outer one which iterates from 0 to n) of the two pointers method affects the complexity.

Here's my solution :

http://codeforces.com/contest/580/submission/13175844

I thought it would give me TLE.

can anyone explain the complexity ?

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

What is this "method of two pointers" ???

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

In E, isn't it possible that hashing will create collisions and hence give an incorrect result? If yes then hacking solutions in E will produce incorrect results for hacks that should be succesful.

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

    Fortunately, nobody knew which mods and bases were used in authors solution :)

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

      I know that the probability of error is small if we choose a good base and a good prime but that doesn't mean that we can ignore the probability that there were collisions in the authors solution as well right?

      • »
        »
        »
        »
        4 years ago, # ^ |
        Rev. 3   Vote: I like it +16 Vote: I do not like it

        Actually it does. Lets do a little math. Assume that N = 105. There are (105)2 substrings of the input. Authors are probably using 32-bit integer to store the modulo, to make calculations easier assume that mod = 109, It means with a uniformly random distribution, for each 0 ≤ x < 109 there are 10 substrings where each of them has a hash value equal to x. For each query probability of collision is equal to , because there are 109.102 ways to choose collision pair and (1010)2 ways to choose any two substring. It means for each query probability of having a correct answer is 1 - 10 - 9, which means probability of having correct answer for all test cases is (1 - 10 - 9)105.80 = 0.992.

        It's up to you to worry about %0.7 probability of fail.

        • »
          »
          »
          »
          »
          4 years ago, # ^ |
            Vote: I like it +8 Vote: I do not like it

          Thanks for the probabilistic insight, is really helpful :)

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

          What about two primes? Like two bases and two mods.

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

My solution to problem B is giving TLE. SOLUTION

Here I have an index j which is run from the end and if the difference is within d then I do not check further for the subsegments of this segment.I do this for every i from 0 to n-1.I am not able to figure out why the TLE.

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

    Your solution goes to O(n^2) because of the for loop in line no. 29.

    Consider the following example: d = 5 5 15 25 35 45 55 65 75 This is the array of money values (values of n[i].m in your case). For i = 0, j starts at 7 and reduces till 0. For i = 1, j again starts at 7 and reduces till 1. And this continues for all the values of i.

    For a general n, the complexity of this loop would be n + (n — 1) + (n -2) + .... This is O(n^2)

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

Can someone explain E in a little more details please? I've never used hashes before and I don't know how to pick the base — why does this work? I mean, there are 10^100000 possible serial numbers, and that's a lot more than possible values of hashes. So why don't we worry about that? How to estimate how good is our picked base?

Also I don't quite understand this segment tree. Seems pretty tough to implement. Is this some kind of modificated segment tree for updating range and querying sum on range? I never really knew how to implement it (only segment-point and point-segment variations), let alone modifications. How can we support this group modification for all hashes — just a little sketch, please? :)

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

For question D can some one explain how the answer is coming 37 for this test case?

5 5 5

3 3 3 3 3

3 4 6

4 1 2

1 5 7

5 2 4

2 3 5

Thanks!

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

Can someone please explain the time complexity in question 580D? How is the time complexity O(2^n * n^2)? We are taking dp[(1<<18)][18], should it not be O(2^n * n) ?

  • »
    »
    4 years ago, # ^ |
    Rev. 2   Vote: I like it +1 Vote: I do not like it

    inside the recursive function we do an O(n) loop over all dishes. so we have a table of size (1 << 18) * 18 with O(n) to fill each cell so overall the complexity is O(2^n * n^2)

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

Please, can someone help me understand what's the problem with my submission for 580A? =/

http://codeforces.com/contest/580/submission/13188909

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

Well, I believe that the std of problem E can be hacked.

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

Solutions are completely unreadable. Specially for the first two questions!

»
4 years ago, # |
Rev. 2   Vote: I like it -29 Vote: I do not like it

why im getting runntime error

include

include

include

using namespace std;

define f(i,n) for(i=0;i<n;i++)

typedef long long int ll; typedef vector v; typedef vector< vector > vv; typedef pair<int,int> p; typedef unsigned long long int ull; int main() { ll n;//number of friends ll d,i,j;//scale for poor ll m,s;//money and friendship of the each friend cin>>n; cin>>d;//friendship factor ull friend_temp=0,friends=0; vector < p > mp(n); for(i=0;i<n;i++) { cin>>m>>s; mp[i].first=m; mp[i].second=s; } sort(mp.begin(),mp.end()); for(i=0;i<n;i++) { friend_temp=0; for(j=i;mp[j].first<mp[i].first+d;j++) { friend_temp+=mp[j].second;

    }
    //cout<<friend_temp<<endl;
    if(friend_temp>friends)
    {

    friends=friend_temp;

}
    if(j==n)
    break;

}
cout<<friends;
return 0;

}

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

I am not able to understand logic for D. can anyone plz help me. thnx

»
4 years ago, # |
  Vote: I like it -6 Vote: I do not like it

Why memset and memcmp work in problem E? :s

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

    because the final order is o(n * q / 32)

    and it seems that (1e10 / 32) isn't big enough for cf servers ...

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

why WA in test 20 ?

http://ideone.com/76fCx6

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

Sorry Guys,
I would't have asked if I could have figured it out myself.
For question C I implemented a simple dfs algo but get WA for test case 26.
Any Ideas?
http://codeforces.com/contest/580/submission/13208683

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

Can someone explain the solution for B in a little more details please?

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

    After you sort them by money ascending order, if M[i]+d <= Mj, then M[i]+d <= M[j+1]. If person j has at least d units of money more than person i, then person j+1 too, so the answer will lie in a contiguous subsequence.

    So you can use two pointers pi and pj. pi will point to the left-most person and pj to the right-most (starting both in 0 and incrementing pj). If at some moment M[pi]+d<=M[pj] (Person pj has at least d units of money more than person pi) then you will have to increment pi until M[pi]+d>M[pj]. The answer will be the segment with largest sum of friendship factor.

    • »
      »
      »
      11 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      In this question, can we have multiple segments where the conditions given in the question are satisfied and then we have to choose the segment which has the largest sum of friendship factor???? I am not sure about the above statement, can you help??

»
4 years ago, # |
Rev. 2   Vote: I like it -8 Vote: I do not like it

chotto_matte_kudasai 's solution for E is very nice :)

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

I'm trying to solve problem E using Segment tree and String hash. My code gets WA29, outputs "NO" instead of "YES", and I fail to find what's wrong in it. Here is the code I submitted Elise-580E.

Could you help me?

  • »
    »
    9 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Did You get it? I am also facing the same problem.

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

can someone pls explain me the logic of question no 580D.

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

UPD modulo and randomization helped.

Can anyone tell me what is wrong with test # 75 http://codeforces.com/contest/580/submission/13208727 http://codeforces.com/contest/580/submission/13229611 I even checked hashes bf http://codeforces.com/contest/580/submission/13229546 They are equal with all keys I tried. Thank you

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

OK guys, I have been wondering for like 5 days now about problem D since I absolutely cannot grasp the idea of the solution the author implemented I will now explain my own idea :

Since it's a dp problem i first look for a subproblems ( optimal substructure )

The question I ask myself is which item is the next item I must pick for the current set and if I pick it should i include it as an item from the bonus satifisfaction collection or not thus I have 3 recursive calls -> 1 for the set without the item,1 if the item is included, and 1 if there's a bonus which I can get for the current item with combination with another item then I pick it for the second recursive call.

Thus it should be something like this :

answer = max(recursive(totalSum,nextIndex),recursive(totalSum + value[currentIndex],nextIndex),foreach i recursive(totalSum + in dp[currentIndex][i]) then mark the visited items by passing an array of visited items to recursive calls.

Does this algorithm seem correct to you as it seems to me? If yes how do you do it bottom up? I have trouble transforming this to two dimensional bottom up approach.

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

    I haven't read your solution but let me explain you in simple language what the author's solution is.

    So he iterates over all the 2^n possibilities (using i=1 to 2^n). Out of them he chooses only those i which have exactly m bits on. Each bit represents the index of the element of the original array. Now that you have the 'i' that have exactly m bits on, you get to the algorithm.

    /// algorithm lets say m=3 so you might have a 'i' as '1101' (0th index as the rightmost). Now lets say I pick the 0th index as my last dish and I add it's taste index to my answer. Now the remaining set of dishes remains as 1100,

    NOTE: the 0th index has turned to 0 from 1.

    Now to my function I will pass the new mask(1100) and the last dish index (0). So while following the same procedure for the rest of the dishes it will add the relation value ( tab[mask][last] ) to the answer. Hope it helps. For better understanding, refer my code

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

Could someone give me some links about hash functions? I've studied it in class but superficially. We talked about hash functions in an abstract mode (i.e. we suppose that f is a good hash function without specifying the body, we only saw the basic hash functions such as f(x)=x%m) without seeing nothing about base of the hash or how to merge two hashes into another (in order to make the sum tree). So in contests i don't know how to approach it in order to make a good hash function or how to manipulate it. Thanks in advance!

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

http://codeforces.com/contest/580/submission/13820453 I keep getting TLE at test 10 for A. What's wrong?

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

Hi.. In problem C, if the question was to get number of distinct WAYS to get to restaurants in the leaf nodes while crossing at most m cats, what would be the approach to solve that?

Thanks

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

why it is giving wrong answer in kefa and company problem i can not understand

include <bits/stdc++.h>

using namespace std; struct data{ long long int m; long long int n; }; bool comp(data A,data B){ return A.m<B.m; } int main() {

long long int i,j=0,a,b,sum=0,maxi=0; struct data hotel[100000];

cin>>a>>b;
for(i=0;i<a;i++){
cin>>hotel[i].m>>hotel[i].n;
}
sort(hotel,hotel+a,comp);

for(i=0;i<a;i++){

    while (hotel[j].m-hotel[i].m<b){
        sum=sum+hotel[j].n;
        j++;
        if(j>a-1) break;
    }
    maxi=max(maxi,sum);
    sum=sum-hotel[i].n;

}
cout<<maxi;

}

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

What is the dp solution of 580A?

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

I am getting WA for C, but cannot figure out what's wrong. Can anyone give me a test case to check my solution

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

I know I am late, but this is a serious issue. Here is my code for 580A:

http://codeforces.com/contest/580/submission/22831784

The solution gives correct output on ideone (https://ideone.com/UVSpRe), but here the output is not same. Even in Custom Test the output showing is wrong. Please help!

  • »
    »
    18 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I know this is an old post but you should never do this

            int n;
    	int a[n];
    	cin>>n;
    

    Arrays must have fixed size. Or else different compilers will react differently.

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

Hi Can anyone help me with D.Kefa and Dishes. I have used top-down dp but my solution is giving TLE. I think I have followed the same process as in the editorial with same time complexity. Thanks in advance. My solution:

»
17 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Please help how you can visit only 6-7 resturant's on the leafs only and the answer is 3 for the problem "Kefa and the Park" don't get it really

  • »
    »
    17 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Sorry i mean the answer is 2 but there is only 1 leave and its not red...

»
6 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Why there is runtime error on test 27 of 580C using python? http://codeforces.com/problemset/submission/580/54878038

»
5 days ago, # |
  Vote: I like it 0 Vote: I do not like it

In solution of problem A, We have never assigned any value to and in for loop we are writting o=max(o,k). How can we do that please help.