Hex Artifact Content
Not logged in

Artifact 8bc8dad70f532eb24755285a261929a4e28f5ea1:


0000: 23 69 6e 63 6c 75 64 65 20 3c 69 6f 73 74 72 65  #include <iostre
0010: 61 6d 3e 0d 0a 23 69 6e 63 6c 75 64 65 20 3c 73  am>..#include <s
0020: 73 74 72 65 61 6d 3e 0d 0a 23 69 6e 63 6c 75 64  stream>..#includ
0030: 65 20 3c 69 6f 6d 61 6e 69 70 3e 0d 0a 23 69 6e  e <iomanip>..#in
0040: 63 6c 75 64 65 20 3c 76 65 63 74 6f 72 3e 0d 0a  clude <vector>..
0050: 23 69 6e 63 6c 75 64 65 20 3c 73 74 72 69 6e 67  #include <string
0060: 3e 0d 0a 23 69 6e 63 6c 75 64 65 20 3c 6d 61 70  >..#include <map
0070: 3e 0d 0a 23 69 6e 63 6c 75 64 65 20 3c 73 65 74  >..#include <set
0080: 3e 0d 0a 23 69 6e 63 6c 75 64 65 20 3c 61 6c 67  >..#include <alg
0090: 6f 72 69 74 68 6d 3e 0d 0a 23 69 6e 63 6c 75 64  orithm>..#includ
00a0: 65 20 3c 6e 75 6d 65 72 69 63 3e 0d 0a 23 69 6e  e <numeric>..#in
00b0: 63 6c 75 64 65 20 3c 69 74 65 72 61 74 6f 72 3e  clude <iterator>
00c0: 0d 0a 23 69 6e 63 6c 75 64 65 20 3c 66 75 6e 63  ..#include <func
00d0: 74 69 6f 6e 61 6c 3e 0d 0a 23 69 6e 63 6c 75 64  tional>..#includ
00e0: 65 20 3c 63 6f 6d 70 6c 65 78 3e 0d 0a 23 69 6e  e <complex>..#in
00f0: 63 6c 75 64 65 20 3c 71 75 65 75 65 3e 0d 0a 23  clude <queue>..#
0100: 69 6e 63 6c 75 64 65 20 3c 73 74 61 63 6b 3e 0d  include <stack>.
0110: 0a 23 69 6e 63 6c 75 64 65 20 3c 63 6d 61 74 68  .#include <cmath
0120: 3e 0d 0a 23 69 6e 63 6c 75 64 65 20 3c 63 61 73  >..#include <cas
0130: 73 65 72 74 3e 0d 0a 23 69 6e 63 6c 75 64 65 20  sert>..#include 
0140: 3c 74 75 70 6c 65 3e 0d 0a 75 73 69 6e 67 20 6e  <tuple>..using n
0150: 61 6d 65 73 70 61 63 65 20 73 74 64 3b 0d 0a 74  amespace std;..t
0160: 79 70 65 64 65 66 20 6c 6f 6e 67 20 6c 6f 6e 67  ypedef long long
0170: 20 4c 4c 3b 0d 0a 74 79 70 65 64 65 66 20 63 6f   LL;..typedef co
0180: 6d 70 6c 65 78 3c 64 6f 75 62 6c 65 3e 20 43 4d  mplex<double> CM
0190: 50 3b 0d 0a 0d 0a 63 6c 61 73 73 20 43 68 6f 63  P;....class Choc
01a0: 6f 6c 61 74 65 44 69 76 69 64 69 6e 67 45 61 73  olateDividingEas
01b0: 79 20 7b 20 70 75 62 6c 69 63 3a 0d 0a 09 69 6e  y { public:...in
01c0: 74 20 66 69 6e 64 42 65 73 74 28 76 65 63 74 6f  t findBest(vecto
01d0: 72 20 3c 73 74 72 69 6e 67 3e 20 63 68 6f 63 6f  r <string> choco
01e0: 6c 61 74 65 29 0d 0a 09 7b 0d 0a 09 09 63 6f 6e  late)...{....con
01f0: 73 74 20 69 6e 74 20 48 20 3d 20 63 68 6f 63 6f  st int H = choco
0200: 6c 61 74 65 2e 73 69 7a 65 28 29 3b 0d 0a 09 09  late.size();....
0210: 63 6f 6e 73 74 20 69 6e 74 20 57 20 3d 20 63 68  const int W = ch
0220: 6f 63 6f 6c 61 74 65 5b 30 5d 2e 73 69 7a 65 28  ocolate[0].size(
0230: 29 3b 0d 0a 09 09 76 65 63 74 6f 72 3c 76 65 63  );....vector<vec
0240: 74 6f 72 3c 69 6e 74 3e 3e 20 73 75 6d 28 48 2b  tor<int>> sum(H+
0250: 31 2c 20 76 65 63 74 6f 72 3c 69 6e 74 3e 28 57  1, vector<int>(W
0260: 2b 31 29 29 3b 0d 0a 09 09 66 6f 72 28 69 6e 74  +1));....for(int
0270: 20 79 3d 48 2d 31 3b 20 79 3e 3d 30 3b 20 2d 2d   y=H-1; y>=0; --
0280: 79 29 0d 0a 09 09 66 6f 72 28 69 6e 74 20 78 3d  y)....for(int x=
0290: 57 2d 31 3b 20 78 3e 3d 30 3b 20 2d 2d 78 29 0d  W-1; x>=0; --x).
02a0: 0a 09 09 09 73 75 6d 5b 79 5d 5b 78 5d 20 3d 20  ....sum[y][x] = 
02b0: 28 63 68 6f 63 6f 6c 61 74 65 5b 79 5d 5b 78 5d  (chocolate[y][x]
02c0: 2d 27 30 27 29 2b 73 75 6d 5b 79 2b 31 5d 5b 78  -'0')+sum[y+1][x
02d0: 5d 2b 73 75 6d 5b 79 5d 5b 78 2b 31 5d 2d 73 75  ]+sum[y][x+1]-su
02e0: 6d 5b 79 2b 31 5d 5b 78 2b 31 5d 3b 0d 0a 0d 0a  m[y+1][x+1];....
02f0: 09 09 69 6e 74 20 62 65 73 74 20 3d 20 30 3b 0d  ..int best = 0;.
0300: 0a 09 09 66 6f 72 28 69 6e 74 20 79 31 3d 31 3b  ...for(int y1=1;
0310: 20 79 31 3c 48 3b 20 2b 2b 79 31 29 0d 0a 09 09   y1<H; ++y1)....
0320: 66 6f 72 28 69 6e 74 20 79 32 3d 79 31 2b 31 3b  for(int y2=y1+1;
0330: 20 79 32 3c 48 3b 20 2b 2b 79 32 29 0d 0a 09 09   y2<H; ++y2)....
0340: 66 6f 72 28 69 6e 74 20 78 31 3d 31 3b 20 78 31  for(int x1=1; x1
0350: 3c 57 3b 20 2b 2b 78 31 29 0d 0a 09 09 66 6f 72  <W; ++x1)....for
0360: 28 69 6e 74 20 78 32 3d 78 31 2b 31 3b 20 78 32  (int x2=x1+1; x2
0370: 3c 57 3b 20 2b 2b 78 32 29 0d 0a 09 09 7b 0d 0a  <W; ++x2)....{..
0380: 09 09 09 69 6e 74 20 59 5b 5d 20 3d 20 7b 30 2c  ...int Y[] = {0,
0390: 20 79 31 2c 20 79 32 2c 20 48 7d 3b 0d 0a 09 09   y1, y2, H};....
03a0: 09 69 6e 74 20 58 5b 5d 20 3d 20 7b 30 2c 20 78  .int X[] = {0, x
03b0: 31 2c 20 78 32 2c 20 57 7d 3b 0d 0a 09 09 09 69  1, x2, W};.....i
03c0: 6e 74 20 73 6d 61 6c 6c 65 73 74 20 3d 20 30 78  nt smallest = 0x
03d0: 33 66 66 66 66 66 66 66 3b 0d 0a 09 09 09 66 6f  3fffffff;.....fo
03e0: 72 28 69 6e 74 20 69 3d 30 3b 20 69 3c 33 3b 20  r(int i=0; i<3; 
03f0: 2b 2b 69 29 0d 0a 09 09 09 66 6f 72 28 69 6e 74  ++i).....for(int
0400: 20 6b 3d 30 3b 20 6b 3c 33 3b 20 2b 2b 6b 29 0d   k=0; k<3; ++k).
0410: 0a 09 09 09 7b 0d 0a 09 09 09 09 69 6e 74 20 61  ....{......int a
0420: 72 65 61 20 3d 20 73 75 6d 5b 59 5b 69 5d 5d 5b  rea = sum[Y[i]][
0430: 58 5b 6b 5d 5d 20 2d 20 73 75 6d 5b 59 5b 69 2b  X[k]] - sum[Y[i+
0440: 31 5d 5d 5b 58 5b 6b 5d 5d 20 2d 20 73 75 6d 5b  1]][X[k]] - sum[
0450: 59 5b 69 5d 5d 5b 58 5b 6b 2b 31 5d 5d 20 2b 20  Y[i]][X[k+1]] + 
0460: 73 75 6d 5b 59 5b 69 2b 31 5d 5d 5b 58 5b 6b 2b  sum[Y[i+1]][X[k+
0470: 31 5d 5d 3b 0d 0a 09 09 09 09 73 6d 61 6c 6c 65  1]];......smalle
0480: 73 74 20 3d 20 6d 69 6e 28 73 6d 61 6c 6c 65 73  st = min(smalles
0490: 74 2c 20 61 72 65 61 29 3b 0d 0a 09 09 09 7d 0d  t, area);.....}.
04a0: 0a 09 09 09 62 65 73 74 20 3d 20 6d 61 78 28 62  ....best = max(b
04b0: 65 73 74 2c 20 73 6d 61 6c 6c 65 73 74 29 3b 0d  est, smallest);.
04c0: 0a 09 09 7d 0d 0a 09 09 72 65 74 75 72 6e 20 62  ...}....return b
04d0: 65 73 74 3b 0d 0a 09 7d 0d 0a 7d 3b 0d 0a 0d 0a  est;...}..};....
04e0: 2f 2f 20 42 45 47 49 4e 20 43 55 54 20 48 45 52  // BEGIN CUT HER
04f0: 45 0d 0a 23 69 6e 63 6c 75 64 65 20 3c 63 74 69  E..#include <cti
0500: 6d 65 3e 0d 0a 64 6f 75 62 6c 65 20 73 74 61 72  me>..double star
0510: 74 5f 74 69 6d 65 3b 20 73 74 72 69 6e 67 20 74  t_time; string t
0520: 69 6d 65 72 28 29 0d 0a 20 7b 20 6f 73 74 72 69  imer().. { ostri
0530: 6e 67 73 74 72 65 61 6d 20 6f 73 3b 20 6f 73 20  ngstream os; os 
0540: 3c 3c 20 22 20 28 22 20 3c 3c 20 69 6e 74 28 28  << " (" << int((
0550: 63 6c 6f 63 6b 28 29 2d 73 74 61 72 74 5f 74 69  clock()-start_ti
0560: 6d 65 29 2f 43 4c 4f 43 4b 53 5f 50 45 52 5f 53  me)/CLOCKS_PER_S
0570: 45 43 2a 31 30 30 30 29 20 3c 3c 20 22 20 6d 73  EC*1000) << " ms
0580: 65 63 29 22 3b 20 72 65 74 75 72 6e 20 6f 73 2e  ec)"; return os.
0590: 73 74 72 28 29 3b 20 7d 0d 0a 74 65 6d 70 6c 61  str(); }..templa
05a0: 74 65 3c 74 79 70 65 6e 61 6d 65 20 54 3e 20 6f  te<typename T> o
05b0: 73 74 72 65 61 6d 26 20 6f 70 65 72 61 74 6f 72  stream& operator
05c0: 3c 3c 28 6f 73 74 72 65 61 6d 26 20 6f 73 2c 20  <<(ostream& os, 
05d0: 63 6f 6e 73 74 20 76 65 63 74 6f 72 3c 54 3e 26  const vector<T>&
05e0: 20 76 29 0d 0a 20 7b 20 6f 73 20 3c 3c 20 22 7b   v).. { os << "{
05f0: 20 22 3b 0d 0a 20 20 20 66 6f 72 28 74 79 70 65   ";..   for(type
0600: 6e 61 6d 65 20 76 65 63 74 6f 72 3c 54 3e 3a 3a  name vector<T>::
0610: 63 6f 6e 73 74 5f 69 74 65 72 61 74 6f 72 20 69  const_iterator i
0620: 74 3d 76 2e 62 65 67 69 6e 28 29 3b 20 69 74 21  t=v.begin(); it!
0630: 3d 76 2e 65 6e 64 28 29 3b 20 2b 2b 69 74 29 0d  =v.end(); ++it).
0640: 0a 20 20 20 6f 73 20 3c 3c 20 27 5c 22 27 20 3c  .   os << '\"' <
0650: 3c 20 2a 69 74 20 3c 3c 20 27 5c 22 27 20 3c 3c  < *it << '\"' <<
0660: 20 28 69 74 2b 31 3d 3d 76 2e 65 6e 64 28 29 20   (it+1==v.end() 
0670: 3f 20 22 22 20 3a 20 22 2c 20 22 29 3b 20 6f 73  ? "" : ", "); os
0680: 20 3c 3c 20 22 20 7d 22 3b 20 72 65 74 75 72 6e   << " }"; return
0690: 20 6f 73 3b 20 7d 0d 0a 76 6f 69 64 20 76 65 72   os; }..void ver
06a0: 69 66 79 5f 63 61 73 65 28 63 6f 6e 73 74 20 69  ify_case(const i
06b0: 6e 74 26 20 45 78 70 65 63 74 65 64 2c 20 63 6f  nt& Expected, co
06c0: 6e 73 74 20 69 6e 74 26 20 52 65 63 65 69 76 65  nst int& Receive
06d0: 64 29 20 7b 0d 0a 20 62 6f 6f 6c 20 6f 6b 20 3d  d) {.. bool ok =
06e0: 20 28 45 78 70 65 63 74 65 64 20 3d 3d 20 52 65   (Expected == Re
06f0: 63 65 69 76 65 64 29 3b 0d 0a 20 69 66 28 6f 6b  ceived);.. if(ok
0700: 29 20 63 65 72 72 20 3c 3c 20 22 50 41 53 53 45  ) cerr << "PASSE
0710: 44 22 20 3c 3c 20 74 69 6d 65 72 28 29 20 3c 3c  D" << timer() <<
0720: 20 65 6e 64 6c 3b 20 20 65 6c 73 65 20 7b 20 63   endl;  else { c
0730: 65 72 72 20 3c 3c 20 22 46 41 49 4c 45 44 22 20  err << "FAILED" 
0740: 3c 3c 20 74 69 6d 65 72 28 29 20 3c 3c 20 65 6e  << timer() << en
0750: 64 6c 3b 0d 0a 20 63 65 72 72 20 3c 3c 20 22 5c  dl;.. cerr << "\
0760: 74 6f 3a 20 5c 22 22 20 3c 3c 20 45 78 70 65 63  to: \"" << Expec
0770: 74 65 64 20 3c 3c 20 27 5c 22 27 20 3c 3c 20 65  ted << '\"' << e
0780: 6e 64 6c 20 3c 3c 20 22 5c 74 78 3a 20 5c 22 22  ndl << "\tx: \""
0790: 20 3c 3c 20 52 65 63 65 69 76 65 64 20 3c 3c 20   << Received << 
07a0: 27 5c 22 27 20 3c 3c 20 65 6e 64 6c 3b 20 7d 20  '\"' << endl; } 
07b0: 7d 0d 0a 23 64 65 66 69 6e 65 20 43 41 53 45 28  }..#define CASE(
07c0: 4e 29 20 7b 63 65 72 72 20 3c 3c 20 22 54 65 73  N) {cerr << "Tes
07d0: 74 20 43 61 73 65 20 23 22 20 3c 3c 20 4e 20 3c  t Case #" << N <
07e0: 3c 20 22 2e 2e 2e 22 20 3c 3c 20 66 6c 75 73 68  < "..." << flush
07f0: 3b 20 73 74 61 72 74 5f 74 69 6d 65 3d 63 6c 6f  ; start_time=clo
0800: 63 6b 28 29 3b 0d 0a 23 64 65 66 69 6e 65 20 45  ck();..#define E
0810: 4e 44 09 20 76 65 72 69 66 79 5f 63 61 73 65 28  ND. verify_case(
0820: 5f 2c 20 43 68 6f 63 6f 6c 61 74 65 44 69 76 69  _, ChocolateDivi
0830: 64 69 6e 67 45 61 73 79 28 29 2e 66 69 6e 64 42  dingEasy().findB
0840: 65 73 74 28 63 68 6f 63 6f 6c 61 74 65 29 29 3b  est(chocolate));
0850: 7d 0d 0a 69 6e 74 20 6d 61 69 6e 28 29 7b 0d 0a  }..int main(){..
0860: 0d 0a 43 41 53 45 28 30 29 0d 0a 09 73 74 72 69  ..CASE(0)...stri
0870: 6e 67 20 63 68 6f 63 6f 6c 61 74 65 5f 5b 5d 20  ng chocolate_[] 
0880: 3d 20 7b 0d 0a 22 39 37 36 38 22 2c 0d 0a 22 36  = {.."9768",.."6
0890: 37 36 37 22 2c 0d 0a 22 35 33 31 33 22 0d 0a 7d  767",.."5313"..}
08a0: 3b 0d 0a 09 20 20 76 65 63 74 6f 72 20 3c 73 74  ;...  vector <st
08b0: 72 69 6e 67 3e 20 63 68 6f 63 6f 6c 61 74 65 28  ring> chocolate(
08c0: 63 68 6f 63 6f 6c 61 74 65 5f 2c 20 63 68 6f 63  chocolate_, choc
08d0: 6f 6c 61 74 65 5f 2b 73 69 7a 65 6f 66 28 63 68  olate_+sizeof(ch
08e0: 6f 63 6f 6c 61 74 65 5f 29 2f 73 69 7a 65 6f 66  ocolate_)/sizeof
08f0: 28 2a 63 68 6f 63 6f 6c 61 74 65 5f 29 29 3b 20  (*chocolate_)); 
0900: 0d 0a 09 69 6e 74 20 5f 20 3d 20 33 3b 20 0d 0a  ...int _ = 3; ..
0910: 45 4e 44 0d 0a 43 41 53 45 28 31 29 0d 0a 09 73  END..CASE(1)...s
0920: 74 72 69 6e 67 20 63 68 6f 63 6f 6c 61 74 65 5f  tring chocolate_
0930: 5b 5d 20 3d 20 7b 0d 0a 22 33 36 37 35 33 35 36  [] = {.."3675356
0940: 32 22 2c 0d 0a 22 39 31 32 37 30 39 33 36 22 2c  2",.."91270936",
0950: 0d 0a 22 30 36 32 36 31 38 37 39 22 2c 0d 0a 22  .."06261879",.."
0960: 32 30 32 33 37 35 39 32 22 2c 0d 0a 22 32 38 39  20237592",.."289
0970: 37 33 36 31 32 22 2c 0d 0a 22 39 33 31 39 34 37  73612",.."931947
0980: 38 34 22 0d 0a 7d 3b 0d 0a 09 20 20 76 65 63 74  84"..};...  vect
0990: 6f 72 20 3c 73 74 72 69 6e 67 3e 20 63 68 6f 63  or <string> choc
09a0: 6f 6c 61 74 65 28 63 68 6f 63 6f 6c 61 74 65 5f  olate(chocolate_
09b0: 2c 20 63 68 6f 63 6f 6c 61 74 65 5f 2b 73 69 7a  , chocolate_+siz
09c0: 65 6f 66 28 63 68 6f 63 6f 6c 61 74 65 5f 29 2f  eof(chocolate_)/
09d0: 73 69 7a 65 6f 66 28 2a 63 68 6f 63 6f 6c 61 74  sizeof(*chocolat
09e0: 65 5f 29 29 3b 20 0d 0a 09 69 6e 74 20 5f 20 3d  e_)); ...int _ =
09f0: 20 31 35 3b 20 0d 0a 45 4e 44 0d 0a 43 41 53 45   15; ..END..CASE
0a00: 28 32 29 0d 0a 09 73 74 72 69 6e 67 20 63 68 6f  (2)...string cho
0a10: 63 6f 6c 61 74 65 5f 5b 5d 20 3d 20 7b 0d 0a 22  colate_[] = {.."
0a20: 30 31 32 22 2c 0d 0a 22 33 34 35 22 2c 0d 0a 22  012",.."345",.."
0a30: 36 37 38 22 0d 0a 7d 3b 0d 0a 09 20 20 76 65 63  678"..};...  vec
0a40: 74 6f 72 20 3c 73 74 72 69 6e 67 3e 20 63 68 6f  tor <string> cho
0a50: 63 6f 6c 61 74 65 28 63 68 6f 63 6f 6c 61 74 65  colate(chocolate
0a60: 5f 2c 20 63 68 6f 63 6f 6c 61 74 65 5f 2b 73 69  _, chocolate_+si
0a70: 7a 65 6f 66 28 63 68 6f 63 6f 6c 61 74 65 5f 29  zeof(chocolate_)
0a80: 2f 73 69 7a 65 6f 66 28 2a 63 68 6f 63 6f 6c 61  /sizeof(*chocola
0a90: 74 65 5f 29 29 3b 20 0d 0a 09 69 6e 74 20 5f 20  te_)); ...int _ 
0aa0: 3d 20 30 3b 20 0d 0a 45 4e 44 0d 0a 2f 2a 0d 0a  = 0; ..END../*..
0ab0: 43 41 53 45 28 33 29 0d 0a 09 73 74 72 69 6e 67  CASE(3)...string
0ac0: 20 63 68 6f 63 6f 6c 61 74 65 5f 5b 5d 20 3d 20   chocolate_[] = 
0ad0: 3b 0d 0a 09 20 20 76 65 63 74 6f 72 20 3c 73 74  ;...  vector <st
0ae0: 72 69 6e 67 3e 20 63 68 6f 63 6f 6c 61 74 65 28  ring> chocolate(
0af0: 63 68 6f 63 6f 6c 61 74 65 5f 2c 20 63 68 6f 63  chocolate_, choc
0b00: 6f 6c 61 74 65 5f 2b 73 69 7a 65 6f 66 28 63 68  olate_+sizeof(ch
0b10: 6f 63 6f 6c 61 74 65 5f 29 2f 73 69 7a 65 6f 66  ocolate_)/sizeof
0b20: 28 2a 63 68 6f 63 6f 6c 61 74 65 5f 29 29 3b 20  (*chocolate_)); 
0b30: 0d 0a 09 69 6e 74 20 5f 20 3d 20 3b 20 0d 0a 45  ...int _ = ; ..E
0b40: 4e 44 0d 0a 43 41 53 45 28 34 29 0d 0a 09 73 74  ND..CASE(4)...st
0b50: 72 69 6e 67 20 63 68 6f 63 6f 6c 61 74 65 5f 5b  ring chocolate_[
0b60: 5d 20 3d 20 3b 0d 0a 09 20 20 76 65 63 74 6f 72  ] = ;...  vector
0b70: 20 3c 73 74 72 69 6e 67 3e 20 63 68 6f 63 6f 6c   <string> chocol
0b80: 61 74 65 28 63 68 6f 63 6f 6c 61 74 65 5f 2c 20  ate(chocolate_, 
0b90: 63 68 6f 63 6f 6c 61 74 65 5f 2b 73 69 7a 65 6f  chocolate_+sizeo
0ba0: 66 28 63 68 6f 63 6f 6c 61 74 65 5f 29 2f 73 69  f(chocolate_)/si
0bb0: 7a 65 6f 66 28 2a 63 68 6f 63 6f 6c 61 74 65 5f  zeof(*chocolate_
0bc0: 29 29 3b 20 0d 0a 09 69 6e 74 20 5f 20 3d 20 3b  )); ...int _ = ;
0bd0: 20 0d 0a 45 4e 44 0d 0a 2a 2f 0d 0a 7d 0d 0a 2f   ..END..*/..}../
0be0: 2f 20 45 4e 44 20 43 55 54 20 48 45 52 45 0d 0a  / END CUT HERE..