AtCoder Beginner Contest 296 (A~D)

A. Alternately

题意:

给定一个长度为 n n n 的只包含 两种 字符的字符串。

判断这个字符串中的字符是否都是交替放置的。

思路:

枚举判断。

代码:

#include <bits/stdc++.h>
using namespace std;

int n;
string s;

int main()
{
    cin >> n;
    cin >> s;

    int f = 1;
    for (int i = 1; i < n; i++){
        if (s[i] == s[i - 1])
            f = 0;
    }
    if (f) puts("Yes");
    else puts("No");

    return 0;
}

B. Chessboard

题意:

给定一个 8 × 8 的网格,规定行号 从下往上 依次是 a、b、c...h ,列号 从左往右 依次是 1、2、3...8

要求找个一个字符 * ,输出其坐标。

思路:

枚举。

代码:

#include <bits/stdc++.h>
using namespace std;

string s[10];

int main()
{
    char r;
    int c;
    for (int i = 0; i < 8; i++){
        cin >> s[i];
        for (int j = 0; j < 8; j++){
            if (s[i][j] == '*'){
                r = j + 'a';
                c = 8 - i;
            }
        }
    }

    cout << r << c << endl;
    
    return 0;
}

C. Gap Existence

题意:

给定一个长度为 n n n 的整数序列和 x x x ,要求在序列中找到两个数 a 、 b a、b ab ,满足 a − b = x a - b = x ab=x .

思路:

将给定的元素都用 set 存储,依次枚举 b ,则要找的 a = b + x ,判断 set 中是否存在满足条件的 a 即可。

代码:

#include <bits/stdc++.h>
using namespace std;

int n, m;
set<int> S;

int main()
{
    cin >> n >> m;
    
    for (int i = 1; i <= n; i++){
        int x;
        cin >> x;
        S.insert(x);
    }

    for (auto x : S){
        if (S.count(x + m)){
            puts("Yes");
            return 0;
        }
    }
    puts("No");
    
    return 0;
}

D. M<=ab

题意:

给定两个整数 n n n m m m ,要求在 1 ~ n 中找到两个数 a 、 b a、b ab ,使得在满足 a × b ≥ m a×b \ge m a×bm 的条件下,同时 a × b a × b a×b 最小。

思路:

a a a b b b 可能是同一个数,也可能一大一小。

我们可以假设 a ≤ b a \le b ab ,枚举 a a a 从 1 ~ n,设 b = ⌈ m a ⌉ b = \lceil \frac{m}{a} \rceil b=am ,每次更新 a × b a × b a×b 的最小值。

同时要保证前提 a ≤ b a \le b ab ,当 a > b a > b a>b 时退出循环,可以知道 a a a 最多枚举 m + 1 \sqrt{m} + 1 m +1 次。

代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std;

ll n, m;

int main()
{
    cin >> n >> m;

    ll res = 2e18;
    for (ll i = 1; i <= n; i++){
        ll x = (m + i - 1) / i;
        if (x <= n) res = min(res, i * x);
        if (i > x) break;
    }

    if (res == 2e18) cout << -1 << endl;
    else cout << res << endl;

    return 0;
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BraumAce

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值