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

Автор awoo, история, 6 лет назад, По-русски

1354A - Alarm Clock

Идея: BledDest

Разбор
Решение (pikmike)

1354B - Ternary String

Идея: BledDest

Разбор
Решение (BledDest)

1354C1 - Simple Polygon Embedding

Идея: adedalic

Разбор

1354C2 - Not So Simple Polygon Embedding

Идея: adedalic

Разбор
Решение (adedalic)

1354D - Multiset

Идея: BledDest

Разбор
Решение (BledDest)

1354E - Graph Coloring

Идея: BledDest

Разбор
Решение (pikmike)

1354F - Summoning Minions

Идея: BledDest

Разбор
Решение 1 (BledDest)
Решение 2 (neal)
Решение 3 (pikmike)

1354G - Find a Gift

Идея: adedalic

Разбор
Решение (adedalic)
  • Проголосовать: нравится
  • +267
  • Проголосовать: не нравится

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

Was skeptical about my approach of C2 but editorial gives a very simple easy to understand proof!

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

Can someone please explain 1354C2 not so simple polygon embedding. I am not able to grasp editorial solution. Thank you.

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

    Me too, and i really hope that someone could teach me using degree measure, not the radian measure.

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

    This could be helpful Here

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

    Just consider the hexagon example in the editorial , You notice that the square has the same side length as the distance between the two opposite vertices(touching the square) in fig 1 , (2 units in the case of hexagon) Now draw a horizontal line between the 2 vertices (touching the square) in fig 1 , If you rotate this line anticlockwise , its horizontal component will start to decrease , but the horizontal component of the diagonal line between the vertices will start to increase , If you rotate a full (90/n) degrees it restores to its original fig1 (why 90/n , because at the center , the angle for each side is (360)/(2n) = 180/n , rotate half that is 90/n , and you achieve symmetry , or in other words the same figure ) Now , if after 90/n , the horizonal component of the diagonal turns to 2 , and for the horizontal vertices it decreases , then there must be a point where they both were same That point by symmetry has to be 90/2n i.e. in the middle

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

took a lot of time to prepare the tutorial. hope it's worth.

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

Although the suggested solution is beautiful, I'd say that Binary search was easier to think of and implement in B.

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

Although the tutorial for B is beautiful, I'd say Binary search was easier to think and implement!

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

 

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

I feel like the king who won the war at 7th attempts. I solved D with Java and submitted 6 times, either got MLE or TLE but submitted the same solution with C++ and got accepted.
There may be other possible solutions but if you want to use tree data structure, then I think you have to use either-

- BIT(Binary Indexed Tree)/fenwick tree which may get accepted in any languages.(I am bad at BIT)
- Or you can use segment tree with C/C++. (Segment tree might not gonna work with other languages. like my java solution.)

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

is it possible to solve D with policy based data structures? i tried and got MLE on test 4.

update: so apparently the PBDS tree has two pointers per node plus the two values of the node, which gives us 4 ints per node in this case, and we have a maximum of 2 mil elements in the set (1m + 1m insertion queries), and so 2 mil * 16 bytes per node is well over the 28MB limit.

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

Anyone explain E please !

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

    I will try to explain it in simpler terms for you. First, you need to understand that there cannot be any odd cycles in the graph. You can see this for yourself by drawing a cycle of length 3 and inducing for larger cycles.

    With that out of the way, let's run BFS on each component, keeping track of the distance to each node from the starting node. If we ever encounter any odd cycles (i.e. the depth of an adjacent visited node is the same parity of the depth of the current node), we can terminate the program and print NO. Otherwise, let's group all nodes with even distance from the starting node and claim that we can assign 2 to each of these. This is possible since they will each be separated by one and only one node, which we can assign 1 or 3. With similar logic, we can assign 2 to all nodes with odd distance from the starting node. If the graph was guaranteed to be connected, we would already be done, by simply checking whether the number of odd distance nodes or the number of even distance nodes equals n_2. However, there can be multiple components, leading us to the second part of the solution.

    Let's add the number of nodes with even distance as an item's weight in a knapsack problem, and do the same with the number of nodes with odd distance. These will also have a type number, which can just be its component id. This is crucial to understanding the rest of the solution. The type of an item represents which component it is in, and the weight of an item represents that we can assign exactly weight with nodes the number 2 in that component.

    To "merge" the components, we will run a very basic knapsack dp, with our state as dp[i][j] = 1 if it is possible to reach total weight j with the first i types of items, and dp[i][j] = 0 otherwise. The transition should be obvious, but notice that we cannot always do dp[i][j] = max(dp[i][j], dp[i - 1][j]) since we must include exactly one item of each type (after all, we can't just forget to assign numbers to a component).

    Finally, we will check whether it is possible by checking whether dp[total_components][n_2] = 1, since this represents that there is a way to assign n_2 nodes the number 2 using all component types. If it is possible, we can simply backtrack for our solution, and we are done.

    Hopefully, this helps you understand the solution slightly better without any complicated terms.

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

      while trying to solve the problem on my own I was able to deduce the impossiblity with odd length cycles.But then never did I realize that this would be using dp.So my question is what would be my intution to detect that this problem requires dp? Please Sir,if you can kindly help....

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

        For me, it was realizing that the question would be impossible without being able to "merge" two components together, after solving the problem on one component. The second step is to really think about what the numbers you get from each component actually mean, and what n_2 really means. How does having x nodes at an even distance help you? Only then will you realize that you are actually trying to find some set of numbers that sum to a specific number, which is a classical dp problem. You don't need much intuition to realize the correlation, just a lot of practice with solving questions of similar type, and knowing how to solve many classical problems.

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

Why am I getting MLE in D if I use policy based data structures in C++?

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

Is lazy needed for problem D?

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

Why it takes too much time for rating changes?

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

Can somebody explain how the count_le(int) in D works? I see the fairly simple code but still dont get it.

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

    The first for loop is straightforward, just count the number of elements less than or equal to x. In the second for loop, if the query is > 0 and if it is less than or equal to x then no matter when it is inserted it'll always contribute to count. As for the negative query, we now have count of elements less than or equal to x till this negative query, hence if the ordered statistics is more than count it won't affect our count as only numbers larger than x are removed. If the order statistics is less than our count, then we have to remove some smaller element hence we are decreasing count. Hope it helps.

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

Can someone explain how the function is monotonous in problem D editorial?

"The minimum in the resulting multiset is the minimum x such that this function returns non-zero for it, and since the function is monotonous, we can find the answer with binary search."

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

    $$$f(x)$$$ — number of elements $$$ \lt = x$$$ in the multiset.

    When we go from $$$x$$$ to $$$x + 1$$$, all the elements $$$ \lt = x$$$ will still be $$$ \lt = x + 1$$$, but new elements (with value exactly $$$x$$$) may also contribute to $$$f(x + 1)$$$. So the value never decreases, thus $$$f(x)$$$ is monotonous.

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

    The function actually returns the no of elements less than equal to x ,so for all elements less than the minimum element the function return a zero value as there are no elements less than the minimum element and for all elements greater than or equal to the min element the fun returns a non zero value(this value may increase by increasing the query element (or index))So it is monotonous because it returns 0 till a certain index and then returns non zero value till the last index.

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

Can anyone explain me How they are coming with solution for C1? I know people are saying it is easy to come up with this solution. But, still i'm not able to figure out this. Thanks.

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

    The first thing i assume that the square will be somehow related to outer circle of polygon. And equation of outer circle of a regular polygon is very easy to figure out. You can search on google for this equation.

    And finally came to an equation for C1 that a be sides of square, r be radius of outer circle then, a*a/4+0.5*0.5=r*r. You can simply get it drawing figure.

    For C2 i get it that a polygon having 2*n sides will fit in a square having sides equal to half of radius of polygon having 4*n sides. Although i don't have a solid proof for this but it passed.

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

    I can tell you my weird approach during the contest but editorial is much simpler. I divided the square into 4 quadrants just like a cartesian coordinate system and then we can tell each quadrant contains $$$(n / 2)$$$ vertices since $$$n$$$ is even. Now see the pic we are starting from the coordinate $$$(0, 0)$$$ then finding the next coordinate $$$(x_i, y_i)$$$.

    The main idea is to find the distance between the initial position and the final position. During the contest, I was not sure if that is true or not.

    for finding the final position we can iterate $$$n$$$ times and use this simple formula $$$(x_{i + 1}, y_{i + 1}) = (x_i + cos(theta), y_i + sin(theta))$$$, and increase $$$theta$$$ by $$$δ$$$ in every iteration.

    Did you notice the formula is not giving the expected value?. here is the reason-

    logically our initially starting position should be $$$(x_1, y1)$$$ instead of $$$(0, 0)$$$, but what I did is shift the $$$(x_1, y_1)$$$ by $$$(-0.5)$$$, and ultimately you will land upon the final position according to the figure.

    my submission

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

Can anyone please explain how the final expression in C2 is obtained?

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

How do we arrive at the formulas for C1 and C2?

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

    For C1, I solved it with a different approach. Basically the formula for internal angle of a n-sided polygon is given by theta = ((n-2)*180))/n We form a triangle as shown in the editorial. The side bisects the internal angle, lets say theta1 = theta/2 So tan(theta1) = x/0.5 Therefore x = tan(theta1)/2 Inorder to find the length of square we multiply x with two. Thus final ans is ans = tan(theta1) Link to my submission

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

    Try this.  You are actually trying to get the length of the purple line, which can be divide into (n-1) parts. In this case, 3 parts. I've separated the purple line with blue lines. The first part(from down to up) should be 1 * sin(theta). The second part should be 1 * sin(theta * 2). The third part should be as same as the first part, which is 1 * sin(theta). btw, theta = 180° / n P.S. this is written in degree, not radian.

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

    For C1, note that for a regular polygon with even number of sides, opposite sides will be parallel. For even n, 2*n will also be divisible by 4. And by symmetry argument, there will be two pairs of opposite parallel sides perpendicular to each other. So we can align the polygon so that this 4 sides will be parallel to the sides of the square, and also touching them to minimize the size of the square.

    Now consider the center of symmetry of the regular polygon, which is also the center of the inscribed circle, of the circumscribed circle, as well as of the square. Construct the triangle with one of the sides of the polygon as base and the center of the polygon as third vertex. The height of this isosceles triangle will be half of the side of the square. The angle of the triangle with tip in the center of the polygon measures 360 deg / (2*n) or 2* pi / (2*n). Construct the height of the triangle. It will split the isosceles triangle into 2 rectangular triangles, whose angle with the tip in the center of the polygon will measure half of the previous angle or pi / (2*n). Tangent of the angle equals to the ratio of the opposite cathetus (half of the side of the regular polygon) to the adjacent cathetus (the height we are looking for). So h = 0.5 / tan(pi / (2*n)), and the side of the square is twice that.

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

    For C2, if we try to align the polygon to have a pair of sides parallel with a pair of sides of the square, then the other 2 sides of the square will touch 2 opposite vertices of the regular polygon, and the distance between 2 opposite parallel sides of the polygon will be shorter than the side of the square, leaving a gap. The long diagonal connecting the 2 vertices touching the sides of the square will also be parallel to 2 opposite sides of the polygon. If we rotate the polygon by the angle pi / (2*n), the situation will be the same, except now vertices of the polygon are touching the other pair of sides of the square. By symmetry, the optimal rotation angle should be midway between these 2 positions, i.e. it is equal to pi / (4*n). The long diagonal will now touch 2 opposite sides of the square like before, but it is at a slant angle (equal to pi / (4*n)) respective to the other pair of sides, instead of parallel.

    To calculate the diagonal length, construct the similar isosceles, and then rectangular triangles as we used in problem C1. The hypotenuse of the rectangular triangle is half of the diagonal, and it equals 0.5 / sin(pi / (2*n)), since sine of the angle is the ratio of the opposite cathetus and the hypotenuse. The full diagonal is 1 / tin(pi / (2*n)).

    To calculate the length of the side of the square, project the diagonal on to the direction parallel to the side of the square (this can also be thought of as constructing a rectangular triangle with the side of the square as one of the catheti and the diagonal of the polygon as the hypotenuse), to obtain the side of the square being equal to cos(pi / (4*n)) * 1 / sin(pi / (2*n)).

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

    May be this will be helpful for you to understand the derivation of formulas to both C1 and C2 problems.

    Problem C1 and C2 Vedio Explanation | Educational Codeforces Round 87

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

Any program where I can draw and rotate the drawings in problem C2 ?

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

Can anyone explain this code to me. 80463378 This is a solution of C1 and C2.

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

Like both the ideas of C1 and C2. One problem, two datas, two levels of difficulty.

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

At first I wasn't a huge fan of problem D because I thought Fenwick/segment tree was the only solution. Having seen this editorial though, I realize it's actually very creative and interesting.

Overall, I feel I learned a lot from this Educational Round. Thank you to all the problem writers!

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

PLEASE explain the BINARY SEARCH and the TWO POINTERS solution for the problem 1354B. I will be grateful.Thank you.

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

Please explain D solution via Fenwicks tree.

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

    We will use the frequency array to build the Fenwick tree. Let's say we have an array freq[1..n] that contains the frequency of all the numbers given in the array, i.e if freq[y] = x, then the number y appears x times in the given array.

    For the query of the first type "add integer K to the multiset" we simply need to increase the freq[k] by 1.

    For the query of second type, we need the smallest number x such that freq[1] + freq[2] + freq[3] + ... + freq[x] is greater than or equal to k. Then we can just decrease the freq[x] by 1.

    Let's say we have functions update(t, val) and getSum(t).

    update(t, val) increases the value of index t by val. freq[t] += val.

    getSum(t) returns freq[1] + freq[2] + freq[3] + ... + freq[t] in log(n) time.

    Then for the query of type one simply use update(t,1). For the query of the second type use Binary search to find the required number and use update(t,-1).

    To find the required number use something like this

    k = abs(k);
    
    int lo = 1, hi = n;
    int idx;
    while(lo <= hi){
        int mid = (lo + hi)/2;
        if(getSum(mid) >= k){
            idx = mid;
            hi = mid-1;
        }else{
            lo = mid+1;
        }
    }
    update(idx, -1); 
    
    

    Finally for the output, just do a linear search to find smallest t such that getSum(t) > 0 and output t.

    Here is my submission. 80502866

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

Can anyone please tell what is wrong in this approach...

#include<bits/stdc++.h>
using namespace std;

int i=0;
bool flag=false;

int find_sub(string s){
	char _1='r',_2='r',_3='r';
	int curr_len=0;
	
	
	_1=s[i];
	for(;i<s.size();i++){
		if(s[i]==_1){
			continue;
		}
		
			curr_len=2;
			_2=s[i];
			i++;
			break;
	}
	
	for(;i<s.size();i++){
		if(s[i]!=_1 && s[i]!=_2){
			curr_len++;
			_3=s[i];
			i++;
			break;
		}
		
		if(s[i]==_1){
			curr_len=2;
			_1=_2;
			_2=s[i];
			continue;
		}
		curr_len++;
	}
	
	if(_1=='r' || _2=='r' || _3=='r'){
		if(flag==false)
		return 0;
		else{
			return INT_MAX;
		}
	}
	return curr_len;
}

solve(string s){
	int min_len=INT_MAX;
	while(i!=s.size()){
		int res=find_sub(s);
		if(min_len>res){
			min_len=res;
		}
		flag=true;
	}
	cout<<min_len<<endl;
}


int main(){
	int t;
	cin>>t;
	while(t--){
		string s;
		cin>>s;
		solve(s);
		i=0;
		flag=false;
	}
}

this is showing wrong ans on 2 test case set at 163rd entry...

please kindly help..

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

How to solve c2 by ternary search ?

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

In problem c1 , how to prove that solution in the editorial is the best one ? Since we can rotate the polygon , there might exist some other orientation such that answer is better than the one editorial ?

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

    essentially, they're using the height of the octagon (or n-gon) from one midpoint of a side to another as the height of the square. any other rotation would increase the size due to pythagorean theorem. take for example, if you used opposite vertices, that would be greater than the two midpoint solution because of pythag theorem (the legs are always smaller than hypotnuse)

    edit: wait nvm it's either the midpoint or the vertice, but either way its guaranteed to be one of those because those are the maximums and minimums respectively

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

    Look at an inscribed circle of $$$2n$$$-gon. Since $$$2n$$$-gon should be embedded in the square and the circle is embedded in $$$2n$$$-gon then the circle must be embedded in the square. And there is no way to embed the circle in the square with side lower than the diameter of the circle.

    But we've shown the example with the square side equal to the diameter, so it's the best answer.

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

I had another approach to C1 (which then can easily be related to C2, as I just checked after seeing the editorial) and I'm not sure if it was explained in the comments.

I noticed that when you have a $$$2n$$$-gon with $$$n$$$ even, you can see that (knowing that the best disposition of the polygon inside the square is when two paralel sides of it are also paralel with a pair of ones in the square) the width of the square, and hence, its side length, is the sum of the lengths of one of the legs of each of the right triangles defined by the number of "not-right" sides in a quadrant, times two, as it replicates symmetrically on the opposite quadrant, and also add it $$$1$$$, as it's the length of one of the sides that is paralel to the ones I mentioned in the square:

Note how the internal angle of each of the triangles formed between the vertices and the center of the polygon are the same as in the triangles I mentioned before. Here's another example in a dodecagon ($$$n = 6$$$):

Knowing all of this, and the fact that I can get the angle of those triangles that "make" the polygon as the amount of sides in half of the polygon (which is $$$n$$$) and the total angle that makes it ($$$\pi rad$$$ or $$$180°$$$), given that it's a regular polygon, it would be $$$\frac{\pi}{n}$$$. And then each of the angles you see it's a multiple of one of them (in the dodecagon, $$$30°, 60°...$$$) as they are all equal and adjacent. Finally, the length of the leg of each right triangle I mentioned before, would be $$$cos(\frac{\pi}{n}*j)$$$ (taking for example, the horizontal one), being the $$$j$$$-th counting, for example, from the bottom. And as it also adds symetrically, it is $$$2*cos(\frac{\pi}{n}*j)$$$.

I can repeat doing that for all $$$j$$$ in $$$[1,\frac{N}{2}]$$$ with a loop, then add it one, and there's your answer.

As some pointed out, one way to see C2, is by calculating the distance between opposite sides of the polygon (taking it with a segment perpendicular to both), and then it seems the optimal solution is to rotate the polygon by $$$\frac{\pi}{4n}$$$. As my approach exactly gets that value, for C2 the only thing to do multiply the answer by $$$cos(\frac{\pi}{4n})$$$, which is getting the rotated segment, and by so the optimal width of the square.

My solution may be worse than the editorial as it has a complexity of $$$O(n)$$$, but I think it's still a nice approach to the problem.

Submissions: C1 C2

I hope I explained it well and may be useful to someone :)

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

Can anyone tell me what is wrong in my submission of 1354B- Ternary String question! Please have a look at the following submission:

https://mirror.codeforces.com/contest/1354/submission/80626322

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

1354D — Multiset My submission: 80598227 why i got wrong answer in test 52?Please help

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

Can anyone explain the editorial for B more elaborately?

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

    have a look on https://mirror.codeforces.com/contest/1354/submission/80605650

    I created a vector A which tells the order of 1,2,3's , and vector B storing their particular frequencies...

    As in if you have a string 332111111221132, then

    A : 3 2 1 2 1 3 2

    B : 2 1 6 2 2 1 1 (hope u get what I've done till now)

    Now just simply iterate A from 2nd to 2nd last position and check that if A[i-1],A[i],A[i+1] all are different (if they are different we can get substring of form 122...223(or something similar), in that substring A[i] will occur B[i] times and A[i-1] and A[i+1] will occur once). Finally we will be finding min of (B[i]+2) whenever A[i-1],A[i],A[i+1] are different...

    Hope this helps, do let me know if you need more help :D

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

I tried to develop a solution for problem 1354C1 - Simple Polygon Embedding by using each triangle rectangle with hypotenuse = 1. My solution just makes the sum over the cosine of each angles below the right angle of the triangles i talked before. The cosine of each angle in blue on the image is the length of the red segment, and i make the sum until i get to the right segment in blue, then i multiply what i already got by 2 because this is the part above the straight segment in blue, and sum 1 which is the length of the straight segment in blue.

The code:

constexpr double PI = 3.14159265359;
constexpr double toRad = PI/180.;

long double total = 0;
int beg = (2*n-2)*180/(2*n) - 90;
long double r = 2.0*beg/(n-2.0);
for(int i = 1; i < n/2; i++){
  total += cos((beg-(i-1)*r)*toRad);
}
cout << setprecision(11) << (total*2+1) << "\n";

I start with the first angle and then i decrease the angle by a rate. The angle i start with is just the angle of the regular polygon minus 90. And the angle should decrease after each vertex until it gets to 0 ( when we reach the straight segment). The rate is the first angle divided by n/2-1 which is the number of vertices until the straight segment and after the first vertice.

For the test case: 1 200

My solution gives the answer: 127.46243899 Which is not the correct one, but i can't find out if the solution is not correct or i'm just having a lot of floating point errors on the sum. Thank you.

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

Problem C1/2 can also be solved by brute force. In detail, you can rotate the polygon within [0, pi/n) for 1000 times. [submission:80507571]

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

The editorial of D , which was explained without data structures was simply awesome.

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

    Please can you explain me how to solve D.Multiset without segment tree or anything like it.I am unable to understand the tutorial.Thanks in advance

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

      Ok, so if we look carefully at the problem statement what matters after all is that we need to return any element that still belongs to the multiset. Now generally in these types of questions the easiset is to track something like minimum or the maximum element, so here it is easy to keep track of the min element. Now as the queries are offline, so we can simply keep track of the smallest element using binary search.

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

Actually D could be solved in linear: 80679881 (It cost me a few times to shrink the memory >_<)

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

in div2 D, what is guarantee that if count_le(mid) > 0 ,then element is present in multiset and even if it is present , then how do we know it will not be removed as a result of k operations. e.g : 5 4 1 2 3 4 5 -5 -1 -3 -1 Here, for element 4, count = 1, but it gets removed from the multiset. How is the solution working?

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

Edit: all good.

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

1354D — Multiset Getting W.A. on test case 12, Can someone provide a test case for why this fails?

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

Could someone let me know how this solution for E is wrong? It's not the conventional method used in the solution, but I thought it would still work: https://mirror.codeforces.com/contest/1354/submission/80695289

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

how to solve D without using any data structure. Someone please explain

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

 adedalic Thanks for nice editorial and problem set . I have a doubt in c2. Since angle cab = 45 (in degrees) and AB has length 0.5 implies BC has length 0.5 . Similarly for other corner also . Hence over all length of the diagonal is $$$1 + 1/tan(π/(2*n))$$$ . The $$$1/tan(π/(2*n))$$$ term comes from c1 (i guess it will be same for all polygons) . We can get the length of the side of the square after dividing by square root of 2.

But this is giving incorrect answer for figures other than hexagon . Is there any error in my assumption ?

my submission : link

Thanks

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

I have a solution for C2 which uses binary search: we can see that a pair of points with the longest distance between them in the 2n-gon, here hexagon, are B and D.

So, we may try to keep these points along the diagonal of the square (as diagonal is the longest distance between two points on a square)

But we still have to leave out some distance ED = BH = x from both ends of the diagonal, and then only place the hexagon's vertices. Because if we try to co-incide the opposite vertices, with the end points of the squares diagonal then since the interior angle of the figure > 90, hence it won't work, it will leave the square.

So, we look for some minimum length x = ED = BH which on leaving out from both ends we may fit the figure inside the square.

On this length x, we may perform a binary search!

80725776

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

In case anyone need Detail explanation for C1 and C2 Here

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

Why is segment tree expected to pass in D when ordered_set fails. Only reason to not use ordered_set in C++ is because it will have 2*n nodes at worst. But segtree also needs 4*n nodes. Please clarify.

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

can anyone explain why my solution gives TLE on test case 4 . even though it is O(N*log(N)*log(N)) code — 80806088

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

I don't understand why my solution gets TLE on testcase 4 . here is the link. can anyone help please?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится
#include<bits/stdc++.h>
#define MAX 1000006
typedef long long int ll;
using namespace std;

int fenwick[MAX], arr[MAX], n;

void update(ll x, ll delta){
    for(;x<=n;x+=x&-x){
        fenwick[x] += delta;
    }
}

int query(int x){
    int sum = 0;
    for(;x>0;x-=(x&-x)){
        sum += fenwick[x];
    }
    return sum;
}

int element_idx(int rank){
    int l=1, r = n;
    int idx = MAX;
    while(l<=r){
        int mid = (l+r)/2;
        if(query(mid)>=rank){
            idx = min(idx, mid);
            r = mid -1;
        }
        else{
            l =  mid + 1;
        }
    }
    return idx;
}

int main(){
    int q;
    cin >> n >> q;
    for(int i=1;i<=n;i++){
        cin >> arr[i];
        update(arr[i], 1);
    }
    int i;
    while(q--){
        cin >> i;
        if(i>0){
            update(i, 1);
        }
        else{
            i = abs(i);
            i = element_idx(i);
            update(i, -1);
        }
    }
    int final = element_idx(1);
    if(final == MAX){
        printf("%d", 0);
    }
    else{
        printf("%d", final);
    }
    return 0;

Code for Multiset 1345-D I am getting Time Limit exceeded for test case 6 can someone pls help?

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

Here's my problem solving stream. I didn't bother explaining what I was doing (like when teaching), only thinking out loud.

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

Can someone please help me with problem F, don't know why this solution is TLEing on test 32: https://mirror.codeforces.com/contest/1354/submission/81679962

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

According to the author 's solution to problem F, I wonder why the constraints are too small and time limit is too high?

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

Can someone please discuss the binary search approach of the ternary string question?

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

Can someone please tell me what is wrong in my java solution for D. I am getting a MLE on Case 8.

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer;

public class Solution{

static int[] BIT;
static int n;


public static void main(String[] args) throws Exception{

    FastScanner fs = new FastScanner();
    PrintWriter out = new PrintWriter(System.out);

    n = fs.nextInt();
    int q = fs.nextInt();

    BIT = new int[n+1];
    for(int i=0;i<n;i++) {
       update(fs.nextInt(),1);
    };

    while(q-->0) {
       int k = fs.nextInt();
       if(k>0) {
         update(k,1);
       }
       else{
         k = -1*k;
         int l = 1;
         int r = n;

         while(r>l) {
          int mid = (l+r)/2;
          if(prefixSum(mid)>=k) {
              r = mid;
          }
          else {
              l = mid+1;
          }
         }
         update(r,-1);
       }
    }

    boolean bool = false;
    for(int i=1;i<=n;i++) {
       if(prefixSum(i)>0) {
         out.println(i);
         bool = true;
         break;
       }
    }

    if(!bool) {
       out.println(0);
    }

    out.close();
}



static void update(int i, int inc) {
    while(i<=n) {
       BIT[i] += inc;
       i = i + i&(-i);
    }
}



static int prefixSum(int i) {
    int sum = 0;
    while(i>0) {
       sum += BIT[i];
       i = i - i&(-i);
    }
    return sum;
}



static class FastScanner{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");

    public String next(){
       while(!st.hasMoreElements()){
         try{
          st = new StringTokenizer(br.readLine());
         } catch(IOException e){
          e.printStackTrace();
         }
       }
       return st.nextToken();
    }

    public int nextInt(){
       return Integer.parseInt(next());
    }

}

}

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

Solved D using the Fenwick tree + Binary search without any optimization. Here is the submission. Idea is to keep frequency bit array.

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

Question G — Find a Gift

This can be solved deterministically with 4*logn i.e. 40 queries in the worst case. You can find a stone deterministically in 20 queries. Method -> let a = {1,2,...n/2,...n} -> Divvy it up in two sets and compare their weight. Then change a to the set with greater weight. If a is odd, remove one element and append to another vector say 'B'. Finally doing it recursively, we would stop with a single element. Add this element to B. The above task in done with 10 queries. Now, simply find the max element in B by asking at most of logn queries. Then the rest is similar to the editorial. Submission 100589414 for reference [Although the code is extremely messy]

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

    If I understood correctly it may not work for this case, am I missing something?

    A = (9, 9, 7, 10, 1, 10, 10, 10)
    s1 = (9, 9, 7, 10) s2 = (1, 10, 10, 10)
    A = (9, 9, 7, 10)
    s1 = (9, 9) s2 = (7, 10)
    A = (9, 9)
    A = (9)
    B' = 9
    

    But 9 is not a stone.

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

For D I have written a n(logn^2) solution using segment tree and fenwick tree separately

but my segment tree solution gave TLE and fenwick tree passed but both of these data structures have same complexity, which is nlogn, right?? or if not how fast is fenwick tree?

Segment tree solution which gave TLE — https://mirror.codeforces.com/contest/1354/submission/233261831 Fenwick tree solution which passed — https://mirror.codeforces.com/contest/1354/submission/233262440