Educational Codeforces Round 1

比赛传送门

A - Tricky Sum

题目传送门

分析

模拟即可,找到最大的 \(m\) 使得 \(2^m\le n\),则答案为 \(\frac{n(n+1)}{2}-2(2^{m+1}-1)\)。

代码

#include <bits/stdc++.h>
#define lson 2 * p
#define rson 2 * p + 1
// #define int long long
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int N = 1e5 + 2;
const int M = 2e1 + 2;
const int K = 1e3 + 2;
const int P = 131;
const int MOD = 1e4 + 7;
const int INF = 0x3f3f3f3f;
const LL LINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-6;
int t, n;

void Solve() {
    scanf("%d", &n);
    int l = 0, nn = n;
    while (nn) {
        l++, nn /= 2;
    }
    LL ori = 1LL * n * (n + 1) / 2, sub = 1;
    for (int i = 1; i <= l; i++) {
        sub *= 2;
    } 
    sub--;
    printf("%lld\n", ori - 2 * sub);
}

signed main() {
    // freopen("rfrt.in", "r", stdin);
    // freopen("rfrt.out", "w", stdout);
    scanf("%d", &t);
    while (t--) {
        Solve();
    }
    return 0;
}

B - Queries on a string

题目传送门

分析

模拟即可,但是赛时写的比较丑。

更优雅的方式,对于原串中的第 \(j(l\le j\le r)\) 位,有移动后的对应位置 \(j'=l+(j-l+k)\bmod(r-l+1)\)。

代码

#include <bits/stdc++.h>
#define lson 2 * p
#define rson 2 * p + 1
// #define int long long
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int N = 1e4 + 2;
const int M = 2e1 + 2;
const int K = 1e3 + 2;
const int P = 131;
const int MOD = 1e4 + 7;
const int INF = 0x3f3f3f3f;
const LL LINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-6;
int m, lens;
char s[N], t[N];

void Solve() {
    int l, r, k;
    scanf("%d%d%d", &l, &r, &k);
    k %= (r - l + 1);
    for (int i = l; i <= r; i++) {
        if (i - k >= l) {
            s[i] = t[i - k];
        } else {
            s[i] = t[r - l + i - k + 1];
        }
    }
    for (int i = 1; i <= lens; i++) {
        t[i] = s[i];
    }
}

signed main() {
    // freopen("rfrt.in", "r", stdin);
    // freopen("rfrt.out", "w", stdout);
    scanf("%s%d", s + 1, &m);
    lens = strlen(s + 1);
    for (int i = 1; i <= lens; i++) {
        t[i] = s[i];
    }
    while (m--) {
        Solve();
    }
    printf("%s", s + 1);
    return 0;
}

C - Nearest vectors

题目传送门

分析

赛时的思路是尝试将 \((x,y)\) 映射到单位圆上,于是就有天然的 \(x=\cos\alpha,y=\sin\alpha\)。

显然有一个观察:最小角一定是两个相邻角之差。

于是对于两个映射过后的相邻角,分别设为 \((\cos\alpha,\sin\alpha),(\cos\beta,\sin\beta)(\alpha>\beta)\),则它们的角度为 \(\alpha-\beta\),且这是一个 \([0,\pi]\) 的角。

考虑到比较大小,容易想到 \(\cos\theta\) 在 \([0,\pi]\) 上单调递减这一特点,求 \(\cos(\alpha-\beta)\),运用差角公式,然后对于每对相邻角,求最大值即可。

但是如何快速找到一对相邻角?换言之,如何将点按照逆时针排序?

有一种思路,对于不同象限的一对点,直接按照象限排序即可;否则可以根据 \(\sin\alpha\) 或者是 \(\cos\alpha\) 的单调性进行判断。但是又没有更简便的方法?


正确的思路应该使用反正切函数 \(\operatorname{atan2}(y,x)\),它的作用是找到一个 \(\theta(-\pi<\theta\le\pi)\),使得 \(\tan\theta=\frac{y}{x}\)。当然,如果 \(x=0\),它也会返回 \(\frac{\pi}{2}\) 或者是 \(\frac{3\pi}{2}\),所以它是非常安全的。如果是负角,只需要加上 \(2\pi\) 就可以转化成正角。

为什么不用 $\operatorname{atan}$ 函数? 当然可以用,但是由于 $\operatorname{atan}$ 的参数为 $\frac{y}{x}$,如果 $x=0$ 就需要特判,否则会 RE。显然 $\operatorname{atan2}$ 就不需要这么多特判。

然后就可以方便地排序,快速找到相邻角了。由于我们已经知道角度了,直接作差比较即可。

这种情况下不推荐用 EPS 作差比较,因为在过程中并没有产生浮点误差。用了反而会错。

代码

#include <bits/stdc++.h>
#define lson 2 * p
#define rson 2 * p + 1
// #define int long long
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int N = 1e5 + 2;
const int M = 5e1 + 2;
const int K = 1e3 + 2;
const int P = 131;
const int MOD = 1e4 + 7;
const int INF = 0x3f3f3f3f;
const LL LINF = 0x3f3f3f3f3f3f3f3f;
const long double EPS = 1e-11;
const long double PI = acos(-1);
int n, ansa, ansb;
long double ans = 2.0 * PI;
struct Angle {
    long double a;
    int idx;
    bool operator < (const Angle &cmp) const {
        return a < cmp.a;
    }
} ang[N];

signed main() {
    // freopen("rfrt.in", "r", stdin);
    // freopen("rfrt.out", "w", stdout);
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        long double x, y;
        scanf("%Lf%Lf", &x, &y);
        ang[i] = {atan2(y, x), i};
        if (ang[i].a < 0) {
            ang[i].a += 2.0 * PI;
        }
    }
    sort(ang + 1, ang + n + 1);
    long double sub = ang[1].a - ang[n].a + 2 * PI;
    if (ans > sub) {
        ans = sub, ansa = ang[1].idx, ansb = ang[n].idx;
    }
    for (int i = 1; i < n; i++) {
        long double sub = ang[i + 1].a - ang[i].a;
        if (ans > sub) {
            ans = sub, ansa = ang[i].idx, ansb = ang[i + 1].idx;
        }
    }
    printf("%d %d", ansa, ansb);
    return 0;
}

D - Igor In the Museum

题目传送门

分析

显然在一个连通的区域内答案一致,所以考虑像并查集一样一个块一个块地处理,搜索即可,注意对于一个障碍而言,从不同方向观察它会看到不同的画,所以需要开 \(4\) 个 \(n\times n\) 的标记数组。

代码

#include <bits/stdc++.h>
#define lson 2 * p
#define rson 2 * p + 1
// #define int long long
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int N = 1e3 + 2;
const int M = 2e1 + 2;
const int K = 1e3 + 2;
const int P = 131;
const int MOD = 1e4 + 7;
const int INF = 0x3f3f3f3f;
const LL LINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-6;
int n, m, k, bel[N][N], tot, dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0}, cnt[N * N];
bool vis[N][N][4];
char s[N][N];
vector<pair<int, int>> v;

void Dfs(int x, int y) {
    bel[x][y] = tot;
    for (int i = 0; i < 4; i++) {
        int nx = x + dx[i], ny = y + dy[i];
        if (s[nx][ny] == '*' && !vis[x][y][i]) {
            vis[x][y][i] = true, cnt[tot]++;
        }
    }
    for (int i = 0; i < 4; i++) {
        int nx = x + dx[i], ny = y + dy[i];
        if (s[nx][ny] == '.' && !bel[nx][ny]) {
            Dfs(nx, ny);
        }
    }
    for (int i = 0; i < 4; i++) {
        int nx = x + dx[i], ny = y + dy[i];
        if (s[nx][ny] == '*' && vis[x][y][i]) {
            vis[x][y][i] = false;
        }
    }
}

signed main() {
    // freopen("rfrt.in", "r", stdin);
    // freopen("rfrt.out", "w", stdout);
    scanf("%d%d%d", &n, &m, &k);
    for (int i = 1; i <= n; i++) {
        scanf("%s", s[i] + 1);
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (!bel[i][j] && s[i][j] == '.') {
                tot++;
                Dfs(i, j);
            }
        }
    }
    while (k--) {
        int x, y;
        scanf("%d%d", &x, &y);
        printf("%d\n", cnt[bel[x][y]]);
    }
    return 0;
}

E - Chocolate Bar

题目传送门

分析

设 \(f_{i,j,k}\) 表示把 \(i\times j\) 的矩形分成恰好 \(k\) 块的最小代价。

枚举它切到哪一行/列,分成两块。每一块要恰好分成多少个,则有:

\[f_{i,j,k}=\min \begin{cases} \min_\limits{1\le i'\le\lfloor\frac{i}{2}\rfloor,0\le k'\le k} f_{i',j,k' }+f_{i-i',j,k-k'}+j^2 \\ \min_\limits{1\le j'\le\lfloor\frac{j}{2}\rfloor,0\le k'\le k} f_{i,j',k' }+f_{i,j-j',k-k'}+i^2 \end{cases} \]

为什么是 \(\lfloor\frac{i}{2}\rfloor\)?因为再往后显然可以和之前的状态对称,于是就有重复,完全没有必要。

下面采用了记忆化搜索的方法,复杂度分析可以给它扔到一个五重循环上(\(i,j,k,i'/j',k'\)),得出复杂度为 \(O(n^5)\),可以接受。

代码

#include <bits/stdc++.h>
#define lson 2 * p
#define rson 2 * p + 1
// #define int long long
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int N = 3e1 + 2;
const int M = 5e1 + 2;
const int K = 1e3 + 2;
const int P = 131;
const int MOD = 1e4 + 7;
const int INF = 0x3f3f3f3f;
const LL LINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-6;
int t, n, m, k, dp[N][N][M];

int Init(int x, int y, int z) {
    if (z == x * y || !z) {
        return dp[x][y][z] = 0;
    }
    if (~dp[x][y][z]) {
        return dp[x][y][z];
    }
    int ans = INF;
    for (int i = 1; i <= x / 2; i++) {
        for (int j = 0; j <= z; j++) {
            ans = min(ans, Init(i, y, j) + Init(x - i, y, z - j) + y * y);
        }
    }
    for (int i = 1; i <= y / 2; i++) {
        for (int j = 0; j <= z; j++) {
            ans = min(ans, Init(x, i, j) + Init(x, y - i, z - j) + x * x);
        }
    }
    return dp[x][y][z] = ans;
}
void Solve() {
    scanf("%d%d%d", &n, &m, &k);
    Init(n, m, k);
    printf("%d\n", dp[n][m][k]);
}

signed main() {
    // freopen("rfrt.in", "r", stdin);
    // freopen("rfrt.out", "w", stdout);
    memset(dp, -1, sizeof(dp));
    // Init(N - 2, N - 2, M - 2);
    scanf("%d", &t);
    while (t--) {
        Solve();
    }
    return 0;
}

F - Cut Length

题目传送门

分析

对于计算几何不是很了解。。。

咕咕咕

代码

咕咕咕

闲话

人机验证比写题还要难(

但是话说开手机热点是不是就能解决(

涨知识了。

posted @ 2026-05-23 23:07  Rfrt  阅读(45)  评论(0)    收藏  举报