Educational Codeforces Round 1

A. Tricky Sum

公式求出1 到 n的和,然后枚举二等整次幂。

#include <bits/stdc++.h>

using namespace std;

#define int long long

void solve(){
    int n; cin >> n;
    int sum = ( 1 + n ) * n  / 2;
    for( int i = 1 ; i <= n ; i <<= 1 )
        sum -= i * 2;
    cout << sum << "\n";
    return ;
}

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

B. Queries on a String

每移动\(r-l+1\)就相当没有移动,移动\(k\)次等价于移动\(k\mod (r-l+1)\)

#include <bits/stdc++.h>

using namespace std;

#define int long long

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    string s, t;
    cin >> s;
    int m;
    cin >> m;
    for (int l, r, k; m; m--) {
        cin >> l >> r >> k;
        k %= (r - l + 1);
        s = s.substr( 0 , l-1 )+s.substr( r - k , k )+s.substr( l-1 , r-l+1-k )+s.substr(r);
    }
    cout << s << "\n";
}

C. Nearest vectors

atan2(y,x)计算的值是$\arctan \frac y x \(的值,注意的是,`atan`的值域是\)[0,\pi]\(,而`atan2`的值域是\)[-\pi,\pi]$

我们用atan2计算出每个向量与\(x\)轴的夹角,然后排序,计算相邻的两个向量的角度即可

#include <bits/stdc++.h>

using namespace std;

#define double long double

typedef pair<double, int> pdi;

const double PI = acos(-1);


int32_t main() {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    int n;
    cin >> n;
    vector<pdi> a;
    for (double i = 1, x, y; i <= n; i++) {
        cin >> x >> y;
        a.emplace_back(atan2(x, y), i);
    }
    sort(a.begin(), a.end());
    auto tmp = [=](int x, int y) {
        double t = a[x].first - a[y].first;
        if (t < 0) t += PI * 2;
        return t;
    };

    double ans = tmp(0, n - 1);
    int l = a[0].second, r = a[n - 1].second;
    for (int i = 1; i < n; i++) {
        if (tmp(i, i - 1) < ans)
            ans = tmp(i, i - 1), l = a[i].second, r = a[i - 1].second;
    }
    cout << l << " " << r;
    return 0;
}

D. Igor In the Museum

.表示空地,*表示墙,空地相连可以组成联通块,每次给一点,问该点所属联通块与多少面墙所相邻。

注意*表示的是四面墙,所以每一个方向过来的都要统计上。

这道题直接 bfs,然后给联通块打标记即可

#include<bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    array dx = {0, 0, 1, -1}, dy = {1, -1, 0, 0};
    int n, m, k;
    cin >> n >> m >> k;
    vector<string> g(n);
    for (auto &i: g) cin >> i;
    vector<int> ans;
    vector<vector<int>> vis(n, vector<int>(m, -1));

    for (int r, c , tag; k; k--) {
        cin >> r >> c, r--, c--;
        if (vis[r][c] != -1) {
            cout << ans[vis[r][c]] << "\n";
            continue;
        }
        tag = ans.size() , ans.push_back(0) , vis[r][c] = tag;
        queue<pair<int, int>> q;
        q.emplace(r, c);
        while (!q.empty()) {
            auto [x, y] = q.front();
            q.pop();
            for (int fx, fy, i = 0; i < 4; i++) {
                fx = x + dx[i], fy = y + dy[i];
                if (fx < 0 || fy < 0 || fx >= n || fy >= m || vis[fx][fy] != -1 ) continue;
                if( g[fx][fy] == '.' ) vis[fx][fy] = tag , q.emplace( fx , fy );
                else ans[tag] ++;
            }
        }
        cout << ans[tag] << "\n";
    }
    return 0;
}

E. Chocolate Bar

f[i][j][k]表示i*j的巧克力吃 k 块的最小花费。

因为是数据范围极小,直接枚举切的方向和位置,以及切好的两块分别吃几块即可。

#include<bits/stdc++.h>

using namespace std;

const int N = 35, K = 55;

int f[N][N][K];

int read() {
    int x = 0, f = 1, ch = getchar();
    while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
    if (ch == '-') f = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
    return x * f;
}

int dfs(int n, int m, int k) {
    if (f[n][m][k] != -1) return f[n][m][k];
    if (n * m == k || k == 0) return f[n][m][k] = 0;
    int ans = INT_MAX;
    for (int a = 0, b = k; a <= k; a++ , b --) {
        for (int i = 1, j = n - 1; i <= j; i++, j--) {
            if (i * m < a || j * m < b) continue;
            ans = min(ans, m * m  + dfs(i, m, a) + dfs(j, m, b));
        }
        for (int i = 1, j = m - 1; i <= j; i++, j--) {
            if (n * i < a || n * j < b) continue;
            ans = min(ans, n * n + dfs(n, i, a) + dfs(n, j, b));
        }
    }
    return f[n][m][k] = ans;
}

void solve() {
    int n = read(), m = read(), k = read();
    printf("%d\n", dfs(n, m, k));
    return;
}

int main() {
    fill(f[0][0], f[N - 1][N - 1] + K, -1);
    for (int t = read(); t; t--)
        solve();
    return 0;
}
posted @ 2023-04-29 17:42  PHarr  阅读(15)  评论(0编辑  收藏  举报