题目链接:Codeforces Round 968 (Div. 2) - Codeforces

总结:C题想到了,但是写成shi了,出得有点慢。

A. Turtle and Good String

tag:签到

Solution:直接判断第一个字符是否与最后一个字符相等即可。

void solve(){
cin >> n;
string s;
cin >> s;
if (s[0] == s[n - 1]){
cout << "No\n";
}
else{
cout << "YES\n";
}
}

B. Turtle and Piggy Are Playing a Game 2

tag:博弈论

Solution:显然每次操作要么是删除最小值,要么是删除最大值。排序后输出中间值即可。

void solve(){
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i ++){
cin >> a[i];
}
sort(a.begin(), a.end());
cout << a[n / 2] << endl;
}

C. Turtle and Good Pairs

tag:思维

Description:给定一个字符串\(s\),对于一对整数\((i, j)\),当\(si == sj\)或者存在一个整数\(k\),使得\(s_k != s_{k + 1}\),且\(s_k != s_i || s_{k + 1} != s_j\),\((i, j)\)为一对好的配对。将字符串重排,使好的配对数最大。

Solution:我们令\(k == i\),那么只要\(s_{i + 1} != s_{i} 且 s_{i + 1} != s_{j}\)即可,那么显然我们需要尽可能多的相邻两个字符不同的组合即尽可能使相邻两个字符不同。

  • 其实只需要按顺序排列字符即可,多的字符全放在后面。

Competing:没仔细想,认为尽可能使相邻字符不同,因此需要判断出现次数最多的字符。

void solve(){
cin >> n;
cin >> s;
int ma = 0; // 出现次数最多的字符
char c;
map<char, int> mp;
vector<int> a(26);
for (int i = 0; i < n; i ++){
mp[s[i]] ++;
if (mp[s[i]] > ma){
ma = mp[s[i]];
c = s[i];
}
a[s[i] - 'a'] ++;
}
if (ma < n - ma + 1){
string ans = "";
int c = 0;
while (c < n){
for (int i = 0; i < 26; i ++){
if (a[i]){
a[i] --;
ans += char(i + 'a');
c ++;
}
}
}
cout << ans << endl;
}
else{
string ans = "";
int cnt = 0;
a[c - 'a'] = 0;
bool flag = true;
while (cnt < n && flag){
flag = false;
for (int i = 0; i < 26; i ++){
if (a[i]){
a[i] --;
ans += c;
ma --;
ans += char(i + 'a');
cnt ++;
flag = true;
}
}
}
while (ma){
ans += c;
ma --;
}
cout << ans << endl;
}
}

D1. Turtle and a MEX Problem (Easy Version)

tag:思维

Description:给定\(n\)个序列,以开始有一个非负整数\(x\):

  • 每次操作可以任选一个序列,令\(x = mex(x, a_1, a_2, ..., a_n)\)。即\(f(x)\)为初始值为\(x\),操作之后可以得到的最大值。
  • 给定一个\(m\),求\(\sum_{0}^{m}f(m)\)。

Solution:对于每个序列,我们最多操作两次,可以得到每个序列的第二个\(mex2\)。那么当\(x\)小于最大的\(mex2\)时,将其变成\(mex2\),否则使用\(x\)本身。

void solve(){
cin >> n >> m;
vector<pii> a(n);
int ma = 0;
for (int i = 0; i < n; i ++){
int len;
cin >> len;
vector<int> t(len + 10);
for (int j = 0; j < len; j ++){
int x;
cin >> x;
if (x < len + 10)
t[x] = 1;
}
int c = 1;
for (int j = 0; j <= len + 5 && c <= 2; j ++){
if (!t[j] && c == 1){
a[i].fi = j;
c ++;
}
else if (!t[j]){
c ++;
a[i].se = j;
ma = max(ma, j);
break;
}
}
}
int ans = 0;
if (m <= ma){
ans += (m + 1) * ma;
}
else{
ans += (ma + 1) * ma;
ans += (ma + 1 + m) * (m - ma) / 2;
}
cout << ans << endl; }

D2. Turtle and a MEX Problem (Hard Version)

tag:思维

Solution:和D1的唯一区别时,在一次操作中每个序列最多使用一次。

  • 对于第\(i\)个序列,我们建一条有向边\(u_i -> v_i\)。那么一次操作\(x\)可以沿着一条出边移动,或者移动到一个任意点\(u\),并断开它的出边。
  • 我们先倒序处理一下,令\(b_i\)等于\(i\)能够到达的最大值,我们先倒序处理一下。
  • 首先一个\(x\)的答案至少是\(f_x\),所有\(x\)的答案至少是\(\max_{1}^{n}u_i\);对于所有出度大于\(1\)的\(i\),\(x\)的答案至少是\(f_i\)。
  • 实现步骤:倒序处理\(f_i\)、记录每个点的出度、记录度大于\(1\)的最大的\(f_i\)、记录最大的\(u_i\)。
void solve(){
cin >> n >> m;
vector<pii> a(n);
int ma = 0;
for (int i = 0; i < n; i ++){
int len;
cin >> len;
ma = max(ma, len);
vector<int> t(len + 10);
for (int j = 0; j < len; j ++){
int x;
cin >> x;
if (x < len + 10)
t[x] = 1;
}
int c = 1;
for (int j = 0; j <= len + 5 && c <= 2; j ++){ // 处理u,v的值
if (!t[j] && c == 1){
a[i].fi = j;
c ++;
}
else if (!t[j]){
c ++;
a[i].se = j;
break;
}
}
}
sort(a.begin(), a.end(),
[&](pii a, pii b){
return a.fi > b.fi;
});
int mma = 0; // 最大的ui
vector<int> b(ma + 10);
vector<int> chu(ma + 10);
for (auto [x, y] : a){ // 倒序处理
mma = max(mma, x);
int t = max(y, b[y]);
b[x] = max(b[x], t);
chu[x] ++;
} int t2 = -1; // 度大于2的,最大的bi
for (auto [x, y] : a){
if (chu[x] >= 2 && b[x] > t2){
t2 = b[x];
}
}
int ans = 0;
for (int i = 0; i <= ma + 5 && i <= m; i ++){
b[i] = max({i, b[i], t2, mma});
ans += b[i];
}
if (m > ma + 5){
ans += (ma + 6 + m) * (m - ma - 5) / 2;
} cout << ans << endl;
}

Codeforces Round 968 (Div. 2)的更多相关文章

  1. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  2. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  3. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  4. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  5. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  6. Codeforces Round #262 (Div. 2) 1003

    Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...

  7. Codeforces Round #262 (Div. 2) 1004

    Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...

  8. Codeforces Round #371 (Div. 1)

    A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...

  9. Codeforces Round #268 (Div. 2) ABCD

    CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...

  10. 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts

    题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...

随机推荐

  1. 源码开放:WebSocket应用示例

    1 WebSocket概述 WebSocket是HTML5下一种新的协议(本质上是一个基于TCP的协议),它实现了浏览器与服务器之间的全双工通信,能够节省服务器资源和带宽,达到实时通讯的目的.WebS ...

  2. JS 正则表示式 字符串匹配 忽略大小写

    在项目中遇到了需要使用字符串进行正则匹配,同时还要忽略大小写可以按照以下方法:1 先使用new RegExp(newVal, 'i')生成需要匹配的规则,其中 'i' 表示忽略大小写2 再对相应的字符 ...

  3. Rework:每个程序员都应该读的一本书

    来源: 萌萌的博客 每一个程序员都有改变世界的梦想,他们不甘平凡,他们想要与众不同,他们想要创立世界上最酷的公司,那具体该如何做呢?风靡全球的<Rework>将告诉你答案. 37signa ...

  4. Linux中touch、mkdir与vi的区别

    mkdir: 建立一个空目录 touch: 建立一个空文档,但是不进入编辑模式 vi:        建立一个空文档,进入编辑模式

  5. uniapp 使用pinpa 持续化更新

    安装依赖 npm i pinia npm i pinia-plugin-persistedstate 新建 index.ts import { createPinia } from 'pinia' i ...

  6. 全网最详细的Spring入门教程

    为什么用Spring 什么是Spring Spring 是一款开源的轻量级 Java 开发框架,旨在提高开发人员的开发效率以及系统的可维护性. Spring的一个最大的目的就是使JAVA EE开发更加 ...

  7. 不求甚解--详解ansible-playbook中roles的用法

    前言 本文将详细介绍ansible-playbook中roles的各种用法,它允许你将相关的任务.变量.处理器.文件和模板等集合在一起,以便于在不同的项目中复用 环境准备 组件 版本 操作系统 Ubu ...

  8. 在Python工具箱中,创建对应子工具集

    目录 问题描述 实现方法 问题描述 在Pro中,新建自定义工具箱后,直接通过操作可以添加工具集. 但是新建python工具箱后,却没有新建的操作.因为python工具箱的对象定义,都是在脚本中定义的, ...

  9. gradle命令与maven命令对照

    Gradle和Maven都是流行的构建工具,用于管理和构建Java项目.下面是Gradle命令和对应的Maven命令对照: 1. 构建项目: - Gradle: `./gradlew build` - ...

  10. vue3笔记

    如何创建vue3项目 基于 vue 脚手架 npm i @vue/cli -g vue create <project-name> cd <project-name> npm ...