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

A. Add Candies

题意

\(1\) 到 \(n\) 个袋子里依次有 \(1\) 到 \(n\) 个糖果,可以选择操作 \(m\) 次,第 \(i\) 次操作可以:

  • 选择一个袋子,除了这个袋子外其他袋子都再放入 \(i\) 个糖果

问是否存在一种操作序列,使得最终 \(n\) 个袋子内的糖果数相同。

题解

将每个袋子内的糖果都加到 \(\frac{n(n + 1)}{2}\) 个。

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
cout << n << "\n";
for (int i = 1; i <= n; i++) cout << i << " \n"[i == n];
}
return 0;
}

B. Numbers Box

题意

给出一个 \(n \times m\) 的矩阵,每次操作可以:

  • 选择任意两个相邻元素各乘以 \(-1\)

可以操作任意次,问矩阵中所有元素之和的最大值为多少。

题解

有零的话可以把所有负数都变为正,答案即所有数的绝对值之和。

否则如果负数个数为奇数,需要减去一个绝对值最小的数。

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
vector<int> a(n * m);
int sum = 0, neg = 0, zero = 0, mi = INT_MAX;
for (auto &x : a) cin >> x, sum += abs(x), neg += x < 0, zero += x == 0, mi = min(mi, abs(x));
if (neg & 1 and zero == 0) sum -= 2 * mi;
cout << sum << "\n";
}
return 0;
}

C. Knapsack

题意

有一个承重为 \(W\) 的背包,另有 \(n\) 个物品,分别重 \(w_i\) ,判断能否找到一些物品使得它们的总重 \(C\) 满足 \(\lceil \frac{W}{2} \rceil \le C \le W\) ,如果可以,输出这些物品的编号。

题解

首先去除 \(w_i > W\) 的元素,然后如果有单个 \(w_i \ge \lceil \frac{W}{2} \rceil\) 直接选取即可,此时余下的 \(w_i\) 均小于 \(\lceil \frac{W}{2} \rceil\) ,如果它们的总和小于 \(\lceil \frac{W}{2} \rceil\) 则无解,否则一定存在满足题意的序列,依次选取至不小于 \(\lceil \frac{W}{2} \rceil\) 即可。

证明

两个小于 \(\lceil \frac{W}{2} \rceil\) 的数相加有两种情况:

  • 小于 \(\lceil \frac{W}{2} \rceil\)
  • 不小于 \(\lceil \frac{W}{2} \rceil\) 且小于 \(W\)

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
long long n, W;
cin >> n >> W;
long long mid_W = (W + 1) / 2;
vector<int> w(n);
for (auto &x : w) cin >> x;
vector<int> p(n);
iota(p.begin(), p.end(), 0);
sort(p.begin(), p.end(), [&](int x, int y) {
return w[x] < w[y];
});
while (p.size() and w[p.back()] > W) {
p.pop_back();
}
if (p.empty() or accumulate(p.begin(), p.end(), 0LL, [&](long long sum, int x) { return sum + w[x]; }) < mid_W) {
cout << -1 << "\n";
continue;
}
if (w[p.back()] >= mid_W) {
cout << 1 << "\n" << p.back() + 1 << "\n";
continue;
}
vector<int> v;
long long sum = 0;
for (auto i : p) {
sum += w[i], v.push_back(i);
if (sum > mid_W) break;
}
cout << v.size() << "\n";
for (auto i : v) cout << i + 1 << " \n"[i == v.back()];
}
return 0;
}

D. Catching Cheaters

题意

给出两个字符串 \(s, t\) ,定义: \(f(s, t) = 4 \cdot LCS(s, t) - |s| - |t|\) 。

找出 \(s, t\) 所有连续子串匹配的可能情况中最大的函数值。

题解

\(dp_{ij}\) 的含义为以 \(s_i\) 和 \(t_j\) 结尾的两个连续子串匹配的最大函数值。

状态转移方程分为两种情况:

  • \(s_i = t_j\)

    • 可以在以 \(s_{i - 1}\) 和 \(t_{j - 1}\) 结尾的两个子串尾部各添加一个字符,即 \(dp_{ij} = dp_{i - 1 j - 1} + 2\)
    • 也可以在两个空串尾部添加一个字符,即 \(dp_{ij} = 0 + 2\)
  • \(s_i \ne t_j\)
    • 可以在以 \(s_{i - 1}\) 或 \(t_{j - 1}\) 结尾的一个子串尾部添加一个字符,即 \(dp_{ij} = max(dp_{i - 1 j}, dp_{i j - 1}) - 1\)

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
string s, t;
cin >> s >> t;
vector<vector<int>> dp(n + 1, vector<int>(m + 1));
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (s[i - 1] == t[j - 1]) dp[i][j] = max(0, dp[i - 1][j - 1]) + 2;
else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) - 1;
ans = max(ans, dp[i][j]);
}
}
cout << ans << "\n";
return 0;
}

Codeforces Round #683 (Div. 2, by Meet IT)【ABCD】的更多相关文章

  1. Codeforces Round #683 (Div. 2, by Meet IT) D. Catching Cheaters (DP)

    题意:给你两个字符串,每次取它们的子串C和D,然后求LCS,得到的贡献为\(4*LCS(C,D)-|C|-|D|\),求最大贡献. 题解:首先应该了解\(O(n^2)\)的LCS的dp写法,然后在此基 ...

  2. Codeforces Round #683 (Div. 2, by Meet IT)

    A 初始情况\(1\) ~ \(n\)堆分别有 \(1\) ~ \(n\) 个糖果,第\(i\)次操作给除了所选堆的糖果数 \(+ i\), 找到一种方案可以使得所有堆糖果数相同,输出操作次数和每次选 ...

  3. Codeforces Round #540 (Div. 3) C. Palindromic Matrix 【暴力】

    任意门:http://codeforces.com/contest/1118/problem/C C. Palindromic Matrix time limit per test 2 seconds ...

  4. Codeforces Round #455 (Div. 2) A. Generate Login【贪心】

    A. Generate Login time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  5. Codeforces Round #683 (Div. 1) Solution

    A. Knapsack 猜个结论--先把所有的东西加起来,如果小于 \(\frac{1}{2}m\) 就输出不合法:如果在 \([\frac{1}{2}m, m]\)之间直接全部输出:若大于 \(m\ ...

  6. Codeforces Round #538 (Div. 2) D. Flood Fill 【区间dp || LPS (最长回文序列)】

    任意门:http://codeforces.com/contest/1114/problem/D D. Flood Fill time limit per test 2 seconds memory ...

  7. Codeforces Round #521 (Div. 3) D. Cutting Out 【二分+排序】

    任意门:http://codeforces.com/contest/1077/problem/D D. Cutting Out time limit per test 3 seconds memory ...

  8. Codeforces Round #254 (Div. 2) DZY Loves Chemistry【并查集基础】

    一开始不知道题意是啥意思,迟放进去反应和后放进去反应有什么区别 对于第三组数据不是很懂,为啥312,132的组合是不行的 后来发现这是一道考察并查集的题目 QAQ 怒贴代码: #include < ...

  9. Codeforces Round #555 (Div. 3) E. Minimum Array 【数据结构 + 贪心】

    一 题面 E. Minimum Array 二 分析 注意前提条件:$0 \le  a_{i} \lt n$ 并且 $0 \le  b_{i} \lt n$.那么,我们可以在$a_{i}$中任取一个数 ...

随机推荐

  1. Nginx集成Naxsi防火墙

    前言 因工作原因,接触到了WAF,今天部署了一下Naxsi,记录一下 GitHub 正文 环境 Centos 7 下载 更新yum yum update -y 安装必要依赖 yum install g ...

  2. python virtualenv 基本使用

    下载 pip install virtualenv 校验是否成功 virtualenv --version 使用 创建env环境 要写一个新项目,使用env先创建环境 cd xx\xx\xx\ # 进 ...

  3. 【SpringMVC】SpringMVC 实现文件上传

    SpringMVC 实现文件上传 文章源码 文件上传回顾 查看 JavaWeb 阶段的文件上传下载 实现步骤: 客户端: 发送 post 请求,告诉服务器要上传什么文件 服务器: 要有一个 form ...

  4. 如何在 Vite 中使用 Element UI + Vue 3

    在上篇文章<2021新年 Vue3.0 + Element UI 尝鲜小记>里,我们尝试使用了 Vue CLI 创建 Vue 3 + Element UI 的项目,而 Vue CLI 实际 ...

  5. SpringBoot2.+restful风格请求方式设置以及表单中日期格式设置

    ​ 1).SpringBoot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(@Bean.@Component)如果有就用用户配置的,如果没有,才自动配置:如果有些组件可以有多个(ViewR ...

  6. kubernets之headless

    一  认识headless服务 1服务以及服务的作用相信大家都已经耳熟能详了吗,服务接受请求,并且随机的将请求转发到相关联的任一pod来处理请求,但是考虑另外一种场景, 如果有客户端需要知道这个服务关 ...

  7. Kubernetes CoreDNS 状态是 CrashLoopBackOff 报错

    查看状态的时候,遇见coredns出现crashlookbackoff,首先我们来进行排错,不管是什么原因,查看coredns的详细信息,以及logs [root@k8s-master coredns ...

  8. windows下的:开始→运行→命令

    开始→运行→命令 集锦                          winver---------检查Windows版本wmimgmt.msc----打开windows管理体系结构(WMI)wu ...

  9. yml文件中${DB_HOST:localhost}的含义

    引自:https://blog.csdn.net/chen462488588/article/details/109057342 今天学习eladmin项目中看到application-dev.yml ...

  10. 使用Logback日志

    使用Logback日志 spring boot内部使用Logback作为日志实现的框架. Logback和log4j非常相似,如果你对log4j很熟悉,那对logback很快就会得心应手. logba ...