AtCoder Beginner Contest 197题解

AtCoder Beginner Contest 19

A - Rotate 题目链接

直接按题目要求顺序输出一下就OK

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e6 + 5;
const ll mod = 1e9 + 7;

char s[10];

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    scanf("%s", s);
    printf("%c%c%c", s[1], s[2], s[0]);
}

B - Visibility 题目链接

四个方向找到第一个是 # \# # 的地方,过程中答案++,注意自己本身也是答案之一。

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e2 + 5;
const ll mod = 1e9 + 7;

char s[N][N];

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int n, m, x, y;
    scanf("%d%d%d%d", &n, &m, &x, &y);
    x--, y--;
    for (int i = 0; i < n; ++i) {
        scanf("%s", s[i]);
    }
    int ans = 1;
    for (int i = x - 1; i >= 0; --i) {
        if (s[i][y] == '#') break;
        ans++;
    }
    for (int i = x + 1; i < n; ++i) {
        if (s[i][y] == '#') break;
        ans++;
    }
    for (int j = y - 1; j >= 0; --j) {
        if (s[x][j] == '#') break;
        ans++;
    }
    for (int j = y + 1; j < m; ++j) {
        if (s[x][j] == '#') break;
        ans++;
    }
    printf("%d", ans);
}

C - ORXOR 题目链接

我们可以发现 N N N 比较小,所以暴力枚举。可以用二进制枚举暴力这个全部情况,每一位中的 0 0 0 1 1 1 代表是否在这个数字后面切。

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e2 + 5;
const ll mod = 1e9 + 7;

int a[N];

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int n, Min = (1 << 30) + 10;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%d", &a[i]);
    }
    int limit = 1 << (n + 1) - 1;
    for (int i = 0; i <= limit; ++i) {
        int i_ = i | 1;
        int now_or = 0, now_xor = 0;
        for (int j = n - 1; j >= 0; --j) {
            now_or |= a[n - 1 - j];
            int op = ((i_ >> j) & 1);
            if (op) {
                now_xor ^= now_or;
                now_or = 0;
            }
        }
        Min = min(Min, now_xor);
    }
    printf("%d", Min);
}

D - Opposite 题目链接

我们可以知道一个正 N N N 边形的顶点一定是共圆的,而且如果 N N N 是偶数的话,我们连接 ( x 0 , y 0 ) (x_0, y_0) (x0,y0) ( x n 2 , y n 2 ) (x_{\frac{n}{2}}, y_{\frac{n}{2}}) (x2n,y2n) ,这条线就是这个正 N N N 边形外接圆的直径(如下图),那么很显然, ( x 1 , y 1 ) (x_1, y_1) (x1,y1) 就是 ( x 0 , y 0 ) (x_0, y_0) (x0,y0) 绕圆心逆时针旋转 α α α 后的点,圆心 ( x m , y m ) (x_m, y_m) (xm,ym) 自然就是 ( x 1 , y 1 ) (x_1, y_1) (x1,y1) 就是 ( x n 2 , y n 2 ) (x_{\frac{n}{2}}, y_{\frac{n}{2}}) (x2n,y2n) 的中点,而我们也可以得知 N N N α α α 正好平分 2 π 2\pi 2π,所以 α = 2 π N α= \frac {2\pi}{N} α=N2π 。那么这里我们可以直接运用绕点的旋转公式:

假设点 ( x 0 , y 0 ) (x_0,y_0) (x0,y0) ( x m , y m ) (x_m,y_m) (xm,ym) 逆时针旋转 a a a 后变成 ( x ′ , y ′ ) (x',y') (x,y),则
x ′ = ( x 0 − x m ) ∗ c o s ( a ) − ( y 0 − y m ) ∗ s i n ( a ) + x m x' = (x_0 - x_m) * cos(a) - (y_0 - y_m) * sin(a) + x_m x=(x0xm)cos(a)(y0ym)sin(a)+xm
y ′ = ( x 0 − x m ) ∗ s i n ( a ) + ( y 0 − y m ) ∗ c o s ( a ) + y m y' = (x_0 - x_m) * sin(a) + (y_0 - y_m) * cos(a) + y_m y=(x0xm)sin(a)+(y0ym)cos(a)+ym
在这里插入图片描述

#include <bits/stdc++.h>

using namespace std;

const double PI = acos(-1.0);

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    double n, x0, y0, xh, yh, xm, ym, a;
    scanf("%lf%lf%lf%lf%lf", &n, &x0, &y0, &xh, &yh);
    a = 2 * PI / n;
    xm = (x0 + xh) / 2;
    ym = (y0 + yh) / 2;
    double x_ = (x0 - xm) * cos(a) - (y0 - ym) * sin(a) + xm;
    double y_ = (x0 - xm) * sin(a) + (y0 - ym) * cos(a) + ym;
    printf("%.10f %.10f", x_, y_);
}

E - Traveler 题目链接

我们对于一种颜色 c c c 来说,我们假设其最左的坐标为 b [ c ] [ 0 ] b[c][0] b[c][0] ,其最右的坐标为 b [ c ] [ 1 ] b[c][1] b[c][1] 。我们可以发现,要想最优,对于某种颜色 c c c 而言,最后一个取的球一定是最大和最小端点中的一个(因为最大最小端点的两个球是肯定要拿的,那么两个端点球肯定有个拿的先后顺序,如果我们先拿的是左端点球,那么我肯定一路扫过去最后拿右端点,不然拿完右端点再返回去拿别的肯定浪费,先拿右端点的情况同理)。那么我们先在可以用 d p dp dp 解决这道题,我们设 d p [ i ] [ j ] dp[i][j] dp[i][j] 的含义为:取完 i i i 种颜色的球,且最后取的是左 ( j = = 0 ) (j==0) (j==0)或右 ( j = = 1 ) (j==1) (j==1) 端点时的最小答案。那么对于 d p [ i ] dp[i] dp[i] 来说,我们可以遍历上次的停留位置(也两种, b [ c − 1 ] [ 0 ] , b [ c − 1 ] [ 1 ] b[c-1][0],b[c-1][1] b[c1][0],b[c1][1]),然后分情况讨论求出答案。这里注意颜色序号是可能不连续的,可以存 v e c t o r vector vector 里面再去处理。

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 2e5 + 5;

ll dp[N][2], b[N][2];
int vis[N];
vector<int> g;

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int n;
    scanf("%d", &n);
    memset(dp, 0x3f, sizeof dp);
    dp[0][0] = dp[0][1] = 0;
    g.push_back(0);
    for (int i = 0; i < n; ++i) {
        ll x; int c;
        scanf("%lld%d", &x, &c);
        if (!vis[c]) {
            b[c][0] = x;
            b[c][1] = x;
            vis[c] = 1;
            g.push_back(c);
            continue;
        }
        b[c][0] = min(b[c][0], x);
        b[c][1] = max(b[c][1], x);
    }
    sort(g.begin(), g.end());
    int len = (int)g.size();
    for (int i = 1; i < len; ++i) {
        int pre = g[i - 1], c = g[i];
        for (int j = 0; j < 2; ++j) {
            int pos = b[pre][j];
            ll lst = dp[pre][j];
            if (pos < b[c][0]) {
                dp[c][0] = min(dp[c][0], 2ll * b[c][1] - pos - b[c][0] + lst);
                dp[c][1] = min(dp[c][1], b[c][1] - pos + lst);
            }
            else if (b[c][0] <= pos && pos <= b[c][1]) {
                dp[c][0] = min(dp[c][0], 2ll * b[c][1] - pos - b[c][0] + lst);
                dp[c][1] = min(dp[c][1], pos + b[c][1] - 2ll * b[c][0] + lst);
            }
            else {
                dp[c][0] = min(dp[c][0], pos - b[c][0] + lst);
                dp[c][1] = min(dp[c][1], pos + b[c][1] - 2ll * b[c][0] + lst);
            }
        }
    }
    int t = g[len - 1];
    ll ans = min(dp[t][0] + abs(b[t][0]), dp[t][1] + abs(b[t][1]));
    printf("%lld", ans);
}
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值