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

Автор awoo, история, 11 месяцев назад, По-русски

1837A - Кузнечик на прямой

Идея: BledDest

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

1837B - Строка сравнений

Идея: BledDest

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

1837C - Лучшая двоичная строка

Идея: BledDest

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

1837D - Раскраска скобок

Идея: BledDest

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

1837E - Договорной плей-офф

Идея: BledDest

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

1837F - Разбор на двоих

Идея: BledDest

Разбор
Решение 1 (awoo)
Решение 2 (awoo)
  • Проголосовать: нравится
  • +63
  • Проголосовать: не нравится

»
11 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

another solution for F. my code

»
11 месяцев назад, # |
  Проголосовать: нравится -19 Проголосовать: не нравится

The solution for D looks a bit long. Here's mine

»
11 месяцев назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

My solution for D, based on some observations.

First of all check whether it is possible to make any coloring at all. This can be checked by just checking the balance of the whole string. If it's 0(basically equal number of '(' and ')') then coloring it is always possible, either as an RBS or as an reversed RBS or both. Only thing left to check is the number of colors.

For this we can start pairing up the brackets(since there are equal number of brackets of both kind). We start by pairing the first '(' with the first ')' and color them based on a condition that if ')' comes before '(' then its reversed otherwise normal. This pairing will always result in either an RBS or an reversed RBS(try it out!).

For example suppose we had colored this set: (()((())))

and when pairing them according to this algorithm we had paired the first '(' with the first ')' even though for an RBS that is not the correct pairing. But still as a whole the string is an RBS!

I haven't yet figured out a proof for this observation and will appreciate any and all inputs, but I found this a much simpler solution to code for D.

»
11 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Is the D question simpler this time?

»
11 месяцев назад, # |
  Проголосовать: нравится +10 Проголосовать: не нравится

$$$O(nlogn)$$$ is possible for F. We just iterate over all possible splits of array and try to greedily balance the sums between prefix and suffix. My solution

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

    Could you pls explain why it's nlogn? It's not obvious to me that your inner "while" loop does constant (maybe amortized?) number of iterations.

    • »
      »
      »
      11 месяцев назад, # ^ |
      Rev. 3   Проголосовать: нравится +10 Проголосовать: не нравится

      Both while loops will be called at most $$$O(n)$$$ times:

      First one exchanges greatest element in multiset $$$ms$$$ with smallest one in $$$ms3$$$. Such moment that $$$ms3$$$ has smaller element than the greatest in $$$ms$$$ happens only whenever we expand prefix (which will only happen $$$O(n)$$$ times). It is quite obvious to see that there will be at most one such exchange after expansion of the prefix, so making it a while loop is quite redundant. Here's a accepted submission with an if statement instead of a while loop.

      Second while loop is easier to analyze since we may notice that it always increases $$$ms$$$ size by one and we are never decreasing it. Since $$$ms$$$ size is bounded by number of elements in input array, then again we get at most $$$O(n)$$$ calls.

»
11 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

For solution problem C, i submited this. Actually i am a beginner i want to know...is my coding style or all thing of my code is right?? Or i have to change something? Please someone tell me.

include <bits/stdc++.h>

using namespace std;

int main() { long long tc; cin>>tc; while(tc--) { string s; cin>>s; char c = '0'; for(long long i=0; i<s.size(); i++) { if(s[i] == '?') { s[i] = c; } else { c = s[i]; } } cout<<s<<'\n'; }

return 0;

}

»
11 месяцев назад, # |
Rev. 2   Проголосовать: нравится +1 Проголосовать: не нравится

In the tutorial for problem E, there is a statement that says:

If both or neither of the teams are from 1 to 2^k, then the answer is immediately 0.

However, it seems that the correct statement should be:

If both or neither of the teams are from 1 to 2^(k-1), then the answer is immediately 0.

or I didn't understand that part.

»
11 месяцев назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

Problem F was qutie educational! I solved it with a binary search too, but instead of a bs over the answer, I used some segment trees to simulate like a frequency sort (using something similar to coordinate compression), then I binary seach on the amount of elements I take from the left $$$x$$$ and $$$k-x$$$ from the right, and similar to the editorial, the prefix sum from the left increase and the prefix sum from the right decrease, so we look for a balance (let those values be close as possible). Complexity: $$$O(n\log n \log n)$$$ (fast enough jeje) Solution

»
11 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I managed to solve F using ternary search and binary search on segment tree. I had to make some hacky optimization to fit it in TL (3992ms)

207420100

»
11 месяцев назад, # |
Rev. 2   Проголосовать: нравится +5 Проголосовать: не нравится

my solution for D:

it is easy to check whether it is possible to make any coloring.

if the answer is yes,check whether it is possible to coloring using 1 color;

if it can't,the answer is 2.

an easy way to coloring is:

first,cut the string half from the middle

coloring all '(' in the left side using color 1.

coloring all ')' in the right side using color 1.

coloring all others using color 2.

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

    Why does it work?

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

      if it is possible to make coloring,it can be showned that the counts of '(' on the leftside equals to the counts of ')' on the rightside.

      prove:consider that

      the counts of '(' on the leftside = a.the counts of '(' on the rightside = b.

      the counts of ')' on the leftside = c.the counts of ')' on the leftside = d.

      than a+b=c+d=n/2 (that is,the total count of '(' equals to the total count of')' )

      a+c=b+d=n/2 (that is,the total count of the leftside equals to the rightside.)

      so a=d&b=c.

      it means that the sequence of color 1:"(((())))" with (a) '(' and (d)')'

      the sequence of color 2:"))))((((" with (b) ')' and (c)'('.

      it's easy to showned both sequences is beautiful.

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

    Great!

»
11 месяцев назад, # |
  Проголосовать: нравится -16 Проголосовать: не нравится

I think the tight limitation in problem F is unnecessary. My $$$O(n \log^2 n)$$$ solution got TLE on test 23.

»
11 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Can someone please explain why 207472463 is getting TLE in java, but same logic in C++ works fine 207473463

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

    The IO you are doing is slow; instead of Scanner, try BufferedReader,instead of System.out, Use PrintWriter.

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

In Problem F why the approach in which we remove maximum (n-k) elements and then minimizing the sum of k elements is not valid

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

    We are finding some $$$min(max(a,b))$$$. Minimize $$$a+b$$$ does not necessary give the optimal answer. Take an example of $$$5 + 8 = 13$$$ and $$$7 + 7 = 14$$$, even though the sum is higher, we get a better result

»
11 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

For problem B, is it possible to create an array of 3 numbers for the following string >><>><>? I found that at least four numbers are needed. For example 5>4>3<4>3>2<3>2.

»
11 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Can we solve problem F using the idea of Binary search (similar to the way we did in minimum allocation of pages)

»
11 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Anyone please let help me find what is wrong with my solution of Comparison String problem. It has same idea. Here is my solution 207744443

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

    The same mistake I made!

    You should find the longest continious increasing or decreasing sequence

    The problem with this idea that if you were increasing ,for instance 5 times, then decreasing 1, then increasing 5 again, you will count 1 to 5 then 4 then 5 to 9 which results in 10 but the answer should be that when decreasing you should begin counting again from 0 then the optimal answer be 6 not 10

»
11 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Hey, can anyone tell me why my solution for question F is incorrect. I first found the k smallest element in o(nlogn ), then I put these in original order. I precalculated the sum of array and took a variable h=0; Then I iterated for n times the array , in which I did h+=a[i] and found minimum=min(minimun,max(h,s-h)); It should be done in o(nlogn). Here is my code

»
11 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Is C question was only on observation??

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

For problem F, I think there is a typo in the third last paragraph of tutorial. Index n should be used for head and n+1 should be used for tail.

»
11 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Why in problem E, the testcase: 3 -1 7 -1 8 2 -1 -1 5 has answer 4? Since team 2 and 4 are in positions 7 and 8 and 4 will be eliminated in the first round only.

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

You can write F in $$$O(n~log^2~n)$$$ — 208816454

Also the solution works fast (1590ms)

»
8 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
3
1 5 3 7 2 4 6 8

Why does the answer of it is 0? Why doesn't it is 1?

»
8 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

the third test case in problem C is incorrect

1??10? given this pattern would not 111100 be a better answe because the cost is 1 (just reverse the whole string)

instead we have 111101 as the correct answer where the cost is 2

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

Can someone please explain how the answer of E for test case

3

-1 7 -1 8 2 -1 -1 5

is 4. Here team 2 and team 4 are going to play together in first round, so team 4 for will be eleminated quarter finals. So shouldn't the answer is 0?

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

    I made the same mistake as you: each term $$$a_i$$$ is the team that has seed i, not the seed that the team i receives.

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

for problem B
consider this test case <<<>>>> and answer should be 4 but Question accepting answer 5 not 4??