fork(2) download
  1. /* paiza POH! vol.2
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/paizen/result/48b92015f8ec069998c2d3a230f0c355
  4.  * author: Leonardone @ NEETSDKASU
  5.  */
  6. import java.util.*;
  7. import java.lang.*;
  8. import java.io.*;
  9.  
  10. class Main
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. Paiza.getInstance().resolve(new MyResolver());
  15. }
  16. }
  17.  
  18. class MyResolver extends Paiza.Resolver
  19. {
  20. private int[][] space2right = null;
  21. private int[][] space2bottom = null;
  22. private int[][] result = null;
  23. private int[] maxSpace2right = null;
  24. private int[] maxSpace2bottom = null;
  25.  
  26. @Override
  27. public void setHome(Paiza.Home home) {
  28. super.setHome(home);
  29.  
  30. space2right = new int[home.getH()][home.getW()];
  31. maxSpace2right = new int[home.getH()];
  32. int allSpaceCount = 0;
  33.  
  34. for (int y = 0; y < home.getH(); y++) {
  35. int count = 0, max = 0;
  36. for (int x = home.getW() - 1; x >= 0; x--) {
  37. if (home.isSpace(x, y)) {
  38. count++;
  39. allSpaceCount++;
  40. if (count > max) {
  41. max = count;
  42. }
  43. } else {
  44. count = 0;
  45. }
  46. space2right[y][x] = count;
  47. }
  48. maxSpace2right[y] = max;
  49. }
  50.  
  51. space2bottom = new int[home.getH()][home.getW()];
  52. maxSpace2bottom = new int[home.getW()];
  53.  
  54. for (int x = 0; x < home.getW(); x++) {
  55. int count = 0, max = 0;
  56. for (int y = home.getH() - 1; y >= 0; y--) {
  57. if (home.isSpace(x, y)) {
  58. count++;
  59. if (count > max) {
  60. max = count;
  61. }
  62. } else {
  63. count = 0;
  64. }
  65. space2bottom[y][x] = count;
  66. }
  67. maxSpace2bottom[x] = max;
  68. }
  69.  
  70. result = new int[301][301];
  71. result[1][1] = allSpaceCount + 1;
  72.  
  73. }
  74.  
  75. @Override
  76. public int resolve(Paiza.Widget widget) {
  77. if (result[widget.getS()][widget.getT()] > 0) {
  78. return result[widget.getS()][widget.getT()] - 1;
  79. }
  80. int count;
  81. if (widget.getS() > widget.getT()) {
  82. count = resolve2bottom(widget);
  83. } else {
  84. count = resolve2right(widget);
  85. }
  86. result[widget.getS()][widget.getT()] = count + 1;
  87. return count;
  88. }
  89.  
  90. private int resolve2right(Paiza.Widget widget) {
  91. int count = 0;
  92. for (int y = 0; y <= home.getH() - widget.getS(); y++) {
  93. if (maxSpace2right[y] < widget.getT()) {
  94. continue;
  95. }
  96. for (int x = 0; x <= home.getW() - widget.getT(); x++) {
  97. if (space2bottom[y][x] >= widget.getS()) {
  98. boolean placeable = true;
  99. for (int dy = 0; placeable && dy < widget.getS(); dy++) {
  100. if (space2right[y + dy][x] < widget.getT()) {
  101. placeable = false;
  102. x += space2right[y + dy][x];
  103. }
  104. }
  105. if (placeable) {
  106. count++;
  107. }
  108. } else if (space2right[y][x] < widget.getT()) {
  109. x += space2right[y][x];
  110. }
  111. }
  112. }
  113. return count;
  114. }
  115.  
  116. private int resolve2bottom(Paiza.Widget widget) {
  117. int count = 0;
  118. for (int x = 0; x <= home.getW() - widget.getT(); x++) {
  119. if (maxSpace2bottom[x] < widget.getS()) {
  120. continue;
  121. }
  122. for (int y = 0; y <= home.getH() - widget.getS(); y++) {
  123. if (space2right[y][x] >= widget.getT()) {
  124. boolean placeable = true;
  125. for (int dx = 0; placeable && dx < widget.getT(); dx++) {
  126. if (space2bottom[y][x + dx] < widget.getS()) {
  127. placeable = false;
  128. y += space2bottom[y][x + dx];
  129. }
  130. }
  131. if (placeable) {
  132. count++;
  133. }
  134. } else if (space2bottom[y][x] < widget.getS()) {
  135. y += space2bottom[y][x];
  136. }
  137. }
  138. }
  139. return count;
  140. }
  141.  
  142. }
  143.  
  144.  
  145. abstract class Resolver
  146. {
  147. protected Paiza.Home home;
  148.  
  149. public void setHome(Paiza.Home home) {
  150. this.home = home;
  151. }
  152.  
  153. public abstract int resolve(Paiza.Widget widget);
  154. }
  155.  
  156. final class Paiza
  157. {
  158. public static abstract class Resolver
  159. {
  160. protected Paiza.Home home;
  161.  
  162. public void setHome(Paiza.Home home) {
  163. this.home = home;
  164. }
  165.  
  166. public abstract int resolve(Paiza.Widget widget);
  167. }
  168.  
  169. public static Paiza getInstance() throws java.lang.Exception {
  170. if (paiza == null) {
  171. paiza = new Paiza();
  172. }
  173. return paiza;
  174. }
  175.  
  176. public void resolve(Resolver resolver) {
  177. init(resolver);
  178. for (Widget widget : widgets) {
  179. answer(resolver.resolve(widget));
  180. }
  181. flush();
  182. }
  183.  
  184.  
  185. public final class Home
  186. {
  187. private final int H;
  188. private final int W;
  189. private int[][] display;
  190. private Home(int H, int W) {
  191. this.H = H;
  192. this.W = W;
  193. display = new int[H][W];
  194. }
  195.  
  196. private void setDisplay(int x, int y, int e) throws java.lang.Exception {
  197. if (x < 0 || x >= W) {
  198. throw new ArrayIndexOutOfBoundsException("x : " + x);
  199. }
  200. if (y < 0 || y >= H) {
  201. throw new ArrayIndexOutOfBoundsException("y : " + y);
  202. }
  203. if (e != 0 && e != 1) {
  204. throw new IllegalArgumentException("e");
  205. }
  206. display[y][x] = e;
  207. }
  208.  
  209. public int getH() {
  210. return H;
  211. }
  212.  
  213. public int getW() {
  214. return W;
  215. }
  216.  
  217. public boolean isSpace(int x, int y) {
  218. if (x < 0 || y < 0 || x >= W || y >= H) {
  219. return false;
  220. }
  221. return display[y][x] == 0;
  222. }
  223. }
  224.  
  225. public final class Widget
  226. {
  227. private final int s;
  228. private final int t;
  229. private Widget(int s, int t) {
  230. this.s = s;
  231. this.t = t;
  232. }
  233.  
  234. public int getS() {
  235. return s;
  236. }
  237.  
  238. public int getT() {
  239. return t;
  240. }
  241. }
  242.  
  243. private static Paiza paiza = null;
  244.  
  245. private Home home;
  246. private ArrayList<Widget> widgets;
  247.  
  248. private Paiza() throws java.lang.Exception {
  249. String[] hw = in.readLine().split(" ");
  250. home = new Home(Integer.parseInt(hw[0]), Integer.parseInt(hw[1]));
  251. for (int y = 0; y < home.getH(); y++) {
  252. String line = in.readLine();
  253. for (int x = 0; x < home.getW(); x++) {
  254. home.setDisplay(x, y, (int)(line.charAt(x) - '0'));
  255. }
  256. }
  257. int N = Integer.parseInt(in.readLine());
  258. widgets = new ArrayList<Widget>(N);
  259. for (int i = 0; i< N; i++) {
  260. String[] st = in.readLine().split(" ");
  261. widgets.add(new Widget(Integer.parseInt(st[0]), Integer.parseInt(st[1])));
  262. }
  263. }
  264.  
  265. private StringBuilder output = null;
  266. private static final String NEWLINE = System.getProperty("line.separator");
  267.  
  268. private void init(Resolver resolver) {
  269. resolver.setHome(home);
  270. output = new StringBuilder(widgets.size() * 6);
  271. }
  272.  
  273. private void answer(int count) {
  274. output.append(count);
  275. output.append(NEWLINE);
  276. }
  277.  
  278. private void flush() {
  279. System.out.print(output);
  280. }
  281. }
  282.  
Success #stdin #stdout 0.07s 380160KB
stdin
5 5
00000
00100
00010
10001
10000
3
2 2
1 1
3 2
stdout
6
20
2