惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
Cloudbric
Cloudbric
G
Google Developers Blog
博客园_首页
博客园 - 司徒正美
N
Netflix TechBlog - Medium
Recorded Future
Recorded Future
博客园 - 叶小钗
C
Check Point Blog
L
LangChain Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Stack Overflow Blog
Stack Overflow Blog
大猫的无限游戏
大猫的无限游戏
Cyberwarzone
Cyberwarzone
Project Zero
Project Zero
V
Vulnerabilities – Threatpost
C
Cisco Blogs
Scott Helme
Scott Helme
Last Week in AI
Last Week in AI
博客园 - 聂微东
T
Threat Research - Cisco Blogs
www.infosecurity-magazine.com
www.infosecurity-magazine.com
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Hacker News
The Hacker News
Forbes - Security
Forbes - Security
Simon Willison's Weblog
Simon Willison's Weblog
I
Intezer
Cisco Talos Blog
Cisco Talos Blog
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
阮一峰的网络日志
阮一峰的网络日志
爱范儿
爱范儿
AWS News Blog
AWS News Blog
C
CERT Recently Published Vulnerability Notes
Google DeepMind News
Google DeepMind News
N
News | PayPal Newsroom
Help Net Security
Help Net Security
B
Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
Tenable Blog
I
InfoQ
S
Securelist
V
Visual Studio Blog
U
Unit 42
博客园 - 【当耐特】
S
Security @ Cisco Blogs

Shiroha白羽的博客

Golang 踩坑 —— interface 为参数的时候传 nil 指针 Codeforces Round 925 (Div. 3) Codeforces Round 924 (Div. 2) Codeforces Round 923 (Div. 3) Codeforces Round 922 (Div. 2) Codeforces Round 921 (Div. 2) Educational Codeforces Round 161 (Rated for Div. 2) Codeforces Round 920 (Div. 3) Codeforces Round 919 (Div. 2) Hello 2024 Good Bye 2023 Codeforces Round 918 (Div. 4) 个人备份的常用 macOS 清理命令 Codeforces Round 917 (Div. 2) Pinely Round 3 (Div. 1 + Div. 2) Educational Codeforces Round 160 (Rated for Div. 2) Codeforces Round 915 (Div. 2) Codeforces Round 914 (Div. 2) Codeforces Round 913 (Div. 3) Educational Codeforces Round 159 (Rated for Div. 2) Codeforces Round 912 (Div. 2) Codeforces Round 911 (Div. 2) Educational Codeforces Round 158 (Rated for Div. 2) Codeforces Round 910 (Div. 2) Codeforces Round 909 (Div. 3) Codeforces Round 908 (Div. 2) Educational Codeforces Round 157 (Rated for Div. 2) C++自定义的字面量 Codeforces Round 907 (Div. 2) Codeforces Round 916 (Div. 3) 关于 LRU map 的一些灵感 2023 杭州站 ICPC 现场赛 反复横跳的 Clang-Tidy(cert-dcl21-cpp) Codeforces Round 906 (Div. 2) 一段奇怪的 CPP 代码 Codeforces Round 905 (Div. 3) Codeforces Round 904 (Div. 2) Codeforces Round 903 (Div. 3) Educational Codeforces Round 156 (Rated for Div. 2) Codeforces Round 902 (Div. 2, based on COMPFEST 15 - Final Round) Codeforces Round 901 (Div. 2) Codeforces Round 900 (Div. 3) Codeforces Round 899 (Div. 2) Educational Codeforces Round#155 (Div. 2) Codeforces Round 898 (Div. 4) CodeTON Round 6 (Div. 2) Codeforces Round 897 (Div. 2) Codeforces Round 896 (Div. 2) Codeforces Round 887 (Div. 2) Codeforces Round 895 (Div. 3) 左值-右值-将亡值 blog.mauve.icu Pinely Round 2 (Div. 1 + Div. 2) Harbour.Space Scholarship Contest 2023-2024 (Div. 1 + Div. 2) Codeforces Round 894 (Div. 3) Codeforces Round 888 (Div. 3) Educational Codeforces Round#153 (Div. 2) Codeforces Round 893 (Div. 2) OTPAUTH,两步验证中的通用协议 Codeforces Round 892 (Div. 2) Codeforces Round 891 (Div. 3) Codeforces Round 890 (Div. 2) Educational Codeforces Round#152 (Div. 2) blog.mauve.icu Java Script 的 null 和 undefined 随想 记一次 SQL LEFT JOIN 没有得到预期结果的错误 Codeforces Round#789(Div. 2) GCC/G++ 预编译头性能优化 使用 Junit5 和 Mockito 实现 SpringBoot 的单元测试最优美的解决方案 centOS 防火墙 docker-compse 的问题 C++ 语言实现动态变化的线程池 Codeforces Round#744 (Div. 3) 计算机图形学 Windows 通过网络访问 WSL2 原生 JavaScript 实现图片裁剪 面试复习(计算机图形学) 面试复习(算法) Codeforces Round#706(Div. 2)-Let's Go Hiking 面试复习(Java) 面试复习(Git) 面试复习(Linux) 面试复习(数据库) 面试复习(计算机网络) 面试复习(操作系统) 面试复习(C++) Codeforces Round#699 (Div. 2) 清理 WSL2 的磁盘占用 Codeforces Round#697 (Div. 3) Windows 下的 NTFS 驱动器索引 BUG 计算机网络复习 记一次 Navicat 连接 MySQL 一直报认证错误(Access denied) 计算机网络实验复习 WSL1 使用 Docker 一直无法启动 我的ACM脚印 2020牛客暑期多校训练营(第三场)D-Points Construction Problem——构造 2020牛客暑期多校训练营(第三场)E-Two Matchings——复杂思维与简单dp 2020牛客暑期多校训练营(第二场)I-Interval——最大流转对偶图求最短路 Educational Codeforces Round 80 D. Minimax Problem——二分+二进制处理 Codeforces Round 606 E. Two Fairs——图论 Codeforces Round 612 (Div. 2) C. Garland——DP
CodeTON Round 7 (Div. 1 + Div. 2, Rated, Prizes!)
Shiroha · 2024-01-28 · via Shiroha白羽的博客

A. Jagged Swaps

大致题意

有一个数组,允许选择一个值,其左右两边都是大于当前值的情况下,将当前值和后面的那个值交换一下位置。问是否可能把整个数组排序好

思路

可以从插入排序的方式去考虑,只需要第一个值是对的就行了

AC code

1
2
3
4
5
6
7
8
9
10
11
void solve() {
int _;
cin >> _;
for (int ts = 0; ts < _; ++ts) {
int n;
cin >> n;
vector<int> data(n);
for (auto& i: data) cin >> i;
cout << (data.front() == 1 ? "YES" : "NO") << endl;
}
}

B. AB Flipping

大致题意

有一个 AB 组成的数组,若存在 AB 这样的子字符串,则翻转 AB,且每个下标只能被翻转一次,问最多可以翻转多少次

思路

若存在一堆连续的 AAAABBB 这样的字符串,那么仅最后一个不能进行翻转,其他所有位置都能发生翻转,所以只需要找出这样的连续对数量即可

需要注意的是,如果是 AABBAABB 这种两组的,虽然对于每一个单独的组而言,最后一个不能翻转,但是整体上,除了最后的最后那个,其他也都可以翻转

AC code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void solve() {
int _;
cin >> _;
for (int ts = 0; ts < _; ++ts) {
int n;
cin >> n;
string str;
str.reserve(n);
cin >> str;
int ans = 0, cntB = 0, flag = 0;
for (auto iter = str.rbegin(); iter != str.rend(); ++iter) {
if (*iter == 'B') {
flag = 1;
cntB++;
} else {
ans += flag + cntB;
cntB = 0;
}
}
cout << max(ans - 1, 0) << endl;
}
}

C. Matching Arrays

大致题意

有 AB 两个数组,每次任意排序 B 数组,问是否存在一个排列,使得 $a_i > b_i$ 的 $i$ 的数量恰好是 $x$

思路

很简单的一个想法:两个数组排序后,然后将 B 数组的前 $x$ 个值放到后面去即可。然后再判断是否符合预期

AC code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void solve() {
int _;
cin >> _;
for (int ts = 0; ts < _; ++ts) {
int n, x;
cin >> n >> x;
vector<pair<int, int>> a(n);
vector<int> b(n);
for (auto& [fst, snd]: a) cin >> fst;
for (int i = 0; i < n; ++i) a[i].second = i;
for (auto& i: b) cin >> i;
sort(b.begin(), b.end());
sort(a.begin(), a.end());
int res = 0;
for (int i = 0; i < n; ++i) {
res += a[i].first > b[(i + x) % n];
}
if (res == x) {
cout << "YES" << endl;
for (int i = 0; i < n; ++i) a[a[i].second].first = b[(i + x) % n];
for (int i = 0; i < n; ++i) cout << a[i].first << " \n"[i == n - 1];
} else cout << "NO" << endl;
}
}

D. Ones and Twos

大致题意

有一个仅有 $1, 2$ 组成数组,每次有两种操作:将其中一个值改成 $1, 2$,问是否存在一个子串,满足求和等于 $x$(x 是每次询问给出的值)

思路

因为数组仅有 $1, 2$ 组成,那么必然,如果总值是 $s$,那么必然可以得到 $s - 2$ 的字串(删掉一侧的值或者删掉两侧的值),
同理也可以得到 $s-4, s-6, \dots$ 直到等于 $0 or 1$

所以只需要维护总和就能解决一般的值了。

而如果需要奇偶性和之和不同,那么必然需要减去一个 $1$,那么只需要找到左右两边最近的 $1$,
然后减去那一侧的 $2$ 和第一个 $1$,就可以得到最大可以满足的和全部之和奇偶性不同的值了,然后可以继续按照上面的推论

AC code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
void solve() {
int _;
cin >> _;
for (int ts = 0; ts < _; ++ts) {
int n, q;
cin >> n >> q;
vector<int> data(n);
for (auto& i: data) cin >> i;
int sum = 0;
set<int> s;
for (const auto& i: data) sum += i;
for (int i = 0; i < n; ++i) if (data[i] == 1) s.insert(i);
for (int qs = 0; qs < q; ++qs) {
int op;
cin >> op;
if (op == 1) {
int x;
cin >> x;
if (x > sum) cout << "NO" << endl;
else if ((x & 1) == (sum & 1)) cout << "YES" << endl;
else {
if (s.empty()) {
cout << "NO" << endl;
continue;
}
const int m = min(*s.begin(), n - *s.rbegin() - 1);
const int tmp = sum - m * 2 - 1;
cout << (x <= tmp ? "YES" : "NO") << endl;
}
} else {
int a, b;
cin >> a >> b;
if (data[a - 1] == 1) s.erase(a - 1);
if (b == 1) s.insert(a - 1);
sum -= data[a - 1] - b;
data[a - 1] = b;
}
}
}
}

E. Permutation Sorting

大致题意

有一个 $n$ 的排列的数组,每次按照如下操作进行

  • 选出其中 $a_i \neq i$ 的 $i$,得到一个由 $i$ 组成的数组 $s$
  • $a_{s_{i \space mod \space k+1}} \leftarrow a_{s_i}$

重复进行后,直到整个数组排序完成,问每一个下标完成排序需要操作几次

思路

从题意来看,其实就是每次将没有满足条件的值,右移一格,直到大家都满足了

由于在正常情况下,每次只移动一格,所以需要的成本就等于位置差值

但是因为有别的值会因为满足位置了,就不需要再路过这个节点了,也就可以省去一次移动成本,例如

index12345
start35412
turn 1235(4)1
turn 2(1)(2)(3)(4)(5)

注意到,本来初始为 $5$ 的值,本应该走 $3$ 次才能到目标地点的,但是因为 $4$ 提前到达目标地点,
所以 $5$ 只需要走两次即可,为了方便表示,我们把 $4$ 的移动描述成 $[3,4]$,同理,那么 $5$ 就是 $[2,5]$

故我们需要找到的是,每个值要进行横跨的时候,会同时跨越的其他区间数量,即有哪些节点,他们开始的位置比当前值晚的同时,结束位置还比当前值早

我们可以用线段树来维护这样的值,如果存在一个 $[l,r]$,那么必然可以对所有 $[r]$ 的区间产生减少一次移动成本的效果
那么我们可以将 $[r+1,n]$ 的区间都 $+1$,而如何表示 $<l$,则可以通过访问顺序来控制,我们保证从左往右访问即可,
每次访问后,将当前节点带来的区间进行删除

需要注意的是,因为移动是环形的,所以需要维护两倍区间长度,不然可能会出错

AC code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
struct SegTree {
vector<int> s, laz;

explicit SegTree(int n): s((n << 1) + 10), laz((n << 1) + 10) {}

int static get(const int l, const int r) {
return (l + r) | (l != r);
}

void up(const int l, const int r) {
const int mid = (l + r) >> 1;
s[get(l, r)] = s[get(l, mid)] + s[get(mid + 1, r)];
}

void build(const int l, const int r) {
laz[get(l, r)] = 0;
if (l == r) {
s[get(l, r)] = 0;
return;
}
const int mid = (l + r) >> 1;
build(l, mid);
build(mid + 1, r);
up(l, r);
}

void push(const int l, const int r) {
const int k = get(l, r);
if (laz[k]) {
const int mid = (l + r) >> 1;
s[get(l, mid)] += laz[k] * (mid - l + 1);
s[get(mid + 1, r)] += laz[k] * (r - mid);
laz[get(l, mid)] += laz[k];
laz[get(mid + 1, r)] += laz[k];
laz[k] = 0;
}
}

void update(const int l, const int r, const int x, const int y, const int w) {
if (l == x && y == r) {
s[get(l, r)] += w * (r - l + 1);
laz[get(l, r)] += w;
return;
}
push(l, r);
if (const int mid = (l + r) >> 1; y <= mid) {
update(l, mid, x, y, w);
} else if (x > mid) {
update(mid + 1, r, x, y, w);
} else {
update(l, mid, x, mid, w);
update(mid + 1, r, mid + 1, y, w);
}
up(l, r);
}

int query(const int l, const int r, const int x) {
if (l == r) {
return s[get(l, r)];
}
push(l, r);
const int mid = (l + r) >> 1;
if (x <= mid) {
return query(l, mid, x);
}

return query(mid + 1, r, x);
}
};

void solve() {
int _;
cin >> _;
for (int ts = 0; ts < _; ++ts) {
int n;
cin >> n;
vector<int> data(n), ans(n);
SegTree tree(2 * n);
for (auto& i: data) cin >> i;
for (int i = 0; i < n; ++i) {
const int target = data[i] > i ? data[i] : data[i] + n;
tree.update(1, 2 * n, target, 2 * n, 1);
if (target < n) tree.update(1, 2 * n, target + n, 2 * n, 1);
}
for (int i = 0; i < n; ++i) {
const int target = data[i] > i ? data[i] : data[i] + n;
tree.update(1, 2 * n, target, 2 * n, -1);
ans[data[i] - 1] = target - (i + 1) - tree.query(1, 2 * n, target);
}
for (int i = 0; i < n; ++i) cout << ans[i] << " \n"[i == n - 1];
}
}