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 题意 给你一个字符串,问你这个字符串在键盘的位置往左边挪一位,或者往右边挪一位字符,这个字符串是什么样子 题解 ...
随机推荐
- Opencv学习笔记(2)
图像处理是图像识别过程中重要一环,一张图像可能包括海量的不明确的信息,图像处理的目的是消除图像中无关的信息,恢复有用的真实信息,增强有效信息的可检测性,最大限度地简化数据. 参考知乎文章链接:http ...
- AutoCAD ObjectARX 二次开发(2020版)--1,下载和部署开发环境--
教程说明: 本教程为2019年10月开始编撰,使用CAD官方最新版本的软件和库.对旧版本仍有参考价值. 本教程中使用的各种软件版本为官方指定匹配版本. 本教程需要你拥有编程基础,对于普通编程常识不再敷 ...
- 如何用BI制作图表组合?
BI(Business Intelligence)是一种通过收集.分析和可视化数据来帮助企业做出决策的技术和工具.在BI中,制作图表组合是一种常见的方式,可以将不同的图表类型组合在一起,以更全面地呈现 ...
- Element的安装与基本使用
一.什么是Element? Element是饿了么团队研发的一套为开发者与设计师等准备的基于Vue2.0的桌面端组件库,使开发人员可以快速拼凑出一套页面 组件:组成网页的部件,例如:超链接,按钮,图片 ...
- 明解Java第一章练习题答案
@ 目录 练习1-1 练习1-2 练习1-3 <明解Java>书籍其他章节答案 练习1-1 如果没有表示程序语句末尾的分号,结果会怎么样呢?请编译程序进行确认. 答:编译器报错 练习1-2 ...
- Qt官网开源最新版下载安装保姆级教程
什么是Qt(了解请跳过) Qt 基本介绍 Qt 是一个跨平台C++图形用户界面应用程序开发框架. 有关 Qt 的详细介绍,可以参考这篇文章: Qt是什么?Qt简介(非常全面) - 李清龙的文章 - 知 ...
- Redis 学习笔记1:数据类型
Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及 zset(sorted set:有序集合). 一.Redis 数据类型-STRING 1.常用 ...
- 万界星空科技MES系统中的生产调度流程
MES系统生产调度的目标是达到作业有序.协调.可控和高效的运行效果,作业计划的快速生成以及面向生产扰动事件的快速响应处理是生产调度系统的核心和关键. 为了顺利生成作业计划,需要为调度系统提供完整的 ...
- ElasticSearch之Close index API
关闭指定的索引. 索引关闭之后: 停止对读.写操作的响应. 停止检索操作的响应. 在索引关闭前,允许执行的操作,关闭之后均不允许执行. ElasticSearch取消对索引的相关维护操作,包含内存中的 ...
- Python——第三章:函数的返回值
函数的返回值: 函数执行之后. 会给调用方一个结果. 这个结果就是返回值 关于return: 函数只要执行到了return. 函数就会立即停止并返回内容. 函数内的return的后续的代 ...