fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int R, C, N, r, c;
  5. int num[300][300];
  6. int all[301]={};//全部的數字有幾種
  7. int sub[301]={};
  8. int nowN;//現在的數字有幾種
  9.  
  10. void recount(int lx,int ly){
  11. for(int n=0; n<=N; n+=1)
  12. sub[n]=0;
  13. for(int dx=0; dx<r; dx+=1)
  14. for(int dy=0; dy<c; dy+=1){
  15. int color=num[ lx+dx ][ ly+dy ];
  16. sub[ color ]+=1;
  17. nowN-= sub[ color ]==all[ color ];
  18. }
  19. }
  20. int main(){
  21. int allN=0;
  22.  
  23. cin>>R>>C>>N>>r>>c;
  24. for(int x=0; x<R; x+=1)
  25. for(int y=0; y<C; y+=1){
  26. cin>>num[x][y];
  27. allN+= all[ num[x][y] ]==0;
  28. all[ num[x][y] ]+=1;
  29. }
  30. //
  31. for(int Lx=0; Lx<=R-r; Lx+=1){
  32. // mask 對齊最左側時才需要重算
  33. nowN=allN;
  34. recount(Lx,0);
  35. cout<<nowN;
  36. // sliding window
  37. for(int Ly=1; Ly<=C-c; Ly+=1){
  38. // 讓最左邊的 column 露出來
  39. for(int dx=0; dx<r; dx+=1){
  40. int color=num[Lx+dx][Ly-1];
  41. nowN+= sub[color]==all[color];
  42. sub[color]-=1;
  43. }
  44. // 讓最右邊的 column 遮起來
  45. for(int dx=0; dx<r; dx+=1){
  46. int color=num[Lx+dx][Ly+c-1];
  47. sub[color]+=1;
  48. nowN-= sub[color]==all[color];
  49. }
  50. cout<<" "<<nowN;
  51. }
  52. cout<<"\n";
  53. }
  54.  
  55. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
0