当前位置:网站首页>[sub matrix quantity statistics] cf1181c flag sub matrix quantity statistics
[sub matrix quantity statistics] cf1181c flag sub matrix quantity statistics
2022-06-30 15:37:00 【Code chess】
Better look and feel :
http://wangyaqii.top/2022/06/29/cf1181c-flag-zi-ju-zhen-shu-liang-tong-ji/
CF1181C Flag Sub matrix quantity statistics
Topic introduction
Topic link :
https://codeforces.com/problemset/problem/1181/C
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-QiCm3rRy-1656492045169)(image-20220629160509729.png)]](/img/91/2a94749d64d153ef1caf81345594a4.png)
Ideas
Maintenance variables
Statistical submatrix generally needs to maintain some arrays , Most of them are vertical maintenance or horizontal maintenance .
This topic maintains two arrays :
d [ i ] [ j ] d[i][j] d[i][j] : ( i , j ) (i, j) (i,j) The position extends down to the maximum number of blocks of the same color
r [ i ] [ j ] r[i][j] r[i][j]: ( i , j ) (i, j) (i,j) The position extends to the right up to the number of blocks of the same color
Let's first consider the width 1 1 1 The situation of , When calculating the contribution of the column, the maximum contribution that the column can reach horizontally to the right .
At the same time, it is necessary to maintain two horizontally extended minimum value arrays :
r [ i ] [ j ] r[i][j] r[i][j] : Prefix ( Horizontal array ) minimum value , ( x , j ) (x, j) (x,j) To ( i , j ) , x < i (i, j),x < i (i,j),x<i The minimum value of the horizontal right extension length , Guarantee ( x , j ) (x, j) (x,j) To ( i , j ) (i, j) (i,j) It's the same color , The minimum value is updated only when the color is the same
r [ i ] [ j ] r[i][j] r[i][j] As stated , But this r [ i ] [ j ] r[i][j] r[i][j] Is updated by using the above array ( See code for details )
l m n [ i ] [ j ] lmn[i][j] lmn[i][j] : suffix ( Horizontal array ) minimum value , ( x , j ) (x, j) (x,j) To ( i , j ) , x > i (i, j), x > i (i,j),x>i The minimum value of the horizontal right extension length , Guarantee ( x , j ) (x, j) (x,j) To ( i , j ) (i, j) (i,j) It's the same color , The minimum value is updated only when the color is the same
Realization
First consider the width of 1 when , Whether the flag can be formed in the longitudinal direction
The following conditions ( The flag of satisfaction consists of three lines , As required )
i + 3 ∗ d [ i ] [ j ] − 1 < = n i + 3 * d[i][j] - 1 <= n i+3∗d[i][j]−1<=n The flag on the third line does not cross the line
d [ i ] [ j ] = = d [ i + d [ i ] [ j ] ] [ j ] d[i][j] == d[i + d[i][j]][j] d[i][j]==d[i+d[i][j]][j] The second and first lines are the same length
d [ i + 2 ∗ d [ i ] [ j ] ] [ j ] > = d [ i ] [ j ] d[i + 2 * d[i][j]][j] >= d[i][j] d[i+2∗d[i][j]][j]>=d[i][j] The length of the third line Greater than or equal to The length of the upper row ( Please think about why , As long as it is greater than or equal to, it can be statistically )
Vertical can form a flag , Then consider the contribution of changing the vertical flag .
Ask for contribution :
Because every vertical flag that meets the conditions will seek contribution , Therefore, for each vertical flag, only the right is required .
Here are some examples :
AAA
BBB
CCC
The leftmost column , Can form 3 In this case
Middle column , Can form 2 In this case
The rightmost column , Can form 1 In this case
It's all about , The total contribution is 3 + 2 + 1
In fact, that is n ( n + 1 ) / 2 n(n + 1) / 2 n(n+1)/2, Or recursion f [ i ] = f [ i − 1 ] + i f[i] = f[i - 1] + i f[i]=f[i−1]+i, The idea is basically the same
Then you need to contribute
We definitely need to find Minimum The current overall contribution can only be calculated by the length of the right extension of , The minimum right extension length needs to be 3 Minimum value of three parts .
Therefore, the above minimum value is required ( Prefixes and suffixes ) Array .
Why use suffix array ?
Here are some examples : The columns that meet the conditions are marked in bold
A ( It doesn't matter )
A
A
B
B
C
C
C( It doesn't matter )
But there is one more letter above and below , If the minimum value is counted , The above section uses the prefix minimum array , The length of the first row extending to the right will be counted , Cause statistical errors . The following sections are the same .
So the above part needs to use the suffix array , The following sections need to use prefix arrays , The middle part doesn't matter
Then the contribution that the front row can make is m i n ( l m n [ i ] [ j ] , r [ i + 2 ∗ d [ i ] [ j ] − 1 ] [ j ] , r [ i + 3 ∗ d [ i ] [ j ] − 1 ] [ j ] ) min({lmn[i][j], r[i + 2 * d[i][j] - 1][j], r[i + 3 * d[i][j] - 1][j]}) min(lmn[i][j],r[i+2∗d[i][j]−1][j],r[i+3∗d[i][j]−1][j])
See the code for the specific overall implementation .
Code
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int N = 1005, mod = 1e9 + 7;
int d[N][N], r[N][N], lmn[N][N];
char s[N][N];
void solve()
{
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++)
cin >> (s[i] + 1);
for(int i = n; i; i--)
{
for(int j = m; j; j--)
{
d[i][j] = d[i + 1][j], r[i][j] = r[i][j + 1];
if(s[i][j] == s[i + 1][j])
d[i][j] ++;
else d[i][j] = 1;
if(s[i][j] == s[i][j + 1])
r[i][j] ++;
else r[i][j] = 1;
}
}
for(int i = n; i; i--)
for(int j = 1; j <= m; j++)
{
lmn[i][j] = r[i][j];
if(i < n && s[i + 1][j] == s[i][j])
lmn[i][j] = min(lmn[i][j], lmn[i + 1][j]);
}
for(int i = 2; i <= n; i++)
for(int j = 1; j <= m; j++)
{
if(s[i][j] == s[i - 1][j])
r[i][j] = min(r[i][j], r[i - 1][j]);
}
ll ans = 0;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
{
if(i + 3 * d[i][j] - 1 <= n && d[i][j] == d[i + d[i][j]][j] && d[i + 2 * d[i][j]][j] >= d[i][j])
ans += min({
lmn[i][j], r[i + 2 * d[i][j] - 1][j], r[i + 3 * d[i][j] - 1][j]});
}
cout << ans << "\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
// cin >> t;
t = 1;
while(t--)
solve();
return 0;
}
边栏推荐
- NPM install --global --save --save dev differences
- Developer practice - the future of Agora home AI audio and video
- 各省GDP可视化案列,附带csv Metabase处理
- Forward declaration of classes
- iMeta | 叶茂/时玉等综述环境微生物组中胞内与胞外基因的动态穿梭与生态功能...
- Super comprehensive redis distributed high availability solution: sentry mechanism
- 1019 general palindromic number (20 points)
- Matlab finds prime numbers within 100
- Don't fight for big companies
- Jupyter notebook basic knowledge learning
猜你喜欢

消息队列十连问
![[matlab] 3D drawing summary](/img/57/05156340ccdd79b866c4df955b3713.jpg)
[matlab] 3D drawing summary

Advanced functions of ES6 operation array map (), filter (), reduce()

Talk about why I started technical writing

How should we understand the variability of architecture design?

Preliminary study on AI noise reduction evaluation system of sound network

Review 2021, embrace change and live up to Shaohua

FoxPro and I

Single cycle CPU of the design group of West University of Technology

Technology sharing | anyrtc service single port design
随机推荐
国债逆回购在哪个平台上买比较安全?
Quick sort (C language)
Super comprehensive redis distributed high availability solution: sentry mechanism
On which platform is it safer to buy Treasury reverse repo?
Basic literacy - four common software architectures
How to browse mobile web pages on your computer
H - Arctic network (minimum spanning tree)
Matlab judge palindrome number (only numbers)
Anyrtc implements application scenarios based on webrtc
Teach you a learning method to quickly master knowledge
Expressions in if statements
J - Borg maze (minimum spanning tree +bfs)
Forward declaration of classes
Rte2021 review of the practice and the way of AI OPS landing
Help you accumulate audio and video knowledge, Agora developer's roaming guide officially set sail
openresty 内置变量
C language \t usage
C language foundation - pointer array - initialization method & constant pointer array, pointer constant array
Developer practice - the future of Agora home AI audio and video
001 data type [basic]