Educational Codeforces Round 125 (Rated for Div. 2) (A-D题解)

源代码:ACM/OpenjudgeNow/Codeforces at master · abmcar/ACM (github.com)

更好的阅读体验: Educational Codeforces Round 125 (Rated for Div. 2) (A-D题解) - Abmcar’s 茶水间

A. Integer Moves

题目大意:

image-20220328160735541

思路:

可以知道最多只需要执行2次,一次移动横坐标,一次移动纵坐标

而当 x 2 + y 2 \sqrt{x^2+y^2} x2+y2 为整数时 只需要一次操作即可

代码:
void solution()
{
    cin >> n >> m;
    if (n == m && m == 0)
    {
        cout << 0 << endl;
        return;
    }
    int tmp = n * n + m * m;
    int tt = int(sqrt(tmp)) * int(sqrt(tmp));
    if (tt == tmp)
        cout << 1 << endl;
    else
        cout << 2 << endl;
}

B. XY Sequence

题目大意:

image-20220328160953768

思路:

由于让求最大值,因此我们知道肯定选择最多的+操作最好

因此我们直接贪心选择+操作,当且仅当采用+操作过大时才使用-操作

代码:
void solution()
{
    cin >> n >> b >> x >> y;
    int nowNum = 0;
    int nowAns = 0;
    for (int i = 1; i <= n; i++)
    {
        if (nowNum + x <= b)
        {
            nowNum += x;
            nowAns += nowNum;
        }
        else
        {
            nowNum -= y;
            nowAns += nowNum;
        }
    }
    cout << nowAns << endl;
}

C. Bracket Sequence Deletion

题目大意:

image-20220328161256763

思路:

题目不难理解,很快我们就可以按照题目写出一个暴力,在写暴力的过程中用了stack判断合法,用reverse判断回文,信心满满交上去,结果

Time limit exceeded on test 5

回过头来再仔细思考题目,我们发现,当前缀为

  • ((

  • ))

  • ()

时,均可以被删除

只有)(不可以

观察)(,发现只有在添加(时,才不合法,如果添加一个),则立刻合法

因此,我们可以用vector来判断,若开头为’('或者开头等于末尾即可删

代码:
void solution()
{
    string oriS;
    int cnt = 0;
    cin >> n;
    cin >> oriS;
    vector<char> tmp;
    for (int i = 0; i < n; i++)
    {
        tmp.push_back(oriS[i]);
        if (tmp.size() > 1)
        {
            if (tmp[0] == '(' || tmp[0] == tmp.back())
            {
                tmp.clear();
                cnt++;
            }
        }
    }
    cout << cnt << " " << tmp.size() << endl;
}

D. For Gamers. By Gamers.

题目大意:

image-20220328163510740

思路:

值得注意的有这么几点

  1. 任何一个单位都不能被杀
  2. 队伍中只能有1种类型的单位

因此,选择更多单位的收益只有攻击力

H i D e > H e D i \frac{H_i}{D_e} > \frac{H_e}{D_i} DeHi>DiHe时,可以胜利(i为自己,e为敌人)

可以得到 H i ∗ D i > H e ∗ D e H_i*D_i > H_e*D_e HiDi>HeDe

由于一个小队只能有一种类型

我们可以把所有代价对应的最大 H i ∗ D i H_i*Di HiDi表示出来,之后对于每一个敌人,二分处理即可

代码:

signed main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    int n, c;
    cin >> n >> c;
    vector<int> cost(n), damage(n), health(n);
    vector<int> maxValue(c + 1);
    for (int i = 0; i < n; i++)
    {
        cin >> cost[i] >> damage[i] >> health[i];
        maxValue[cost[i]] = max(maxValue[cost[i]], damage[i] * health[i]);
    }
    for (int i = 1; i <= c; i++)
        for (int j = 2; j * i <= c; j++)
        {
            maxValue[i * j] = max(maxValue[i * j], maxValue[i] * j);
        }
    for (int i = 1; i <= c; i++)
        maxValue[i] = max(maxValue[i - 1], maxValue[i]);
    int m;
    cin >> m;
    while (m--)
    {
        int td, th;
        cin >> td >> th;
        int nowValue = td * th;
        if (nowValue >= maxValue.back())
        {
            cout << "-1" << endl;
            continue;
        }
        cout << upper_bound(maxValue.begin(), maxValue.end(), nowValue) - maxValue.begin() << endl;
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Abmcar

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

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

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

打赏作者

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

抵扣说明:

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

余额充值