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. void recount(int lx, int ly){
  10. for(int n=0;n<=N;n++)
  11. sub[n]=0;
  12. for(int dx=0;dx<r;dx++){
  13. for(int dy=0;dy<c;dy++){
  14. int color=num[lx+dx][ly+dy];
  15. sub[color]+=1;
  16. nowN-=sub[color]==all[color];
  17. }
  18. }
  19. }
  20.  
  21. int main() {
  22. int allN=0;
  23. cin>>R>>C>>N>>r>>c;
  24. for(int x=0;x<R;x++){
  25. for(int y=0;y<C;y++){
  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. for(int Ly=1;Ly<=C-c;Ly++){
  37. //讓最左邊的一列露出來、最右邊的遮起來
  38. for(int dx=0;dx<r;dx++){
  39. int color =num[Lx+dx][Ly-1];
  40. nowN+=sub[color]==all[color];
  41. sub[color]-=1;
  42. }
  43. for(int dx=0; dx<r; dx+=1){
  44. int color=num[Lx+dx][Ly+c-1];
  45. sub[color]+=1;
  46. nowN-= sub[color]==all[color];
  47. }
  48. cout<<" "<<nowN;
  49. }
  50. cout<<"\n";
  51. }
  52. return 0;
  53. }
Success #stdin #stdout 0.01s 5304KB
stdin
5 6 9 3 4
7 1 5 3 9 5
4 5 4 5 1 2
6 1 6 2 9 7
4 7 1 5 8 8
3 4 3 3 5 3
stdout
8 8 7
8 9 7
8 9 8