Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Problems and Solutions from AtCoder Beginner Contest 053

Tech May 19 2

A

int N; std::cin >> N;
std::cout << (N < 1200 ? "ABC" : "ARC") << "\n";

B

Problem:

Snuke wants to take a substring from ( s ) that starts with 'A' and ends with 'Z'.

Given ( s ), find the length of the longest such substring. It is guaranteed that at least one such substring exists.

( 2 \leq |s| \leq 2 \times 10^{5} )

Solution:

It might seem like a two-pointer problem, but it's actually simpler: the answer is the distance from the first 'A' to the last 'Z', i.e., ( pos_Z - pos_A + 1 ).

std::string s; std::cin >> s;
int r = s.size() - 1, l = 0;
while (l < (int)s.size() && s[l] != 'A') l++;
while (r >= 0 && s[r] != 'Z') --r;
std::cout << r - l + 1 << "\n";

C

Problem:

A standard die has faces numbered ( 1 ) through ( 6 ), and opposite faces sum to ( 7 ).

You can start witth any face on top, and then repeatedly roll the die 90 degrees in any direction (the die must always land with a face flat on the table).

Given a positive integer ( x ), find the minimum number of rolls needed so that the sum of the numbers on the top face over the entire sequence is at least ( x ).

Solution:

The numbers 6 and 5 are not opposite each other (since 6+5=11 ≠ 7), so they are adjacent. This means we can alternate between them.

We can achieve a sum of 11 in two rolls (6 then 5, or 5 then 6). So we can use ( \lfloer \frac{x}{11} \rfloor ) cycles of two rolls (6+5). That leaves a remainder.

After these cycles, we can choose to roll a 6 next. Then:

  • If the remainder ( r = x \bmod 11 ) is 0, no extra rolls.
  • If ( 1 \leq r \leq 6 ), one extra roll (the 6).
  • If ( 7 \leq r \leq 10 ), two extra rolls (6 and then 5, or 5 and then 6, whichever gives enough sum).

Total rolls = ( 2 \times \lfloor \frac{x}{11} \rfloor + \text{extra} ).

D

Problem:

You have a pile of cards. You may perform the following operation any number of times (including zero): select any three cards, eat the largest and smallest numbered cards, and return the remaining card to the pile.

Find the maximum number of cards you can keep such that all remaining cards have distinct numbers.

Solution:

Let's count the cards that must be discarded. For each distinct value, we can keep at most one card. So the number of cards that need to be removed is ( tot = \sum (cnt_i - 1) ) where ( cnt_i ) is the count of cards with value ( i ).

The operation removes exactly two cards each time (the max and min of the three selected). So we can remove at most ( 2k ) cards in ( k ) operations.

If ( tot ) is even, we can pair up the discard cards and remove them all, because we can always select two cards that need to be discarded and one card that we want to keep.

If ( tot ) is odd, one discard card remains after pairing. However, to remove that last discard card, we need to also remove another card (which could be a keeper that we otherwise would have kept). So effectively, we have to remove one extra card beyond the discard pile. Thus the number of cards we can keep is: ( n - \lceil \frac{tot}{2} \rceil \times 2 ).

In other words:

  • If ( tot ) is even, remaining cards = ( n - tot ).
  • If ( tot ) is odd, remaining cards = ( n - tot - 1 ).

This is equivalnet to: ( n - \lceil \frac{tot}{2} \rceil \times 2 ).

Tags: AtCoderABC

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.