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

A - Most Unstable Array

题意

构造大小为 $n$,和为 $m$ 的非负数组 $a$,使得相邻元素之差的绝对值之和最大。

题解

稍加推导发现:将 $m$ 拆分和单独用 $m$ 结果是一样的,所以可以直接用 $0$ 和 $m$ 构造。

代码

#include <bits/stdc++.h>
using namespace std; void solve() {
int n, m; cin >> n >> m;
if (n == 1) cout << 0 << "\n";
else if (n == 2) cout << m << "\n";
else cout << 2 * m << "\n";
} int main() {
int t; cin >> t;
while (t--) solve();
}

B - Two Arrays And Swaps

题意

有大小为 $n$ 的数组 $a$ 和 $b$,最多可以交换 $a$ 中某一元素和 $b$ 中某一元素 $k$ 次,问数组 $a$ 中元素的最大和为多少。

题解

取 $a$ 中 $n$ 个和 $b$ 中前 $k$ 大个元素中前 $n$ 大个元素即可。

代码

#include <bits/stdc++.h>
using namespace std; void solve() {
int n, k; cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> b(n);
for (int i = 0; i < n; i++) {
cin >> b[i];
}
sort(b.rbegin(), b.rend());
for (int i = 0; i < k; i++) {
a.push_back(b[i]);
}
sort(a.rbegin(), a.rend());
cout << accumulate(a.begin(), a.begin() + n, 0) << "\n";
} int main() {
int t; cin >> t;
while (t--) solve();
}

C - Board Moves

题意

有一边长为奇数 $n$ 的正方形网格,每一次一个方块可以移到相邻八个位置中的某一个,问所有方块移到一个位置最少需要移动几次。

题解

从中心考虑:中心外第 $i$ 圈的每个方块移到中心需要 $i$ 步。

代码

#include <bits/stdc++.h>
using ll = long long;
using namespace std; void solve() {
int n; cin >> n;
ll ans = 0, a = 3, b = 1;
for (ll i = 1; i <= (n - 1) / 2; i++) {
ans += (a * a - b * b) * i;
a += 2, b += 2;
}
cout << ans << "\n";
} int main() {
int t; cin >> t;
while (t--) solve();
}

D - Constructing the Array

题意

有一大小为 $n$ 初始时每个元素均为 $0$ 的数组,第 $i$ 次操作如下:

  • 选取最长最靠左的一段连续为 $0$ 的子区间
  • 如果区间长度为奇数,$a_{\frac{l+r}{2}} = i$
  • 如果区间长度为偶数,$a_{\frac{l+r-1}{2}} = i$

输出进行 $n$ 次操作后的数组。

题解

递归分解区间,按照题意排序赋值即可。

代码

#include <bits/stdc++.h>
using namespace std; vector<pair<int, int>> v; int cal(int l, int r) {
return ((r - l + 1) % 2 == 1) ? (l + r) / 2 : (l + r - 1) / 2;
} void recur(int l, int r) {
if (l > r) return;
v.push_back({l, r});
recur(l, cal(l ,r) - 1);
recur(cal(l ,r) + 1, r);
} void solve() {
v.clear();
int n; cin >> n;
recur(1, n);
sort(v.begin(), v.end(), [&] (pair<int, int> a, pair<int, int> b) {
if (a.second - a.first != b.second - b.first)
return a.second - a.first > b.second - b.first;
else
return a.first < b.first;
});
int a[n + 1] = {};
for (int i = 0; i < n; i++)
a[cal(v[i].first ,v[i].second)] = i + 1;
for (int i = 1; i <= n; i++)
cout << a[i] << " \n"[i == n];
} int main() {
int t; cin >> t;
while (t--) solve();
}

E - K-periodic Garland

题意

将一个 $01$ 串变为相邻两个 $1$ 之间周期为 $k$ 的字符串最少需要改变多少字符。

题解

把原字符串分为 $k$ 个周期子串,每次考虑一个子串时需要将其他子串中的 $1$ 都变为 $0$,然后对当前周期子串进行 $dp$,$dp_i$ 表示周期子串中位置 $i$ 的字符变为 $1$,之前的字符变为合法至少需要改变多少字符。

代码

#include <bits/stdc++.h>
using namespace std; int cal(const string &s) {
int n = s.size();
int all = count(s.begin(), s.end(), '1');
int dp[n] = {};
int pref = 0;
int ans = all;
for (int i = 0; i < n; i++) {
int cur = (s[i] == '1');
pref += cur;
dp[i] = 1 - cur;
if (i > 0) dp[i] += min(dp[i - 1], pref - cur);
ans = min(ans, dp[i] + all - pref);
}
return ans;
} void solve() {
int n, k; cin >> n >> k;
string s; cin >> s;
int tot_one = count(s.begin(), s.end(), '1');
vector<string> v(k);
for (int i = 0; i < k; i++)
for (int j = i; j < n; j += k)
v[i] += s[j];
int ans = 1e9;
for (auto &it : v)
ans = min(ans, cal(it) + tot_one - count(it.begin(), it.end(), '1'));
cout << ans << "\n";
} int main() {
int t; cin >> t;
while (t--) solve();
}

F - Decreasing Heights

题意

有一个 $n * m$ 、高度不等的三维坐标系,从 $a_{00}$ 走到 $a_{{(n-1)}{(m-1)}}$,每次只能从 $a_{ij}$ 向 $a_{(i+1)j}$ 或 $a_{i(j+1)}$ 行走,且 $a_{xy} - a_{ij} = 1$,每个位置的高度可以不限次地 $-1$,问最少需要减少多少高度。

题解

如果 $a_{00}$ 是确定的,那么每个点 $a_{ij}$ 的高度应为 $a_{00} + i + j$,因为每移动一格高度都会 $+1$,这与路径无关。

那么,根据状态转移方程:

$dp_{ij} = min(dp_{{(i - 1)}{j}} + dp_{{i}{(j - 1)}}) + a_{ij} - (a_{00} + i + j)$

推导即可。

但是 $a_{00}$ 需要变化的高度是不确定,注意到如果有一条从 $a_{00}$ 到 $a_{{(n-1)}{(m-1)}}$ 的最优路径,那么这条路径上一定有点的高度不需要减少,因为如果都需要减少,那么可以增加 $a_{00}$ 的高度来抵消共同减少的部分,如果增加后的高度高于 $a_{00}$,呢么 $a_{00}$ 不需要减少,这条路径上的的高度都需要减少,即仍有点的高度不需要减少。

至此,可以枚举高度不变化的点反推出 $a_{00}$ 的高度,由此得解。

代码

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
const ll INF = 1e18; void solve() {
int n, m; cin >> n >> m;
ll a[n][m] = {};
for (int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
cin >> a[i][j];
ll ans = INF;
for (int x = 0; x < n; x++) {
for(int y = 0; y < m; y++) {
ll a_00 = a[x][y] - x - y;
if (a_00 > a[0][0]) continue;
ll dp[n][m] = {};
fill(*dp, *dp + n * m, INF);
dp[0][0] = a[0][0] - a_00;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ll a_ij = a_00 + i + j;
if (a_ij > a[i][j]) continue;
if (i > 0) dp[i][j] = min(dp[i][j], dp[i - 1][j] + a[i][j] - a_ij);
if (j > 0) dp[i][j] = min(dp[i][j], dp[i][j - 1] + a[i][j] - a_ij);
}
}
ans = min(ans, dp[n - 1][m - 1]);
}
}
cout << ans << "\n";
} int main() {
int t; cin >> t;
while (t--) solve();
}

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

  1. Codeforces Round #642 (Div. 3) D. Constructing the Array (优先队列)

    题意:有一个长度为\(n\)元素均为\(0\)的序列,进行\(n\)次操作构造出一个新序列\(a\):每次选择最长的连续为\(0\)的区间\([l,r]\),使得第\(i\)次操作时,\(a[\fra ...

  2. 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 ...

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

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

  4. Codeforces Round #368 (Div. 2)

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

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

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

  6. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  7. Codeforces Round #262 (Div. 2) 1003

    Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...

  8. Codeforces Round #262 (Div. 2) 1004

    Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...

  9. Codeforces Round #371 (Div. 1)

    A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...

随机推荐

  1. epoll的陷阱

    Starvation 特别提出在ET模式下,因为需要一次性把数据读完,如果一次性通知的数据过大,有可能处理时间过长,导致同一线程其他的事件长时间等待.这个不仅仅是ET模式下,也不仅仅是epoll模型下 ...

  2. Java JVM——9.方法区

    前言 方法区是运行时数据区的最后一个部分: 从线程共享与否的角度来看: 大家可能在这里有些疑惑,方法区和元空间的关系到底是怎样的?请往下看,下面会为大家解惑. 栈.堆.方法区的交互关系 下面就涉及了对 ...

  3. ps的参数解释

    [root@bogon ~]# ps axuUSER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND user启动进程的用户 pid  表示进程标志 ...

  4. 【Oracle】instr()函数详解

    1)instr()函数的格式  (俗称:字符查找函数) 格式一:instr( string1, string2 )    /   instr(源字符串, 目标字符串) 格式二:instr( strin ...

  5. SDUST数据结构 - chap8 查找

    选择题: 函数题: 6-1 二分查找: 裁判测试程序样例: #include <stdio.h> #include <stdlib.h> #define MAXSIZE 10 ...

  6. 误删数据库怎么办?mysql 回滚,撤销操作,恢复数据

    刚刚不小心把数据库删掉了,于是想着上网上找找有没有可以恢复数据库的方法,没想到还真有,除了备份以外,还有以下方法. 在mysql有时执行了错误的update或者delete时导致大量数据错误恢复的办法 ...

  7. oracle编译表上失效USERDBY脚本

    对表进行DLL操作之后,依赖这个表的一些存储过程,触发器等会失效,可以用下边的脚本进行重编译 /* Formatted on 2020/7/8 上午 09:31:31 (QP5 v5.163.1008 ...

  8. PYTHON爬虫实战_垃圾佬闲鱼爬虫转转爬虫数据整合自用二手急速响应捡垃圾平台_3(附源码持续更新)

    说明 文章首发于HURUWO的博客小站,本平台做同步备份发布. 如有浏览或访问异常图片加载失败或者相关疑问可前往原博客下评论浏览. 原文链接 PYTHON爬虫实战_垃圾佬闲鱼爬虫转转爬虫数据整合自用二 ...

  9. 2.4V升5V芯片,8uA功耗,低功耗升压电路图

    2.4V升5V,可用于USB拔插充电,也可以用于把两节镍氢电池2.4V升压到5V,的固定输出稳压电压值,同时输出电流可达1A,0.5A等 首先是先说下0.5A的这款的话,是比较低功耗的,8uA左右的输 ...

  10. Docker 拉取镜像速度太慢

    Docker Hub 是我们分发和获取 Docker 镜像的中心,但由于服务器位于海外,经常会出现拉取/上传镜像时速度太慢或无法访问的情况.再加上运营方不断对 Docker Hub 的免费使用进行限制 ...