dalex's blog

By dalex, 10 years ago, translation, In English

Recently we ran another ACM ICPC quarterfinal qualification contest, whose results influenced the list of teams that will go to Saratov this autumn. The corresponding gym contest on Codeforces will be held on Sunday, Sep 21, 2014, at 11:00 AM.

Link to the contest: 2014, Samara SAU ACM ICPC Quarterfinal Qualification Contest

The list of our previous contests:

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

| Write comment?
»
10 years ago, # |
  Vote: I like it +6 Vote: I do not like it

Thx~It seems Chinese have no time to participate in it because of the online contest

»
10 years ago, # |
  Vote: I like it +5 Vote: I do not like it

How to solve E.? My idea was to use DP. Try all possible split points and solve sub-problem (like matrix chain multiplication) but I couldn't implement it (size of string was an issue as well).

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

    no DP lol, just check for odd and each char contains less then n/2 times.

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

      Could you elaborate?

      • »
        »
        »
        »
        10 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it
        map<char, int> s;
        	int i, count = 0;
        	char c;
        	scanf("%c", &c);
        	while (c != '\n')
        	{
        		s[c]++;
        		scanf("%c", &c);
        		count++;
        	}
        	if (count % 2)
        	{
        		cout << "NO";
        		return 0;
        	}
        	for (c = 'a'; c <= 'z'; c++)
        	{
        		if (s[c] > count / 2)
        		{
        			cout << "NO";
        			return 0;
        		}
        	}
        	cout << "YES" << endl;
        
        • »
          »
          »
          »
          »
          10 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Why is this correct?

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

            When you have exact n/2 same letters (for ex. 'a') your strategy is to erase letter 'a' and not 'a'. After this operation you'll have again half 'a' letters.

            It is clear that it is possible.

            You can't erase more than 1 letter 'a' per turn. You are going to do n/2 turns, so you must have less or equal n/2 'a' letters =)

            Sorry for my bad english.

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

              Could someone explain me why this is not correct?

              I have an stack, for each letter I check the letter on the top of my stack. If it's the same I "push" the new letter, otherwise I "pop" the last one (different letters so could be eliminated) At the end I check the size of my stack, if it's zero my answer is YES

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

                Your algorithm can't pass test "abcc":

                1. Push 'a'
                2. 'b' != 'a' => Pop 'a'
                3. Push 'c'
                4. 'c' == 'c' => Push 'c'

                Size of stack will be 2 and your answer will be "NO"

                But you can remove 'b' and 'c', then 'a' and 'c', so correct answer is "YES"

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

        I can be proved by math induction!

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

    My friend solved this with an extremly short greedy algorithm. Define d[x] as the number of letter x in the string. Declare it as d[300] so that it cover all kind of letters in the ASCII table (not needed though).

    At the start of the algorithm: res = s.length(); Then we iterate through every possible character x, and decrease res by d[x]. If d[x] > res-d[x] then the result is NO, else if the algorithm reach the end, the result is YES.

»
10 years ago, # |
  Vote: I like it -15 Vote: I do not like it

Guys, can you explain why you write all these treaps and splay trees in problems that don't require them?

»
10 years ago, # |
  Vote: I like it +5 Vote: I do not like it

What is the approach to solve problem I ? In sample test 3 , Why is there no possible answer : I think the possible answer might be 1 2 3 2 . I might be wrong , am I missing anything ?

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

    two region is differents color if only if these is neighbors. In other word if region u the same color region v -> these ara not neighbors

»
10 years ago, # |
  Vote: I like it +16 Vote: I do not like it

How to solve Problem K (Two Pirates)? Why is greedy approach not working for this problem? And what can be perfect approach to solve this question.Any help?

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

    A counterexample to greedy : 99,1,2,100.

    My method :

    Use DP, let dp[2k] denote the maximum that the first pirate can get after 2k turns, and the two pirates take only the first 2k items. So we can write dp[2k]=max( dp[2k-2]+a[2k-1] , dp[2k-2]+a[2k] , dp[2k-2]-m+a[2k-1]+a[2k] ),

    where m is the minimal value that the first pirate took in dp[2k-2].

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

      My code gives 199 2 as output for this test case.That is as expected to happen.And can you please explain your DP .How you come to it ?

      Here is what I did : Suppose I am having a segment tree to update an elemnt and find maximum in a range. cin>>arrLength;

      long long  s=0;
      for(long long  i=0;i<arrLength;i++){
          cin>>data[i];
          s+=data[i];
      }
      initSegmentTree();
      buildSegmentTree();
      long long A=0;
      long long B=0;
      long long  c=0,j,d=0;
      for(long long  i=0;i<arrLength;i+=2){
              for(j=c;j<arrLength;j++){
                  if(data[j]!=INT_MIN)
                  break; 
              }
              c=j;
              for(j=c+1;j<arrLength;j++){
                  if(data[j]!=INT_MIN)
                  break;
              }
              if(j>=arrLength)
              {
                  A+=data[c];
                  break;
              }
              d=j;
              long long  pos = query(c,arrLength-1);
              if(data[pos]-data[c]>data[c]-data[d]){
                  A+=data[pos];
                  update(pos,INT_MIN);
                  update(c,INT_MIN);
                  c++;
              }
              else{
                  A+=data[c];
                  update(c,INT_MIN);
                  update(d,INT_MIN);
                  c++;
              }
      }
    • »
      »
      »
      10 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      First of all, thanks for the solution. :)

      I tried to implement your idea, and it passes the 2 given test cases (and the ones I made) but I keep getting Wrong Answer on Test Case 1.

      Can somebody help me in figuring out where is my mistake?

      My code: 7949600

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

    Anti-greedy test: 6 3 8 7 2 4 1 5. We should take 8 and 7, and remain 6 and 3 at the first two iterations.

    The process described in the problem is equivalent to the following: the first pirate takes all items, but every even step he throws away the cheapest one. Easy to implement with priority queue.

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

      Is answer for your test case 23 13 (As is provided by my approach)?Also whats the priority criteria to throw away the cheapest item.Please explain.Is position of element used as priority criteria?

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

        Optimal solution is 24 12: first pirate takes 8, 7, 5 and 4.

        About priority criteria: is the word "cheapest" so unclear?

        On every prefix [1, k] of the array (k is even) first pirate must take no more than k/2 items. Think about it. But we take all items, and when we discover that we have taken too many items, we throw away the one with the lowest price. It happens after every even turn.

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

someone explain the approach behind problem M

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

    Answer:

    Length — a*b

    (b-1)*a+1, (b-1)*a+2, .. (b-1)*a+a, (b-2)*a+1,(b-2)*a+2,..,(b-2)*a+a, ..., 1,2..,a

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

      Can you please explain how you arrived at this solution. I had no idea on how to proceed :(

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

        I saw a pattern in small tests:)

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

Excuse me, could anyone explain the meaning of Prob A? Where is the black hole and whether it could move? Thanks a lot!

Sorry for my poor English.

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

    You have a circle moving into a triangle, you cant move so 1 point of circle is out of triangle. Calculate the (surface where some point of circle can be)/(surface of triangle)

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

      Thank you! I misunderstood the word "part" in the problem.

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

Any hints for solving C and I( I don't understand simple test case #3)?

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

    To solve the problem I, read this: https://en.wikipedia.org/wiki/If_and_only_if

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

      Of course, i know what is "if and only if".

      But i understand from this: "The final version of the map two regions must have different color if and only if they are neighbour regions." => I can use only two colors or i'm wrong?

      Can you check my code 7895549?

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

        Why only two colors? If we invert that statement, we get "two regions must have the same color if and only if they are not neighbour regions". Create the inverse of the graph and paint each connected component its own color. Then check if all conditions are satisfied.

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

          Sorry , could you please elaborate . I didn't get your comment.

          • »
            »
            »
            »
            »
            »
            10 years ago, # ^ |
              Vote: I like it 0 Vote: I do not like it
            1. Condition (two regions must have different color if and only if they are neighbour regions) is equivalent to (two regions must have the same color if and only if they are not neighbour regions).

            2. Consider an inverse graph. All its neighbour vertices must have the same color.

            3. Find its connected components and paint each of them its own color.

            4. Check if the condition from (1) is true.

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

              At first I thought the problem was asking for whether the graph is k-color-able or not. Now I realize that I misunderstood the problem. "if and only if" really changed the meaning.

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

    C Let a and b the desired numbers, then:

    a*b=n*(a+b) -> a*b-n*(a+b)+n*n=n*n -> (a-n)*(b-n)=n^2, so

    a-n and b-n — divisors of n^2. (a,b) = (d+n,n^2/d+n)

    We need to find all divisors of n^2.

    You can find the factorization of n for sqrt(n). n = p1^a1*...*pk^ak, n^2 = p1^(2*a1)*...*pk^(2*ak). And then recursively find all possible divisors of n^2 using factorization of n^2.

    http://pastebin.com/LVKRQaEq

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

      Another solution of C:

      n * (a + b) = ab

      Let's g = gcd(a, b), so a = g * a', b = g * b'

      n * g * (a' + b') = g * g * a' * b' -> n * (a' + b') = g * a' * b'

      g = n * (a' + b') / (a' * b')

      We can see, that gcd(a', b') == 1 -> a' and b' can't be divisors of (a' + b') -> a', b' are divisors of n

      So solution is: Iterate over all pairs(a', b') of divisors n, check gcd(a', b') == 1, find g = n * (a' + b') / (a' * b') -> a = g * a', b = g * b'

      http://pastebin.com/LtBR5Suh

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

        This is how i solved the problem.

        ab = nab => b = na/(a-n)

        This is a function b(a). Graph has asymptote a = n. For all (a < n) => b < 0. => Our possible b looks like b = n + c = na/(a-n); where is c >= 1. Ok, lets solve this.

        a = n*n/c + n; b = n + c.

        code: http://pastebin.com/rU1cDd1L

»
10 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Can someone tell me why my solution for problem E is not correct ? Basically, my idea consists of matching each character with another one different from it.

 string s;
	cin >> s;

	int al[26];
	for( int i = 0; i < 26; i++ )
		al[i] = 0;

	int n = s.size();
	for( int i = 0; i < n; i++ )
		al[s[i] - 'a']++;

	sort(al, al+26);
	reverse(al, al+26);
	
	bool ok = 1;
	for( int i = 0; i < 26; i++ ) {
		for( int j = i + 1; j < 26; j++ ) {
			int del = min(al[i], al[j]);

			al[i] -= del;
			al[j] -= del;
		}

		if( al[i] != 0 ) ok = 0;
	}

	if( ok ) cout << "YES" << endl;
	else	 cout << "NO"  << endl;

	return 0;
}
  • »
    »
    10 years ago, # ^ |
      Vote: I like it +2 Vote: I do not like it

    I solved problem E with this pseudo code:

     If length odd -> print NO
     else If numbers of character X in string > length/2 print NO
     else print YES
    
    • »
      »
      »
      10 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Normally, my code should handle those cases. Can you spot the problem please ?

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

        What if al = {8, 6, 4, 4}?

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

          My code will give NO. How can the matching be done ?

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

            This is not very hard, just take a pen and paper and figure it out by yourself.

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

              I always end up with two characters. How come please.

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

                Every turn choose two maximal numbers and descrease them by 1.

»
10 years ago, # |
  Vote: I like it +5 Vote: I do not like it

Can problem L use other idea other than splay?

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

    Use three deques: for left, middle and right parts, and one boolean flag — if the middle part is reversed.

    ALMOST EVERYONE has used treaps or splay trees. It's just a three star local SSAU contest, of course, there is much easier solution!

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

      Can you explain it in more detail?How does the reverse operation update?

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

        Easy, just midReversed ^= true.

        Then, if you move one of the heads, let's say it's the left head moving to the left, you should 1) pop the last character from the left deque 2) push it into the mid deque, and depending on midReversed flag it is push_front or push_back.

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

          in the question l<r is given then how this is possible for n==1

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

How to solve A.?

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

    Simple geometry: http://ideone.com/bEzVz0

    The key thing is a knowledge about similar triangles and their proportions: https://en.wikipedia.org/wiki/Triangle#Similarity_and_congruence

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

      I did not understand your solution. How did you use similar triangles? The only part I understood is using Heron's theorem for area. Please explain.

      • »
        »
        »
        »
        10 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it
        1. S == p*r

        2. If S1 and S2 are the areas of two similar triangles, then S1 / S2 == k^2, where k is a ratio of their sides

        3. The same is correct for the areas of the similar parts of the similar triangles

        • »
          »
          »
          »
          »
          10 years ago, # ^ |
            Vote: I like it -17 Vote: I do not like it

          What is this? Come on dude. Invest some time in writing something readable.

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

            Maybe you find some time to learn basic school geometry? Believe me, it will be very helpful.

            • »
              »
              »
              »
              »
              »
              »
              10 years ago, # ^ |
              Rev. 2   Vote: I like it -12 Vote: I do not like it

              Just explain your solution in English and don't try to make an impression :)

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

                Draw a triangle that is similar to the given one and for that the inscribed circle has the radius r. The rest is up to you.

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

    Let me try to explain it.(Atleast my solution, don't know if it is the best one) With Cosine Theorem, we can calculate angles of the given triangle. Now, we need to subtract from area 3 surfaces(in every corner, one), where some point of the circle will never be albe to be in. The picture will look like this: The famous picture You need to subtract the red surface from red+green one Colored one^^ Red+green one is 2*surface of right-angled triangle with sides r and the opposite angle one of the angles of the triangle/2(Can be easy calculated by some easy trigonometry) Red one is (PI-angle/2)/(2*PI)*(r*r*PI) (Surface of the circle) Do that for every 3 points(A,B,C), add the green surfaces, and print 1-(sum of green surfaces/area of rectangle(Can be calculated with Heron's formula. Wish I helped.

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

      It's like using splay trees in problem L, see my solution above :)

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

In Problem G, I tried a greedy approach since all coins are multiples of each other ! But it doesn't work can someone give me a counter example!
My code is here

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

    It doesn't work because you haven't thought about overflow of integer types.

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

      Thank you ! I thought that the max would be 109 × 105 while it is 109 × 105

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

    Just stop that if coinValue>sum, you wont be able to use that coin eitherway.

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

How to solve H,J,K?

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

    H: binary search on the cost of the cheapest trick made

    J: find the answer in the following form: aa..aabcaa..aadeaa..aafg......

    K: see in the comments above

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

      what's the second testcase of J,I can't find a worng case for my code.

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

      Could you explain H? I can't understand how we get an answer if we know the cheapest trick made.

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

        We should count total number and total cost of tricks with price >X — we'll made them all. Total cost may be calculated as sum of few arithmetic progressions.

        And we also know that cheapest trick has cost X, therefore all remaining tricks (total number of tricks minus number of tricks with cost >X) also have cost X.

»
10 years ago, # |
  Vote: I like it +5 Vote: I do not like it

can we have an editorial?

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

    You can find parts of editorial in comments here or ask for the missing problems

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

How to solve I?

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

What's test 24 of prob C? I got TLE but I don't know why.

»
10 years ago, # |
  Vote: I like it +23 Vote: I do not like it

My codes: A B C D E F G H I J K L M