ucode.vn's blog

By ucode.vn, history, 2 years ago, In English

1650A - Deletions of Two Adjacent Letters

Idea : MikeMirzayanov

Tutorial:

There will be one character left in the end, so we have to delete all the characters going before and after it. That is, delete some prefix and suffix. Since we always delete some substring of length $$$2$$$, we can only delete the prefix and suffix of even length, it means the answer is $$$YES$$$ in the case when there is an odd position in $$$s$$$ with the character $$$c$$$ and $$$NO$$$ otherwise.

Solution (ucode.vn):

#include<bits/stdc++.h>

using namespace std;

int main() {
    int t; cin >> t;
    while (t--) {
        string s; char t; cin >> s >> t;
        int ok = 0;
        for (int i = 0; i < s.length(); i++) {
            if (i % 2 == 0 && s[i] == t[0]) ok = 1; 
        }
        cout << (ok ? "YES\n" : "NO\n");
    }
}

1650B - DIV + MOD

Idea:Vladosiya

Tutorial:

Consider $$$f_a(r)$$$. Note that $$$⌊\frac{r}{a}⌋$$$ is maximal over the entire segment from $$$l$$$ to $$$r$$$, so if there is $$$x$$$ in which $$$f_a$$$ gives $$$a$$$ greater result, then $$$x \mod a > r \mod a$$$.

Note that numbers from $$$r − r \mod a$$$ to $$$r$$$ that have an incomplete quotient when divided by $$$a$$$ equal to $$$⌊\frac{r}{a}⌋$$$ do not fit this condition (and are guaranteed to have a value fa less than $$$f_a(r)$$$. And the number $$$x = r − r \mod a − 1$$$:

Has the maximum possible remainder $$$x \mod a = a − 1$$$; Has the maximum possible $$$⌊\frac{r}{a}⌋$$$ among numbers less than $$$r−r \mod a$$$. So there are two candidates for the answer — these are $$$r$$$ and $$$r − r \mod a − 1$$$. The second candidate is suitable only if it is at least $$$l$$$. It remains only to compare the values of $$$f_a$$$ and select the maximum.

Solution (ucode.vn)

#include<bits/stdc++.h>

using namespace std;

void solve() {
    int l, r, x;
    cin >> l >> r >> x;
    int res = r / x + r % x;
    int m = r / x * x - 1;
    if (m >= l) res = max(res, m / x + m % x);
    cout << res << "\n";
}

int main() {
    int t; cin >> t;
    while (t--)
        solve();
    }
}

1650C - Weight of the System of Nested Segments

Idea:ucode.vn

Tutorial:

We create the structure to stores for each point its coordinate, weight, and index in the input data. So we sort the $$$points$$$ increasing by weight. Now we sort the $$$points$$$ increasing by its coordinate.

So the $$$i^{th}$$$ segment is $$$points[i]$$$ and $$$points[2 \times n - i + 1]$$$

Print the output!!!!!!!!!!

Solution (ucode.vn):

#include<bits/stdc++.h>
using namespace std;
struct point {
    int w, p, id; // int weight, position, index;
};

void solve() {
    int n, m; cin >> n >> m;
    vector<point> points(m);
    for (int i = 0; i < m; i++) {
        cin >> points[i].p >> points[i].w;
        points[i].id = i + 1;
    }
    sort(points.begin(), points.end(), [] (point a, point b){
        return a.w < b.w;
    });
    int s = 0;
    for (int i = 0; i < m; i++) {
        if (i < 2 * n) s += points[i].w;
        else points.pop_back();
    }
    cout << s << "\n";
    sort(points.begin(), points.end(), [] (point a, point b){
        return a.p < b.p;
    });
    for (int i = 0; i < n; i++) {
        cout << points[i].id << ' ' << points[2 * n - i - 1].id << "\n";
    }
}

int main() {
    int t; cin >> t;
    while (t--) solve();
}

1650D - Twist the Permutation

Idea:MikeMirzayanov

Tutorial: The first thing to notice — the answer always exists. For $$$n$$$ numbers $$$1 ⋅ 2⋅3 \cdot \dots n=n!$$$ answer choices, as well as $$$n!$$$ permutation combinations. It remains only to restore the answer from this permutation.

We will restore by performing reverse operations. On the $$$i-th$$$ $$$(i=n, n−1, …, 2, 1)$$$ operation will be selectd the first $$$i$$$ elements of the array and rotate them $$$d[i]$$$ times to the left ( elements with numbers $$$i+1$$$ and more remain in their places).

Where $$$d[i]$$$ is equal to $$$0$$$ if $$$i=1$$$, otherwise $$$d[i]=(index+1) \mod i$$$, and $$$index$$$ – is the index of the number $$$i$$$.

Thus, for each $$$i$$$ from right to left, performing a left cyclic shift operation, we move the number $$$i$$$ at index $$$i$$$. Solution (ucode.vn):

#include <bits/stdc++.h>

using namespace std;

void solve() {
    int n;
    cin >> n;
    int a[n];
    for (int i = 0; i < n; ++i) cin >> a[i];
    int res[n];
    for (int i = n; i > 0; --i) {
        int ind = 0;
        for (int j = 0; j < i; ++j) {
            ind = a[j] == i ? j : ind;
        }
        int b[i];
        for (int j = 0; j < i; ++j) {
            b[(i - 1 - ind + j) % i] = a[j];
        }
        for (int j = 0; j < i; ++j) {
            a[j] = b[j];
        }
        res[i - 1] = i != 1 ? (ind + 1) % i : 0;
    }
    for (int i = 0; i < n; ++i) cout << res[i] << ' ';
    cout << '\n';
}
int main() {
    int t; cin >> t;
    while (t--) solve();
}

Full text and comments »

  • Vote: I like it
  • -63
  • Vote: I do not like it

By ucode.vn, history, 2 years ago, In English

Hello! Codeforces Round $$$#776$$$ (Div. $$$3$$$) will start at Tuesday, March $$$8$$$, $$$2022$$$ at $$$21:35UTC+7$$$. You will be offered $$$7 \to 8$$$ problems with expected difficulties to compose an interesting competition for participants with ratings up to $$$1600$$$. However, all of you who wish to take part and have a rating of 1600 or higher, can register for the round unofficially.

The round will be hosted by rules of educational rounds (extended $$$ACM-ICPC$$$). Thus, solutions will be judged on preliminary tests during the round, and after the round, it will be a $$$12$$$-hour phase of open hacks.

You will be given $$$7 \to 8$$$ problems and $$$2$$$ hours and $$$15$$$ minutes to solve them.

Note that the penalty for the wrong submission in this round (and the following Div. $$$3$$$ rounds) is $$$10$$$ minutes.

Remember that only the trusted participants of the third division will be included in the official standings table. As it is written by link, this is a compulsory measure for combating unsporting behavior. To qualify as a trusted participant of the third division, you must:

take part in at least five rated rounds (and solve at least one problem in each of them), do not have a point of $$$1900$$$ or higher in the rating. Regardless of whether you are a trusted participant of the third division or not, if your rating is less than $$$1600$$$, then the round will be rated for you.

Thanks to MikeMirzayanov for the platform, help with ideas for problems and for coordination of our work. Problems have been created and written by ITMO University teams: MikeMirzayanov, MisterGu, myav, Gol_D, Aris, SixtyWithoutExam, me Vladosiya.

Also many thanks to mango_lassi, espr1t, karemo, starboy_jb, Fly_37, omikad, Katya_Goryachkina, Omja, teraqqq, Bugman, Jostic11, yorky, 4dr and doreshnikov for testing the contest and valuable feedback.

Good luck!

The editorial

Full text and comments »

  • Vote: I like it
  • -39
  • Vote: I do not like it

By ucode.vn, history, 2 years ago, In English

sum(a[i,j]) = prefix_sum[j] — prefix_sum[i-1] => sum(a[i,j]) = 0 when 1 prefix_sum in the previous prefix_sum or this prefix_sum equal 0

I have a set data structure that stores prefix_sum1 . values and 1 variable prefix_sum Browse each index i from 0 -> n-1 : add prefix_sum a[i] if prefix_sum is in prefix_sum1 then return True If prefix_sum is 0, return True add prefix_sum1 prefix_sum Then all box fields that return True have been processed. And Returns False

Full text and comments »

  • Vote: I like it
  • -8
  • Vote: I do not like it