比赛链接: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. M43 第一阶段考试

    一.解答题 1.统计当前主机的TCP协议网络各种连接状态出现的次数 netstat -an | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a ...

  2. Spring Boot 应用使用spring session+redis启用分布式session后,如何在配置文件里设置应用的cookiename、session超时时间、redis存储的namespace

    现状 项目在使用Spring Cloud搭建微服务框架,其中分布式session采用spring session+redis 模式 需求 希望可以在配置文件(application.yml)里设置应用 ...

  3. (十二)random模块

    大致有以下几个函数: print(random.random()) #0到1的浮点型 print(random.randint(1,6)) #1到6的整型 print(random.randrange ...

  4. 痞子衡嵌入式:MCUBootFlasher v3.0发布,为真实的产线操作场景而生

    -- 痞子衡维护的NXP-MCUBootFlasher工具(以前叫RT-Flash)距离上一个版本(v2.0.0)发布过去一年半以上了,这一次痞子衡为大家带来了全新版本v3.0.0,从这个版本开始,N ...

  5. 入门OJ:扫雪

    扫雪1 题目描述 大雪履盖了整个城市,市政府要求冬季服务部门尽快将一些街道(列在一份清单中)的积雪清除掉以恢复交通,整个城市由许多交叉路口和街道构成,当然任意两个交叉路口都是直接或间接连通的,清单给出 ...

  6. Mac中安装Git

    Mac 安装git 打开Mac终端输入git命令 如果出现以下代码说明已经安装 usage: git [--version] [--help] [-C <path>] [-c <na ...

  7. 【Android】关于连续多次点击控件的控制方案(新建监听类)

    参考:防止Android过快点击造成多次事件的三种方法_胖胖的博客-CSDN博客 实现逻辑很简单: 设置限定时间 在用户点击时开始计时 若计时未超过限定时间,则不允许触发点击事件 因还未学习过Rxja ...

  8. 安装OpenDaylight及Openflow插件

    1. 安装 Java 和 Maven CentOS7: yum install java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64 ma ...

  9. 在OpenDaylight controller上开发App

    安装环境:Ubuntu18.04 一.安装依赖 1. 安装JDK: sudo apt update sudo apt install openjdk-8-jdk-headless 选择默认的 JDK: ...

  10. join 查询优化

    在开发中往往会出现查询多表联查的情况,那么就会用到 join 查询. Join查询种类 为了方便说明,先定义一个统一的表,下面再做例子. CREATE TABLE `t2` ( `id` int(11 ...