记第一场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) 题解的更多相关文章

  1. Codeforces Round #182 (Div. 1)题解【ABCD】

    Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...

  2. Codeforces Round #608 (Div. 2) 题解

    目录 Codeforces Round #608 (Div. 2) 题解 前言 A. Suits 题意 做法 程序 B. Blocks 题意 做法 程序 C. Shawarma Tent 题意 做法 ...

  3. Codeforces Round #525 (Div. 2)题解

    Codeforces Round #525 (Div. 2)题解 题解 CF1088A [Ehab and another construction problem] 依据题意枚举即可 # inclu ...

  4. Codeforces Round #528 (Div. 2)题解

    Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...

  5. Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F

    Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...

  6. Codeforces Round #677 (Div. 3) 题解

    Codeforces Round #677 (Div. 3) 题解 A. Boring Apartments 题目 题解 简单签到题,直接数,小于这个数的\(+10\). 代码 #include &l ...

  7. Codeforces Round #665 (Div. 2) 题解

    Codeforces Round #665 (Div. 2) 题解 写得有点晚了,估计都官方题解看完切掉了,没人看我的了qaq. 目录 Codeforces Round #665 (Div. 2) 题 ...

  8. Codeforces Round #160 (Div. 1) 题解【ABCD】

    Codeforces Round #160 (Div. 1) A - Maxim and Discounts 题意 给你n个折扣,m个物品,每个折扣都可以使用无限次,每次你使用第i个折扣的时候,你必须 ...

  9. 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 题解 直接 ...

  10. Codeforces Round #271 (Div. 2)题解【ABCDEF】

    Codeforces Round #271 (Div. 2) A - Keyboard 题意 给你一个字符串,问你这个字符串在键盘的位置往左边挪一位,或者往右边挪一位字符,这个字符串是什么样子 题解 ...

随机推荐

  1. 解决Vscode中代码格式化时老换行

    问题: 小颖用vscode的格式化代码后发现代码老是换行,有时看起来就很难受,比如下面的: 问度娘后终于弄好啦,记录下,省的以后换电脑了重装了vscode又不会了,主要是百度给的解决方法好几个,但有的 ...

  2. DFT与ATE IP TEST

    IP的DFT设计测试与ATE IP TEST是一个设计,测试活动吗? 不是. 这两个设计对于前端工农村很容易搞混,认为是同一个人负责,同一个活动.实际情不是. DFT主要空DSC控制器对IP进行扫描, ...

  3. AgileConfig 1.8.0 已适配 .NET8

    Hello 大家好.本月圈子里最大的事莫过于 .NET8 正式 release.群友们都在适配 .NET8.抽个周末我也把 AgileConfig 升级到了 .NET8.下面把升级的过程简单记录一下, ...

  4. ssm整合-项目异常处理方案

    项目异常分类: 项目异常处理方案: 需要自定义异常处理 然后在处理器中加入 package com.itheima.controller; import com.itheima.exception.B ...

  5. 一招MAX降低10倍,现在它是我的了

    一.背景 性能优化是一场永无止境的旅程. 到家门店系统,作为到家核心基础服务之一,门店C端接口有着调用量高,性能要求高的特点. C端服务经过演进,核心接口先查询本地缓存,如果本地缓存没有命中,再查询R ...

  6. java.lang.TypeNotPresentException: Type javax.servlet.http.HttpServletRequest not present

    完整的报错信息 java.lang.TypeNotPresentException: Type javax.servlet.http.HttpServletRequest not present at ...

  7. S32Kxxx bootloader之CAN FD UDS bootloader

    了解更多关于bootloader 的C语言实现,请加我Q扣: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). 六年前, 汽车内ECU ...

  8. 在 IIS 上生成经典 ASP 网站

    场景:在 IIS 上生成经典 ASP 网站 本文档将指导你完成安装 IIS 和配置经典 ASP 网站的过程. 经典 ASP 是服务器端脚本环境,可用于创建和运行动态 Web 应用程序. 借助 ASP, ...

  9. jenkins删除构建历史并重置构建序号

    系统管理 工具和动作-->脚本命令执行 删除之前,现在已经构建了156次  输入脚本 println(Jenkins.instance.getJobNames()) //查看获取任务名列表//要 ...

  10. ElasticSearch之Exists API

    检查指定名称的索引是否存在. 命令样例如下: curl -I "https://localhost:9200/testindex_002?pretty" --cacert $ES_ ...