题集链接

A. Tokitsukaze and All Zero Sequence

题意

Tokitsukaze 有一个长度为 n 的序列 a。 对于每个操作,她选择两个数字 ai 和 aj (i≠j; 1≤i,j≤n)。

如果 ai=aj,则将其中之一更改为 0,否则将它们都更改为 min(ai,aj)。

Tokitsukaze 想知道将序列中的所有数字变为 0 的最小操作次数。可以证明答案总是存在的。

思路

由题意可知

如果 a 中含有 0 只要把所以非零数和 0 进行比较就可以了,需要操作的次数为非 0 数的个数。

如果 a 中没有 0 但是有两个相同的数,操作数为 n

如果 a 中没有相同的数,操作数为 n+1

代码

#include <bits/stdc++.h>
using namespace std; void solve()
{
int n;
cin >> n;
map<int, int> ma;
for (int i = 1; i <= n; i++)
{
int a;
cin >> a;
ma[a]++;
}
if (ma[0] > 0)
{
cout << n - ma[0] << endl;
return;
}
for (auto x : ma)
{
if (x.second >= 2)
{
cout << n << endl;
return ;
}
}
cout << n + 1 << endl;
} int main()
{
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}

B1. Tokitsukaze and Good 01-String (easy version)

题意

这是问题的简单版本。两个版本之间的唯一区别是,较难的版本额外要求最小数量的子段。

Tokitsukaze 有一个二进制字符串s长度n,仅由0和1组成,n是偶数。

现在Tokitsukaze s成最小数量的连续子段,并且对于每个子段,每个子段中的所有数都是相同的。之后,s如果所有子段的长度都是偶数,则认为是好的。

例如,如果ss为“ 11001111 ”,将分为“ 11 ”、“ 00 ”和“ 1111 ”。它们的长度是2,2,4分别是偶数,所以“ 11001111 ”是好的。

另一个例子,如果s为“ 1110011000 ”,将分为“ 111 ”、“ 00 ”、“ 11 ”和“ 000 ”,它们的长度分别为3,2,2,3. 显然,“ 1110011000 ”不好。

Tokitsukaze 想使s改变一些位置的值. 具体来说,她可以执行任意次数的操作:改变 si 为“ 0 ”或“ 1 ”(1≤i≤n)。

你能告诉她最少的修改次数吗?

思路

因为字段长度都为偶数,因此我们可以每次判断2位,这2位必须为相同的数。

我们只要记录有几个不同的就是要修改的次数。

代码

#include <bits/stdc++.h>
using namespace std; void solve()
{
int n;
string s;
cin >> n >> s;
int cnt = 0;
for (int i = 0; i < n; i = i + 2)
{
if (s[i] != s[i + 1])
cnt++;
}
cout << cnt << endl;
} int main()
{
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}

B2. Tokitsukaze and Good 01-String (hard version)

题意

这是问题的困难版本。两个版本之间的唯一区别是,较难的版本额外要求最小数量的子段。

Tokitsukaze 有一个二进制字符串s长度n,仅由0和1组成,n是偶数。

现在Tokitsukaze s成最小数量的连续子段,并且对于每个子段,每个子段中的所有数都是相同的。之后,s如果所有子段的长度都是偶数,则认为是好的。

例如,如果ss为“ 11001111 ”,将分为“ 11 ”、“ 00 ”和“ 1111 ”。它们的长度是2,2,4分别是偶数,所以“ 11001111 ”是好的。

另一个例子,如果s为“ 1110011000 ”,将分为“ 111 ”、“ 00 ”、“ 11 ”和“ 000 ”,它们的长度分别为3,2,2,3. 显然,“ 1110011000 ”不好。

Tokitsukaze 想使s改变一些位置的值. 具体来说,她可以执行任意次数的操作:改变 si 为“ 0 ”或“ 1 ”(1≤i≤n)。

你能告诉她最少的修改次数吗?同时,她还想知道最小的子段数s可以在所有操作数最少的解决方案中进行划分。

思路

计算次数的方法同上。

我们可以在每次判断2个数不同时对其进行修改,每次都修改成和左边相同。

然后在遍历求字段数。

需要注意的是当开头的两个数不同时我们可以把它们都赋值为1 或 0 ,分别求出子段数再输出最小值。

代码

#include <bits/stdc++.h>
using namespace std; void solve()
{
int n;
string s;
cin >> n >> s;
int cnt = 0;
string s1 = s, s2 = s;
if (s[0] != s[1])
{
cnt = 1;
s1[0] = '0';
s1[1] = '0';
s2[0] = '1';
s2[1] = '1';
for (int i = 2; i < n - 1; i = i + 2)
{
if (s[i] != s[i + 1])
{
cnt++;
if (s1[i - 1] == '1')
{
s1[i] = '1';
s1[i + 1] = '1';
}
else
{
s1[i] = '0';
s1[i + 1] = '0';
}
if (s2[i - 1] == '1')
{
s2[i] = '1';
s2[i + 1] = '1';
}
else
{
s2[i] = '0';
s2[i + 1] = '0';
}
}
}
int a = 0, b = 0;
for (int i = 1; i < n; i++)
{
if (s1[i] != s1[i - 1])
a++;
if (s2[i] != s2[i - 1])
b++;
}
cout << cnt << " " << min(a, b) + 1 << endl;
return;
}
for (int i = 2; i < n - 1; i = i + 2)
{
if (s[i] != s[i + 1])
{
cnt++;
if (s[i - 1] == '1')
{
s[i] = '1';
s[i + 1] = '1';
}
else
{
s[i] = '0';
s[i + 1] = '0';
}
}
}
int a = 0;
for (int i = 1; i < n; i++)
{
if (s[i] != s[i - 1])
a++;
}
cout << cnt << " " << a + 1 << endl;
} int main()
{
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}

C. Tokitsukaze and Strange Inequality

题意

给定一个序列 s ,找出 [a,b,c,d](1<=a<b<c<d<=n),满足    的数量。

思路

固定 b 和 c。
每次只需找到 [1,b−1]中小于  的值的数量以及 [c+1,n] 中小于  的数量,相乘之后累加就是答案。
预处理一个前缀和数组 dp[i][j],表示前 i 个中小于等于 j 的数量有多少个。

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5010;
int T, n, s[N], dp[N][N];
void solve()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
dp[i][j] = 0;
for (int i = 1; i <= n; i++)
{
cin >> s[i];
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
dp[i][j] += dp[i - 1][j];
}
for (int j = s[i]; j <= n; j++)
dp[i][j]++;
}
ll ans = 0;
for (int i = 2; i <= n - 2; i++)
{
for (int j = i + 1; j <= n - 1; j++)
{
ll a = dp[i - 1][s[j]];
ll b = dp[n][s[i]] - dp[j][s[i]];
ans += a * b;
}
}
cout << ans << endl;
;
}
int main()
{
ios::sync_with_stdio(0);
cin >> T;
while (T--)
{
solve();
}
return 0;
}

Codeforces Round #789 (Div. 2)的更多相关文章

  1. Codeforces Round #789 (Div. 2) A-C

    Codeforces Round #789 (Div. 2) A-C A 题目 https://codeforces.com/problemset/problem/1677/A 题解 思路 知识点:模 ...

  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. Ubuntu 下 firebird 数据库的安装和配置

    Ubuntu 下 firebird 数据库的安装和配置 1.简介 本文主要是 Ubuntu 下 firebird 数据库的安装和目录迁移,同样适用于 Debian 系统:Ubuntu 20.0.4 f ...

  2. Apache Doris Oracle ODBC外表使用指南

    1.软件环境 操作系统:ubuntu 18.04 Apache Doris :0.15 Postgresql数据库:oracle 19c UnixODBC:2.3.4 Oracle ODBC :ins ...

  3. XCTF练习题---CRYPTO---混合编码解析

    XCTF练习题---CRYPTO---混合编码解析 flag:cyberpeace{welcometoattackanddefenceworld} 解题步骤: 1.观察题目,下载附件进行查看 2.看到 ...

  4. QMetaObject::connectSlotsByName: No matching signal for XXX 原理探究

    问题引出: 在尝试实现<Qt5.9 c++开发指南>混合UI编程章节时,用纯代码形式实现了个小按钮,然后加了个对应的槽函数,运行时就提示了这个信息. 原因探究: 首先查阅官方手册中的说明: ...

  5. 干货长文:Linux 文件系统与持久性内存介绍

    关注「开源Linux」,选择"设为星标" 回复「学习」,有我为您特别筛选的学习资料~ 1.Linux 虚拟文件系统介绍 在 Linux 系统中一切皆文件,除了通常所说的狭义的文件以 ...

  6. Java 效率工具, 大幅度提高开发效率

    你是否有遇到过这样的情况,在开发过程中需要比较两列数据,但使用文本比对工具的话他是按行基准比对的,我还得对每列数据先进行排序,但排序又去哪里排, 想到 excel 可以排序 , 折腾下来,特别麻烦, ...

  7. 基于STM32+华为云IOT设计智能称重系统

    摘要:选择部署多个重量传感器和必要的算法.通过WiFi 通信模块.GPS定位模块,采集车辆称重数据一地理位置信息,并通过网络发送至云平台,设计图形化UI界面展示称重.地图位置等重要信息,实现对称重系统 ...

  8. ElasticSearch7.3学习(二十六)----搜索(Search)参数总结、结果跳跃(bouncing results)问题解析

    1.preference 首先引入一个bouncing results问题,两个document排序,field值相同:不同的shard上,可能排序不同:每次请求轮询打到不同的replica shar ...

  9. linux篇-linux 下建立多个tomcat

    第一步:复制,解压 将准备好的tomcat压缩包复制到你准备安装的目录,我的tomcat压缩包名字是tomcat.tar.gz,我的安 装目录是 /usr/java/tomcat 第二步:解压tomc ...

  10. MyBatis 结果映射总结

    前言 结果映射指的是将数据表中的字段与实体类中的属性关联起来,这样 MyBatis 就可以根据查询到的数据来填充实体对象的属性,帮助我们完成赋值操作.其实 MyBatis 的官方文档对映射规则的讲解还 ...