比赛链接:https://codeforces.com/contest/1409

A. Yet Another Two Integers Problem

题意

给出两个数 $a$ 和 $b$,有以下两种操作:

  • $a+=k$
  • $a-=k$
  • $k \in [1, 10]$

问将 $a$ 变为 $b$ 最少需要操作多少次。

题解

$a,b$ 之差的绝对值对 $10$ 取上整。

代码

#include <bits/stdc++.h>
using namespace std; void solve() {
int a, b;
cin >> a >> b;
cout << (abs(b - a) + 9) / 10 << "\n";
} int main() {
int t;
cin >> t;
while (t--) solve();
}

B. Minimum Product

题意

给出两个数 $a$ 和 $b$,每次可以选择一个数减一,该操作最多执行 $k$ 次,同时要求 $a$ 不能小于 $x$,$b$ 不能小于 $y$,问 $a \cdot b$ 的最小值。

题解

如果给一个数减一,最佳方案就是一直给它减一,减到底后再减另一个数。

然后枚举先减 $a$ 还是先减 $b$ 即可。

代码一

#include <bits/stdc++.h>
using namespace std; void solve() {
int a, b, x, y, n;
cin >> a >> b >> x >> y >> n;
auto cal = [](int a, int b, int x, int y, int n) {
int mi = min(n, a - x);
n -= mi;
a -= mi;
mi = min(n, b - y);
n -= mi;
b -= mi;
return 1ll * a * b;
};
cout << min(cal(a, b, x, y, n), cal(b, a, y, x, n)) << "\n";
} int main() {
int t;
cin >> t;
while (t--) solve();
}

代码二

#include <bits/stdc++.h>
using namespace std; void solve() {
int a, b, x, y, n;
cin >> a >> b >> x >> y >> n;
auto cal = [&](int a, int b, int x, int y) {
int da = min(n, a - x);
int db = min(n - da, b - y);
return 1ll * (a - da) * (b - db);
};
cout << min(cal(a, b, x, y), cal(b, a, y, x)) << "\n";
} int main() {
int t;
cin >> t;
while (t--) solve();
}

C. Yet Another Array Restoration

题意

给出一个数组的大小 $n$ 和其中的两个元素 $x,y(x<y)$,试还原这个数组,要求:

  • 数组中有 $n$ 个不同的正整数
  • 数组排序后相邻元素之差相等
  • 数组中的最大值尽可能地小

题解

数组相邻元素之差一定是 $y-x$ 的因子,据此分为以下三种情况:

  • 因子过小,$x$ 到 $y$ 间可以容纳的元素个数大于 $n-2$,继续寻找更大的因子
  • 因子适中,$x$ 到 $y$ 间可以容纳的元素个数等于 $n-2$,此时以 $x$ 为起点,因子大小为步长输出 $n$ 次即可
  • 因子过大,$x$ 到 $y$ 间可以容纳的元素个数小于 $n-2$,因为最大值要尽量小,所以应尽可能地向 $x$ 的左端拓展正起点

代码

#include <bits/stdc++.h>
using namespace std; void solve() {
int n, x, y;
cin >> n >> x >> y;
int len = y - x;
for (int p = 1; p <= len; p++) {
if (len % p == 0) {
int can_hold = len / p - 1;
if (can_hold > n - 2) {
continue;
} else if (can_hold == n - 2) {
for (int i = x; i <= y; i += p)
cout << i << " \n"[i == y];
return;
} else {
int extra = n - 2 - can_hold;
int cnt = 0;
for (int i = x - p * min(extra, x / p - (x % p == 0)); cnt < n; i += p)
cout << i << " \n"[++cnt == n];
return;
}
}
}
} int main() {
int t;
cin >> t;
while (t--) solve();
}

D. Decrease the Sum of Digits

题意

给出一个数 $n$,每次可以给 $n$ 加上 $1$,找出使得 $n$ 的数位之和 $\le s$ 的最少操作数。

题解

给一个数位操作最终一定会使该数位的值变为 $0$(否则会一直增加数位之和),从低位到高位模拟即可。

代码

#include <bits/stdc++.h>
using namespace std; int sum(string s) {
int res = 0;
for (char c : s) res += c - '0';
return res;
} void solve() {
string n;
int s;
cin >> n >> s;
long long ans = 0, p = 1;
n = "0" + n;
for (int i = n.size() - 1; sum(n) > s; i--) {
ans += ('9' + 1 - n[i]) * p;
n[i] = '0';
++n[i - 1];
p *= 10;
}
cout << ans << "\n";
} int main() {
int t;
cin >> t;
while (t--) solve();
}

E. Two Platforms

题意

给出一个平面 $n$ 个点的坐标,有两个长为 $k$ 的平台,每个平台可以选择一个位置固定放置,问当所有点同时开始下落后,两个平台加起来最多可以接到多少个点。

题解

因为平台的纵坐标可以无限低所以只考虑横坐标即可。

将点的横坐标排序并计算两个 $dp$ 数组:

  • $dp1$ 以该点为起点放置平台可以接到多少个点
  • $dp2$ 以该点或之后的点为起点放置平台最多可以接到多少个点

枚举第一个平台的起点 $i$,然后找到第一个坐标大于 $x+k$ 的点 $j$,答案即 $max(dp1[i] + dp2[j])$ 。

代码一

二分查找点 $j$

#include <bits/stdc++.h>
using namespace std; void solve() {
int n, k;
cin >> n >> k;
vector<int> x(n), y(n);
for (int i = 0; i < n; i++)
cin >> x[i];
for (int i = 0; i < n; i++)
cin >> y[i];
sort(x.begin(), x.end());
vector<int> dp1(n);
for (int i = 0; i < n; i++) {
int j = upper_bound(x.begin(), x.end(), x[i] + k) - x.begin();
dp1[i] = j - i;
}
vector<int> dp2(dp1);
for (int i = n - 2; i >= 0; i--)
dp2[i] = max(dp2[i], dp2[i + 1]);
int ans = 0;
for (int i = 0; i < n; i++) {
int j = upper_bound(x.begin(), x.end(), x[i] + k) - x.begin();
ans = max(ans, dp1[i] + (j < n ? dp2[j] : 0));
}
cout << ans << "\n";
} int main() {
int t;
cin >> t;
while (t--) solve();
}

代码二

双指针查找点 $j$

#include <bits/stdc++.h>
using namespace std; void solve() {
int n, k;
cin >> n >> k;
vector<int> x(n), y(n);
for (int i = 0; i < n; i++)
cin >> x[i];
for (int i = 0; i < n; i++)
cin >> y[i];
sort(x.begin(), x.end());
vector<int> dp1(n);
for (int i = 0, j = 0; i < n; i++) {
while (j < n and x[j] <= x[i] + k)
++j;
dp1[i] = j - i;
}
vector<int> dp2(dp1);
for (int i = n - 2; i >= 0; i--)
dp2[i] = max(dp2[i], dp2[i + 1]);
int ans = 0;
for (int i = 0, j = 0; i < n; i++) {
while (j < n and x[j] <= x[i] + k)
++j;
ans = max(ans, dp1[i] + (j < n ? dp2[j] : 0));
}
cout << ans << "\n";
} int main() {
int t;
cin >> t;
while (t--) solve();
}

F. Subsequences of Length Two

题意

给出两个小写字母串 $s$ 和 $t$,$t$ 长为 $2$。每次可以替换 $s$ 中的一个字母,该操作最多执行 $k$ 次,问 $t$ 作为 $s$ 的子序列最多可以出现多少次。

题解

$dp[i][j][k]$ 为前 $i$ 位操作 $j$ 次后有 $k$ 个 $t_0$ 时的子序列个数。

代码

#include <bits/stdc++.h>
using namespace std;
constexpr int INF = 1e9; void chmax(int &x, int y) {
x = max(x, y);
} int main() {
int n, k;
cin >> n >> k;
string s, t;
cin >> s >> t;
vector<vector<vector<int>>> dp(n + 1, vector<vector<int>>(n + 1, vector<int>(n + 1, -INF)));
dp[0][0][0] = 0;
for (int i = 0; i < n; i++) {
for (int ck = 0; ck <= k; ck++) {
for (int cnt0 = 0; cnt0 <= n; cnt0++) {
if (dp[i][ck][cnt0] == -INF) continue;
int e0 = s[i] == t[0];
int e1 = s[i] == t[1];
int e01 = t[0] == t[1];
//不操作
chmax(dp[i + 1][ck][cnt0 + e0], dp[i][ck][cnt0] + (e1 ? cnt0 : 0));
if (ck < k) {
//s[i]变为t[0]
chmax(dp[i + 1][ck + 1][cnt0 + 1], dp[i][ck][cnt0] + (e01 ? cnt0 : 0));
//s[i]变为t[1]
chmax(dp[i + 1][ck + 1][cnt0 + e01], dp[i][ck][cnt0] + cnt0);
}
}
}
}
cout << *max_element(dp[n][k].begin(), dp[n][k].end()) << "\n";
}

Codeforces Round #667 (Div. 3)的更多相关文章

  1. Codeforces Round #667 (Div. 3) B、C、D、E 题解

    抱歉B.C题咕了这么久 B. Minimum Product #枚举 #贪心 题目链接 题意 给定四个整数\(a, b, x, y\),其中\(a\geq x, b\geq y\),你可以执行不超过\ ...

  2. Codeforces Round #667 (Div. 3) E. Two Platforms (双指针)

    题意:有\(n\)个点往下落,你可以在最下面放两个长度为\(k\)的板子,问做多能接到多少个点. 题解:这题给纵坐标\(y\)完全没有用,我们先对横坐标\(x\)排序,然后从左边开始枚举,用\(l[i ...

  3. Codeforces Round #667 (Div. 3) D. Decrease the Sum of Digits (贪心)

    题意:给你一个正整数\(n\),每次可以对\(n\)加一,问最少操作多少次是的\(n\)的所有位数之和不大于\(s\). 题解:\(n\)的某个位置上的数进位,意味这后面的位置都可以被更新为\(0\) ...

  4. Codeforces Round #667 (Div. 3) C. Yet Another Array Restoration (数学)

    题意:给你两个数字\(x\)和\(y\),让你构造一个长度为\(n\)的序列,要求包含\(x\)和\(y\),并且排序后相邻两项的差值相等. 题解:有排序后相邻两项的差值相等可知,构造的序列排序后一定 ...

  5. Codeforces Round #667 (Div. 3) B. Minimum Product (贪心,数学)

    题意:给你\(a\)和\(b\)两个数,每次操作可以是任意一个数\(-1\),最多操作\(n\),并且\(a\ge x\),\(b\ge y\),求操作后\(a*b\)的最小值. 题解:观察样例并且在 ...

  6. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  7. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  8. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  9. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

随机推荐

  1. 【高精度】计算2的N次方

    题目相关 [题目描述] 任意给定一个正整数N(N≤100),计算2的n次方的值. [输入] 输入一个正整数N. [输出] 输出2的N次方的值. [输入样例] 5 [输出样例] 32 分析 本题考察的是 ...

  2. 初识JWT

    1.JWT是什么 官方网站 JWT是JSON Web Token的简称.是一种开放标准(RFC 7519),定义了一种紧凑且自包含的方式,以JSON对象的形式在各方之间安全地传输信息,因为他被数字签名 ...

  3. Java并发包源码学习系列:ReentrantReadWriteLock读写锁解析

    目录 ReadWriteLock读写锁概述 读写锁案例 ReentrantReadWriteLock架构总览 Sync重要字段及内部类表示 写锁的获取 void lock() boolean writ ...

  4. 怎么启用apache的mod_log_sql模块将所有的访问信息直接记录在mysql中

    怎么启用apache的mod_log_sql模块将所有的访问信息直接记录在mysql中

  5. 【Linux】find删除365天以前的文件详细解析

    find . -name "*" -mtime +365 -exec rm -rf {} \; -mtime +365  文件被修改的时间,最后一次发生到现在365天 -atime ...

  6. 【ORA】ORA-27125:unable to create shared memory segment

    在安装Oracle 10g的时候出现一个了错误,在网上总结了一下大牛写的文章 ORA-27125:unable to create shared memory segment 安装时出现这个错误安装会 ...

  7. 基于Asp.Net Core 5.0依赖Quartz.Net框架编写的任务调度web管理平台

    源码地址: https://github.com/246850/Calamus.TaskScheduler 演示地址:http://47.101.47.193:1063/ 1.Quartz.NET框架 ...

  8. 跨平台导PDF,结合wkhtmltopdf很顺手

    前言 好东西要分享,之前一直在使用wkhtmltopdf进行pdf文件的生成,常用的方式就是先安装wkhtmltopdf,然后在程序中用命令的方式将对应的html生成pdf文件,简单而且方便:但重复的 ...

  9. Windows Server 2008 R2系统安装

    把系统U盘插到服务器上,然后启动服务器进入BIOS界面选择U盘启动. 根据服务器的不同,进入BIOS界面的按钮也不一样,主流的有F10.F11.F12.F2.ESC.delete等. 在从BIOS启动 ...

  10. CobalStrike 4.0 生成后门几种方式 及 主机上线后基础操作

    出品|MS08067实验室(www.ms08067.com) 本文作者:BlackCat(Ms08067内网安全小组成员) CobalStrike 4.0 生成后门几种方式 步骤:Attacks-〉P ...