Source code for submission s939

Go to diff to previous submission

fms.cpp

  1. #include <cstdio>
  2. #include <cmath>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <iomanip>
  6. #include <iostream>
  7. #include <limits.h>
  8. #include <map>
  9. #include <queue>
  10. #include <vector>
  11. #include <set>
  12. #include <stack>
  13. #include <bitset>
  14. #include <string>
  15.  
  16. using namespace std;
  17.  
  18. typedef pair<int,int> ii;
  19. typedef vector<int> vi;
  20. typedef vector<ii> vii;
  21. typedef set<int> si;
  22. typedef set<ii> sii;
  23.  
  24. #define MP make_pair
  25. #define PB push_back
  26. #define REP(i,a) for ( int i = 0; i < int(a); i++)
  27. #define FOR(i,a,b) for ( int i = int(a); i<=int(b); i++)
  28. #define FORD(i,a,b) for(int i= int(a); i>=int(b); i--)
  29.  
  30. const int INF = 1<<29;
  31. typedef long long int ll;
  32.  
  33. int ar, ac, tr, tc;
  34. char board[1001][1001];
  35. int tiles[1001][1001];
  36.  
  37. int isIn( int r1, int r2, int c1, int c2 )
  38. {
  39. int count = 0;
  40. r1 = max( r1, 0 );
  41. r2 = min( r2, ar - 1 );
  42. c1 = max( c1, 0 );
  43. c2 = min( c2, ac - 1 );
  44. //printf("%d %d %d %d\n", r1, r2, c1, c2);
  45. FOR( i, r1, r2 )
  46. FOR( j, c1, c2 )
  47. if ( board[i][j] == 'X' )
  48. ++count;
  49. return count;
  50. }
  51.  
  52. void updateTile( int r1, int r2, int c1, int c2, int x, int y )
  53. {
  54. r1 = max( r1, 0 );
  55. r2 = min( r2, ar - 1 );
  56. c1 = max( c1, 0 );
  57. c2 = min( c2, ac - 1 );
  58. if ( c1 > 0 )
  59. FOR( i, r1, r2 )
  60. if ( board[i][c1-1] == 'X' )
  61. --tiles[x][y];
  62. if (c2 <=ac-1 )
  63. FOR( i, r1, r2 )
  64. if ( board[i][c2] == 'X' )
  65. ++tiles[x][y];
  66. }
  67.  
  68. int countTiles( int r, int c )
  69. {
  70. int count = 0;
  71. for ( int i = r, i2=0; i < ar; i += tr, i2++ )
  72. for ( int j = c, j2=0; j < ac; j += tc, j2++ ) {
  73. updateTile(i,i+tr-1,j,j+tc-1,i2,j2);
  74. if ( tiles[i2][j2] >0 )
  75. ++count;
  76. }
  77. return count;
  78. }
  79.  
  80. void prepare( int r, int c )
  81. {
  82. int i2 = 0, j2;
  83. for ( int i = r; i < ar; i += tr, ++i2 )
  84. {
  85. j2 = 0;
  86. for ( int j = c; j < ac; j += tc, ++j2 )
  87. tiles[i2][j2] = isIn( i, i+tr-1, j, j+tc-1 );
  88. }
  89. }
  90.  
  91. int main()
  92. {
  93. while ( scanf("%d%d%d%d", &ar, &ac, &tr, &tc) == 4 )
  94. {
  95. FOR( i, 0, ar-1 )
  96. {
  97. FOR( j, 0, ac-1 )
  98. scanf(" %c", &board[i][j]);
  99. //scanf("%*c");
  100. }
  101. int count = INT_MAX;
  102. prepare( -tr+1, -tc+1 );
  103. FOR( i, -tr+1, 0 )
  104. {
  105. FOR( j, -tc+1, 0 )
  106. {
  107. if ( i == -tr+1 && j == -tc+1 )
  108. continue;
  109. int tmp = countTiles( i, j );
  110. count = min( count, tmp );
  111. //printf("%d-%d\n", tmp, count);
  112. }
  113. ++i;
  114. if ( i >= 0 )
  115. break;
  116. FORD( j, 0, -tc+1 )
  117. {
  118. if ( i == -tr+1 && j == -tc+1 )
  119. continue;
  120. int tmp = countTiles( i, j );
  121. count = min( count, tmp );
  122. //printf("%d-%d\n", tmp, count);
  123. }
  124. }
  125. printf("%d\n", count);
  126. // break;
  127. }
  128. return 0;
  129. }
  130.  
  131.  

Diff to submission s878

fm.cpp

--- c5.s878.cteam013.fm.cpp.0.fm.cpp
+++ c5.s939.cteam013.fm.cpp.0.fms.cpp
@@ -33,7 +33,9 @@
 int  ar, ac, tr, tc;
 char board[1001][1001];
+int  tiles[1001][1001];
 
-bool isIn( int r1, int r2, int c1, int c2 )
+int isIn( int r1, int r2, int c1, int c2 )
 {
+        int count = 0;
         r1 = max( r1, 0 );
         r2 = min( r2, ar - 1 );
@@ -44,6 +46,22 @@
                 FOR( j, c1, c2 ) 
                         if ( board[i][j] == 'X' )
-                                return true;
-        return false;
+                                ++count;
+        return count;
+}
+
+void updateTile( int r1, int r2, int c1, int c2, int x, int y )
+{
+        r1 = max( r1, 0 );
+        r2 = min( r2, ar - 1 );
+        c1 = max( c1, 0 );
+        c2 = min( c2, ac - 1 );
+        if ( c1 > 0 )
+                FOR( i, r1, r2 ) 
+                        if ( board[i][c1-1] == 'X' )
+                                --tiles[x][y];
+        if (c2 <=ac-1 )
+        FOR( i, r1, r2 ) 
+                if ( board[i][c2] == 'X' )
+                        ++tiles[x][y];
 }
 
@@ -51,11 +69,22 @@
 {
         int count = 0;
-        for ( int i = r; i < ar; i += tr )
-                for ( int j = c; j < ac; j += tc )
-                        if ( isIn( i, i+tr-1, j, j+tc-1 ) ) {
-                                ++count;
-        //printf("YEP\n");
-      }
-        return count;
+        for ( int i = r, i2=0; i < ar; i += tr, i2++ )
+                for ( int j = c, j2=0; j < ac; j += tc, j2++ ) {
+                  updateTile(i,i+tr-1,j,j+tc-1,i2,j2);
+                  if ( tiles[i2][j2] >0 ) 
+                   ++count;
+                } 
+  return count;
+}
+
+void prepare( int r, int c )
+{
+        int i2 = 0, j2;
+        for ( int i = r; i < ar; i += tr, ++i2 )
+        {
+                j2 = 0;
+                for ( int j = c; j < ac; j += tc, ++j2  )
+                        tiles[i2][j2] = isIn( i, i+tr-1, j, j+tc-1 );
+        }
 }
 
@@ -71,11 +100,27 @@
                 }
                 int count = INT_MAX;
+                prepare( -tr+1, -tc+1 );
                 FOR( i, -tr+1, 0 )
+                {
                         FOR( j, -tc+1, 0 )
                         {
+                                if ( i == -tr+1 && j == -tc+1 )
+                                        continue;
+                                int tmp = countTiles( i, j );
+        count = min( count, tmp );
+                                //printf("%d-%d\n", tmp, count);
+                        }
+                        ++i;
+                        if ( i >= 0 )
+                                break;
+                        FORD( j, 0, -tc+1 )
+                        {
+                                if ( i == -tr+1 && j == -tc+1 )
+                                        continue;
                                 int tmp = countTiles( i, j );
         count = min( count, tmp );
                                 //printf("%d-%d\n", tmp, count);
                         }
+                }
                 printf("%d\n", count);
         //      break;