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

推荐订阅源

博客园_首页
博客园 - 【当耐特】
IT之家
IT之家
M
MIT News - Artificial intelligence
酷 壳 – CoolShell
酷 壳 – CoolShell
Martin Fowler
Martin Fowler
V
Visual Studio Blog
F
Fortinet All Blogs
The Cloudflare Blog
Last Week in AI
Last Week in AI
博客园 - 司徒正美
G
Google Developers Blog
Vercel News
Vercel News
爱范儿
爱范儿
小众软件
小众软件
WordPress大学
WordPress大学
I
InfoQ
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
MongoDB | Blog
MongoDB | Blog
A
About on SuperTechFans
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Check Point Blog
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

Louis C Deng's Blog

RoPE: Properties, Patterns, and Long-Context Behavior CS336 Assignment 1: Large Language Model Training and Inference CS231n Lecture Note: Generative Models CS231n Lecture Note: Self-Supervised Learning CS231n Lecture Note: Large Scale Distributed Training 自動微分 | DIY 實現自己的 PyTorch From RNNs to Transformers CS231n Lecture Note VII: Recurrent Neural Networks Uncovering Batch & Layer Normalization CS231n Lecture Note VI: CNN Architectures and Training CS231n Lecture Note V: Convolution Neural Networks Basics Demystifying Softmax Loss: A Step-by-Step Derivation for Linear Classifiers Backpropagation: A Vector Calculus Perspective CS231n Lecture Note IV: Neural Networks and Backpropagation CS231n Lecture Note III: Optimization CS231n Lecture Note II: Linear Classifiers CS231n Lecture Note I: Image Classification CSAPP Cache Lab II: Optimizing Matrix Transposition CSAPP Cache Lab I: Let's simulate a cache memory! CS188 Search Lecture Notes III CS188 Search Lecture Notes II How to Use TouchID for Sudo Commands on macOS CS188 Search Lecture Notes I RECAP2025: 留白 CSAPP Bomb Lab 解析 x64 暫存器速查表 CSAPP Data Lab 解析 矩陣的 Modified Gram Schmidt 方法 聊一聊位掩碼(Bit Mask) 整數溢位與未定義行為
P3147 USACO16OPEN 262144 P 題解
Louis C Deng · 2022-11-24 · via Louis C Deng's Blog

DP 系列。

題面

看題,Luogu

合併相鄰的相同數字,變成數字加一。求獲得的最大值。

思考

最初想到的是基礎的區間 DP,不做解釋:

1
2
3
4
5
6
7
8
9
10
11
12
13
long long ans = 0;
for(int len = 2; len<=N; len++){
for(int i = 1; i+len-1<=N; i++){
int y = i+len-1;
for(int k = i; k<y; k++){
if(DP[i][k] == DP[k+1][y]){
DP[i][y] = max(DP[i][y], DP[i][k] + 1);
}
}
ans = max(ans, DP[i][y]);
}
}
cout << ans << endl;

但是 $ 2 \leq n \leq 262144 $ ,顯然會 MLE。

最佳化

借鑑思路,我們發現可以使用類似倍增的方法去做。

用狀態 f[i][j] 表示 合成之後結果為 i,右端點為 j 的區間的左端點位置,如果 值為 0 即 不可行。

因為題目要找兩個相鄰相等的區間,合成。有:

1
2
3
f[i][j] = f[i-1][f[i-1][j]];

把 f[i][j] 拆分成兩個能合成為 i-1 的區間

1
2
3
4
5
                 f[i-1][j]
|------<i-1>-----|----<i-1>-----|
j f[i-1][f[i-1][j]]

如果 f[i-1][j] 或 f[i-1][f[i-1][j]] 不成立,f[i][j] 就不成立,即轉移為 0

那如何表示結果?

記錄 ans,如果 f[i][j] 可行,就更新 ans。因為 i 遞增,所以不需要 max 操作。

得到

1
2
3
4
5
6
7
8
9
10
for(int i =2; i<=58; i++){
for(int j = 1; j<=N; j++){
if(!DP[i][j]){
DP[i][j] = DP[i-1][DP[i-1][j]];
}
if(DP[i][j]){
ans = i;
}
}
}

注意我們倍增合併,所以 log2262144+40=58log_2{262144} + 40 = 58 是可能獲得的最大值。

初始化

顯然不合並是可行的,所以在輸入的時候,初始化

1
2
3
4
5
for(int i = 1; i<=N; i++){
int in;
cin >> in;
DP[in][i] = i+1;
}

關於 i+1:為了避免區間重複,我們 f[i][j] 表示的區間是左閉右開區間,所以右端點是 i+1

小結

這道題是區間 DP 狀態最佳化,DP 學習之路漫漫,還需要多加練習。