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

Автор BledDest, история, 9 лет назад, перевод, По-русски
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
Разбор задач Educational Codeforces Round 21
  • Проголосовать: нравится
  • +78
  • Проголосовать: не нравится

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

Автокомментарий: текст был обновлен пользователем BledDest (предыдущая версия, новая версия, сравнить).

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

If we are able to solve problem E by iterating over all possible values of 3-weighted objects + dp on triple (cost, cnt1, cnt2), can we just solve it with dp on quadruple (cost, cnt1, cnt2, cnt3)? Better, can we solve any knapsack problem with N distinct weights with dp on (N + 1)-uple (cost, cnt1, cnt2, ..., cntN)?

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

any suggestions how this solutions for D was hacked? http://codeforces.me/contest/808/submission/27138172

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

what does unexpected verdict during hacking mean?

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

My solution of E is a kind of greedy and Dp solution. In this problem we have a knapsack problem. The knapsack has a size much bigger than the size of its components. So we can make a greedy approach until the size of knapsack is X, then make a knapsack DP on the rest of components on a knapsack of size X. I fix X = 300 (3*100 ) arbitrarily. A doubt: How we can find the minimum X that we guarantee a optimal solution in this case? :p My solution:http://codeforces.me/contest/808/submission/27144481

Edit: It is Wrong! Sorry!

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

    It is wrong because if you have, for example, one souvenir with cost 5 and weight 3 and then two hundred souvenirs with cost 3 and weight 2 and you are able to carry 400 weight, you would greedily choose the 3-cost souvenir right at the start, which is not optimal.

    Now, what if you make sure you leave enough unpicked items of each weight before the end of the greedy phase? That is, you refuse to pick the item with the best cost-to-weight ratio if it makes your list with that specific weight too short. Then, the dynamic programming should have enough of each weight to optmize the remainders of the knapsack.

    My intuition says this approach should work, but I can't come up with a proof or even the right limits for when to start the DP and for how many items of each weight we should keep.

    EDIT: Nevermind, it's just wrong. Unless there are specific and artifical limits on the costs of the items, one greedy choice in enough to make the algorithm incorrect.

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

Ignore

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

I am not able to understand letter E, can someone explain me more clearly please ?

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

Can anybody explain why we just need to optimize the 'cost' in problem E solution without regarding to the 'cnt1' and 'cnt2' ?

Much appreciated!

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

Can someone explain the ternary search solution to problem E?

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

    keep the best m elements of weight 1

    keep the best m/2 elements of weight 2

    keep the best m/3 elements of weight 3

    sort all of them in non-increasing order

    sum[i][j] is the sum of the first j elements of weight i

    /* from the editorial above

    We can iterate on the number of 3-elements we will take (in this editorial k-element is a souvenir with weight k). When fixing the number of 3-elements (let it be A), we want to know the best possible answer for the weight m - 3A, while taking into account only 1-elements and 2-elements.

    */

    let y be m — 3A , we need to take B 2-elemensts and C 1-elements with total weight equal to y

    y = B*2 + C*1

    we will take the first B elements of weight 2 and the first C elements of weight 1

    the ternary search is on B with this function F(i) = sum[2][i] + sum[1][y-2*i]

    it is correct because F(0) <= F(1) <= .... <= F(B) >= F(B+1) ...>= F(m/2)

    and we are searching for the best B

    the answer will be : sum[3][A] + sum[2][B] + sum[1][C]

    take a look at my code if you need : code

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

I don't understand D?

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

    Here's one approach.

    Maintain two multisets, one for prefix elements and one for suffix elements. Traverse through the array in the given order and keep prefix and suffix sums as you traverse. Also maintain the two multisets, adding the current element in prefix multiset and erasing it from suffix multiset.

    If prefix sum is greater than suffix sum, search in the prefix multiset for their difference halved. If such element exists, output YES.

    If suffix sum is greater than prefix sum, search in suffix multiset. If you can't find such element, output NO.

    Corner Cases: When n=1, answer is always NO.

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

      I traversed the array both ways keeping the sum of all elements encountered. Then when ever the sum is exceeding the half of the total sum I'm checking whether the difference has been encountered. This means I'll be able to spot an element which i have to remove from the prefix and add to the suffix in order to get the answer. 27175419

      Please let me know if I have miss understood the question.

      EDIT: This is the working solution 27176375...

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

Problem F is something good to learn about mincut , thanks

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

I think problem F is solvable with binary search + edmond blossom with a complexity O(N^3logN), not sure if it would pass.

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

Can anyone explain binary(not ternary!) search solution for problem E (if there is one)? I tried to solve it this way with binary search but it fails on test case 9: separate elements in 3 arrays, every weight goes in one of them. Sort arrays Lets fix number of 3-elements we will use. then I do 2 steps: 1. step — first binary search for number of 2-elements, and while doing that, binary search for number of 1-elements (so binary search in binary search :D ) 2. step — same but oppposite: first binary search for 1-elements, then binary search for 2-elements inside. Why we do bs in bs twice? Because maybe optimal solution is to take 0 2-elements and 100 1-elements, so if we just binary search for 2-elements first, then we may skip this solution.

code: http://codeforces.me/contest/808/submission/27160206

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

Does problem D mean that we get to swap the elements? For instance, example #3 shows that we need to swap the elements 3 and 4 to make the sums equal. (2 2 3 4 5) -> (2 2 4 3 5). I'm asking this because it did not work, obviously :(.

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

    Not swap elements, just PUT one element in different position. In example above, you took number 3 and put it right from number 4. Number 4 didnt move anywhere, but it seemed as it did because you moved 3 from its position.

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

    In general, no, you cannot swap two arbitrary elements, you are only allowed to remove one element and place it somewhere else. Notice, hoewever, that if you take the 3 of (2, 2, 3, 4, 5) and place it one position to the left, the final result is equivalent to that of swapping it with the 4.

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

      So, from what I understand from your kind answers, there are two cases

      1. Removing an element from prefix(or suffix) and add it to suffix(or prefix), which means <sumPrefix — element, sumSuffix + element>
      2. Moving an element at the boundary of prefix / suffix in which case, the swap happens <sumprefix — prefixElem + suffixElem, sumsuffix — sufixElem + prefixElem>

      Please correct me if I'm wrong

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

Another approach for E: Sort all souvenirs in decreasing order by cost / weight (if equal it's better to choose souvenir with least weight).

Now pick greedily up to m - X weight (we'll choose X later). After that we have res + X weight to fill (1 <  = res <  = 3 — residue after greedy stage of algorithm).

Now we'll use naive approach (either standard knapsack dp or even iterate on all possible counts of 1-souvenir, 2-souvenir, 3-souvenir). It turns out as far as we have really small weights, we can choose X to be relatively small (intuitively we can choose X to be 3 * 3 + 2 * 3 + 1 * 3 = 6 * 3 = 18).

My submission http://codeforces.me/contest/808/submission/27144235 so feel free to hack it;)

P.S. For ones who might want to understand crappy code above. In submission I have 3 vectors (for 1-souvenirs, 2-souvenirs, 3-souvenirs respectively) and 3 pointers to handle greedy picking souvenirs and loops at the end.

Edited:

Thanks @Lewin for the hack. Yeah, interesting. I was pretty sure this approach works:) Gotta find out if it's a bug or incorrect approach

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

What if we have several pairs of cnt1, cnt2 for optimal cost dp[w]?

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

What is the time complexity of D using sets of search.

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

Here is another approach to problem E. Suppose we have only two kinds of weight, 2 and 3. Then we can easily solve the problem with sorting and (two pointers or binary search).

The main idea is that we can reduce the main problem to this easy one. Solve the problem twice: choosing odd number of items of weight 1, or even. If we decide to choose even, we can pair the weight-1 items and convert them to weight-2 items. And if we decide to choose odd, first pick the biggest item of weight 1. The remaining part is same as the even version.

27164728

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

    Such a beautiful solution. While trying to solve problem, I also came on idea to pair weight-1 items to get weight-2 or even to take triples of weight 1 items. But I thought that it wont be correct because it may be optimal to choose only one weight-1 item. Now I see — either you pick greatest weight-1 item and pair others, or you pair all of them... Thank you.

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

    Why can this subtask be solved using greedy approach unlike thr initial?

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

      Initial problem can also be solved with greedy approach(selecting the most valuable items from each group). But the time complexity will be O(N^2) as we have to deal with 3 groups. After reducing the number of groups from 3 to 2, the problem can be solved in O(N log N).

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

http://codeforces.me/contest/808/submission/27164939 About the problem E ? I don't think is wrong. Who can help me?

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

In G you don't need to consider all 26 characters. From position j you either move forward by tj or you start from position P(j), where P is prefix function. My solution for reference: 27165597

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

I thought I understand DP solution for E, but then I realised that I dont. Here is my problem; lets say that dp[i].cost = x. then you say we update dp[i+1], dp[i+2],dp[i+3] with this value. But what if there is way to get weight i, with cost y, such that y < x, but in that case we used less elements of weight 1 lets say. So, maybe dp[i+1] will be better if updated with cost y + cost of next weight 1. Am I right?

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

    I don't think there is a difference due to the fact we use best k-elements available. For instance, to get x you use 1-elements e1[1] + ... + e1[m] (in sorted order) and to get y e1[1] + ... + e1[m -1]. You claim that y < x and y + e[m] > x + e[m + 1] (if e[m + 1] exists) which is obviously wrong, in worst case (if e[m + 1] doesn't exist) y + e[m] = x, so y can't be strictly bigger than x, I suppose.

    Edit: y + e[m] can be bigger than x in worst case, of course, if we take into account 2 and 3-elements , so the question remains unanswered.

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

    You can not update [i+3] state choosing only from 1s and 2s. And we don't care about free souvenirs, we will use the most expensive of them anyway. Example: 1-w: [2 3] 2-w: [2 5] m = 2. dp[0] = {0, 0, 0}, dp[1] = {3, 1, 0}. dp[2] choosing from (0 + 5) and (3 + 2). They are equal, so two variants of new tuple: {5, 0, 1} and {5, 2, 0}. But we are interested only in the first value, and it must be the biggest. if (cost of 1-w + 1-w) == (cost of 2-w) we could use any of them (weights are equal) and use another later if have extra weight reserve.

    Sorry if I didn't unserstand you question and wrote something strange :D

    My realization if needed: http://codeforces.me/contest/808/submission/27421196

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

why F we can't use more than one card of magic number is 1?

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

Could someone please help me understand as to why greedy approach of G not correct. As i am replacing the given string in the specified string from back and then count the total number of the given string .Any explaination of this would be really helpful .

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

I got TLE in this code for problem B can someone please point out the error??

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

What is the time-complexity for D?

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

How do we do ternary search (or its reduction to the binary search version) for E if it isn't necessarily true that the function is strictly increasing/decreasing?

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

How did this randomized solution 27148367 pass 808F - Card Game ?
Are the test data too weak or the probability of success is really high?

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

In Problem F, I get WA test 11.

Can someone help me please? My solution ==> 27199504

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

In problem G: what are other ways to represent the states? thanks

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

My code http://codeforces.me/contest/808/submission/27212742 for Problem A is running wrong in test case 2, for input 201, when checked in submission it shows output 96, whereas in my compiler and other online compilers it is showing output 99, don't know what to do.. help if possible...

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

I am getting WA on test 21 for problem D ,although I checked it with lot of hack cases. Any help would be appreciated. http://codeforces.me/contest/808/submission/27217259

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

problem:G... first I am finding in string s, all the possible positions where the string t can end and storing in boolean array, eg for (win???dwin,win) f=[0,0,1,0,0,1,0,0,0,1] . And then I am finding the LPS for string s. And then using this DP state :(lt=len of string t,ls =len of string s)
for(i=lt;i<ls;i++) { if(flag[i]) dp[i]=dp[ i-lt+lps[lt-1] ] + 1 ; else dp[i]=dp[i-1]; } I am getting right answer for most of the test cases...except test cases 56,60,64. Can anyone pl explain why am I getting WA for these test cases.

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

there is another solution for problem E. we can sort the array and select front item greedily, then do some fix to generate right answer. 27342219

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

Why cant E be solved by simple 0-1 knapsack problem like this?

http://ideone.com/EiWMGd

What is wrong in this approach?

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

Here's (another?) solution for E.

My greedy algorithm calculates answer for state with weight not more than w + 1 using the value of only state w. For this there are three types of transitions add one 1 - element to w state, add one 2 - element and remove one 1 - element from w state; or add one 2 - element to w state.

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

93619776 Can someone tell me why this code of mine fails at test case 60?

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

Problem D: what did the problem ask?? if I am allowed to move just one element or more (problem statement said some)..

7

1 8 15 5 7 6 2

one ac submission is giving "NO" for this input..

but i can Divide the elements as

15 7 -> 22

1 8 5 6 2 -> 22

so answer should be "YES". BledDest