Codeforces gym 101102D 스택
2288 단어 단조 창고
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Given an R×C grid with each cell containing an integer, find the number of subrectangles in this grid that contain only one distinct integer; this means every cell in a subrectangle contains the same integer.
A subrectangle is defined by two cells: the top left cell (r1, c1), and the bottom-right cell (r2, c2) (1 ≤ r1 ≤ r2 ≤ R) (1 ≤ c1 ≤ c2 ≤ C), assuming that rows are numbered from top to bottom and columns are numbered from left to right.
Input
The first line of input contains a single integer T, the number of test cases.
The first line of each test case contains two integers R and C (1 ≤ R, C ≤ 1000), the number of rows and the number of columns of the grid, respectively.
Each of the next R lines contains C integers between 1 and 109, representing the values in the row.
Output
For each test case, print the answer on a single line.
Example
input
1
3 3
3 3 1
3 3 1
2 2 5
output
16
제목: 몇 개의 하위 행렬 요소가 유일하는지 구하기
문제풀이: 매거 행렬의 오른쪽 하단을 단조로운 창고로 왼쪽 상단의 가능한 점을 유지합니다
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
int a[1005][1005],up[1005][1005];
dequesp;
int main(){
int t;
scanf("%d",&t);
while(t--){
int n,m,i,j,k;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
scanf("%d",&a[i][j]);
up[i][j]=(a[i][j]==a[i-1][j])?up[i-1][j]:i;
}
}
ll ans=0,sum;
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
int p=i-up[i][j]+1;
if(a[i][j]!=a[i][j-1]){
while(!sp.empty())sp.pop_front();
sum=0;
sp.push_back(p);
sum+=p;
ans+=sum;
}
else{
int now=0;
while(!sp.empty()&&sp.back()>p){
now++;
sum-=sp.back();
sp.pop_back();
}
for(k=1;k<=now+1;k++){
sum+=p;
sp.push_back(p);
}
ans+=sum;
}
}
}
printf("%lld
",ans);
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
bzoj4576【Usaco2016 Open】262144Time Limit: 10 Sec Memory Limit: 128 MB Submit: 97 [ Submit][ Status][ Discuss] She is particularly intrigued by the cur...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.