Advertisement
a53

Dreptunghi2

a53
Apr 20th, 2021
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int MAXN = 1000;
  6. const int MAXS = 355;
  7. const int MOD = 1e9 + 7;
  8.  
  9. struct Cell
  10. {
  11. int x1, y1, x2, y2;
  12. bool operator <(const Cell& other) const
  13. {
  14. if (x1 == other.x1)
  15. {
  16. if (x2 == other.x2)
  17. {
  18. if (y1 == other.y1)
  19. return y2 < other.y2;
  20. return y1 < other.y1;
  21. }
  22. else
  23. {
  24. return x2 < other.x2;
  25. }
  26. }
  27. else
  28. {
  29. return x1 < other.x1;
  30. }
  31. }
  32. };
  33. map<Cell, int>mp;
  34. char s[MAXS];
  35. int rep[MAXS];
  36. int sol[MAXN + 5];
  37. int a[MAXN + 5][MAXN + 5];
  38. int m, k, t;
  39. int mx, my;
  40. void getRep(int n)
  41. {
  42. int nr = 0;
  43. for (int i = 1; i <= n; ++i)
  44. {
  45. if ('0' <= s[i] && s[i] <= '9')
  46. {
  47. nr = nr * 10 + s[i] - '0';
  48. }
  49. else
  50. {
  51. if (nr)
  52. rep[++m] = nr, nr = 0;
  53. if (s[i] == 'H')
  54. rep[++m] = -1;
  55. else if (s[i] == 'V')
  56. rep[++m] = -2;
  57. else
  58. rep[++m] = -3;
  59. }
  60. }
  61. }
  62. void Cerinta1(int n)
  63. {
  64. int ans = 0;
  65. for (int i = 1; i <= n; ++i)
  66. if (s[i] == '*')
  67. ans++;
  68. cout<<ans<<'\n';
  69. }
  70.  
  71. void solve(int l, int r)
  72. {
  73. mx = max(mx, l);
  74. my = max(my, r);
  75. if (rep[k] == -3)
  76. {
  77. k++;
  78. return ;
  79. }
  80. if (rep[k] == -1)
  81. {
  82. k++;
  83. int nl = l + rep[k];
  84. k++;
  85. solve(l, r);
  86. solve(nl, r);
  87. }
  88. else
  89. {
  90. k++;
  91. int nr = r + rep[k];
  92. k++;
  93. solve(l, r);
  94. solve(l, nr);
  95. }
  96. }
  97.  
  98. void Cerinta2(int n)
  99. {
  100. k = 1;
  101. solve(1, 1);
  102. cout<<mx<<" "<<my;
  103. }
  104. void Partaj(int x1, int y1, int x2, int y2)
  105. {
  106. if (rep[k] == -3)
  107. {
  108. k++;
  109. t++;
  110. for (int i = x1; i <= x2; ++i)
  111. for (int j = y1; j <= y2; ++j)
  112. a[i][j] = t;
  113. return ;
  114. }
  115. if (rep[k] == -1)
  116. {
  117. k++;
  118. int nx = x1 + rep[k];
  119. k++;
  120. Partaj(x1, y1, nx - 1, y2);
  121. Partaj(nx, y1, x2, y2);
  122. }
  123. else
  124. {
  125. k++;
  126. int ny = y1 + rep[k];
  127. k++;
  128. Partaj(x1, y1, x2, ny - 1);
  129. Partaj(x1, ny, x2, y2);
  130. }
  131. }
  132.  
  133. int getw(int x1, int y1, int x2, int y2)
  134. {
  135. if (mp.count({x1, y1, x2, y2}))
  136. return mp[ {x1, y1, x2, y2}];
  137. int ind = -1, r = 0, okk = 0;
  138. for (int i = x1; i < x2; ++i)
  139. {
  140. bool ok = 1;
  141. for (int j = y1; j <= y2 && ok; ++j)
  142. if (a[i][j] == a[i + 1][j])
  143. ok = 0;
  144. if (ok && a[i][y2] < a[i + 1][y1])
  145. {
  146. ind = i;
  147. okk = 1;
  148. r = (r + 1LL * getw(x1, y1, ind, y2) * getw(ind + 1, y1, x2, y2)) % MOD;
  149. }
  150. }
  151. ind = -1;
  152. for (int j = y1; j < y2; ++j)
  153. {
  154. bool ok = 1;
  155. for (int i = x1; i <= x2 && ok; ++i)
  156. if (a[i][j] == a[i][j + 1])
  157. ok = 0;
  158. if (ok && a[x2][j] < a[x1][j + 1])
  159. {
  160. ind = j;
  161. okk = 1;
  162. r = (r + 1LL * getw(x1, y1, x2, ind) * getw(x1, ind + 1, x2, y2)) % MOD;
  163. }
  164. }
  165. if (okk == 0)
  166. r = 1;
  167. mp[ {x1, y1, x2, y2}] = r;
  168. return r;
  169. }
  170.  
  171. void Cerinta3(int n)
  172. {
  173. k = 1;
  174. solve(1, 1);
  175. k = 1;
  176. Partaj(1, 1, mx, my);
  177. cout<<getw(1, 1, mx, my)<<'\n';
  178. }
  179.  
  180. void getm(int x1, int y1, int x2, int y2)
  181. {
  182. int ind = -1;
  183. for (int i = x1; i < x2; ++i)
  184. {
  185. bool ok = 1;
  186. for (int j = y1; j <= y2 && ok; ++j)
  187. if (a[i][j] == a[i + 1][j])
  188. ok = 0;
  189. if (ok && a[i][y2] < a[i + 1][y1])
  190. {
  191. ind = i;
  192. break;
  193. }
  194. }
  195. if (ind != -1)
  196. {
  197. sol[++k] = -1;
  198. sol[++k] = ind - x1 + 1;
  199. getm(x1, y1, ind, y2);
  200. getm(ind + 1, y1, x2, y2);
  201. }
  202. else
  203. {
  204. for (int j = y1; j < y2; ++j)
  205. {
  206. bool ok = 1;
  207. for (int i = x1; i <= x2 && ok; ++i)
  208. if (a[i][j] == a[i][j + 1])
  209. ok = 0;
  210. if (ok && a[x2][j] < a[x1][j + 1])
  211. {
  212. ind = j;
  213. break;
  214. }
  215. }
  216. if (ind != -1)
  217. {
  218. sol[++k] = -2;
  219. sol[++k] = ind - y1 + 1;
  220. getm(x1, y1, x2, ind);
  221. getm(x1, ind + 1, x2, y2);
  222. }
  223. else
  224. {
  225. sol[++k] = -3;
  226. }
  227. }
  228. }
  229.  
  230. void Cerinta4(int n)
  231. {
  232. k = 1;
  233. solve(1, 1);
  234. k = 1;
  235. Partaj(1, 1, mx, my);
  236. k = 0;
  237. getm(1, 1, mx, my);
  238. for (int i = 1; i <= k; ++i)
  239. if (sol[i] == -1)
  240. cout<<"H";
  241. else if (sol[i] == -2)
  242. cout<<"V";
  243. else if (sol[i] == -3)
  244. cout<<"*";
  245. else
  246. cout<<sol[i];
  247. cout<<'\n';
  248. }
  249.  
  250. int main()
  251. {
  252. int p;
  253. cin>>p;
  254. cin.get();
  255. cin.getline(s+1, MAXS);
  256. int len = strlen(s + 1);
  257. getRep(len);
  258. if(p == 1)
  259. Cerinta1(len);
  260. else if (p == 2)
  261. Cerinta2(len);
  262. else if (p == 3)
  263. Cerinta3(len);
  264. else
  265. Cerinta4(len);
  266. return 0;
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement