Codeforces Round 1006 (Div. 3) 补题+题解

news/2025/2/28 15:18:03/文章来源:https://www.cnblogs.com/ZhangDT/p/18743346

A. New World, New Me, New Array

贪心的想每次都赋值一个$ p $ 如果正好和为 $ k $ 则答案就是 \(k/p\) ,否则是\(k/p+1\)

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 10;void solve() {int n, k, p;cin >> n >> k >> p;if (n * p < abs(k)) {cout << -1 << '\n';return;}int ans = abs(k / p);if (k % p != 0) ans++;cout << ans << '\n';
}signed main() {ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);int t = 1;cin >> t;while (t--)solve();
}

B. Having Been a Treasurer in the Past, I Help Goblins Deceive

简单地想,\(O(n)\) 扫描一遍记录前后各放多少个 "-" 会使子序列最多,直接计算即可。

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 10;void solve() {string s;int n;cin >> n;cin >> s;int cnt = 0, cnt1 = 0;for (auto t : s) {if (t == '-') cnt++;}int mx = 0;cnt1 = s.size() - cnt;for (int i = 1; i <= cnt; i++) {int x = i, y = cnt - i;mx = max(mx, x * y);}cout << mx*cnt1 << '\n';
}signed main() {ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);int t = 1;cin >> t;while (t--)solve();
}

C. Creating Keys for StORages Has Become My Main Skill

贪心 ,\([0,n-1]\) 尽可能取,维护或值,如果恰好为 \(x\) 则是答案。否则尽可能修改大的将最后一项取为 \(x\),这样 \(MEX\) 最大。

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 10;void solve() {int n, x;cin >> n >> x;int now = 0;vector<int> ans(n+10, 0);for (int i = 1; i < n; i ++ ) {if ((x | i) == x) {ans[i] = i;now |= i;}}if (now != x) {ans[n - 1] = x;}for (int i = 0; i < n; i ++ ) {cout << ans[i] << ' ';}cout << '\n';
}signed main() {ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);int t = 1;cin >> t;while (t--)solve();
}

D. For Wizards, the Exam Is Easy, but I Couldn't Handle It

很明显反转数的个数变化只与选择的区间有关,与区间外的数无关,区间的贡献则是区间中小于 \(l\) 的个数减去大于 \(l\) 的个数 。

\(n^2\) 暴力枚举,在过程中记下 \([l,r]\) 区间中大于 \(l\) 和小于 \(l\) 的个数,然后统计贡献最大的区间便是答案。

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 10;void solve() {int n;cin >> n;vector<int> a(n + 1);for (int i = 1; i <= n; i++) {cin >> a[i];}int ans = 0;pair<int, int> anss;for (int l = 1; l <= n; l++) {int x = 0, y = 0;for (int r = l; r <= n; r++) {if (a[r] > a[l]) x++;if (a[r] < a[l]) y++;if (y - x >= ans) {ans = y - x;anss.first = l, anss.second = r;}}}cout << anss.first << ' ' << anss.second << '\n';
}signed main() {ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);int t = 1;cin >> t;while (t--)solve();
}

E. Do You Love Your Hero and His Two-Hit Multi-Target Attacks?

大致题意:Akito需要在坐标系中放置$ n $ 根法杖,且这些法杖的位置必须是不同的整数坐标点。放置后,恰好有$ k $ 对法杖位于同一条水平线或垂直线上。

构造题,利用组合数的知识 \(C(x,2)\) 尽可能构造即可。

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 10;void solve() {int n;cin >> n;if (n == 0) {cout << "2\n0 0\n1 1\n";return;}vector<pair<int, int>> ans;int yu = n;int ls = 1, d = 0;while (yu > 2) {for (int i = 2; i <= 500; i++) {int x = (i - 1) * i / 2;if (x > yu) {int res = (i - 1) * (i - 2);yu -= res / 2;for (int j = ls; j <= ls + i - 1 - 1; j++) {ans.push_back({-j, d});}ls = ls + i - 1;d -= 1;break;}}}if (yu == 2) {ans.push_back({1, 1});ans.push_back({2, 1});ans.push_back({3, 2});ans.push_back({4, 2});} else if (yu == 1) {ans.push_back({1, 1});ans.push_back({2, 1});}cout << ans.size() << '\n';for (pair<int, int> p : ans) {cout << p.first << ' ' << p.second << '\n';}
}signed main() {ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);int t = 1;cin >> t;while (t--)solve();
}

F. Goodbye, Banker Life

打表找一下规律可以发现第 $ n $ 行是由最近的第 $ n-2^x $ 行转移过来的,那么很明显可以递归,通过递归记录一下答案,处理一下直接输出。代码实现比较抽象,具体看代码。

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 10;
int n, x;
int ksm(int x, int y) {int ans = 1;while (y) {if (y & 1)ans = ans * x;y >>= 1;x = x * x;}return ans;
}
vector<int> po;
void init() {for (int i = 32; i >= 0; i--) {int x = ksm(2, i);po.push_back(x);}
}
deque<pair<string, int>> de;
void dfs(int u) {if (u == 1 || (u & (u - 1)) == 0) {if (u == 1) {de.push_front({"1", 1});} else {de.push_back({"1", u});}return;}for (int i = 0; i < 32; i++) {if (po[i] <= u) {dfs(u - po[i]);int yu = u - 2 * (u - po[i]);de.push_back({"0", yu});de.push_back({"x", 0});return;}}
}
void pr();
void pr1(int l, int r);
void pr0(int l, int r);
void solve() {while (de.size()) de.pop_front();cin >> n >> x;string s = to_string(1);for (int i = 0; i < 32; i++) {if (n == po[i]) {pr();return;}}dfs(n);vector<pair<string, int>> p;while (de.size()) {pair<string, int> it = de.front();de.pop_front();p.push_back(it);}for (int i = 0; i < p.size(); i++) {string a = p[i].first;int b = p[i].second;if (a == "0") {pr0(1, b);} else if (a == "1") {pr1(1, b);} else if (a == "x") {int len = 0;if (p[i - 1].first == "0") len = i - 1;else len = i;string lp = "";for (int j = 0; j < len; j++) {int cnt = p[j].second;if (p[j].first == "0")for (int k = 1; k <= cnt; k++) {cout << 0 << ' ';lp += "0";} else if (p[j].first == "1") {for (int k = 1; k <= cnt; k++) {cout << x << ' ';lp += "1";}} elsefor (char t : p[j].first) {if (t == '1') cout << x << ' ', lp += "1";else if (t == '0') cout << 0 << ' ', lp += "0";}}p[i].first = lp;}}cout << '\n';
}
void pr() {for (int i = 1; i <= n; i++)cout << x << ' ';cout << '\n';
}
void pr1(int l, int r) {for (int i = l; i <= r; i++) cout << x << ' ';
}
void pr0(int l, int r) {for (int i = l; i <= r; i++) cout << 0 << ' ';
}signed main() {ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);int t = 1;init();cin >> t;while (t--)solve();
}
/*
第 1 行: 1
第 2 行: 1 1
第 3 行: 1 0 1
第 4 行: 1 1 1 1
第 5 行: 1 0 0 0 1
第 6 行: 1 1 0 0 1 1
第 7 行: 1 0 1 0 1 0 1
第 8 行: 1 1 1 1 1 1 1 1
第 9 行: 1 0 0 0 0 0 0 0 1
第 10行: 1 1 0 0 0 0 0 0 1 1
*/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/891180.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

西数硬盘二次开盘数据恢复之国外损坏带回在北京恢复失败盘片划伤

这是一块西部数据2T的移动硬盘,北京客户寄过来的,用户在国外工作的时候就损坏不识别了,但当地修复不了,回国后就在北京找了一家数据恢复公司进行处理,但由于盘片有划伤,没能恢复出数据,后来客户找到我们,想再尝试一下,因为里面有海外工作时几百G的工程项目资料,比较重…

mysql多表

一、表格 员工表 sid:员工编号、name:姓名、age:年龄、woektime_start 入职时间,incoming工资 dept2 部门编号部门表 dept1 部门编号,dept_name 部门名称二、建表语句 CREATE table dept( dept1 VARCHAR(6), dept_name VARCHAR(20)) default charset=utf8; INSERT into d…

重磅 | Cloud Ace 推出 GenAIOps 服务:加速企业生成式 AI 商业落地

Cloud Ace 宣布推出 GenAIOps 服务,主要业务是面向国内外企业:提供与生成式 AI 运维支持相关的专业服务及运维服务 转售符合客户需求的高度创新产品及咨询服务该服务旨在以尖端技术赋能企业,为客户提供最优的 AI 解决方案,最大化生成式 AI 的商业价值与成果。联系我们—Clo…

SAP 中类的使用

一、事务代码:SE24 二、步骤 2.1、创建类:se24实例化类型public:该类可以在任何地方进行初始化Protected:只能在自身和或类的子类中进行初始化Private: 只能自身的类中进行初始化Abstract: 不可以创建实例。 2.2、创建属性 2.2.1、选择属性 在属性分页签中先按上图填写好属性…

字符串数组指针变量的地址关系

deque是一个指向字符串数组的指针变量(char **deque)。下面我用图示来介绍下,字符串数组的相关概念。 首先定义一个字符串数组指针 char **deque;然后再申请N个数组空间 deque = (char **)malloc(N * sizeof(char *));此时deque就是一个拥有N个字符串指针的数组。然后向第一…

牛客题解 | 斐波那契数列_1

牛客题库题解题目 题目链接 描述 此题是非常经典的入门题了。我记得第一次遇到此题是在课堂上,老师拿来讲“递归”的(哈哈哈)。同样的类型的题还有兔子繁殖的问题。大同小异。此题将用三个方法来解决,从入门到会做。 考察知识:递归,记忆化搜索,动态规划和动态规划的空间…

牛客题解 | 整数中1出现的次数(从1到n整数中1出现的次数)

牛客题库题解题目 题目链接 题目的主要信息:输入一个整数 n ,求1~n这n个整数的十进制表示中1出现的次数举一反三: 学习完本题的思路你可以解决如下题目: JZ17. 打印从1到最大的n位数 JZ15. 二进制中1的个数 方法一:按位统计法(推荐使用) 思路: 数字都是由位数组成,某…

牛客题解 | 数组中重复的数字

牛客题库题解题目 题目链接 题目的主要信息:一个长度为\(n\)的数组中只有0到\(n-1\)的数字 需要找出其中任意一个重复出现的数字举一反三: 学习完本题的思路你可以解决如下题目: JZ56. 数组中只出现一次的两个数字 JZ50. 第一个只出现一次的字符 JZ75. 字符流中第一个不重复…

牛客题解 | 数据流中的中位数

牛客题库题解题目 题目链接 题目主要信息:寻找数据的中位数 数据量在不断输入增长举一反三: 学习完本题的思路你可以解决如下题目: BM46. 最小的k个数 方法一:插入排序法(推荐使用) 知识点:插入排序 插入排序是排序中的一种方式,一旦一个无序数组开始排序,它前面部分就…

牛客题解 | 数组中出现次数超过一半的数字

牛客题库题解题目 题目链接 题目主要信息:题目给出一个长度为n的数组,其中有一个数字出现次数超过了数组长度的一半,需要我们找出这个数字 输入数组非空,保证有解,这样就不用考虑特殊情况举一反三: 学习完本题的思路你可以解决如下题目: BM52. 数组中只出现一次的两个数字…

牛客题解 | 数值的整数次方

牛客题库题解题目 题目链接 题目的主要信息:求一个浮点数的整数次方 整数有正有负 不可以使用库函数,也不需要判断大数问题举一反三: 学习完本题的思路你可以解决如下题目: JZ83. 剪绳子(进阶版) 方法一:直接运算(推荐使用) 思路: 既然是求次方,那我们做不断累乘就可…

支付宝 IoT 设备入门宝典(下)设备经营篇

本篇会以支付宝 IoT 设备经营为中心,介绍常见的设备相关能力和问题解决方案,帮助商户利用设备进行运营动作,让设备更好的帮助自己上篇介绍了支付宝 IoT 设备管理,但除了这些基础功能外,商户还可以利用设备进行一些运营动作,让设备更好的帮助自己,本篇就会以设备经营为中…