Olympiad in Informatics

Codeforces Round #549 (Div. 2) Problem-Solving Report (CF1143)

E - Lynyrd Skynyrd

Problem Summary

Given a permutation \(p[1-n]\) and a \([1,n]\)-valued array \(a[]\), answer \(q\) queries: does the subsegment \([l,r]\) of the array contain a cyclic shift of the permutation?

Solution

Consider scanning forward: for the number at each position, find the position of the “nearest previous cyclic-shift number” to its left, and at the same time update its own position for the next iterations to use:

// Input these data:
scanf("%d%d%d", &n, &m, &q);
// Make position mapping;
for (int i = 1; i <= n; i++)
    scanf("%d", &perm[i]), pos[perm[i]] = i;
// Make it circular;
perm[0] = perm[n];
for (int i = 1; i <= m; i++)
{
    scanf("%d", &arr[i]);
    // Get the previous position of current number in the permutation;
    int x = perm[pos[arr[i]] - 1];
    // Save data and update the nearest position;
    fa[0][i] = pre[x], pre[arr[i]] = i;
}

Then we can consider binary lifting: how many steps the number at a position jumps back along the cyclic sequence. This is just the routine stuff. After that, we can consider building a binary-lifting array \(g[][]\): it records where in the array a position lands after jumping back \(n-1\) steps:

for (int i = 1; (1 << i) <= m; i++)
    for (int j = 1; j <= m; j++)
        fa[i][j] = fa[i - 1][fa[i - 1][j]];
for (int i = 1; i <= m; i++)
{
    int l = n - 1, p = i;
    for (int j = 19; j >= 0; j--)
        if (l & (1 << j))
            p = fa[j][p];
    // Get the position of the previous (n - 1) big steps;
    g[0][i] = p;
}

Then we can work with it just like a sparse table: for a query pair \((l,r)\), we can find the largest number in \(g\) (that is, the one closest to \(r\)), then check validity by testing whether it is greater than \(l\).

Full Code

// E.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2e5 + 200;
int perm[MAX_N], pos[MAX_N];
int arr[MAX_N], pre[MAX_N], n, m, q, fa[20][MAX_N], g[20][MAX_N], lg[MAX_N];
int main()
{
    scanf("%d%d%d", &n, &m, &q);
    for (int i = 1; i <= n; i++)
        scanf("%d", &perm[i]), pos[perm[i]] = i;
    perm[0] = perm[n];
    for (int i = 1; i <= m; i++)
    {
        scanf("%d", &arr[i]);
        int x = perm[pos[arr[i]] - 1];
        fa[0][i] = pre[x], pre[arr[i]] = i;
    }
    for (int i = 1; (1 << i) <= m; i++)
        for (int j = 1; j <= m; j++)
            fa[i][j] = fa[i - 1][fa[i - 1][j]];
    for (int i = 1; i <= m; i++)
    {
        int l = n - 1, p = i;
        for (int j = 19; j >= 0; j--)
            if (l & (1 << j))
                p = fa[j][p];
        g[0][i] = p;
    }
    for (int i = 2; i <= m; i++)
        lg[i] = lg[i >> 1] + 1;
    for (int i = 1; (1 << i) <= m; i++)
        for (int j = 1; j + (1 << i) - 1 <= m; j++)
            g[i][j] = max(g[i - 1][j], g[i - 1][j + (1 << (i - 1))]);
    while (q--)
    {
        int l, r;
        scanf("%d%d", &l, &r);
        int k = lg[r - l + 1];
        int ans = max(g[k][l], g[k][r - (1 << k) + 1]);
        if (ans >= l)
            putchar('1');
        else
            putchar('0');
    }
    return 0;
}

F - U2

Problem Analysis

Given \(n\) points, consider that every two points with different \(x\) coordinates form a parabola with a fixed quadratic coefficient. How many of the parabolas the points form with each other contain no point other than their own inside?

Solution

This problem is actually pretty interesting. We consider deleting the useless points: points that are bound never to be considered. Let the points be \(A(x_1,y_1),B(x_2,y_2),C(x_3,y_3)\), where \(A,B\) form the parabola \(y=x^2+bx+c\). The condition for \(A,B\) to survive is \(y_3<x_3^2+bx_3+c\). We can get lots of inequalities like this, and then notice they can be summarized as \(y_3-x_3^2<bx_3+c\), so when reading in the points, we transform each point \((x,y)\) into \((x,y-x^2)\), and then just compute the upper convex hull.

Code

// F.cpp
#include <bits/stdc++.h>
#define ll long long
#define pr pair<ll, ll>
using namespace std;
const int MAX_N = 101000;
pair<ll, ll> prs[MAX_N];
ll n, tmpx, tmpy, q[MAX_N], tot;
bool check(int a, int b, int c)
{
    ll x1 = prs[b].first - prs[a].first, y1 = prs[b].second - prs[a].second;
    ll x2 = prs[c].first - prs[b].first, y2 = prs[c].second - prs[b].second;
    return x1 * y2 - x2 * y1 >= 0;
}
int main()
{
    scanf("%lld", &n);
    for (int i = 1; i <= n; i++)
    {
        scanf("%lld%lld", &tmpx, &tmpy);
        prs[i].first = tmpx, prs[i].second = tmpy - tmpx * tmpx;
    }
    sort(prs + 1, prs + 1 + n);
    for (int i = 1; i <= n; i++)
    {
        while (tot >= 1 && prs[q[tot]].first == prs[i].first)
            tot--;
        while (tot >= 2 && check(q[tot - 1], q[tot], i))
            tot--;
        q[++tot] = i;
    }
    printf("%d", tot - 1);
    return 0;
}