Codeforces Round #792 (Div. 1 + Div. 2) // C ~ E
比赛链接:Dashboard - Codeforces Round #792 (Div. 1 + Div. 2) - Codeforces
C. Column Swapping
题意:
给定一个n*m大小的二维数组,要求只进行一次两列交换,使得得到的新数组的每行都是不减的。特别的,允许交换的两列是相同列。
思路:构造
可以考虑复制一份数组,并将其排序,那么两个数组中出现数字大小不同的列就是需要进行交换的列。
设需要交换的列的数量为num:
◇ 当num=0,无需交换,可以直接选择交换1 1。
◇ 当num=2时,尝试交换两列,并判断交换后,每行的这两列位置的数字是否已与排序后的该位置的数字相等。
◇ 当num>2时,那么需要交换的次数是大于1的,故无法满足要求。
参考代码:
#include <bits/stdc++.h>
#define LL long long
using namespace std; const int N = 200010; int n, m;
vector<int> v[N], nv[N]; bool check(int c1, int c2)
{
for(int i = 0; i < n; i++)
if(v[i][c1] != nv[i][c2] || v[i][c2] != nv[i][c1]) return false;
return true;
} void solve()
{
cin >> n >> m;
for(int i = 0; i < n; i++) v[i].clear(); for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
int x;
scanf("%d", &x);
v[i].push_back(x);
}
nv[i] = v[i];
sort(nv[i].begin(), nv[i].end());
} int pos1 = -1, pos2 = -1;
bool ok = true;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(v[i][j] != nv[i][j]) {
if(pos1 == -1 || pos1 == j) pos1 = j;
else if(pos2 == -1 || pos2 == j) pos2 = j;
else ok = false;
}
}
} if(!ok) puts("-1");
else {
if(pos1 == -1 && pos2 == -1) puts("1 1");
else if(check(pos1, pos2)) printf("%d %d\n", pos1 + 1, pos2 + 1);
else puts("-1");
}
} int main()
{
int test;
cin >> test;
while(test--) solve(); return 0;
}
D. Traps
题意:
给定一个大小为n的数组a[]为基础花费,以及最大跳过次数k:每次跳过可直接越过一格,但同时,后续走的每一格都增加1的附加花费。当从 i-1 走到 i 且已进行 cnt 次跳过操作时,此次花费为ai + cnt。求从0走到n所需的最小花费。
思路:贪心 构造
取 ai + i 最大的 min(n, k) 个格子跳过,然后模拟一下求总花费即可。
证明:
☆ 跳过次数应为min(n,k):
假设当前跳过次数为K,且仍有可跳过的格子且还有跳过次数,那么考虑跳过最后一个未跳过的格子,最终的花费必定只减不增。因此,跳过次数越多,结果越优。
☆ 取 ai + i 较大的格子跳过:
假设有ax + x > ay + y,若只跳过x和y其中某一格,有两种情况,其中二者出现差异的部分仅在区间 [x,y]内,若假设此前跳过了cnt步,则有:
① 跳过x:cost1 = sum[x + 1,y] + (cnt + 1) * (y - x).
② 跳过y:cost2 = sum[x,y - 1] + cnt * (y - x).
进而,cost1 - cost2 = ay - ax + y - x = ay + y - (ax + x) < 0 ,即跳过x的花费比跳过y更小。
因此,应考虑优先跳过 ai + i 较大的格子。
参考代码:
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int,int>
using namespace std; const int N = 200010; int n, k, a[N], id[N];
PII p[N]; void solve()
{
cin >> n >> k;
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
p[i] = { a[i] + i, i };
}
sort(p + 1, p + 1 + n, greater<PII>());
for(int i = 1; i <= n && i <= k; i++) id[i] = p[i].second;
sort(id + 1, id + 1 + min(n, k)); int cnt = 0;
LL res = 0;
for(int i = 1; i <= n; i++) {
if(cnt < k && i == id[cnt + 1]) ++ cnt;
else res += a[i] + cnt;
}
cout << res << endl;
} int main()
{
int test;
cin >> test;
while(test--) solve(); return 0;
}
E. MEX vs DIFF
题意:
给一个大小为n的非负数组a,以及k次操作,每次操作可以将a中任意一个数变成任意数。
有mex为不包含与a中的最小的非负整数,dif为a中不同数的数量,求dif - mex的最小值。
思路:贪心 构造
可以发现dif必然不小于mex,dif - mex的值实际上等于:大于mex的不同数的个数。所以最终答案应该令mex尽量大,同时,让大于mex的不同数的个数尽量小。
令mex尽量大:先求mex的最大值。
令大于mex的不同数的个数尽量小:muitiset维护一下,让大于mex的数中,数量较少的数优先进行改变操作。
参考代码:
#include <bits/stdc++.h>
using namespace std; const int N = 100010; int n, k, a[N];
map<int,int> num, dnum; void solve()
{
num.clear();
cin >> n >> k;
for(int i = 1; i <= n; i++) scanf("%d", &a[i]), ++ num[a[i]];
dnum = num; // 求mex最大值
sort(a + 1, a + 1 + n);
int mex = 0;
while(num[mex]) ++ mex;
for(int i = n, j = k; i && j; i--, j--) {
if(a[i] < mex) break;
num[mex] = 1, -- num[a[i]];
while(num[mex]) ++ mex;
} // 求mex最大时,dif最小值
multiset<int> S;
for(auto i : dnum) {
if(i.first > mex) S.insert(i.second);
}
int dif = mex;
for(auto i : S) {
if(k >= i) k -= i;
else ++ dif;
} cout << dif - mex << endl;
} int main()
{
int test;
cin >> test;
while(test--) solve(); return 0;
}
Codeforces Round #792 (Div. 1 + Div. 2) // C ~ E的更多相关文章
- Codeforces Round #792 (Div. 1 + Div. 2) A-E
Codeforces Round #792 (Div. 1 + Div. 2) A-E A 题目 https://codeforces.com/contest/1684/problem/A 题解 思路 ...
- Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship
Problem Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...
- Educational Codeforces Round 63 (Rated for Div. 2) 题解
Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...
- Educational Codeforces Round 39 (Rated for Div. 2) G
Educational Codeforces Round 39 (Rated for Div. 2) G 题意: 给一个序列\(a_i(1 <= a_i <= 10^{9}),2 < ...
随机推荐
- Linux 设置开机自启动脚本(ES、MySQL、Nacos、Nginx)
/etc/rc.d/init.d 中文件会在 Linux 系统各项服务都启动完毕之后再被运行 cd /etc/rc.d/init.d:新建xxx.sh文件. chmod +x xxx.sh,赋予可执行 ...
- SpringMVC和spring集成
步骤:1.web.xml中配置spring的监听和spring配置文件位置 2.编写spring类并在spring的配置文件里配置bean 说明:源码中spring核心配置文件导入springAnno ...
- Java并发3
轻量级锁:如果一个有多个线程访问,但多线程访问的时间是错开的,没有竞争,那么可以使用轻量级锁来优化: monitor:重量级锁: 正常我们使用synchronized时,没有竞争则是轻量级锁,当遇到竞 ...
- Centos7 中安装Elasticsearch
1.下载安装包 1.1 下载elasticsearch 7.13.3 curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/e ...
- SQL server设置连接数
SQLServer查看及设置最大连接数 很多时候自己本地开发会遇到 ,打开几个连接正常访问 之后就报错误,这时候需要调整sqlserver 最大连接数. 1. 查询最大连接数 SELECT val ...
- BUUCTF-小明的保险箱
小明的保险箱 16进制打开可以发现存在一个RAR压缩包,压缩包里面应该就是flag文本 使用ARCHPR破解即可
- 基于.NetCore开发博客项目 StarBlog - (13) 加入友情链接功能
系列文章 基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客? 基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目 基于.NetC ...
- python小题目练习(一)
题目:输出1+2+3+4+5+--+100的总数,并打印出这行式子 代码展示:# 1.定义一个初识变量total,用于后面每次循环进行累加值 total = 0# 2.利用for循环遍历累加for i ...
- Python自动化办公:批量将文件按分类保存,文件再多,只需一秒钟解决
序言 (https://jq.qq.com/?_wv=1027&k=GmeRhIX0) 当我们电脑里面的文本或者或者文件夹太多了,有时候想找到自己想要的文件,只能通过去搜索文件名,要是名字忘记 ...
- mariadb安装配置(主从配置)
主服务器192.168.206.183 从服务器192.168.206.193 1.创建并编辑 /etc/yum.repos.d/MariaDB.repo文件(主从都要做) [mariadb] nam ...