Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

# | User | Rating |
---|---|---|

1 | tourist | 3411 |

2 | moejy0viiiiiv | 3333 |

3 | W4yneb0t | 3218 |

4 | LHiC | 3199 |

5 | TakanashiRikka | 3178 |

6 | Petr | 3163 |

7 | izrak | 3109 |

8 | anta | 3106 |

9 | ershov.stanislav | 3105 |

10 | Um_nik | 3070 |

# | User | Contrib. |
---|---|---|

1 | rng_58 | 180 |

2 | tourist | 171 |

3 | csacademy | 170 |

4 | Petr | 162 |

5 | Swistakk | 159 |

6 | Errichto | 156 |

7 | matthew99 | 152 |

8 | Zlobober | 151 |

9 | zscoder | 139 |

10 | Endagorion | 138 |

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial is loading...

Tutorial of Educational Codeforces Round 21

↑

↓

Codeforces (c) Copyright 2010-2017 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Sep/25/2017 07:08:02 (c4).

Desktop version, switch to mobile version.

User lists

Name |
---|

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)?Well, it seems that it doesn't work. Can someone explain why it works for two values and not for three?

I have the same doubt with you! I also what to know how to use ternary search sloving this problem.

Have you solve this? If not, you can read my code, I use ternary search to solve this problem.

I am sorry for that I saw your reply too late.But does your code is use ternary search? I consider ternary search is trie but your code is use binary search. Anyhow, thanks your new idea.

Consider the case n=4, m=7 with items A=(1,3), B1=(2,4), B2=(2,4) and C=(3,6).

Then the (only) optimal solution for dp[4]=A+C, for dp[5]=A+B1+B2, dp[6]=A+Bx+C and dp[7]=B1+B2+C, but there is no way to create the solution for dp[7] by adding items to any of the three solutions before.

It apparently doesn't work for weights 1, 2 and 3. but can you give a proof that it works for weights 1 and 2 only? I can't figure it out.

When you have only two weights, you can solve the problem greedily: first take the minimal number of necessary items so there is a multiple of the lcm remaining (in this case this means taking a single item of weight 1 when the wanted sum is odd) and after that greedily take the best groups of a single item of total weight equal to the lcm until you have the wanted sum (in this case this means either taking a single item of weigth 2 or two items of weight 1).

It is then relatively easy to prove that the dp solution gives the same results as the greedy solution above (for example by induction).

Why can't we make the same argument for three numbers 1, 2, 3 with lcm 6?

At a given state there could be 6

x+anumber of 1's, 3y+bnumber of 2's and 2z+cnumber of 3's. So if we consider all values ofa,bandcfor dp transitions, it should work right?thanks your sample, it very useful for me because my poor dp skill.

Many thanks! Your example was extremely useful. I've managed to solve it with quadruple dp. The point was in trying to change dp[m-1] or dp[m-2]. If they have 1-element, it can be replaced with more expensive 2-element or 3-element respectively.

I tried this method, and got several WA, but finally received an AC. If you take 1&2&3-element in consideration at the same time, then there are four possible way to renew dp[i]:first:dp[i-1]+ 1-element; second: dp[i-2]+ 2-element; third: dp[i-3]+ 3-element; ※fourth: dp[i-2]- 1-element+ 3-element.What's more, it can be proved that all other way is equal to the four ways above. This is my AC code: http://paste.ubuntu.com/24652108/

thanks a lot, what my poor dp skill

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

When you check the position of target delta value

what does unexpected verdict during hacking mean?

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.com/contest/808/submission/27144481`

Edit: It is Wrong! Sorry!

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.

Flow network must be oriented, that isn't said in the editorial. Hope that this will help someone

Thank you for pointing it out. It is added to the editorial now.

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

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

Much appreciated!

my code is a implementation of E in editorial, cnt1 and cnt2 can be used to determine how to renew dp[i](like above). Here it is 27157016

I mean, why we ONLY need to optimize(maximize) the COST.

The cnt1 and cnt2 definitely means something. But why we can ignore it.

I want a proof or something like that.

my code here 27149084

and another code using quadruple get WA which I cannot figure out why. 27163738

Hey, can this be cleared out. If coldwater got it or anybody, please tell why there is WA with 1, 2, 3 and cost together but not with 1, 2 and cost?

Can someone explain the ternary search solution to problem E?

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

Can you give me your evidence to prove prove that

F(0) <= F(1) <= .... <= F(B) >= F(B+1) ...>= F(m/2)I also dont get this. What if last 1-element ( that has least price) has bigger cost than first 2-element (has highest cost of all 2-elements) ? then F(0) >= F(1)

B isn't literally bigger than 0 and less than m/2

it might be 0 or m/2

the idea is that the graph of the function F is increasing then at some point is decreasing

it's correct because we sorted the 3 types of elements in decreasing order

when we take 1 more 2-element with sum greater than the last 2 1-elements we took before the answer is increasing

but at some point , taking 1 more 2-element with sum less than the last 2 1-elements we took before the answer will start to decrease

you need to try an example to understand it well

Thank you your help. I unserstood

Why do u take first m/2 elements of weight 2 and m/3 elements of weight 3? Maybe in optimal solution we will take worst element of weight 3? Please explain. Also, any good reference on ternary search? Edit: wow, cant believe I asked this..thanks for reply

Cos it maximal number for 3-w elements could be only m/3 and for 2-w m/2.

Does ternary search still work if we iterate on the number of 1-elements?

I thought ternary search is only possible when the function is strictly increasing then decreasing (or vice versa). How come this still works if you have the <= instead of < in this case?

It works because in this case F(0) < F(1) < .... < F(B1) = F(B1+1) = ... = F(B2) > F(B2+1) ...> F(m/2). If there also are equal elements in other places, you are right and ternary search is not guaranteed to work.

I don't understand D?

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.

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...

Here is how I solved it:

Remember the partial sums (let the sum of the first i terms be s[i]). If s[n] is odd (total sum) then you can't divide it in 2 of the same sum. In the case s[n] is even:

Iterate i from 1 to n. For every i check if s[i] == s[n]/2 (you can remove a number and place it in the same spot). If yes output and you are done. Otherwise, do binary search over the first i elements of s (the terms are positive so s is increasing). Search for s[n]/2-a[i] (so if you move i to a position j, j < i, you can just look at the first j and the rest).

Then do another binary search on i+1, n and search for s[n]/2+a[i]. (you would move i at position j, i < j, and the sum of the first j-1 is s[j-1] — a[i], which you need to be equal to s[n]/2. so you need s[j-1] = s[n]/2 + a[i])

Hope this helped!

Problem F is something good to learn about mincut , thanks

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

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.com/contest/808/submission/27160206

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 :(.

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.

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.

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

Please correct me if I'm wrong

If I understand what you are saying, you are correct, but there is no need to divide the problem into two cases, the second one is just a particular occurence of the first.

Thank you very much.

is it allowed to swap more than 1 element? isnt this np ?

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-Xweight (we'll chooseXlater). After that we haveres+Xweight 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

Xto be relatively small (intuitively we can chooseXto be 3 * 3 + 2 * 3 + 1 * 3 = 6 * 3 = 18).My submission http://codeforces.com/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

This comment proposed a similar solution, and you can see my counterexample as an answer.

In fact I still believe this approach works with the following tune: After greedy step we have to remove worst 3 1-souvenirs, worst 3 2-souvenirs, worst 3 3-souvenirs and perform naive approach (looping and knapsack dp).

Nice idea. I got AC with it.

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

Exactly..Why no one has the answer yet?

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

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

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.

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

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).

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

In G you don't need to consider all 26 characters. From position

jyou either move forward byt_{j}or you start from positionP(j), wherePis prefix function. My solution for reference: 27165597I 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?

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.Yeah, I wanted to say you that you didnt count 2 elements and 3 elements, and then saw that you updated comment. So, anyone with answer?

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.com/contest/808/submission/27421196

There is some bug with my solution: http://codeforces.com/contest/808/submission/27133213 Does anybody know how to fix it?

UPD. Bug disappeared. It's ok now.

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

Cause in this case we have sum equal to 2(1+1) which is prime.

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 .

Dude, u better change your dp before asking any question.

Dont worry its not for you

Consider

s1 = "a????bcab",s2 = "abca????b" andt=abcab.For

s1 optimal answer is "aabcabcab" and fors2 it is "abcabcabb", andtoccurs 2 times in both of these.If you do greedy from front, you'd get "

abcabbcab" as answer fors1, which has 1 occurrence oft.If you do greedy from back, you'd get "

abcaabcab" as answer fors2, which has 1 occurrence oft.Hope this helps!

Thanks Dude but could you help me understanding the dp approach ?

Do you know the KMP algorithm and understand it well?

i know KMP algo but dont have a deep understanding of it just basics !

My solution is here.

There are two main functions

`pre`

and`go`

.In

`pre`

, first I create the table`b`

where`b[i+1]`

stores length of longest proper suffix (which is also its prefix) of the string`p[0:i+1]`

(substring of p starting at`0`

and of length`i+1`

). Then I calculate`n[i][j]`

which is same as the next array defined in the editorial.My

`go`

function now uses the above two arrays to calculate the`dp`

and`cnt`

values.`dp[i,j]`

tells if there exists a placing of characters such that last`j`

characters of s[0:i+1] are same as first`j`

characters of`p`

.Use this information and try to understand the code, and why it is correct.

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

you have used "cin,cout", because of this, you got TLE, if you change "cin,cout" to "scanf,printf), it will work much faster

if you submit it with c++14 (http://codeforces.com/contest/808/submission/27183006) it will get AC. cin,cout are faster in c++14 than c++11 if you use boost(ios::syn..).

What is the time-complexity for D?

O(n log(n))if you use something like std::set to maintain the elements on the prefix.I thought of the same complexity, but i thought it was wrong. Thank you :)

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?

See my comment: http://codeforces.com/blog/entry/52010?#comment-360483 Here I explained my TRY of binary search inside binary search. It passed 9 test cases and I didnt really try much to debug it. But from my solution you can see that you can do bs. If you separate items (weight i in group i — so 3 gruops totally) and then sort, you have increasing function.

Isn't it only nondecreasing, not increasing?

Well for binary search non decreasing is ok. And I cant help you with ternary cause I also have problems with that solution.somewhere here in comments there is ternary search solution written but I dont understand why function is first increasing and then decreasing (user didnt prove it, he just said it).

How did this randomized solution 27148367 pass 808F - Card Game ?

Are the test data too weak or the probability of success is really high?

In Problem F, I get WA test 11.

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

You forgot to add reverse edges in

`dinic.adjList[]`

You are right, Thank you very much xD

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

My code http://codeforces.com/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...

Rounding inside

`pow()`

differs slightly among implementations (details here), so surrounding`pow()`

with another`round()`

fixes the issue. It's usually preferred to use exponentiation by squaring when exponents are integers, though.Thanks Bro...

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.com/contest/808/submission/27217259

Changing int pre[] to ll pre[] will do.

Thanks :)

is it only allowed to move 1 element only ... ? why do i think this is np ?

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.

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

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

http://ideone.com/EiWMGd

What is wrong in this approach?

It's O(n*W) which is too slow for this task.

Yes exatly. First thing which came to my mind after reading problem statement was knapsack. But then I read editorials but doesn't makes much sense to me. Will someone care to explain why this isn't plain knapsack problem?

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 statew. For this there are three types of transitions add one 1 - element towstate, add one 2 - element and remove one 1 - element fromwstate; or add one 2 - element towstate.Can somebody explain why my solution fails on test case 21. Here is my submission