Codeforces Round #700 (Div. 2)

Codeforces Round #700 (Div. 2)

A. Yet Another String Game

**题意: ** Alice和Bob轮流对字符串每一位进行改变操作,每一位只能被改变一次,Alice先手,Alice要使字符串字典序尽可能小,Bob要使字符串字典序尽可能大,求最后结果。

题解: 贪心。A尽可能往小的改,能改a就改a,要不然改为b,要不然改为c。B尽可能往大的改,能改z就改z,要不然改为y,要不然改为x…

代码:

#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;
typedef long long LL;
int t;
int main() {
    cin >> t;
    while (t--) {
        string s;
        cin >> s;
        for (int i = 0; i < s.size(); i++) {
            if (i % 2 == 0) {
                if (s[i] != 'a')
                    cout << 'a';
                else
                    cout << 'b';
            } else {
                if (s[i] != 'z')
                    cout << 'z';
                else
                    cout << 'y';
            }
        }
        cout << endl;
    }
    return 0;
}

B. The Great Hero

**题意: ** 对于给定的所有怪物与英雄的攻击力和血量,判断英雄能否击败所有怪物(包括同归于尽)。

题解: 贪心。按照怪兽的攻击力升序排序,然后分别计算击败每个怪物的攻击次数,然后扣除血量,特判最后一个怪物能否同归于尽。

代码:

#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;
typedef long long LL;
LL t, A, B, n;

struct node {
    LL a, b;
} a[N];
bool cmp(node a, node b) {
    return a.a < b.a;
}

int main() {
    cin >> t;
    while (t--) {
        cin >> A >> B >> n;
        for (int i = 0; i < n; i++) cin >> a[i].a;
        for (int i = 0; i < n; i++) cin >> a[i].b;
        int flag = 0;
        sort(a, a + n, cmp);
        for (int i = 0; i < n; i++) {
            LL times = LL(ceil(1.0 * a[i].b / A));  //英雄需要打多少次
            LL vul = times * a[i].a;
            if (B - vul <= 0) {
                if (i == n - 1) {
                    if (B - (times - 1) * a[i].a > 0)
                        continue;
                    else
                        flag = 1;
                } else {
                    flag = 1;
                    break;
                }
            }
            B -= vul;
        }
        if (flag)
            cout << "NO" << endl;
        else
            cout << "YES" << endl;
    }
    return 0;
}

C. Searching Local Minimum

**题意: ** 给出一个排列的长度,每次你可以询问得到一个下标元素的值,要求在100次询问内找出满足a[i]<min(a[i-1],a[i+1])的下标i。 1 < = n < = 1 0 5 1<= n <= 10^5 1<=n<=105

题解: 二分。维护一个区间 [ l , r ] [l, r] [l,r]表示 a [ l − 1 ] > a [ l ] , a [ r + 1 ] > a [ r ] a[l-1]>a[l], a[r+1]>a[r] a[l1]>a[l],a[r+1]>a[r]。每次我都询问 a [ m i d ] a[mid] a[mid] a [ m i d + 1 ] a[mid+1] a[mid+1],然后每次二分缩小范围,如果当前的 a [ m i d ] < a [ m i d + 1 ] a[mid] < a[mid+1] a[mid]<a[mid+1],那就 r = m i d r = mid r=mid,否则 l = m i d + 1 l = mid + 1 l=mid+1。最后区间长度为1时,这个元素就符合题意。

代码:

#include <bits/stdc++.h>

#define int long long
using namespace std;

inline int read() {
   int s = 0, w = 1;
   char ch = getchar();
   while (ch < '0' || ch > '9') {if (ch == '-') w = -1; ch = getchar();}
   while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
   return s * w;
}

int const MAXN = 2e5 + 10;
int n, m, T, a[MAXN];

void get(int x) {
    if (x < 1 || x > n) return;
    cout << "? " << x << endl;
    cout.flush();
    a[x] = read();
}

signed main() {
    n = read();
    int l = 1, r = n;
    while(l < r) {
        int mid = (l + r) >> 1;
        get(mid), get(mid + 1);
        if (a[mid] < a[mid + 1]) r = mid;
        else l = mid + 1;
    }
    cout << "! " << r << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值