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

Автор awoo, история, 3 года назад, По-русски

1598A - Computer Game

Идея: BledDest

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

1598B - Groups

Идея: fcspartakm

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

1598C - Delete Two Elements

Идея: fcspartakm

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

1598D - Training Session

Идея: BledDest

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

1598E - Staircases

Идея: BledDest

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

1598F - RBS

Идея: BledDest

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

1598G - The Sum of Good Numbers

Идея: BledDest

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

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

The easiest solution for C(in my opinion): 131432277

P.S. sum * (n-2) = (sum — a[i] — a[j]) * n => we can find for every a[i] all a[j] in O(n log n) using binary search

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

    If you simplify that equation, you would get (a[i] + a[j]) = (2 * sum) / n, so basically this is just a 2-sum problem where the target value is (2 * sum) / n,

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

      Yep, u r right (:

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

        Can you please check my soln. I have used 2-sum approach but it is giving me WA on test 2. 131563369

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

          ++

          I also tried the same way and getting wa on 78th in 2tc..hbarp can you pls tell me why it's wa.

          solution: 131653903

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

            I havent looked at the correctness of your algorithm but ans = ans + c * d; line has int overflow for sure.

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

              XD. Thanks, for mentioning this. now, after using it long it still showing wa on that tc. Can you please tell me why is it so?

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

                Changing ans = ans + c * d; to ans = 1LL*ans + c * d; doesn't fixes anything. ans is already long long int. Over flow is in multiplication of c*d, change this to ans = ans + c*1LL*d.

                Also ll val = (n * (n - 1)) / 2; has int overflow. Just having val of type long long int doesn't do anything. n and n-1 are still of int type. Change this to n*1LL*(n-1)/2.

                Also, next time just uses C++ 64 bit and long long everywhere.

                Also, if (st.size() == 1) is incorrect fix. You will still get WA on

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

sorry i forgot that x is a good number :-_

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

IDk why but my solution for C gives TLE on Test 17 even though it's the exact same solution as the tester's : https://codeforces.com/contest/1598/submission/131542518

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

Why using unordered_map gives TLE in this question... searching elements using hash map should be faster than the map which takes log(n) time to search instead.
131514464

Also I don't get it... how by adding just these 2 lines which where on a blog,
mp.reserve(1024);
mp.max_load_factor(0.25);
solves the problem of TLE and my soln gets accepted.
131524838

Then I tried a custom hash function rather than the built-in hash function. Which also got accepted. Also the same solution gets accepted in python using dictionaries which also uses hashmap.
Does this mean that the C++ built-in hash function is not good ?
131520347

I tried to find but didn't get some concrete answers when to use map or unordered_map ?
Or using custom hash is better not ?
Please if anyone can help it in.

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

    There are many hacks on unordered_map and I suppose we should avoid using it in Codeforces contests. Here a blog written by neal showing how it works.

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

    It's just because c++'s hash function is deterministic so people can hack on this. max_load_factor() basically increase the number of buckets which will break the malicious hacks.

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

    Note to self: ALWAYS use custom hash function when dealing with unordered map no matter how unnecesary in a codeforces contest

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится +18 Проголосовать: не нравится
    mp.reserve(1024);
    mp.max_load_factor(0.25);
    

    only changes the test that's required to hack the solution, it doesn't make it unhackable.

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

Nice solution for D!

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

I suppose that the time complexity of F should be $$$O(n2^n + A\log A)$$$ instead of $$$O(2^n + A\log A)$$$. (:

Could someone tell me if there are some mistakes?

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

    Can you tell me what is A in this time complexity? I understand where $$$N$$$ and $$$2^N$$$ comes from but I just can't find anything related to $$$A$$$.

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

    You are right, it's $$$n\cdot 2^n$$$ instead of $$$2^n$$$.

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

Any other solution for problem D?

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

In Problem D, I don't understand why the only bad triples are (xa,ya),(xb,ya),(xa,by). Isn't there a possibility of having three same topics, such (1, 1), (1, 2), (1, 3)? I've been looking at the explanation for so long but still can't figure it out :(

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

    but their difficulties are different so they aren't bad triples.

    The problem statement says "at least one of the conditions are met"

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

For D, can someone explain to me why there can't be a triple of the form [(Xa, Ya) , (Xb , Yb) , (Xa, Yb)] ? EDIT: I figured it out, it's actually the same thing except that the third pair is the central one, instead of the first.

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

For 1598C:

No, you don't have to use map in such a hacky way (or in another word, full of patches).

131572801

======================================

Instead of keeping a map of all elements, we keep a map for number of first i items ([0, i-1])

So the core logic can be much more easier:

in C#:

for (int i = 0; i < n; i++){
    // update the answer
    if (cnt.ContainsKey(target - data[i])) answer += cnt[target - data[i]];
    // update the cnt map, for (i + 1) to use.
    if (!cnt.ContainsKey(data[i])) cnt.Add(data[i], 0);
    cnt[data[i]]++;
}

or in C++:

for (int i = 0; i < n; i++){
    // update the answer
    answer += cnt[target - data[i]];
    // update the cnt map, for (i + 1) to use.
    cnt[data[i]]++;
}
»
3 года назад, # |
Rev. 12   Проголосовать: нравится 0 Проголосовать: не нравится

i tried to add photo for my comment but the comment become empty any one can help me

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

In problem D can anyone please explain why we won't be overcounting in the editorial solution?

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

    problems and diff. are unique pair, no overlap when multiplication as in editorial

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

Alternative solution for 1598D - Training Session. I did see restriction that all problems pairs of parameters are different, but I had no idea how to use this fact. My solution doesn't rely on this fact at all. Solution turned out to be much harder so I wasted a lot of time to implement it during round.

Here is main idea. Think of how to calculate all triples with different topics.

There is easy way

Apply the same idea to problem's difficulties. We count something twice. What to do?

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

Detailed description how to implement 1598E - Staircases in $$$O((n + m + q) \log (n+m))$$$ time and space.

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

for que B https://codeforces.com/contest/1598/submission/131729850 can u check this once i don't which test i am missing so pls

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

A slightly different solution of 1598F - RBS.

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

in problem C why i am getting tle.. Pls tell[problem:1598C] https://codeforces.com/contest/1598/submission/131512968

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

    This is due to the use of unordered_map instead of map. Try map and you will get AC.

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

      even , i used unordered map earlier it also gave me tle but when i am using map it is not giving tle . can anyone explain why is it so ? please.

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

        I think it may be because of special anti-hash testcases.

        Unordered map relies on hashing to determine where the value of a key is stored. When there are hash collisions, the the hash function makes the collided keys share the same position. This is done with a linked list, which needs to be linearly searched if you are looking for a key that has collided with other keys.

        Essentially, you can design testcases that abuse this workaround, causing unordered_map access complexity to approach O(n)

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

132438878 can anybody help me with this I'm getting tle even though the time complexity of my son is same as that of the solution :)

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

My solution for F fails at test 32. Can someone please look at the submission and point out the error?

I would be happy to explain what my code does if it is not clear. Thanks.

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

A Dp approach to problem E in $$$O(NM + Q*min(N,M))$$$

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

For problem D, is there anyway to find the number of triples that are different both in topic and in difficulty?

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

Problem A can be solved using DFS like this: 161393885.

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

What's up, I have no idea why the 2020 case fails me, could someone please help me.230300057

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

hey I have a doubt in ques C sorry if its too silly.. should not the ans+=min(freq of a[j] , freq of (2*sum/n a[j]). ie minimum of the counts should be added right? because if needed sum is 10 and freq of 7 is 4 and freq of 3 is 2 then pair with sum 10 should be only 2 right?

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

    the number of pair should 4 times 2 equals 8.

    You have four different ways to choose a 7 and two different ways to choose a 3.

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

1598C - Delete Two Elements

1598C — Delete Two Elements [python3]

Thanks for the tutorial!

Here is the translation to python3:

import os, sys, getpass, platform
import math, random, decimal, queue, heapq, bisect, itertools, functools, collections, string, operator, timeit, pprint
from queue import Queue, PriorityQueue, LifoQueue
from itertools import accumulate, chain, combinations, combinations_with_replacement, compress, count, cycle, dropwhile, filterfalse, groupby, islice, permutations, product, repeat, starmap, takewhile, tee, zip_longest
from functools import cmp_to_key, lru_cache, partial, partialmethod, reduce, singledispatch, total_ordering
from random import choice, choices, shuffle, sample, random, randint, randrange, uniform, seed, getstate, setstate, getrandbits
from string import ascii_letters, ascii_lowercase, ascii_uppercase, digits, hexdigits, octdigits, punctuation, printable, whitespace
from heapq import heapify, heappush, heappop, heapreplace, nlargest, nsmallest
from bisect import bisect, bisect_left
from collections import defaultdict, OrderedDict, deque, Counter
from math import gcd, factorial, isqrt, comb, perm, prod
cond = getpass.getuser() == '4a'
if cond:
    sys.stdin = open(os.path.join(os.getcwd(), 'a.txt'), 'r')


I = lambda: [int(a) for l in sys.stdin for a in l.strip().split()]
S = lambda: [a for l in sys.stdin for a in l.strip().split()]
IM = lambda: [[int(a) for a in l.split()] for l in sys.stdin]
SM = lambda: [[a for a in l.split()] for l in sys.stdin]
D = lambda k=1: {i: list(map(int, input().split())) for i in range(1, 1 + int(input()) * k)}
DS = lambda: {i: [(int(x[0]), x[1]) for _ in range(int(input()))
                  for x in [input().split()]] for i in range(int(input()))}

d8 = ((1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1))
d4 = ((1, 0), (0, 1), (-1, 0), (0, -1))
az, AZ = ascii_lowercase, ascii_uppercase
mod, inf = 1_000_000_007, float('inf')
CNR = lambda n, r: factorial(n) // (factorial(r) * factorial(n - r))
PNR = lambda n, r: factorial(n) // factorial(r)

A = IM()

if cond:
    print(A)


def solution(A):
    for i in range(1, len(A)):
        if i % 2 == 1:
            size = A[i][0]
        else:
            arr = A[i]
            res = 0
            counts = Counter(arr)
            sum_arr = sum(arr)
            if 2 * sum_arr % size != 0:
                print(0)
                continue

            target = 2 * sum_arr / size
            for i in range(size):
                first = arr[i]
                second = target - first
                if second in counts and counts[second] > 0:
                    res += counts[second]

                if first == second:
                    res -= 1

            print(res // 2)


solution(A)