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

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

1452A - Robot Program

Идея: BledDest

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

1452B - Toy Blocks

Идея: adedalic

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

1452C - Two Brackets

Идея: BledDest

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

1452D - Radio Towers

Идея: BledDest

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

1452E - Two Editorials

Идея: BledDest

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

1452F - Divide Powers

Идея: adedalic

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

1452G - Game On Tree

Идея: adedalic

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

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

What about rating updates?

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

I think, in tutorial G, Alice and Bob had been swapped. It's Alice who has one starting vertex, and it's Bob who chases her, not vice versa.

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

why i get tle in test case 20 of problem E

my submission : 99017296

logic

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

If anyone didn't understand B. Here is an easy solution-

For solution to exist let us consider an element any index i. Then, for solution to exist we should be able to increment all values at all indexes to the max value in the array because we can't decrement the max value anyway.

Thus, a[i]>=(a[max]-a1)+(a[max]-a2)......(a[max]-a[max])
or, a[i]>=a[max]*n-1-(S-a[i]) where S is total sum of all elements.
or, S>=a[max]*n-1;

So solution boils down to two cases-

1. When S<a[max]*n-1 then the answer is just the difference of these two.

2. If S>a[max]*n-1 then (S-a[max]*n-1) should be equally divisible by (n-1), so just increment S until divisible by (n-1) and the answer is difference of the two.

You can look at my submission here

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

My code gives me wrong answer in test case 3 any one help Question no B

void solve() { ll n; cin>>n;

ll a[n];
ll mx = 0 ;
for (ll i= 0;i<n;i++)
{
    cin>>a[i] ;
    mx = max(mx, a[i]);
}

if (n==2)
{
    cout<<0<<endl;
    return ;
}

ll sum = accumulate(a , a+n , 0) ;

ll temp = mx*(n-1)*(1ll) ;

if (sum == temp)
cout<<0<<endl;
else if(sum <temp)
cout<<abs(sum-temp)<<endl;
else
{
    for (ll i=1;i<=100000;i++)
    {
        if (((n-1)*i*(1ll)) == sum)
        {
            cout<<0<<endl;
            return ;
        }
        else if (((n-1)*i*(1ll)) > sum)
        {
            cout<<(abs(n-1)*i*(1ll) - sum)<<endl;
            return;
        }
    }
}

}

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

Proof by Induction for problem D is great but can someone tell me if there is a mathematical proof for that?

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

    If we have a tower with power p, it will cover 2p-1 spots, thus the length of the subsegment covered by a tower is always odd.Now we need to find a way to divide a length of n into a number of odd length subsegments. This can be done via dynammic programming, which reduces to finding the nth Fibonacci number. In the end , just divide this by 2^n.

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

      I'm sorry but I didn't need the proof for the solution to this problem. I was wondering how did that function get reduced to just finding the Fibonacci number for a particular input.(which was proved by Induction by the Editorialist)

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

        f(n) = f(n-1) + f(n-3) + f(n-5) + .... f(n-2) = f(n-3) + f(n-5) + .... thus f(n) = f(n-1) + f(n-2) Is this what you were looking for?

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

          Yeah exactly, thanks for putting it that way. Made it so easy to understand.

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

            i didnt get it, can you explain how that comment helped you? i m still confused

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

              Just look at what he wrote:

              f(n)= f(n-1) + f(n-3) + f(n-5) + .... The above equation should be sufficient to tell you why is this the answer to the problem. You can occupy 1,3,5 ... positions at a time let us call it k and question is reframed as finding the solution to the remaining f(n-k) values which is what the equation above tells.

              Now, Just replace n by n-2 in the above equation and we get,
              f(n-2) = f(n-3) + f(n-5) + .... This is just the same as f(n) without the f(n-1) part.

              thus f(n) = f(n-1) + f(n-2)

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

        Here's the intuitive proof I came up with while upsolving.

        First let's solve it like a normal dp problem (forgetting about the fibonacci numbers). We sweep from left to right. Let $$$dp[i]$$$ mean the number of ways to cover only the first $$$i$$$ positions.

        How do our transitions work? First, $$$dp[0] = 1$$$ because there is one way to cover the first $$$0$$$ positions (don't have any towers at all). Now loop over all i's (from left to right).

        Consider the current $$$dp[i]$$$. We can cover some range with exactly one tower if and only if the length of the segment is odd (so we can put the tower in the midpoint). So $$$dp[i]$$$ is equal to the sum of all dp values where the index has a different parity than i (so $$$dp[i] = dp[i-1]+dp[i-3]+dp[i-5]...$$$). Though this would work, it runs in $$$O(n^2)$$$, so it needs to be optimized.

        The optimization is pretty simple. Because you don't care about the actual values of all previous dp's but just the parity. So instead of storing the whole dp array, just have it store 2 values, $$$dp[0]$$$ and $$$dp[1]$$$. $$$dp[0]$$$ stores the numbers of ways to cover an even prefix, and $$$dp[1]$$$ stores the number of ways to cover an odd prefix.

        You still loop from 1 to n but the transitions are now $$$dp[i \mod 2] = dp[i \mod 2] + dp[1-(i \mod 2)]$$$. This is exactly the Fibonacci recurrence.

        Hope this was helpful! Thanks!

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

    I had a different approach to this problem which is quite intuitive imo. We are effectively choosing a subset out of $$$n$$$ towers such that the sum of powers of those towers cover the entire range $$$1-n$$$.This is equivalent to number of odd integral solutions of the equation $$$ p_1 + p_2 + ... + p_r = n $$$.This can be found out easily by converting it to the form $$$ (2*x_1 + 1) + (2*x_2 + 1) + .. + (2*x_r + 1) = n $$$ and finding non negative integral solution for $$$x_1 , x_2 ,... x_r$$$. Multiply this with the probability of choosing $$$r$$$ towers = $$$1/{2^{n}}$$$ to get the required answer solution

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

Why this Solution do not get TLE in problem G? I solved only with bfs/dfs. There is some property?

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

In Problem D Editorial

How this statement is true?

Covering all towns can be expressed as splitting n into the sum of several positive odd integers

Anyone, Please Explain.

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

    Each town that has power must power itself, and an equal number of towns either side of it.

    Suppose it powers K towns to its left, then it must also power K towns to its right. So in total it powers 2*K+1 towns.

    All the towers together power N towns, and the set is partitioned such that no town is powered twice. As such,

    N = sum (i = 1 to j) (2*Ki + 1), where there are j towns receiving power directly, and the ith town powers Ki either side of it.

    The numerator of the answer is the number of unique sets of j and K1,...,Kj such that the equality holds.

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

What is the logic of taking max of the array in calculation of k for B?

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

    We need an end state where each of the n-1 boxes has exactly (TOTAL)/(n-1). Suppose we choose one of the non-max boxes to redistribute: then all the boxes must have at least the max starting value, since we cannot take any away. Therefore max*(n-1) is a lower bound for the total number.

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

IN PROBLEM B Why (a.sum() + n — 2) / (n — 1) and not (a.sum()) / (n — 1) i didn't get it

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

Can anybody pls help me.

I am getting index out of bounds even though it looks correct.

link

Problem E.

My idea is mostly the same.

Sorting based on l+r. Storing count of tasks upto ith task in pref.

table[i][j] stores sum of tasks of first j (li,ri) pairs for k consecutive tasks = [i,i+k-1].

finally all pairs from 1 to ind-1 go to [i,i+k-1] and rest to [j,j+k-1] [ind is index where l+r > i+j+k-1 ... (r-j > i+k-1-l)]

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

Why does this give same results, in modular exponentiation?

ll powe(ll x, ll y){
x = x%mod, y=y%(mod-1);
ll ans = 1;
while(y>0){
if (y&1){
ans = (1ll * x * ans)%mod;
}
y>>=1;
x = (1ll * x * x)%mod;
}
return ans;
}

than this

ll powe(ll x, ll y){
x = x%mod;
ll ans = 1;
while(y>0){
if (y&1){
ans = (1ll * x * ans)%mod;
}
y>>=1;
x = (1ll * x * x)%mod;
}
return ans;
}

The only difference is we do y=y%(mod-1) in the first function.

Edit: figured it out its fermat's theorem, thanks everyone

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

Why solutions with complexity O(nmk) can get AC for problem E? The example are below: submission 1 and submission 2

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

Can someone explain problem E's solution a bit more.Thanks in advance! xD!!

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

Problem E,why the solution is right?I spend a long time understanding the code,but can't prove it's correctness.The tutorial seems to ignore this.

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

    you may think that algorithm is incorrect because of prefix and suffix and that one of them may contain some element which should have been in the other one but it doesn't matter because answer for cases like this will not be the final answer anyway because we are considering all possibilities and final answer will be the case where the conditions are satisfied.

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

Why i get tle for B?

submission : 99050069

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

There is another nice approach to Problem D using combinatorics.

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

cin >> a >> b; cout << a + b + abs(a — b) — (a ^ b ? 1 : 0) << endl;

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

Why the divide function in problem D is mul(x, binpow(y, MOD — 2)) ? I understand the idea behind mul() and binpow(), but why they can perform the divide operation?

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

Can anyone please tell me what's wrong in my solution for problem C. Submission : 99756850

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

nothing!

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

I have a $$$O(nm)$$$ solution for problem E. Basically, we do scanline. For each participant, we brute force over the left author, and see when can we have the left author as a winner for that participant, and then we do the same for right author. It's really cool IMO. 100792298

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

More expalnation for problem D :

Let's say S(n) is the set of ways to write n as a sum of odd numbers.

We can partition this set into two subsets: A(n) and B(n), where A(n) is the set of sums where the last summand is a 1, and B(n) is the set of all other sums.

you see that A(n) (last number always equal 1) has the same size as S(n−1). you see that B(n) (last two cities are joined to a previouse one) has the same size as S(n−2).

So, you find that |S(n)|=|A(n)|+|B(n)|=|S(n−1)|+|S(n−2)|, which is the Fibonacci recurrence relation. You can then prove by induction that your sequence is equal to the Fibonacci sequence.

Example: S(5)={1+1+1+1+1,1+1+3,1+3+1,3+1+1,5}, A(5)={1+1+1+1+1,1+3+1,3+1+1}, B(5)={1+1+3,5}. You can see that A(5) has the same size as S(4) and B(5) has the same size as S(3).