Codeforces Round #653 (Div. 3) 题解
记第一场CF赛,完成3道题
代码更新(降低时间复杂度和修改写法):21.1.23
A题 Required Remainder
题意:给你 x、y、n 求最大的k (k<=n) 使得k%x==y
**做法:模拟 **
寻找最大整除数,一开始while循环找,直接TLE2
#include<bits/stdc++.h>
using namespace std;
int main() {
int t, x, y,n; cin >> t;
while (t--) {
cin >> x >> y >> n;
cout << (n - y) / x * x + y << endl;
}
return 0;
}
B题 Multiply by 2, divide by 6
题意:给你一个数n 每次两个操作:乘2 除6 求最少的操作 得到1
做法:循环整除6,每次+1,然后循环整除3,每次加2
#include<bits/stdc++.h>
using namespace std;
int main() {
long long a, n, k, t;
cin >> t;
while (t--) {
cin >> a;
k = 0, n = 0;
while (a % 6 == 0) { a /= 6; ++k; }
while (a % 3 == 0) { a /= 3; k+=2; }
if (a != 1)k = -1;
cout << k << endl;
}
}
C. Move Brackets
题意:给你只包含'(' ')' 的字符串,每次操作可以选择一个字符 要么移到最后面 要么移到最前面,求最少的操作使得字符串是括号匹配的字符串
做法:先统计 '(' 个数,遇 ')' k> 0时 k--
#include<bits/stdc++.h>
using namespace std;
int main() {
int t,n, k; char c; cin >> t;
while (t--) {
k = 0;
cin >> n; while (n--) {
cin >> c;
if (c == '(')k++;
else if (k > 0)--k;
}
cout << k << endl;
}
}
第四题开始就没做出来了,记录dalao们的题解
D. Zero Remainder Array
题意:给你n长度的整数数组 a ,一个x 每次操作可以选择一个 下标i 让a[i]+=x 然后x自增1 操作2是 不选择下标 单独的x自增1
做法:a[i]对k取模 得到离k(k-a[i]%k)的距离,统计离k距离相同数量最多的,num 和 mx 答案就是 (num-1)*k+mx+1;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int t;
cin >> t;
while (t--) {
ll n, k, ans = 0, a;
cin >> n >> k;
map<ll, ll> mp;
for (int i = 0; i < n; ++i) cin >> a, mp[(k - (a % k)) % k]++;
for (auto it : mp)
if (it.first) ans = max(ans, (it.second - 1) * k + it.first + 1);
cout << ans << '\n';
}
return 0;
}
E1. Reading Books (easy version)
题意:给n本书,以及一个K 每本书 可能是 (Alice 喜欢 Bob 不喜欢)(Alice 不喜欢 Bob 喜欢)(Alice 不喜欢 Bob 不喜欢) 每本书有一个阅读时间t[i] 每次只能选择一本书进行阅读。现在要求ALICE 和Bob 都至少读k本自己喜欢的书,并且时间消耗最少。
做法:
三种情况分开保存,(Alice 喜欢 Bob 不喜欢)a数组, (Alice 不喜欢 Bob 喜欢)b数组,(Alice 不喜欢 Bob 不喜欢)c数组
对a数组 b数组分别选前k个,不足k个从c里面选。都满k个后 再从c里面选共同的 同时去掉 a里面、b里面得到 的两个最大值。
// Author : RioTian
// Time : 21/01/23
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int _;
vector<int> a, b, c;
void solve() {
int n, k, t, x, y, ans = 0;
cin >> n >> k;
for (int i = 1; i <= n; ++i) {
cin >> t >> x >> y;
if (x == 1 && y == 1)
c.push_back(t);
else if (x == 1)
a.push_back(t);
else if (y == 1)
b.push_back(t);
}
sort(a.begin(), a.end()), sort(b.begin(), b.end()),
sort(c.begin(), c.end());
n = min(a.size(), b.size());
for (int i = 0; i < n; ++i) c.push_back(a[i] + b[i]);
sort(c.begin(), c.end());
if (c.size() < k)
ans = -1;
else {
for (int i = 0; i < k; ++i) ans += c[i];
}
cout << ans << endl;
}
int main() {
// freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
solve();
}
E2
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, int> pli;
pli av[200001], bv[200001], cv[200001], dv[200001];
int main() {
int n, k, m;
scanf("%d %d %d", &n, &m, &k);
int aa = 0, bb = 0, cc = 0, dd = 0;
ll as = 0, bs = 0, cs = 0, ds = 0;
for (int i = 0; i < n; i++) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
if (b == 1 && c == 1) av[aa++] = {a, i};
else if (b == 1) bs += a, bv[bb++] = {a, i};
else if (c == 1) cs += a, cv[cc++] = {a, i};
else ds += a, dv[dd++] = {a, i};
}
sort(av, av + aa); sort(bv, bv + bb);
sort(cv, cv + cc); sort(dv, dv + dd);
int mai = -1, mbi = -1, mci = -1, mdi = -1;
int bi = bb, ci = cc, di = dd, q;
ll ans = 1e18, ret;
for (int i = 0; i <= aa; i++) {
if (i) as += av[i-1].first;
q = max(0, k - i);
for (int j = 0; j < 3; j++) {
if (bi < bb) bs += bv[bi++].first;
if (ci < cc) cs += cv[ci++].first;
if (di < dd) ds += dv[di++].first;
}
if (q > bb || q > cc || (i + 2*q) > m || i+bb+cc+dd < m) continue;
while (i+bi+ci+di > m) {
ll bt = (bi>q?bv[bi-1].first:-1e18);
ll ct = (ci>q?cv[ci-1].first:-1e18);
ll dt = (di>0?dv[di-1].first:-1e18);
if (bt >= ct && bt >= dt) bs -= bv[--bi].first;
else if (ct >= bt && ct >= dt) cs -= cv[--ci].first;
else ds -= dv[--di].first;
}
ret = as + bs + cs + ds;
if (ans > ret) {
ans = ret;
mai = i, mbi = bi, mci = ci, mdi = di;
}
}
if (ans > 1e17) return !printf("-1");
printf("%lld\n", ans);
for (int i = 0; i < mai; i++) printf("%d ", av[i].second + 1);
for (int i = 0; i < mbi; i++) printf("%d ", bv[i].second + 1);
for (int i = 0; i < mci; i++) printf("%d ", cv[i].second + 1);
for (int i = 0; i < mdi; i++) printf("%d ", dv[i].second + 1);
}
F. Cyclic Shifts Sorting
题意:给你n长度 整数数组 a[i] 每次选择一个下标i 然后对(i,i+1,i+2)进行向右移动 i+2移到i。问最少的操作使得这个数组是递增的数组,如果不能得到则输出-1
做法:
复杂模拟,每次找到最小的那个值,然后不断的反转 移到前面去,进行n-2轮后 得到两种情况:
第一种:1 2 3 4 5 有序
第二种:1 2 3 5 4 无序。
对于第二种则需要将 5 4 的位置倒一下 选择n-2下标:
1 2 4 3 5
接着对 4 3 倒一下:
1 3 2 4 5
此时发现 只有前面出现两个相同的时候才可以得到有序的数组。
例如:
1 2 3 3 5 4 => 1 2 3 4 3 5 => 1 2 3 3 4 5
#include <bits/stdc++.h>
using namespace std;
int n, t[502], p, w;
vector<int> sol;
void q(int a) {
sol.push_back(a);
swap(t[a + 2], t[a]), swap(t[a + 1], t[a + 2]);
}
int main() {
cin >> w;
while (w--) {
cin >> n, sol.clear();
for (int i = 1; i <= n; i++) cin >> t[i];
for (int i = 1; i <= n - 2; i++) {
p = i;
for (int j = i; j <= n; j++)
if (t[j] < t[p]) p = j;
if (p == n) p -= 2, q(p);
if ((p - i) % 2) q(p - 1), p++;
while (p > i) p -= 2, q(p);
}
if (t[n] < t[n - 1])
for (int i = 1; i <= n - 2; i++)
if (t[i] == t[i + 1]) {
for (int j = i; j <= n - 2; j++) q(j), q(j);
break;
}
if (t[n - 2] == t[n]) q(n - 2);
if (t[n] < t[n - 1])
cout << -1 << "\n";
else {
cout << sol.size() << "\n";
for (int i = 0; i < sol.size(); i++) cout << sol[i] << " ";
cout << "\n";
}
}
return 0;
}
Codeforces Round #653 (Div. 3) 题解的更多相关文章
- Codeforces Round #182 (Div. 1)题解【ABCD】
Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...
- Codeforces Round #608 (Div. 2) 题解
目录 Codeforces Round #608 (Div. 2) 题解 前言 A. Suits 题意 做法 程序 B. Blocks 题意 做法 程序 C. Shawarma Tent 题意 做法 ...
- Codeforces Round #525 (Div. 2)题解
Codeforces Round #525 (Div. 2)题解 题解 CF1088A [Ehab and another construction problem] 依据题意枚举即可 # inclu ...
- Codeforces Round #528 (Div. 2)题解
Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...
- Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F
Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...
- Codeforces Round #677 (Div. 3) 题解
Codeforces Round #677 (Div. 3) 题解 A. Boring Apartments 题目 题解 简单签到题,直接数,小于这个数的\(+10\). 代码 #include &l ...
- Codeforces Round #665 (Div. 2) 题解
Codeforces Round #665 (Div. 2) 题解 写得有点晚了,估计都官方题解看完切掉了,没人看我的了qaq. 目录 Codeforces Round #665 (Div. 2) 题 ...
- Codeforces Round #160 (Div. 1) 题解【ABCD】
Codeforces Round #160 (Div. 1) A - Maxim and Discounts 题意 给你n个折扣,m个物品,每个折扣都可以使用无限次,每次你使用第i个折扣的时候,你必须 ...
- Codeforces Round #383 (Div. 2) 题解【ABCDE】
Codeforces Round #383 (Div. 2) A. Arpa's hard exam and Mehrdad's naive cheat 题意 求1378^n mod 10 题解 直接 ...
- Codeforces Round #271 (Div. 2)题解【ABCDEF】
Codeforces Round #271 (Div. 2) A - Keyboard 题意 给你一个字符串,问你这个字符串在键盘的位置往左边挪一位,或者往右边挪一位字符,这个字符串是什么样子 题解 ...
随机推荐
- SpringBoot + 通义千问 + 自定义React组件,支持EventStream数据解析!
一.前言 大家好!我是sum墨,一个一线的底层码农,平时喜欢研究和思考一些技术相关的问题并整理成文,限于本人水平,如果文章和代码有表述不当之处,还请不吝赐教. 最近ChatGPT非常受欢迎,尤其是在编 ...
- MySQL的索引为什么使用B+树而不使用跳表?
目录 MySQL的索引为什么使用B+树而不使用跳表? 1.B+树的结构 2.跳表的结构 3.B+树和跳表的区别 1.B+树新增数据会怎么样 跳表新增数据 4.Mysql的索引为什么使用B+树而不使用跳 ...
- wps表格怎么打印选中区域的内容?
打印选中区域的内容,您可以按照以下步骤进行操作: 选择要打印的区域 打开 WPS 表格,在工作表中选择您希望打印的区域.您可以拖动鼠标或使用键盘中的方向键来选择单元格. 设置打印区域 一旦您选中了需要 ...
- [ABC309Ex] Simple Path Counting Problem
Problem Statement We have a grid with $N$ rows and $M$ columns. We denote by $(i,j)$ the cell in the ...
- 配置tabBar导航菜单与open跳转差异
"tabBar": { "color": "#333", "selectedColor": ...
- Ruby 版本升级
一.升级原因 在开发shopify app的时候,提示我当前的Ruby版本不支持(如下图),所以需要升级Ruby. 由于Ruby 中的一些 Gem 依赖于 OpenSSL 库,所以更改 Ruby 版本 ...
- ElasticSearch-document文档数据-增删改
文档就是相当于每条记录. 每个文档(数据记录行)都有几个元数据,分别是: _index,表示该文档是那个索引中的. _type,表示文档的类型 _id,文档的唯一ID编号 _score,相关性分数. ...
- IntelliJ IDEA下载安装,以及关联gitee
https://www.jetbrains.com.cn/ 点击下载 IntelliJ IDEA Ultimate 旗舰版(收费) IntelliJ IDEA Community 社区版(免费) 安装 ...
- Spring MVC的生命周期与简单三大组件的简单介绍
1.说到Spring MVC就会想到它是基于MVC设计模式的思想来设计的: 那么MVC设计模式是什么呢? 下面来介绍一下 MVC 设计模式 MVC是模型(model)-视图(view)-控制器(con ...
- spring-mvc 系列:HttpMessageConverter(@RequestBody、RequestEntity、@ResponseBody、@RestController、ResponseEntity、文件上传下载)
目录 一.@RequestBody 二.RequestEntity 三.@ResponseBody 四.SpringMVC处理json 五.@RestController 六.ResponseEnti ...