Codeforces Round 905 (Div. 3) 题解 A-E

2023-10-24 09:04
文章标签 codeforces round div 题解 905

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

A - Morning

原题链接

题目描述
给你一个模板字符串1234567890,在每一步操作中,1. 你可以让光标移动至相邻的数字(但是 1 1 1相邻的数字只有 2 2 2 0 0 0相邻的数字只有 9 9 9) 2. 或者你可以打印当前光标指向数字。起初光标指向 1 1 1的位置,现给定一个长度为 4 4 4的密码串,要求你进行最少的操作打印这个密码串。

思路:模拟

  • 用一个Map存下对应字符的位置,然后枚举密码串,模拟移动的步数即可。
public static void solve() throws IOException{String s = readString();Map<Character, Integer> map = new HashMap<>();int p = 1;for (char ch = '1'; ch <= '9'; ch++) {map.put(ch, p++);}map.put('0', p);int cnt = 0;char cur = '1';// 当前光标指向的字符for (int i = 0; i < s.length(); i++) {if (s.charAt(i) != cur) {cnt += Math.abs(map.get(cur) - map.get(s.charAt(i)));// 移动cur = s.charAt(i);}cnt++;// 打印}printWriter.println(cnt);
}

B - Chemistry

原题链接

题目描述
给定一个长度为 n n n的字符串 s s s和一个整数 k k k,问是否能从 s s s中删除 k k k个字符使得 s s s为回文字符串,删除 k k k个字符后的 s s s可以任意排列。

思路:分类讨论

  • 统计 s s s中所有字符出现的次数,如果全部都为偶数次或者只有一个字符出现的次数为奇数次,那么一定可以构成回文串,否则需要将(odd - 1)个出现的次数为奇数次的字符删除,例如ecadc,只需要将ead中任意两个字符删去即可构成回文串。
public static void solve() throws IOException{int n = readInt(), k = readInt();String s = readString();Map<Character, Integer> map = new HashMap();for (int i = 0; i < s.length(); i++) {char ch = s.charAt(i);map.put(ch, map.getOrDefault(ch, 0) + 1);}int odd = 0;for (int val : map.values()) {if ((val & 1) == 1) {odd++;}}if (odd == 1) {printWriter.println("YES");} else {k -= (odd - 1);if (k >= 0 && k < (n - (odd - 1))) {printWriter.println("YES");} else {printWriter.println("NO");}}
}

C - Raspberries

原题链接

题目描述
给定一个长度为 n n n的数组a和一个整数 k k k,在一次操作中,你可以选择任意位置让 a i a_i ai= a i + 1 a_i+1 ai+1,求使得数组 a a a所有数的乘积能整出 k k k的最少操作数。

思路:分类讨论

  • k = 4 k=4 k=4 k ≠ 4 k \neq 4 k=4的情况进行讨论
    – 1. 如果 k = 4 k = 4 k=4,那么判断有多少个数是 2 2 2的倍数,① 如果有至少 2 2 2个数是 2 2 2的倍数,那么最后乘积一定是 4 4 4的倍数,操作数为 0 0 0 ② 只有 1 1 1个数是 2 2 2的倍数,那么对其他任意数 + 1 +1 +1即可,操作数为 1 1 1 ③ 没有数是 2 2 2的倍数,那么就看有没有 3 3 3 7 7 7,如果有,只需要在 3 3 3 7 7 7上面 + 1 +1 +1,即构成 4 4 4的倍数,操作数为 1 1 1,如果没有,操作只数能为 2 2 2
    – 2. 如果 k ≠ 4 k \neq 4 k=4,那么只需要构造出一个 k k k的倍数即可。
public static void solve() throws IOException{int n = readInt(), k = readInt();int[] a = utils.nextIntArray(n);int min = Integer.MAX_VALUE;if (k == 4) {int cnt = 0;int s = 0;// 3或 7出现的次数for (int i = 1; i <= n; i++) {if (a[i] == 3 || a[i] == 7) s++;while (a[i] % 2 == 0 && a[i] != 0) {cnt++;a[i] /= 2;}}if (cnt >= 2) {printWriter.println(0);} else {if (cnt == 1) {printWriter.println(1);} else {if (s >= 1) {// 出现了3或7printWriter.println(1);} else {printWriter.println(2);}}}} else {for (int i = 1; i <= n; i++) {int t = (a[i] + k - 1) / k * k;min = Math.min(min, Math.abs(t - a[i]));}printWriter.println(min);}
}

D - In Love

原题链接

题目描述
你有一个数轴,起初上面没有画任何线段,现在你要进行q次操作:

  • + l r,在数轴上画一条[l,r]的线段
  • - l r,擦去一条数轴上[l,r]的线段

每次操作后,你需要判断数轴上是否存在两条不相交的线段。

思路:贪心

  • 维护所有线段中最大的左端点 L L L和最小的右端点 R R R即可,如果存在最大的左端点大于最小的右端点,那么一定存在两条不相交的线段。
public static void solve() throws IOException{int n = readInt();TreeMap<Pair, Integer> map1 = new TreeMap<>(new Comparator<Pair>() {@Overridepublic int compare(Pair o1, Pair o2) {return o2.first - o1.first;// 维护最大的 L}});TreeMap<Pair, Integer> map2 = new TreeMap<>(new Comparator<Pair>() {@Overridepublic int compare(Pair o1, Pair o2) {return o1.first - o2.first;// 维护最小的 R}});for (int i = 1; i <= n; i++) {String s = readString();int a = Integer.parseInt(readString()), b = Integer.parseInt(readString());Pair pair1 = new Pair(a, b), pair2 = new Pair(b, a);if (s.equals("+")) {map1.put(pair1, map1.getOrDefault(pair1, 0) + 1);map2.put(pair2, map2.getOrDefault(pair2, 0) + 1);} else {map1.put(pair1, map1.getOrDefault(pair1, 0) - 1);map2.put(pair2, map2.getOrDefault(pair2, 0) - 1);if (map1.get(pair1) == 0) map1.remove(pair1);if (map2.get(pair2) == 0) map2.remove(pair2);}// 最大的 l大于最小的 rif (map1.size() > 0 && map2.size() > 0 && map1.firstEntry().getKey().first > map2.firstEntry().getKey().first) {printWriter.println("YES");} else {printWriter.println("NO");}}
}

E - Look Back

原题链接

题目描述
给你一个长度为 n n n的数组 a a a,你可以进行以下操作:设置 a i a_i ai= a i ∗ 2 a_i*2 ai2,你需要求出最少的操作数使得 a a a数组是一个不递减数组。

思路:模拟+技巧

  • 暴力模拟时,很容易发现 a i a_i ai将会越来越大,从而导致超时。那么我们可以通过一个技巧,即当 a i a_i ai达到一定值时,限制他的大小在一个数的附近波动即可,从而降低时间复杂度。
public static void solve() throws IOException{int n = readInt();long[] a = utils.nextLongArray(n);long res = 0, pre = a[1], pow = 0;for (int i = 2; i <= n; i++) {long cnt = 0;while (a[i] < pre) {a[i] *= 2;cnt++;}res += cnt + pow;pre = a[i];while (pre > 1E10) {// 让a[i]在 1e10附近波动pre /= 2;pow++;}}printWriter.println(res);
}

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



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

相关文章

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+ 套笔试题,笔试真题 会在第一时间跟新 🍄 题面描述等均已改编,如果和你笔试题看到的题面描述