smu暑期培训第十次比赛题解

A.AtCoder - abc314_d

这是一道字符串变化的题,当t == 3时,经过这一步操作,这一步操作后,字符串要么全部都是大写,要么全部都是小写,基于贪心的思想,我们可以找到最后一个2或3出现的位置,在这一次操作中将其全部进行变化,之后的字符变化按照顺序变化就行。

#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;

constexpr int N = 5e5 + 10;

void solve() {
    int n;
    cin >> n; // 输入字符串长度
    string s;
    cin >> s; // 输入字符串
    int m;
    cin >> m; // 操作数
    int tt[N], x[N];
    char c[N];
    int count1 = 0;
    int count2 = 0;
    int ans2 = 0, ans3 = 0;
    for (int i = 1; i <= m; i++) {
        cin >> tt[i] >> x[i] >> c[i]; // 输入操作类型、位置和字符
        if (tt[i] == 2) {
            ans2 = i; // 记录最后一个类型为2的操作的索引
        } else if (tt[i] == 3) {
            ans3 = i; // 记录最后一个类型为3的操作的索引
        }
    }
    if (ans2 == 0 && ans3 == 0) {
        for (int i = 1; i <= m; i++) {
            s[x[i] - 1] = c[i]; // 根据操作更新字符串中的字符
        }
    } else {
        if (ans2 > ans3) {
            for (int i = 1; i <= m; i++) {
                if (tt[i] == 1 && i != ans2) {
                    s[x[i] - 1] = c[i]; // 根据操作更新字符串中的字符
                } else if (i == ans2) {
                    for (int j = 0; j < n; j++) {
                        if (s[j] >= 'A' && s[j] <= 'Z') {
                            s[j] = s[j] - 'A' + 'a'; // 将大写字母转换为小写字母
                        }
                    }
                }
            }
        } else {
            for (int i = 1; i <= m; i++) {
                if (tt[i] == 1 && i != ans3) {
                    s[x[i] - 1] = c[i]; // 根据操作更新字符串中的字符
                } else if (i == ans3) {
                    for (int j = 0; j < n; j++) {
                        if (s[j] >= 'a' && s[j] <= 'z') {
                            s[j] = s[j] - 'a' + 'A'; // 将小写字母转换为大写字母
                        }
                    }
                }
            }
        }
    }
    cout << s << "\n"; // 输出最终的字符串
}

int main() {
    int t;
    //cin >> t;
    t = 1;
    while (t--) {
        solve(); // 调用solve函数处理输入并输出结果
    }
}

B.

AtCoder - abc314_c 

这个题是一个模拟题,我们们需要将每一个颜色的字母移动到最右边距离他最近的具有相同颜色字母的位置上,如果最右边没有字母,就移动到最左边距离最远的那个

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

typedef long long ll;

constexpr int N = 2e5 + 10;

void solve() {
    int n, m;
    cin >> n >> m; // 输入 n 和 m

    string s;
    cin >> s; // 输入字符串 s

    //s = " " + s;
    vector<int> v[N]; // 定义一个二维向量 v,用于存储每个数字出现的位置
    int a[N]; // 定义一个数组 a,用于存储输入的数字序列
    for (int i = 0; i < n; i++) {
        cin >> a[i]; // 输入数字序列的每个元素
        v[a[i]].push_back(i); // 将当前数字的位置添加到对应的向量中
    }

    for (int i = 1; i <= m; i++) {
        char c = s[v[i][v[i].size() - 1]]; // 获取当前数字在字符串 s 中的最后一个字符
        for (int j = v[i].size() - 1; j >= 1; j--) {
            s[v[i][j]] = s[v[i][j - 1]]; // 将当前数字的前一个位置的字符赋值给当前位置
        }
        s[v[i][0]] = c; // 将最后一个位置的字符赋值给当前数字的第一个位置
    }

    cout << s << "\n"; // 输出结果字符串
}

int main() {
    int t;
    //cin >> t;
    t = 1; // 设置 t 的值为 1

    while (t--) {
        solve(); // 调用 solve 函数解决问题
    }
}

C.

AtCoder - abc131_d 

该题可以直接对每个任务完成的截止时间进行排序,如果说结束时间相同,就可根据时间长短排序,对所有任务时间进行累加,如果说在完成某个任务的过程中,其任务时间超过了截止时间,就说明完成不了,反之可以完成。

#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;

constexpr int N = 2e5 + 10;

struct time {
    int all; // 活动的总时间
    int end; // 活动的结束时间
} tt[N];

bool cmp(struct time a, struct time b) {
    if (a.end == b.end) {
        return a.all < b.all;
    }
    return a.end < b.end;
}

void solve() {
    int n;
    cin >> n; // 输入活动数量
    for (int i = 1; i <= n; i++) {
        cin >> tt[i].all >> tt[i].end; // 输入每个活动的总时间和结束时间
    }
    sort(tt + 1, tt + 1 + n, cmp); // 按照结束时间对活动进行排序
    ll sum = 0; // 记录已安排活动的总时间
    int flag = 1; // 标记是否能按照要求安排所有活动
    for (int i = 1; i <= n; i++) {
        sum += tt[i].all; // 累加已安排活动的总时间
        if (sum > tt[i].end) {
            flag = 0; // 若已安排的总时间超过当前活动的结束时间,则无法按要求安排所有活动
            break;
        }
    }
    if (flag) {
        cout << "Yes" << "\n"; // 若能按要求安排所有活动,则输出"Yes"
    } else {
        cout << "No" << "\n"; // 若无法按要求安排所有活动,则输出"No"
    }
}

int main() {
    int t;
    //cin >> t;
    t = 1;
    while (t--) {
        solve(); // 调用solve函数处理输入并输出结果
    }
}

D.C - Go Home

本题可以直接对1到n进行累加求和,每次对count加1,如果大于等于x则输出count的值

#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;

void solve() {
    int x;
    cin >> x; // 输入一个整数x
    ll sum = 0; // 记录累加和
    int count = 0; // 记录累加的次数
    for (int i = 1; i <= x; i++) {
        sum += i; // 累加i到sum中
        count++; // 累加次数加1
        if (sum >= x) {
            break; // 如果累加和大于等于x,则跳出循环
        }
    }
    cout << count << "\n"; // 输出累加次数
}

int main() {
    int t;
    //cin >> t;
    t = 1;
    while (t--) {
        solve(); // 调用solve函数处理输入并输出结果
    }
}

E.B - Guidebook

本题可直接对结构体数组进行排序

#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;

constexpr int N = 110;

struct resturant{
    string name;
    int score;
    int id;
    int id1;
}d[N];

int cmp(resturant a, resturant b){
	if(a.name == b.name)
		return a.score > b.score;
	return a.name < b.name;
}

void solve() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> d[i].name >> d[i].score;
        d[i].id = i;
    }
    sort(d + 1, d + 1 + n, cmp);
    for (int i = 1; i <= n; i++) {
        cout << d[i].id << "\n";
    }

}

int main() {
    int t;
    //cin >> t;
    t = 1;
    while (t--) {
        solve();
    }
}

F.

AtCoder - abc155_a

 

直接判断三个数里面是否只有两个数字相等

#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;

void solve() {
    int a, b, c;
    cin >> a >> b >> c;
    if (a == b && b == c) {
        cout << "No" << "\n";
    } else if (a != b && b != c && a != c) {
        cout << "No" << "\n";
    } else {
        cout << "Yes" << "\n";
    }
}

int main() {
    int t;
    //cin >> t;
    t = 1;
    while (t--) {
        solve();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值