AtCoder ABC 159E Dividing Chocolate

Topic links: https://atcoder.jp/contests/abc159/tasks/abc159_e

Subject to the effect

  There is a rectangular region W is $ H $ $ $ row by row $ H * S $ small squares in which white and black small square, rectangular area needs to be resolved now, can only cut the vertical or horizontal shear (not turning, all in the end only), cut at least ask a few times, to make cut out each piece in the number of white squares equal to or less than $ K $ months?

analysis

  Given by the subject can be found in the data, the number of lines less, it is possible to enumerate all horizontal shear force method, and then the vertical direction may be resolved using a greedy.

code show as below

  . 1 #include <bits / STDC ++ H.>
   2  the using  namespace STD;
   . 3  
  . 4  / * ------------------- the Define the Start ---------- --------- * / 
  . 5 typedef BOOL BL;                         // boolean 
  . 6 typedef char SB;                         // signed 1 byte, 8 
  . 7 typedef unsigned char UB;                 // unsigned byte 1, 8 bit 
  . 8 typedef short SW;                         // signed short, 16 
  . 9 typedef unsigned shortThe UW;                 // unsigned short, 16 
10 typedef Long SDW;                         // signed integer, 32-bit 
. 11 typedef unsigned Long UDWs;                // unsigned integer, 32-bit 
12 is typedef Long  Long SLL;                     // there unsigned long integer, 64-bit 
13 is typedef unsigned long  long ULL;             // unsigned long, 64 
14 typedef char CH;                         // single character 
15 typedef a float R32;                        // 单精度浮点数
 16 typedef double R64;                        // 双精度浮点数
 17 
 18 #define Rep(i, n) for (register SDW i = 0; i < (n); ++i)
 19 #define For(i, s, t) for (register SDW i = (s); i <= (t); ++i)
 20 #define rFor(i, t, s) for (register SDW i = (t); i >= (s); --i)
 21 #define foreach(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
 22 #define ms0(a) memset(a,0,sizeof(a))
 23 #define msI(a) memset(a,0x7f,sizeof(a))
 24 #define LOWBIT(x) ((x)&(-x))
 25 
 26 #define MP make_pair
 27 #define PB push_back
 28 #define ft first
 29 #define sd second
 30 
 31 #define pr(x) cout << #x << " = " << x << "  "
 32 #define prln(x) cout << #x << " = " << x << endl
 33 
 34 const ULL mod = 1e9 + 7;                //常用模数(可根据题目需要修改)
 35 const ULL inf = 0x7fffffff;                const36used to represent infinity//
 0x7fffffffffffffffLL infLL = ULL;     // used to represent infinity 
37 [  / * ------------------- -------------- the Define End ----- * / 
38 is  
39  const UDWs maxN = + 1E3 . 7 ;
 40  SDW H, W is, K;
 41 is SDW S [ 13 is ] [maxN];
 42 is SDW ANS = INF;
 43 is   
44 is  SDW GetSum (SDW X1, SDW Y1, SDW X2, SDW Y2) {
 45      return S [X2] [Y2] - S [X2] [Y1 - . 1 ] - S [X1 - . 1 ] [Y2] + S [X1 - . 1 ] [Y1 - . 1 ] ;
 46  }
 47  
48 void INPUT () {
 49      CIN H >> >> >> W is K;
 50      the For (I, . 1 , H) {
 51 is          the For (J, . 1 , W is) {
 52 is              Scanf ( " % 1D " , & S [I] [J]);
 53 is              S [I] [J] = S + [I - . 1 ] [J] + S [I] [J - . 1 ] - S [I - . 1 ] [J - . 1 ]; // record prefix and facilitate the calculation 
54 is          }
 55      }
 56 is  }
 57 is  
58  void Solve () {
 59      //All enumeration cutting method on the horizontal direction 
60      the Rep (I, . 1 << (H - . 1 )) { 
 61 is          // use greedy cutting method in the vertical direction 
62 is          Vector <SDW> POS; // record the position of the horizontal cut 
63 is          SDW tmp = I;
 64          SDW RET = __builtin_popcount (I);
 65          
66          pos.PB ( 0 );
 67          the while (tmp) {
 68              SDW X = LOWBIT (tmp);
 69              tmp = tmp & - . 1 ;
 70              
71 is              POS .pb (__ builtin_ctz (X) + . 1 );
72          }
 73 is          pos.PB (H);
 74          
75          SDW A = . 1 , B = . 1 ;
 76          
77          the while (B <= W is) {
 78              the For (J, . 1 , pos.size () - . 1 ) {
 79                  IF ( GetSum (POS [J - . 1 ] + . 1 , a, POS [J], B)> K) {
 80                      IF (a == B) { // case not cut directly out of 
81                          B = W is + . 1 ;
 82                          RET = INF;
 83                         break;
 84                     }
 85                     else {
 86                         ++ret;
 87                         a = b;
 88                         --b;
 89                         break;
 90                     }
 91                 }
 92             }
 93             ++b;
 94         }
 95         
 96         ans = min(ans, ret);
 97     }
 98 }
 99 
100 void output(){
101     cout << ans << endl;
102 }
103 
104 int main() {
105     input();
106     solve();
107     output();
108     return 0;
109 }
View Code

 

Guess you like

Origin www.cnblogs.com/zaq19970105/p/12585814.html