仲冬小记(26.11)
2026.11
88周作业单
A. CF1141F2 Same Sum Blocks (Hard)
似乎可以枚举区间和,转化成线段覆盖,但是来点 dp。
设 \(f_i\) 表示和为 \(i\) 不相交的最大区间数,同时记录方案,都可以用 map 实现。
转移时,先做前缀和,再暴力枚举区间转移即可。
Code
\(cnt_i\) 表示的是题解中 \(f_i\) 的含义。
#include <bits/stdc++.h>
#define int long long
#define ll long long
#define ull unsigned long long
#define inf 1e18
#define eps 1e-9
#define ls (2 * k)
#define rs (2 * k + 1)
using namespace std;
const int mod = 1e9 + 7;
const int N = 1e6 + 5, M = 4e4 + 5;
const int dx[4] = {0, 0, 1, -1}, dy[4] = {-1, 1, 0, 0};
int n, a[N], S[N];
unordered_map <int, int> cnt;
unordered_map <int, vector< pair<int, int> > > lst;
inline void solve(){
cin >> n;
for(int i = 1;i <= n; i++)
cin >> a[i], S[i] = S[i - 1] + a[i];
int mx = 0, k = 0;
for(int i = 1;i <= n; i++){
for(int j = 0;j < i; j++){
if(lst[S[i] - S[j]].empty() || lst[S[i] - S[j]].back().second <= j){
lst[S[i] - S[j]].push_back({j + 1, i});
cnt[S[i] - S[j]] += 1;
if(cnt[S[i] - S[j]] > mx)
k = S[i] - S[j], mx = cnt[k];
}
}
}
cout << mx << "\n";
for(auto tmp : lst[k]){
int l = tmp.first, r = tmp.second;
cout << l << " " << r << "\n";
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int tc = 1;
while(tc--) solve();
return 0;
}
B. CF1473D Program
怎么是海亮模拟赛的题。
注意到每次操作 \(x\) 至多变化 1,所以 \(x\) 的变化范围一定是一个连续区间。
先做一遍前缀和,在分别处理出前后缀中前缀和的最大及最小值。
每次询问时,将两部分拼成一个区间,注意特判 \(l = 1\) 或 \(r = n\) 的情况。
Code
#include <bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define inf 1e18
#define eps 1e-9
#define ls (2 * k)
#define rs (2 * k + 1)
using namespace std;
const int mod = 1e9 + 7;
const int N = 1e6 + 5, M = 4e4 + 5;
const int dx[4] = {0, 0, 1, -1}, dy[4] = {-1, 1, 0, 0};
string s;
int n , q, sum[N];
int premin[N], premax[N], sufmin[N], sufmax[N];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int tc; cin >> tc;
while(tc--){
cin >> n >> q >> s, s = " " + s;
for(int i = 1;i <= n; i++)
sum[i] = sum[i - 1] + (s[i] == '+' ? 1 : -1);
premax[0] = 0, premin[0] = 0;
for(int i = 1;i <= n; i++){
premax[i] = max(premax[i - 1], sum[i]);
premin[i] = min(premin[i - 1], sum[i]);
}
sufmax[n] = sufmin[n] = sum[n];
for(int i = n - 1;i >= 1; i--){
sufmax[i] = max(sufmax[i + 1], sum[i]);
sufmin[i] = min(sufmin[i + 1], sum[i]);
}
while(q--){
int l, r; cin >> l >> r;
if(l == 1 && r == n){
cout << 1 << "\n";
continue;
}
int delta = sum[r] - sum[l - 1];
int ll = min(0ll, premin[l - 1]), lr = max(0ll, premax[l - 1]);
int rl = min(0ll, sufmin[r + 1] - delta), rr = max(0ll, sufmax[r + 1] - delta);
if(l == 1) cout << rr - rl + 1 << "\n";
else if(r == n) cout << lr - ll + 1 << "\n";
else cout << max(lr, rr) - min(ll, rl) + 1 << "\n";
}
}
return 0;
}
C. CF1136D Nastya Is Buying Lunch
D. CF1119E Pavel and Triangles
神秘贪心题。
分析一下限制,可以发现只能拼出等腰三角形或等边三角形。
结论大致就是能拼出等边三角形一定要优先拼等边三角形。
感性理解一下,一条边如果先和其他相等的边拼等边一定优于等待以后具有不确定性的边来拼等腰。
所以从小到大考虑,如果前面有剩下的边,先和前面的边配对,否则先拼等边,再考虑和后面的边拼等腰。
Code
#include <bits/stdc++.h>
#define int long long
#define ll long long
#define ull unsigned long long
#define inf 1e18
#define eps 1e-9
#define ls (2 * k)
#define rs (2 * k + 1)
using namespace std;
const int mod = 1e9 + 7;
const int N = 1e6 + 5, M = 4e4 + 5;
const int dx[4] = {0, 0, 1, -1}, dy[4] = {-1, 1, 0, 0};
int n, a[N];
inline void solve(){
cin >> n;
for(int i = 1;i <= n; i++) cin >> a[i];
int ans = 0, lst = 0;
for(int i = 1;i <= n; i++){
int v = min(a[i] - (a[i] & 1), lst);
a[i] -= v, lst -= v;
ans += (a[i] / 3), a[i] %= 3;
lst += a[i] * 2, ans += a[i];
}
cout << ans - lst / 2 << "\n";
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int tc = 1;
while(tc--) solve();
return 0;
}
E. CF1142B Lynyrd Skynyrd
看到倍增标签后会做了qwq
注意到,在循环移位的过程中,排列的元素相对位置不变,即 \(p_i\) 后面一定是 \(p_{i \mod n + 1}\)。
一共要跳 \(n - 1\) 步,可以先倍增求出从点 \(i\) 开始,向后跳 \(2 ^ k\) 后的位置。
再将 \(n - 1\) 二进制分解,求出从点 \(i\) 开始向后跳 \(n - 1\) 步后的位置。
对于每个询问,贪心地找到以 \([l, r]\) 中一点为起点,向后跳 \(n - 1\) 步的最小的位置,可以用 st 表维护。
时间复杂度为 \(O(m \log m)\)。
Code
#include <bits/stdc++.h>
#define int long long
#define ll long long
#define ull unsigned long long
#define inf 1e18
#define eps 1e-9
#define ls (2 * k)
#define rs (2 * k + 1)
using namespace std;
const int mod = 1e9 + 7;
const int N = 2e5 + 5, M = 4e4 + 5;
const int dx[4] = {0, 0, 1, -1}, dy[4] = {-1, 1, 0, 0};
int n, m, q, p[N], a[N], to[N];
int lst[N], pl[N][25], st[N][20];
inline int query(int l, int r){
int t = log2(r - l + 1);
return min(st[l][t], st[r - (1 << t) + 1][t]);
}
inline void solve(){
cin >> n >> m >> q;
for(int i = 1;i <= n; i++) cin >> p[i];
for(int i = 1;i <= m; i++) cin >> a[i];
for(int i = 1;i <= n; i++)
to[p[i]] = p[i % n + 1], lst[i] = m + 1;
for(int i = m;i >= 1; i--){
if(lst[to[a[i]]]) pl[i][0] = lst[to[a[i]]];
lst[a[i]] = i;
}
for(int i = 1;i <= 20; i++){
pl[m + 1][i - 1] = m + 1;
for(int x = m;x >= 1; x--)
pl[x][i] = pl[pl[x][i - 1]][i - 1];
}
for(int i = 1;i <= m; i++){
int pos = i;
for(int k = 20;k >= 0; k--)
if((n - 1) & (1 << k)) pos = pl[pos][k];
st[i][0] = pos;
}
for(int j = 1;j <= 20; j++)
for(int i = 1;i + (1 << j) - 1 <= m; i++)
st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
string ans = "";
for(int o = 1, l, r;o <= q; o++){
cin >> l >> r;
auto nx = query(l, r);
ans += (nx <= r ? '1' : '0');
}
cout << ans << "\n";
return ;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int tc = 1;
while(tc--) solve();
return 0;
}
F. CF1140E Palindrome-less Arrays
G. CF1136E Nastya Hasn't Written a Legend
H. CF1453E Dog Snacks
I. CF1137D Cooperative Game
J. P9984 [USACO23DEC] A Graph Problem P
89 周作业单
A. CF1151B Dima and a Bad XOR
B. CF1142A The Beatles
C. CF1119D Frets On Fire
D. CF1120A Diana and Liana
E. CF1154F Shovels Shop
F. CF1207F Remainder Problem
G. P17775 安和昴不是罪过
H. P6170 [USACO16FEB] Circular Barn G
I. P17846 回天
J. CF1129C Morse Code
90 周作业单
A. CF1132C Painting the Fence
B. CF1156B Ugly Pairs
C. CF1151C Problem for Nazar
D. CF1165F2 Microtransactions (hard version)
E. CF1151E Number of Components
F. CF1736D Equal Binary Subsequences
G. P17437 「LWLV Round 1」Young
H. P6275 [USACO20OPEN] Sprinklers 2: Return of the Alfalfa P
I. CF1394B Boboniu Walks on Graph
题目中的限制,说明每个点只会有一条出边。
要使所有点可以回到自己,新图一定有若干环组成,这等价于所有点的出度和入度均为 1。
\(k\) 的值域很小,一共有 \(k!\) 种方案,至多 362880,考虑爆搜出每一种方案进行统计。
怎么快速判断方案是否合法呢?
所有点的入度都是 1,等价于不存在入度大于等于 2 的点。
如果 \(c_i\) 和 \(c_j\) 会分别使点 \(u\) 和 \(v\) 同时指向点 \(x\),此时就是非法状态,那么 \(c_i\) 和 \(c_j\) 不能同时共存。
可以枚举点 \(x\),处理出所有互相冲突的 \(c_i\) 和 \(c_j\),爆搜方案时 \(O(k ^ 2)\) 判断是否合法。
时间复杂度为 \(O(k! \times k ^ 2 + \sum{deg_v ^ 2})\)。
注意每个点的入边可能很多,需要去重,记得特判同一种状态出现多次。
Code
#include <bits/stdc++.h>
#define int long long
#define ll long long
#define ull unsigned long long
#define inf 1e18
#define eps 1e-9
#define ls (2 * k)
#define rs (2 * k + 1)
using namespace std;
const int mod = 1e9 + 7;
const int N = 2e5 + 5, M = 4e4 + 5;
const int dx[4] = {0, 0, 1, -1}, dy[4] = {-1, 1, 0, 0};
bool in[10][10][10][10];
int n, m, k, d[N], ans, nx[10], cnt[10][10];
vector < pair<int, int> > E[N], clt[N];
void calc(int x){
if(x == k + 1){
for(int i = 1;i <= k; i++)
for(int j = i;j <= k; j++)
if(in[i][nx[i]][j][nx[j]]) return ;
return ans += 1, void();
}
for(int i = 1;i <= x; i++)
nx[x] = i, calc(x + 1);
return ;
}
inline void solve(){
cin >> n >> m >> k;
for(int i = 1, u, v, w;i <= m; i++){
cin >> u >> v >> w;
E[u].push_back({w, v}), d[u] += 1;
}
for(int i = 1;i <= n; i++)
sort(E[i].begin(), E[i].end());
for(int i = 1;i <= n; i++){
for(int j = 0;j < d[i]; j++){
int to = E[i][j].second;
clt[to].push_back({d[i], j + 1});
}
}
for(int i = 1;i <= n; i++){
for(auto tmp : clt[i])
cnt[tmp.first][tmp.second] += 1;
sort(clt[i].begin(), clt[i].end());
clt[i].erase(unique(clt[i].begin(), clt[i].end()), clt[i].end());
for(int x = 0;x < (int)clt[i].size(); x++){
if(cnt[clt[i][x].first][clt[i][x].second] > 1)
in[clt[i][x].first][clt[i][x].second][clt[i][x].first][clt[i][x].second] = true;
cnt[clt[i][x].first][clt[i][x].second] = 0;
for(int y = x + 1;y < (int)clt[i].size(); y++){
in[clt[i][x].first][clt[i][x].second][clt[i][y].first][clt[i][y].second] = true;
in[clt[i][y].first][clt[i][y].second][clt[i][x].first][clt[i][x].second] = true;
}
}
}
calc(1);
cout << ans << "\n";
return ;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int tc = 1;
while(tc--) solve();
return 0;
}

浙公网安备 33010602011771号