Hex Artifact Content
Not logged in

Artifact 403128c17a6bd8b1d739fa5f5ccec53e324694ae:


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 63 73 74 72 69 6e 67 3e 0d 0a 75 73 69 6e 67  <cstring>..using
0150: 20 6e 61 6d 65 73 70 61 63 65 20 73 74 64 3b 0d   namespace std;.
0160: 0a 74 79 70 65 64 65 66 20 6c 6f 6e 67 20 6c 6f  .typedef long lo
0170: 6e 67 20 4c 4c 3b 0d 0a 74 79 70 65 64 65 66 20  ng LL;..typedef 
0180: 63 6f 6d 70 6c 65 78 3c 64 6f 75 62 6c 65 3e 20  complex<double> 
0190: 43 4d 50 3b 0d 0a 0d 0a 63 6c 61 73 73 20 50 6f  CMP;....class Po
01a0: 75 72 69 6e 67 57 61 74 65 72 20 7b 0d 0a 70 75  uringWater {..pu
01b0: 62 6c 69 63 3a 0d 0a 09 69 6e 74 20 6e 62 69 74  blic:...int nbit
01c0: 73 28 69 6e 74 20 6e 29 20 7b 0d 0a 09 09 69 6e  s(int n) {....in
01d0: 74 20 63 6e 74 20 3d 20 30 3b 0d 0a 09 09 77 68  t cnt = 0;....wh
01e0: 69 6c 65 28 6e 29 20 7b 0d 0a 09 09 09 69 66 28  ile(n) {.....if(
01f0: 6e 26 31 29 20 2b 2b 63 6e 74 3b 0d 0a 09 09 09  n&1) ++cnt;.....
0200: 6e 3e 3e 3d 31 3b 0d 0a 09 09 7d 0d 0a 09 09 72  n>>=1;....}....r
0210: 65 74 75 72 6e 20 63 6e 74 3b 0d 0a 09 7d 0d 0a  eturn cnt;...}..
0220: 09 69 6e 74 20 67 65 74 4d 69 6e 42 6f 74 74 6c  .int getMinBottl
0230: 65 73 28 69 6e 74 20 4e 2c 20 69 6e 74 20 4b 29  es(int N, int K)
0240: 20 0d 0a 09 7b 0d 0a 09 09 66 6f 72 28 69 6e 74   ...{....for(int
0250: 20 69 3d 30 3b 3b 2b 2b 69 29 0d 0a 09 09 09 69   i=0;;++i).....i
0260: 66 28 20 6e 62 69 74 73 28 4e 2b 69 29 20 3c 3d  f( nbits(N+i) <=
0270: 20 4b 20 29 0d 0a 09 09 09 09 72 65 74 75 72 6e   K )......return
0280: 20 69 3b 0d 0a 09 7d 0d 0a 7d 3b 0d 0a 0d 0a 2f   i;...}..};..../
0290: 2f 20 42 45 47 49 4e 20 43 55 54 20 48 45 52 45  / BEGIN CUT HERE
02a0: 0d 0a 23 69 6e 63 6c 75 64 65 20 3c 63 74 69 6d  ..#include <ctim
02b0: 65 3e 0d 0a 64 6f 75 62 6c 65 20 73 74 61 72 74  e>..double start
02c0: 5f 74 69 6d 65 3b 73 74 72 69 6e 67 20 74 69 6d  _time;string tim
02d0: 65 72 28 29 20 7b 20 6f 73 74 72 69 6e 67 73 74  er() { ostringst
02e0: 72 65 61 6d 20 6f 73 3b 20 6f 73 20 3c 3c 20 22  ream os; os << "
02f0: 20 28 22 20 3c 3c 20 69 6e 74 28 28 63 6c 6f 63   (" << int((cloc
0300: 6b 28 29 2d 73 74 61 72 74 5f 74 69 6d 65 29 2f  k()-start_time)/
0310: 43 4c 4f 43 4b 53 5f 50 45 52 5f 53 45 43 2a 31  CLOCKS_PER_SEC*1
0320: 30 30 30 29 20 3c 3c 20 22 20 6d 73 65 63 29 22  000) << " msec)"
0330: 3b 20 72 65 74 75 72 6e 20 6f 73 2e 73 74 72 28  ; return os.str(
0340: 29 3b 20 7d 0d 0a 0d 0a 74 65 6d 70 6c 61 74 65  ); }....template
0350: 20 3c 74 79 70 65 6e 61 6d 65 20 54 3e 20 73 74   <typename T> st
0360: 72 69 6e 67 20 70 72 69 6e 74 5f 61 72 72 61 79  ring print_array
0370: 28 63 6f 6e 73 74 20 76 65 63 74 6f 72 3c 54 3e  (const vector<T>
0380: 20 26 56 29 20 7b 20 6f 73 74 72 69 6e 67 73 74   &V) { ostringst
0390: 72 65 61 6d 20 6f 73 3b 20 6f 73 20 3c 3c 20 22  ream os; os << "
03a0: 7b 20 22 3b 20 66 6f 72 20 28 74 79 70 65 6e 61  { "; for (typena
03b0: 6d 65 20 76 65 63 74 6f 72 3c 54 3e 3a 3a 63 6f  me vector<T>::co
03c0: 6e 73 74 5f 69 74 65 72 61 74 6f 72 20 69 74 65  nst_iterator ite
03d0: 72 20 3d 20 56 2e 62 65 67 69 6e 28 29 3b 20 69  r = V.begin(); i
03e0: 74 65 72 20 21 3d 20 56 2e 65 6e 64 28 29 3b 20  ter != V.end(); 
03f0: 2b 2b 69 74 65 72 29 20 6f 73 20 3c 3c 20 27 5c  ++iter) os << '\
0400: 22 27 20 3c 3c 20 2a 69 74 65 72 20 3c 3c 20 22  "' << *iter << "
0410: 5c 22 2c 22 3b 20 6f 73 20 3c 3c 20 22 20 7d 22  \","; os << " }"
0420: 3b 20 72 65 74 75 72 6e 20 6f 73 2e 73 74 72 28  ; return os.str(
0430: 29 3b 20 7d 0d 0a 69 6e 74 20 76 65 72 69 66 79  ); }..int verify
0440: 5f 63 61 73 65 28 63 6f 6e 73 74 20 69 6e 74 20  _case(const int 
0450: 26 45 78 70 65 63 74 65 64 2c 20 63 6f 6e 73 74  &Expected, const
0460: 20 69 6e 74 20 26 52 65 63 65 69 76 65 64 29 20   int &Received) 
0470: 7b 20 69 66 20 28 45 78 70 65 63 74 65 64 20 3d  { if (Expected =
0480: 3d 20 52 65 63 65 69 76 65 64 29 20 63 65 72 72  = Received) cerr
0490: 20 3c 3c 20 22 50 41 53 53 45 44 22 20 3c 3c 20   << "PASSED" << 
04a0: 74 69 6d 65 72 28 29 20 3c 3c 20 65 6e 64 6c 3b  timer() << endl;
04b0: 20 65 6c 73 65 20 7b 20 63 65 72 72 20 3c 3c 20   else { cerr << 
04c0: 22 46 41 49 4c 45 44 22 20 3c 3c 20 74 69 6d 65  "FAILED" << time
04d0: 72 28 29 20 3c 3c 20 65 6e 64 6c 3b 20 63 65 72  r() << endl; cer
04e0: 72 20 3c 3c 20 22 5c 74 45 78 70 65 63 74 65 64  r << "\tExpected
04f0: 3a 20 5c 22 22 20 3c 3c 20 45 78 70 65 63 74 65  : \"" << Expecte
0500: 64 20 3c 3c 20 27 5c 22 27 20 3c 3c 20 65 6e 64  d << '\"' << end
0510: 6c 3b 20 63 65 72 72 20 3c 3c 20 22 5c 74 52 65  l; cerr << "\tRe
0520: 63 65 69 76 65 64 3a 20 5c 22 22 20 3c 3c 20 52  ceived: \"" << R
0530: 65 63 65 69 76 65 64 20 3c 3c 20 27 5c 22 27 20  eceived << '\"' 
0540: 3c 3c 20 65 6e 64 6c 3b 20 7d 20 72 65 74 75 72  << endl; } retur
0550: 6e 20 30 3b 7d 0d 0a 0d 0a 74 65 6d 70 6c 61 74  n 0;}....templat
0560: 65 3c 69 6e 74 20 4e 3e 20 73 74 72 75 63 74 20  e<int N> struct 
0570: 43 61 73 65 5f 20 7b 20 43 61 73 65 5f 28 29 7b  Case_ { Case_(){
0580: 73 74 61 72 74 5f 74 69 6d 65 3d 63 6c 6f 63 6b  start_time=clock
0590: 28 29 3b 7d 20 7d 3b 0d 0a 63 68 61 72 20 54 65  ();} };..char Te
05a0: 73 74 5f 28 2e 2e 2e 29 3b 0d 0a 69 6e 74 20 54  st_(...);..int T
05b0: 65 73 74 5f 28 43 61 73 65 5f 3c 30 3e 29 20 7b  est_(Case_<0>) {
05c0: 0d 0a 09 69 6e 74 20 4e 20 3d 20 33 3b 20 0d 0a  ...int N = 3; ..
05d0: 09 69 6e 74 20 4b 20 3d 20 31 3b 20 0d 0a 09 69  .int K = 1; ...i
05e0: 6e 74 20 52 65 74 56 61 6c 20 3d 20 31 3b 20 0d  nt RetVal = 1; .
05f0: 0a 09 72 65 74 75 72 6e 20 76 65 72 69 66 79 5f  ..return verify_
0600: 63 61 73 65 28 52 65 74 56 61 6c 2c 20 50 6f 75  case(RetVal, Pou
0610: 72 69 6e 67 57 61 74 65 72 28 29 2e 67 65 74 4d  ringWater().getM
0620: 69 6e 42 6f 74 74 6c 65 73 28 4e 2c 20 4b 29 29  inBottles(N, K))
0630: 3b 20 7d 0d 0a 69 6e 74 20 54 65 73 74 5f 28 43  ; }..int Test_(C
0640: 61 73 65 5f 3c 31 3e 29 20 7b 0d 0a 09 69 6e 74  ase_<1>) {...int
0650: 20 4e 20 3d 20 31 33 3b 20 0d 0a 09 69 6e 74 20   N = 13; ...int 
0660: 4b 20 3d 20 32 3b 20 0d 0a 09 69 6e 74 20 52 65  K = 2; ...int Re
0670: 74 56 61 6c 20 3d 20 33 3b 20 0d 0a 09 72 65 74  tVal = 3; ...ret
0680: 75 72 6e 20 76 65 72 69 66 79 5f 63 61 73 65 28  urn verify_case(
0690: 52 65 74 56 61 6c 2c 20 50 6f 75 72 69 6e 67 57  RetVal, PouringW
06a0: 61 74 65 72 28 29 2e 67 65 74 4d 69 6e 42 6f 74  ater().getMinBot
06b0: 74 6c 65 73 28 4e 2c 20 4b 29 29 3b 20 7d 0d 0a  tles(N, K)); }..
06c0: 69 6e 74 20 54 65 73 74 5f 28 43 61 73 65 5f 3c  int Test_(Case_<
06d0: 32 3e 29 20 7b 0d 0a 09 69 6e 74 20 4e 20 3d 20  2>) {...int N = 
06e0: 31 30 30 30 30 30 30 3b 20 0d 0a 09 69 6e 74 20  1000000; ...int 
06f0: 4b 20 3d 20 35 3b 20 0d 0a 09 69 6e 74 20 52 65  K = 5; ...int Re
0700: 74 56 61 6c 20 3d 20 31 35 38 30 38 3b 20 0d 0a  tVal = 15808; ..
0710: 09 72 65 74 75 72 6e 20 76 65 72 69 66 79 5f 63  .return verify_c
0720: 61 73 65 28 52 65 74 56 61 6c 2c 20 50 6f 75 72  ase(RetVal, Pour
0730: 69 6e 67 57 61 74 65 72 28 29 2e 67 65 74 4d 69  ingWater().getMi
0740: 6e 42 6f 74 74 6c 65 73 28 4e 2c 20 4b 29 29 3b  nBottles(N, K));
0750: 20 7d 0d 0a 0d 0a 74 65 6d 70 6c 61 74 65 3c 69   }....template<i
0760: 6e 74 20 4e 3e 20 76 6f 69 64 20 52 75 6e 5f 28  nt N> void Run_(
0770: 29 20 7b 20 63 65 72 72 20 3c 3c 20 22 54 65 73  ) { cerr << "Tes
0780: 74 20 43 61 73 65 20 23 22 20 3c 3c 20 4e 20 3c  t Case #" << N <
0790: 3c 20 22 2e 2e 2e 22 20 3c 3c 20 66 6c 75 73 68  < "..." << flush
07a0: 3b 20 54 65 73 74 5f 28 43 61 73 65 5f 3c 4e 3e  ; Test_(Case_<N>
07b0: 28 29 29 3b 20 52 75 6e 5f 3c 73 69 7a 65 6f 66  ()); Run_<sizeof
07c0: 28 54 65 73 74 5f 28 43 61 73 65 5f 3c 4e 2b 31  (Test_(Case_<N+1
07d0: 3e 28 29 29 29 3d 3d 31 20 3f 20 2d 31 20 3a 20  >()))==1 ? -1 : 
07e0: 4e 2b 31 3e 28 29 3b 20 7d 0d 0a 74 65 6d 70 6c  N+1>(); }..templ
07f0: 61 74 65 3c 3e 20 20 20 20 20 20 76 6f 69 64 20  ate<>      void 
0800: 52 75 6e 5f 3c 2d 31 3e 28 29 20 7b 7d 0d 0a 69  Run_<-1>() {}..i
0810: 6e 74 20 6d 61 69 6e 28 29 20 7b 20 52 75 6e 5f  nt main() { Run_
0820: 3c 30 3e 28 29 3b 20 7d 0d 0a 2f 2f 20 45 4e 44  <0>(); }..// END
0830: 20 43 55 54 20 48 45 52 45 0d 0a 0d 0a            CUT HERE....