Educational Codeforces Round 78 (Rated for Div. 2) 题解
Shuffle Hashing
\]
处理出 \(s_1\) 中各个字符出现的次数,然后双指针维护 \(s_2\) 中每一段长度为 \(len(s_1)\) 的串中字符出现的次数,如果存在某一段和 \(s_1\) 的字符次数相同,则是答案。
view
#include <map>
#include <set>
#include <list>
#include <tuple>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define INOPEN freopen("in.txt", "r", stdin)
#define OUTOPEN freopen("out.txt", "w", stdout)
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e2 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
int n, m, k;
int cas, tol, T;
int cnt[26];
char s1[maxn], s2[maxn];
bool ok() {
for(int i=0; i<26; i++) if(cnt[i]) return 0;
return 1;
}
int main() {
scanf("%d", &T);
while(T--) {
mes(cnt, 0);
scanf("%s%s", s1+1, s2+1);
n = strlen(s1+1), m = strlen(s2+1);
if(n>m) {
puts("NO");
continue;
}
for(int i=1; i<=n; i++) cnt[s1[i]-'a']++;
for(int i=1; i<=n; i++) cnt[s2[i]-'a']--;
bool f = 0;
for(int i=n; i<=m; i++) {
if(ok()) f = 1;
if(i==m) break;
cnt[s2[i+1]-'a']--;
cnt[s2[i-n+1]-'a']++;
}
puts(f ? "YES" : "NO");
}
return 0;
}
A and B
\]
说出来你可能不信,强行 \(oeis\) 过了。
view
#include <map>
#include <set>
#include <list>
#include <tuple>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define INOPEN freopen("in.txt", "r", stdin)
#define OUTOPEN freopen("out.txt", "w", stdout)
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e5 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
ll n, m;
int cas, tol, T;
int main() {
scanf("%d", &T);
while(T--) {
ll a, b;
scanf("%lld%lld", &a, &b);
n = abs(a-b);
ll k=0;
for(; ; k++) {
if(k*(k+1)/2 <= n && n<(k+1)*(k+2)/2) break;
}
ll tk = k*(k+1)/2;
ll ans;
if(n == tk) ans = k;
else {
if(k%2 == 1) {
if((n-tk)%2==1) ans = k+2;
else ans = k+1;
} else {
if((n-tk)%2==1) ans = k+1;
else ans = k+3;
}
}
printf("%lld\n", ans);
}
return 0;
}
Berry Jam
\]
预处理后半段中 \(1\) 比 \(2\) 多吃 \(x\) 瓶所需要的最少步数,然后枚举前半段中吃到第 \(i\) 瓶处,\(1\) 还需要比 \(2\) 多吃 \(y\) 瓶,然后在后半段预处理中找答案。
view
#include <map>
#include <set>
#include <list>
#include <tuple>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define INOPEN freopen("in.txt", "r", stdin)
#define OUTOPEN freopen("out.txt", "w", stdout)
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 2e5 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
int n, m;
int cas, tol, T;
int a[maxn];
unordered_map<int, int> mp;
int main() {
scanf("%d", &T);
while(T--) {
mp.clear();
scanf("%d", &n);
int y = 0;
for(int i=1; i<=n+n; i++) {
scanf("%d", &a[i]);
y += a[i]==1 ? 1:-1;
}
if(y == 0) {
printf("0\n");
continue;
}
mp[0] = 0;
for(int i=n+1, x=0; i<=n+n; i++) {
x += a[i]==1 ? 1:-1;
if(!mp.count(x)) mp[x] = i-n;
}
// for(auto t : mp) printf("%d %d\n", t.fi, t.se);
int ans = inf;
for(int i=n; i>=0; i--) {
if(mp.count(y))
ans = min(ans, n-i+mp[y]);
if(!i) break;
y -= a[i]==1 ? 1:-1;
}
printf("%d\n", ans);
}
return 0;
}
Segment Tree
\]
把线段先按 \(l\) 在按 \(r\) 排序,然后枚举第 \(i\) 条线段,判断它可以和哪些线段连边。
可以发现,在枚举第 \(i\) 条线段时,前 \(i-1\) 条线段的 \(l\) 一定都是比我的 \(l\) 小的,所以我其实是需要找到前 \(i-1\) 条线段中,找到所有满足 \(p[i].l \leq p[j].r \leq p[i].r\) 的所有 \(j\)。
这一段区间是连续的,所以我们可以维护一个 \(set\) 的 \(pair\),用来存放前 \(i-1\) 条边的 \(r\) 位置和编号。然后用 \(set\) 的二分来快速找到所有的 \(j\)。
又因为想要形成一棵树,这也就意味着最多只会添加 \(n-1\) 条边,那么整体复杂度就不会太大。
view
#include <map>
#include <set>
#include <list>
#include <tuple>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define l first
#define r second
#define pb push_back
#define pii pair<int, int>
#define INOPEN freopen("in.txt", "r", stdin)
#define OUTOPEN freopen("out.txt", "w", stdout)
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 5e5 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
int n, m;
int cas, tol, T;
int fa[maxn];
pii p[maxn];
set<pii> st;
int find(int x) {
return fa[x]==x ? x : fa[x]=find(fa[x]);
}
bool bind(int x, int y) {
x = find(x), y = find(y);
if(x == y) return 0;
fa[x] = y;
return 1;
}
int main() {
scanf("%d", &n);
for(int i=1; i<=n; i++) {
scanf("%d%d", &p[i].l, &p[i].r);
fa[i] = i;
}
sort(p+1, p+1+n);
st.clear();
int sz = 0, f = 1;
for(int i=1; i<=n; i++) {
auto pos = st.lower_bound({p[i].l, -1});
for(auto j = pos; j!=st.end(); j++) {
if((*j).l > p[i].r) break;
sz++;
if(sz==n || !bind(i, (*j).r)) {
f = 0;
break;
}
}
if(!f) break;
st.insert({p[i].r, i});
}
set<int> ans;
for(int i=1; i<=n; i++) ans.insert(find(i));
puts(ans.size()==1&&f ? "YES" : "NO");
return 0;
}
Tests for problem D
\]
考虑模拟一下第一个样例,它的放置规则是先把 \(1\) 看成整棵树的根,那么可以先确定 \(p[1].r = 2*n\),然后它有两个直接儿子,所以我需要在 \(r\) 前面留两个空给这两个儿子放 \(r\) 用,现在已经没有直接儿子了,为了防止新的交叉出现,接下来我就放上自己的 \(l\),对于下面的儿子也是同理,可以递归处理。
然后就是儿子的 \(l\) 问题了,由于 \(1\) 的各个儿子不能有交叉部分,也就意味着这些得是重合起来的,所以一开始放在最后的 \(r\),其对应的 \(l\) 就应该尽量小,所以我越早放在后面的儿子,应该越晚去 \(dfs\) 确定其 \(l\)。
为了防止数字重复被用到,可以用一个 \(set\) 来维护还可以用的数字。
view
#include <map>
#include <set>
#include <list>
#include <tuple>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define l first
#define r second
#define pb push_back
#define pii pair<int, int>
#define INOPEN freopen("in.txt", "r", stdin)
#define OUTOPEN freopen("out.txt", "w", stdout)
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 5e5 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
int n, m;
int cas, tol, T;
set<int> st;
pii p[maxn];
vector<int> g[maxn];
void dfs(int u, int fa) {
int len = g[u].size();
for(int i=0; i<len; i++) if(g[u][i] != fa) {
p[g[u][i]].r = *(--st.end());
st.erase((--st.end()));
}
p[u].l = *(--st.end());
st.erase((--st.end()));
// printf("p%d .l = %d .r = %d\n", u, p[u].l, p[u].r);
for(int i=len-1; ~i; i--) if(g[u][i] != fa) {
dfs(g[u][i], u);
}
}
int main() {
scanf("%d", &n);
for(int i=2, u, v; i<=n; i++) {
scanf("%d%d", &u, &v);
g[u].pb(v), g[v].pb(u);
}
p[1].r = 2*n;
for(int i=1; i<2*n; i++) st.insert(i);
dfs(1, 1);
for(int i=1; i<=n; i++) printf("%d %d\n", p[i].l, p[i].r);
return 0;
}
/*
3
1 2
1 3
*/
Educational Codeforces Round 78 (Rated for Div. 2) 题解的更多相关文章
- Educational Codeforces Round 63 (Rated for Div. 2) 题解
Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...
- Educational Codeforces Round 65 (Rated for Div. 2)题解
Educational Codeforces Round 65 (Rated for Div. 2)题解 题目链接 A. Telephone Number 水题,代码如下: Code #include ...
- Educational Codeforces Round 64 (Rated for Div. 2)题解
Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...
- Educational Codeforces Round 60 (Rated for Div. 2) 题解
Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...
- Educational Codeforces Round 58 (Rated for Div. 2) 题解
Educational Codeforces Round 58 (Rated for Div. 2) 题目总链接:https://codeforces.com/contest/1101 A. Min ...
- Educational Codeforces Round 78 (Rated for Div. 2) D. Segment Tree
链接: https://codeforces.com/contest/1278/problem/D 题意: As the name of the task implies, you are asked ...
- Educational Codeforces Round 78 (Rated for Div. 2) C. Berry Jam
链接: https://codeforces.com/contest/1278/problem/C 题意: Karlsson has recently discovered a huge stock ...
- Educational Codeforces Round 78 (Rated for Div. 2) B. A and B
链接: https://codeforces.com/contest/1278/problem/B 题意: You are given two integers a and b. You can pe ...
- Educational Codeforces Round 78 (Rated for Div. 2) A. Shuffle Hashing
链接: https://codeforces.com/contest/1278/problem/A 题意: Polycarp has built his own web service. Being ...
随机推荐
- Acwing40. 顺时针打印矩阵
地址 https://www.acwing.com/solution/acwing/content/3623/ 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字. 样例 输入: [ [, ...
- DirectShow 获取音视频输入设备列表
开发环境:Win10 + VS2015 本文介绍一个 "获取音频视频输入设备列表" 的示例代码. 效果图 代码下载 代码下载(VC2015):Github - DShow_simp ...
- django--DateTimeField字段orm操作
django中的models.py是我们用来创建数据库里需要的表的,规定表中各个字段的类型,选择合适的字段类型尤为重要,常用的字段类型有:CharField.choice.IntegerField.T ...
- PHP中查询指定时间范围内的所有日期,月份,季度,年份
/** * 查询指定时间范围内的所有日期,月份,季度,年份 * * @param $startDate 指定开始时间,Y-m-d格式 * @param $endDate 指定结束时间,Y-m-d格式 ...
- React中useEffect使用
2019-08-24 07:00:00 文摘资讯 阅读数 1364 收藏 博文的原始地址 之前我们已经掌握了useState的使用,在 class 中,我们通过在构造函数中设置 this.s ...
- 【前端知识体系-NodeJS相关】浅谈NodeJS中间件
1. 中间件到底是个什么东西呢? [!NOTE] 中间件其是一个函数,在响应发送之前对请求进行一些操作 function middleware(req,res,next){ // 做该干的事 // 做 ...
- git 创建分支 提交到远程分支
git 创建分支 并 提交到远程分支 git branch 0.可以通过git branch -r 命令查看远端库的分支情况 1,从已有的分支创建新的分支(如从master分支),创建一个dev分支 ...
- python 遍历一个数组
list_test =[1,2,3,4,5] for i in range(0,len(list_test)): print(i) 结果是 1 2 3 4 5 注意 for i in range 是前 ...
- 【转载】修改Windows下键盘按键对应功能的一些方案
原文见:https://sites.google.com/site/xiangyangsite/home/technical-tips/windows-tips/multi_media_key_cus ...
- opencv::BackgroundSubtraction基本原理
背景消除 BS算法 - 图像分割(GMM – 高斯混合模型) - 机器学习(KNN –K个最近邻) BackgroundSubtractor (父类) - BackgroundSubtractorMO ...