Codeforces Round 921 (Div. 2)题解(A-C)

2024-02-16 20:52
文章标签 codeforces round div 题解 921

本文主要是介绍Codeforces Round 921 (Div. 2)题解(A-C),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

A

We Got Everything Covered!

思路

可以采用划分区域的思想。

  • 共划分为 n 个区域
  • 每一个区域含有所包含的前 k 个字母

证明:

  1. 正确性:无论所要求的字符串str为什么,只需要在区域i中取出 s t r [ i ] str[i] str[i]即可
  2. 最优性:若需要的字符串全部是a,那么s必定含有n个a,对于其他的字母同理。
    最后可以得到,s最少的字母数量为 n ⋅ k n \cdot k nk

代码

#include<bits/stdc++.h>
using namespace std;inline void Print(int k){for(int i = 0; i < k; i++){putchar('a' + i);}
}
int main()
{int T;cin >> T;while(T--){int n, k;scanf("%d%d", &n, &k);for(int i = 1; i <= n; i++){Print(k);}puts("");}return 0;
}

B

A Balanced 问题集et?

思路

暴力枚举gcd。

如果一群数字的gcd已经确定,那么其他数字的值便为gcd的倍数。

  1. n 不可以整除 gcd:在这种情况下,这群数字的和一定不会是n(舍去这种情况)
  2. n 可以整除gcd,但是商却小于k,这种情况下,即使每一个数字均为gcd,结果仍然大于n,不可能。
  3. n 可以整除gcd,商大于等于k,此时可以把若干个gcd合并为数组中的一个元素。

代码

#include<bits/stdc++.h>
using namespace std;
int ans;int x, n;inline void solve(int k){if(x / k >= n){ans = max(ans, k);}
}
int main()
{int T;cin >> T;while(T--){scanf("%d%d", &x, &n);ans = 0;for(int i = 1; (long long)i * i <= (long long)x; i++){if(x % i == 0){solve(i);solve(x / i);}}printf("%d\n", ans);}return 0;
}

C

Did We Get Everything Covered?

思路

首先对所给定的s进行分组,从左向右扫描,当刚好扫描满一个区间的时候,确定边界,继续扫描下一个区间。

区间为包含所有前 k 个字母的区间

  1. 如果已有的区间大于等于n,那么根据A中的知识,必定满足条件
  2. 如果区间个数不足n,那么进行构造。
    • 我们选取每一个区间的最后一个元素(每个区间最后一个元素一定仅仅出现一次,这使得这一个元素将用完一个区间)
    • 当超过已有的区间的时候,我们选取最后一个不完整区间中的没有出现的元素。

代码

#include<bits/stdc++.h>
using namespace std;
int n,  k, m;
typedef bitset<30> bs;
bs last, target;
const int N = 2e3;
char s[N];inline int getNum(char x){return x - 'a' + 1;
}bool solve(vector<pair<int, int>> &fenzu){bs tmp;int lpos = 1;tmp.reset();for(int i = 1; i <= m; i++){tmp.set(getNum(s[i]));if(tmp.count() >= k){tmp.reset();fenzu.push_back({lpos, i});lpos = i + 1;if(fenzu.size() >= n){return true;}}}last = tmp;return false;
}
int main()
{int T;cin >> T;while(T--){scanf("%d%d%d", &n, &k, &m);vector<pair<int, int>> fenzu;scanf("%s", s + 1);target.reset();for(int i = 1; i <= k; i++){target.set(i);}if(solve(fenzu)){puts("YES");}else{puts("NO");int cnt = 0;for(auto t : fenzu){putchar(s[t.second]);cnt++;}char theChar;for(int i = 1; i <= k; i++){if(!last.test(i)){theChar = 'a' - 1 + i;break;}}while(cnt < n){putchar(theChar);cnt++;}puts("");}}return 0;
}

这篇关于Codeforces Round 921 (Div. 2)题解(A-C)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/715683

相关文章

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

C - Word Ladder题解

C - Word Ladder 题解 解题思路: 先输入两个字符串S 和t 然后在S和T中寻找有多少个字符不同的个数(也就是需要变换多少次) 开始替换时: tips: 字符串下标以0开始 我们定义两个变量a和b,用于记录当前遍历到的字符 首先是判断:如果这时a已经==b了,那么就跳过,不用管; 如果a大于b的话:那么我们就让s中的第i项替换成b,接着就直接输出S就行了。 这样

CSS实现DIV三角形

本文内容收集来自网络 #triangle-up {width: 0;height: 0;border-left: 50px solid transparent;border-right: 50px solid transparent;border-bottom: 100px solid red;} #triangle-down {width: 0;height: 0;bor

【秋招笔试】9.07米哈游秋招改编题-三语言题解

🍭 大家好这里是 春秋招笔试突围,一起备战大厂笔试 💻 ACM金牌团队🏅️ | 多次AK大厂笔试 | 大厂实习经历 ✨ 本系列打算持续跟新 春秋招笔试题 👏 感谢大家的订阅➕ 和 喜欢💗 和 手里的小花花🌸 ✨ 笔试合集传送们 -> 🧷春秋招笔试合集 🍒 本专栏已收集 100+ 套笔试题,笔试真题 会在第一时间跟新 🍄 题面描述等均已改编,如果和你笔试题看到的题面描述