Codeforces Round #700 (Div. 2) A~C题解

写在前边

链接:Codeforces Round #699 (Div. 2)

A. Yet Another String Game

链接:A题链接

题目大意:

给定一个字符串,有两位同学来操作这个字符串,一个同学负责使得字符串字典序变大,另一个同学负责使得字符串字典序变小,轮到他们的时候他们无论如何都要操作一次,如果当前字符是a,那么即使轮到了负责使字典序变小的同学来操作,他也要将其变成'b',求最后的字符串。

思路

模拟即可,遇到a,z字符特判一下。

代码:

#include <iostream>

#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <map>
#include <cstring>

using namespace std;

#define Inf 0x3f3f3f3f
#define PII pair<int, int>
#define P2LL pair<long long, long long>
#define endl '\n'

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<long long> VLL;
typedef vector<int> VI;

void solve() {
    string s, res = "";
    cin >> s;
    for (int i = 0; i < s.size(); i++) {
        if (i % 2 == 0) { //Alice
            res += (s[i] == 'a' ? 'b' : 'a');
        } else {
            res += (s[i] == 'z' ? 'y' : 'z');
        }
    }
    cout << res << endl;
}

int main()
{
    //ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    int t;
    scanf("%d", &t); 
    while (t--) {
        solve();
    }
    system("pause");
    return 0;
}

B. The Great Hero

链接:B题链接

题目大意:

英雄打怪兽,没有谁先手之分,英雄与怪兽都有血量与攻击力,并且都会收到对方的伤害,攻击的顺序不限,判断最后英雄是否能干掉所有怪兽,即使英雄牺牲也算。

思路

这种题目常规思路真的很难搞,很容易就出现一些浮点问题,而且还所以不如我们可以先预处理出如果英雄能干掉所有的怪兽所需要的HP记为sum,然后想,我们如果可以用最少的血量干掉血量多伤害大的怪兽那肯定血赚,比如我们的攻击是7,初始血量为8,这时候有一个攻击力为8的怪兽,血量为7,如果我们最先攻击这只怪兽,那么我们就与其同归于尽了,其他的怪兽就打不了了,所以收益很低,而我们如果干掉其他所有怪物后再最后攻击这只怪物,还剩下一点血的时候,与其同归于尽收益肯定更大,而这一步排序不容易实现,所以我们可以考虑一下英雄的最后一击,如果英雄的最后一击之前有血量大于0,那么说明英雄就可以完美干掉所有怪物,所以我们可以枚举所有小怪的攻击力,判断sumattack[i]<HP如果出现就说明我们一定可以按照一定的顺序去干掉所有的怪兽,而这个顺序是什么我们不需要在意,时间复杂度O(n)

代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <map>
#include <cstring>

using namespace std;

#define Inf 0x3f3f3f3f
#define PII pair<int, int>
#define P2LL pair<long long, long long>
#define endl '\n'

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<long long> VLL;
typedef vector<int> VI;

const int N = 1E5 + 10;
int attack[N], hp[N];
int Attack, HP, n;
int cost[N]; //干掉每只怪花的力气

void solve() {
    scanf("%d%d%d", &Attack, &HP, &n);
    for (int i = 0; i < n; i++) scanf("%d", &attack[i]);
    for (int i = 0; i < n; i++) scanf("%d", &hp[i]);

    LL sum = 0;
    for (int i = 0; i < n; i++) sum += (LL)(hp[i] + Attack - 1) / Attack * attack[i]; //承受攻击

    for (int i = 0; i < n; i++) {
        if (sum - attack[i] < HP) {
            puts("YES");
            return;
        }
    }

    puts("NO");
}

int main()
{
    //ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    int t, cnt = 1;
    scanf("%d", &t);
    int q = t;
    while (t--) {
        solve();
    }
    system("pause");
    return 0;
}

C. Searching Local Minimum

链接:C题链接

题目大意:

一道交互题。

思路

第一次听说这种类型的题目,现在也还是懵懵了,大概就是输出会被机器获取当成输入,输出称为机器的输入,对于这道一而言,我们就是维护一个[l,r]区间最后趋于一个点,并且满足,ar+1>ar, al1<al,这个点就是答案,于是用二分来实现,对于a[mid]<a[mid]+1,让r=mid,反之l=mid+1

代码:

#include <iostream>

#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <map>
#include <cstring>

using namespace std;

#define Inf 0x3f3f3f3f
#define PII pair<int, int>
#define P2LL pair<long long, long long>
#define endl '\n'

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<long long> VLL;
typedef vector<int> VI;

LL gcd(LL a, LL b) {
    return b ? gcd(b, a % b) : a;
}

const int N = 1E5 + 10;

int a[N];
int n;

void query(int x) {
    if (x >= 1 && x <= n) {
        printf("? %d\n", x);
        fflush(stdout);
        scanf("%d", &a[x]);
    }
}

void solve() {
    scanf("%d", &n);
    a[0] = a[n + 1] = 1e9;
    int l = 1, r = n;
    while (l < r) {
        int mid = l + r >> 1;
        query(mid);
        query(mid + 1);
        if (a[mid] < a[mid + 1]) r = mid;
        else l = mid + 1;
    }
    printf("! %d\n", l);
    fflush(stdout);
}   

int main()
{
    //ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    solve();
    system("pause");
    return 0;
}

另外一道交互题A. Bear and Prime 100.

题目大意:

我们会对系统进行询问,如果我们询问的数是那个隐藏的数的约数,那么系统会回答yes,要求在二十次询问之内,判断出隐藏的数是否为质数,例如:那个隐藏的数为14,那么我们如果打印27或者14,系统都会回答yes

思路

我们要做的就是枚举所有可能的质因数,然后根据系统的回答来判断那个隐藏的数的约数个数,从而判断它是否为约数。

首先根据算术基本定理,如果一个数是合数,那么它一定可以被唯一的分解成两个或两个以上的质数的积,因此最后20次询问之后只需要判断出是否有两个或两个以上的质数,所以我们要做的就是要考虑枚举哪些数才能在确保二十次询问之内判断出一个[1,100]的数。

首先我们仅仅需要考虑50之内的质数,因为判断n是否为质数的一种方式就是枚举到n2即可,结合实际如果有一个大于50的质数,那么它与最小的质数相乘也大于100了,那么现在就有了2,3,5,7,11,13,17,19,23,29,31,37,41,43,47十五个数。

还要想到一些特例,质数的平方,493649这四个数,所以总共就有19个数了,因此我们完全可以在20次询问之内判断出一个数是否为质数了

代码:

#include <cstdio>
#include <vector>
#include <map>
#include <cstring>
 
using namespace std;
 
#define Inf 0x3f3f3f3f
#define PII pair<int, int>
#define P2LL pair<long long, long long>
#define endl '\n'
 
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<long long> VLL;
typedef vector<int> VI;
 
LL gcd(LL a, LL b) {
    return b ? gcd(b, a % b) : a;
}
 
int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 4, 9, 25, 49};
 
void solve() {
    int cnt = 0;
    char s[10];
    for (int i = 0; i < 19; i++) {
        cout << primes[i] << endl;
        cin >> s;
        if (!strcmp(s, "yes")) cnt++;
    }
    if (cnt >= 2) cout << "composite" << endl;
    else cout << "prime" << endl; 
}
 
int main()
{
    //ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    solve();
 
    return 0;
}
posted @   Xxaj5  阅读(73)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示