写在前边

链接:Educational Codeforces Round 105 (Rated for Div. 2)

A. ABC String

链接:A题链接

题目大意:

给定一个有\(A、B、C\)大写字母组成的字符串,然后让我们用\('('\)与\(')'\)来替换三个字母,同类的字母只能用一种类型的括号,问组成的括号序列是否是合法的括号序列。

思路

发现一个性质,位于左边第一个的字母只能用'(',位于右边第一个的字母只能用')',因此左右两边不能是同一个字母,所以这就有两个字母所用的括号确定了,剩下那个只需要构造成数量较少的那个类型的括号就好了,然后字符串构造出来之后就用栈检查一下。

代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <map>
#include <cstring>
#include <stack> //#pragma GCC optimize(2)
//#pragma GCC optimize(3,"Ofast","inline") using namespace std; #define Inf 0x3f3f3f3f
#define PII pair<int, int>
#define P2LL pair<long long, long long>
#define endl '\n' typedef long long LL;
typedef unsigned long long ULL;
typedef vector<long long> VLL;
typedef vector<int> VI; const int Mod = 10000007; void solve() {
string s;
cin >> s;
int n = s.length();
if (s[0] == s[n - 1]) {
puts("NO");
return;
}
map<char, int> mp;
for (int i = 0; i < n; i++) mp[s[i]]++; string ss = "";
for (int i = 0; i < n; i++) {
if (s[i] == s[0]) ss += '(';
else if (s[i] == s[n - 1]) ss += ')';
else ss += (mp[s[0]] > mp[s[n - 1]] ? ')' : '(');
} stack<int> st;
for (int i = 0; i < ss.size(); i++) {
if (ss[i] == '(') st.push(ss[i]);
else if (ss[i] == ')') {
if (st.empty()) {
puts("NO");
return;
}
if (st.size()) st.pop();
}
} if (st.empty()) puts("YES");
else puts("NO");
} int main()
{
//ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int t;
scanf("%d", &t);
while (t--) {
solve();
} return 0;
}

B. Berland Crossword

链接:B题链接

题目大意:

给定\(u,r,d,l\)四个量,表示在一个\(n*n\)的网格中上边有\(u\)个涂黑,右边有\(r\)个涂黑,下边有\(d4个涂黑,左边有\)l$个涂黑,问是否能满足要求。

思路

由于只由在拐角处的方格能影响相邻的一行和一列,因此我们只需要二进制枚举四个拐角然后判断一行或一列中中间的网格(由\(n-2\)个)是否能装下需要的网格即可。

代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <map>
#include <cstring> //#pragma GCC optimize(2)
//#pragma GCC optimize(3,"Ofast","inline") using namespace std; #define Inf 0x3f3f3f3f
#define PII pair<int, int>
#define P2LL pair<long long, long long>
#define endl '\n' typedef long long LL;
typedef unsigned long long ULL;
typedef vector<long long> VLL;
typedef vector<int> VI; const int Mod = 10000007; LL gcd(LL a, LL b) {
return b ? gcd(b, a % b) : a;
} void change(int x, int &u, int &r, int &d, int &l) {
switch (x) {
case 0:
u--, l--;
break;
case 1:
u--, r--;
break;
case 2:
r--, d--;
break;
case 3:
l--, d--;
break;
}
} void solve() {
int n, u, r, d, l;
cin >> n >> u >> r >> d >> l; for (int mask = 0; mask < (1 << 4); mask++) {
int uu = u, rr = r, dd = d, ll = l, q;
for (int j = 0; j < 4; j++) {
if (mask >> j & 1) change(j, uu, rr, dd, ll); //搞四个角
}
if (uu < 0 || rr < 0 || dd < 0 || ll < 0) { //如果光角上放的就大于需要了
continue;
}
int re = n - 2;
if (re >= uu && re >= rr && re >= dd && re >= ll) {
puts("YES");
return;
}
}
puts("NO");
} int main()
{
//ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int t;
scanf("%d", &t);
while (t--) {
solve();
}
return 0;
}

C. 1D Sokoban

链接:B题链接

题目大意:

推箱子游戏,最初一个人站在\(0\)点,有\(n\)个箱子分布在左右两侧,只能往左边或往右边推箱子,并且不能跨过一个箱子去推当前箱子后边的箱子,如果正在推的当前箱子后边还有一个箱子,那么推的时候后边的箱子会跟着动,然后还有\(m\)个特殊位置,求经过一定的推箱子操作后使得有多少个箱子在特殊位置上,求这个最大值。

思路

一开始没有看到不能跨过箱子这个条件,然后贪心加二分搞了一个多小时\(WA\)了,然后又根据正确思路,码出来了,花了昨晚一晚上和今天两个小时。。好怀疑人生。

思路就是:首先明白,对于负数的位置和正数的位置我们需要分开操作,并且操作相同,然后我们可以看答案是怎么被贡献得到的,可以枚举每一个特殊位置,假设当前页数位置为\(k\):

对于\(>k\)的数轴位置上,我们还没有推到,所以说这一部分的就是原本就在特殊位置上的箱子

那么对于\(\leq k\)的数轴位置上,这一部分箱子是我们一直推到\(k\)的,也就是说在位置\(k\)以及之前有一串箱子,画个图就是这样:



那么对于\(k\)这个状态,答案就是后一部分已经在特殊位置上的箱子数和在它所处位置以及之前的所有箱子所覆盖的特殊点的数量之和。

那么对于后一部分,就可以直接用一个后缀和来维护。

对于前一部分,我们需要算出那个连续的子段覆盖了多少特殊点,那么这里怎么求呢,用两个二分即可。

第一个二分是求出在当前位置\(k\)以及之前有多少个箱子,即子段的长度,记为:\(num\)。

第二个二分,求出第一个大于等于最左边箱子的特殊位置的点。

更新答案。

代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <map>
#include <cstring> //#pragma GCC optimize(2)
//#pragma GCC optimize(3,"Ofast","inline") using namespace std; #define Inf 0x3f3f3f3f
#define PII pair<int, int>
#define P2LL pair<long long, long long>
#define endl '\n'
#define pub push_back
#define pob pop_back typedef long long LL;
typedef unsigned long long ULL;
typedef vector<long long> VLL;
typedef vector<int> VI; const int Mod = 10000007; int cal(VI& box, VI& pos) {
int n = box.size(), m = pos.size();
VI suff(m + 2);
map<int, int> tag;
for (int i = 1; i < n; i++) tag[box[i]] = 1; for (int i = m - 1; i >= 1; i--) {
suff[i] = suff[i + 1] + tag[pos[i]]; //处理寻找有多少个箱子在特殊位置上
}
int res = suff[1];
for (int i = 1; i < m; i++) {
int num = upper_bound(box.begin() + 1, box.end(), pos[i]) - box.begin() - 1;//计算出有多少个箱子在当前位置左边
//upper_bound第一个大于pos[i]的位置,那么减一就是第一个小于等于pos[i]的位置的箱子。
int left = lower_bound(pos.begin() + 1, pos.end(), pos[i] - num + 1) - pos.begin();
res = max(res, i - left + 1 + suff[i + 1]);
} return res;
} void solve() {
int n, m;
scanf("%d%d", &n, &m); VI positiveA, positiveB, negativeA, negativeB;
positiveA.pub(-1), negativeA.pub(-1);
positiveB.pub(-1), negativeB.pub(-1);
for (int i = 0; i < n; i++) {
int a;
scanf("%d", &a);
if (a > 0) positiveA.pub(a);
if (a < 0) negativeA.pub(-a);
}
for (int i = 0; i < m; i++) {
int b;
scanf("%d", &b);
if (b > 0) positiveB.pub(b);
if (b < 0) negativeB.pub(-b);
} reverse(negativeA.begin() + 1, negativeA.end());
reverse(negativeB.begin() + 1, negativeB.end()); printf("%d\n", cal(positiveA, positiveB) + cal(negativeA, negativeB));
} int main()
{
//ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int t;
scanf("%d", &t);
while (t--) {
solve();
} return 0;
}

Educational Codeforces Round 105 (Rated for Div. 2) A-C题解的更多相关文章

  1. Educational Codeforces Round 105 (Rated for Div. 2)

    A. ABC String 题目:就是用'('和')'来代替A,B,C并与之对应,问是不是存在这样的对应关系使得'('和')'正好匹配 思路:第一个和最后一个字母是确定的左括号或者是右括号,这样就还剩 ...

  2. Educational Codeforces Round 61 (Rated for Div. 2) D,F题解

    D. Stressful Training 题目链接:https://codeforces.com/contest/1132/problem/D 题意: 有n台电脑,每台电脑都有初始电量ai,也有一个 ...

  3. Educational Codeforces Round 81 (Rated for Div. 2) A-E简要题解

    链接:https://codeforces.com/contest/1295 A. Display The Number 贪心思路,尽可能放置更多位,如果n为奇数,消耗3去放置一个7,剩下的放1 AC ...

  4. Educational Codeforces Round 64 (Rated for Div. 2)题解

    Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...

  5. [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和)

    [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和) E. Permuta ...

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

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

  8. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  9. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

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

随机推荐

  1. 2023-07-13 C#深拷贝功能以及推荐使用方式

    C#深拷贝功能以及推荐使用方式 [作者]长生 深拷贝 深拷贝是用于在对引用对象进行复制时的一种操作方式.平常我们新建一个对象,然后直接赋值,只是对地址引用的赋值,在修改新建的对象时,也会对我们复制的对 ...

  2. 内网渗透大杀器fscan

    fscan简介 一款内网综合扫描工具,方便一键自动化.全方位漏扫扫描.支持主机存活探测.端口扫描.常见服务的爆破.ms17010.redis批量写公钥.计划任务反弹shell.读取win网卡信息.we ...

  3. 数据分析师如何用SQL解决业务问题?

    本文来自问答. 提问:数据分析人员需要掌握sql到什么程度? 请问做一名数据分析人员,在sql方面需要掌握到什么程度呢?会增删改查就可以了吗?还是说关于开发的内容也要会?不同阶段会有不同的要求吗? 正 ...

  4. 《UNIX 传奇:历史与回忆》读后感

    <UNIX 传奇:历史与回忆> 是 bwk(Brian W. Kernighan)2019 年的新作,回忆了 UNIX 在大半个世纪的风雨历程,是一本引人入胜的书籍.通过对 UNIX 操作 ...

  5. 2023CISCN华中赛区re

    2023CISCN华中赛区re 当时出的题 misc3-babyandroid 找so文件,加密过程也不复杂 每三个一组进行加密 这里就是先每个减去65 然后 大概是 y1=(31x1)%26+65 ...

  6. 7.1 C++ STL 非变易查找算法

    C++ STL 中的非变易算法(Non-modifying Algorithms)是指那些不会修改容器内容的算法,是C++提供的一组模板函数,该系列函数不会修改原序列中的数据,而是对数据进行处理.查找 ...

  7. Doris 再次启动FE失败的思考

    Doris再次启动FE失败的思考 背景描述 在昨天已经成功下载安装最新稳定版docker.拉取doris-0.15.0版本的镜像.将镜像挂载道本地Doris源码目录.完成了doris的编译之后,今天在 ...

  8. WPF学习 - 自定义窗体(二)

    上一篇文章写了如何创建自定义窗体:使用 WindowChrome 或者 WindowStyle="None"这两种方式.本文将讲述如何设置窗体的效果(以阴影效果为例),以及在效果模 ...

  9. signalr断开连接后重新连接

    signalr断开连接后重新连接 产品需求连接signalr 不稳定,连着连着就断了,场面十分尴尬,导致产品经理现场被批!!(内心无比高兴 ) 分析得出问题现象: 服务器因某些特殊原因,导致服务停止一 ...

  10. Burp Suite抓包工具配置代理手机抓取数据包

    工作中很多手机上的问题因为环境差异导致无法在pc设备上完整的模拟真实物理手机,因此需要方法能抓取到手机设备上所有数据包发送详情.发现了这个好用的数据包抓取工具Burp Suite. 一.配置流程: 1 ...