div1 250pt:

  题意:100*100的01矩阵,找出来面积最大的“类似国际象棋棋盘”的子矩阵。

  解法:枚举矩阵宽(水平方向)的起点和终点,然后利用尺取法来找到每个固定宽度下的最大矩阵,不断更新答案。

  1 // BEGIN CUT HERE
  2 
  3 // END CUT HERE
  4 #line 5 "TheMatrix.cpp"
  5 #include<cstdio>
  6 #include<sstream>
  7 #include<cstring>
  8 #include<cstdlib>
  9 #include<ctime>
 10 #include<cmath>
 11 #include<cassert>
 12 #include<iostream>
 13 #include<string>
 14 #include<vector>
 15 #include<map>
 16 #include<set>
 17 #include<queue>
 18 #include<stack>
 19 #include<algorithm>
 20 using namespace std;
 21 typedef long long ll;
 22 typedef pair<int,int> pii;
 23 class TheMatrix
 24 {
 25     public:
 26     bool check(vector<string>& s,int start,int end,int row){
 27         for(int i = start + 1;i <=end;i++)
 28             if(s[row][i]==s[row][i-1])return false;
 29         return true;
 30     }
 31     bool ok(vector<string>& s,int start,int end,int row){
 32         for(int i=start;i<=end;i++)
 33             if(s[row][i]==s[row-1][i])return false;
 34         return true;
 35     }
 36     int MaxArea(vector <string> s){
 37     //$CARETPOSITION$
 38         int answer = 0;
 39         int n=s.size(),m=s[0].size();
 40         for(int i=0;i<m;i++)
 41             for(int j=0;j<m;j++){
 42                 int down=0,up=0;
 43                 for(down=0;down<n;down++){
 44                     if(check(s,i,j,down)){
 45                         up=down+1;
 46                         while(up<n&&ok(s,i,j,up))up++;
 47                         int area=(j-i+1)*(up-down);
 48                         answer=max(answer,area);
 49                         down=up-1;
 50                     }
 51                 }
 52             }
 53         return answer;
 54     }
 55 
 56 // BEGIN CUT HERE
 57     public:
 58     void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); if ((Case == -1) || (Case == 6)) test_case_6(); if ((Case == -1) || (Case == 7)) test_case_7(); }
 59     private:
 60     template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
 61     void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
 62     void test_case_0() { string Arr0[] = {"1",
 63  "0"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2; verify_case(0, Arg1, MaxArea(Arg0)); }
 64     void test_case_1() { string Arr0[] = {"0000"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; verify_case(1, Arg1, MaxArea(Arg0)); }
 65     void test_case_2() { string Arr0[] = {"01"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2; verify_case(2, Arg1, MaxArea(Arg0)); }
 66     void test_case_3() { string Arr0[] = {"001",
 67  "000",
 68  "100"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2; verify_case(3, Arg1, MaxArea(Arg0)); }
 69     void test_case_4() { string Arr0[] = {"0"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; verify_case(4, Arg1, MaxArea(Arg0)); }
 70     void test_case_5() { string Arr0[] = {"101",
 71  "010"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 6; verify_case(5, Arg1, MaxArea(Arg0)); }
 72     void test_case_6() { string Arr0[] = {"101",
 73  "011",
 74  "101",
 75  "010"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 8; verify_case(6, Arg1, MaxArea(Arg0)); }
 76     void test_case_7() { string Arr0[] = {"11001110011000110001111001001110110011010110001011",
 77  "10100100010111111011111001011110101111010011100001",
 78  "11101111001110100110010101101100011100101000010001",
 79  "01000010001010101100010011111000100100110111111000",
 80  "10110100000101100000111000100001011101111101010010",
 81  "00111010000011100001110110010011010110010011100100",
 82  "01100001111101001101001101100001111000111001101010",
 83  "11010000000011011010100010000000111011001001100101",
 84  "10100000000100010100100011010100110110110001000001",
 85  "01101010101100001100000110100110100000010100100010",
 86  "11010000001110111111011010011110001101100011100010",
 87  "11101111000000011010011100100001100011111111110111",
 88  "11000001101100100011000110111010011001010100000001",
 89  "00100001111001010000101101100010000001100100001000",
 90  "01001110110111101011010000111111101011000110010111",
 91  "01001010000111111001100000100010101100100101010100",
 92  "11111101001101110011011011011000111001101100011011",
 93  "10000100110111000001110110010000000000111100101101",
 94  "01010011101101101110000011000110011111001111011100",
 95  "01101010011111010000011001111101011010011100001101",
 96  "11011000011000110010101111100000101011011111101100",
 97  "11100001001000110010100011001010101101001010001100",
 98  "11011011001100111101001100111100000101011101101011",
 99  "11110111100100101011100101111101000111001111110111",
100  "00011001100110111100111100001100101001111100001111",
101  "10001111100101110111001111100000000011110000100111",
102  "10101010110110100110010001001010000111100110100011",
103  "01100110100000001110101001101011001010001101110101",
104  "10110101110100110110101001100111110000101111100110",
105  "01011000001001101110100001101001110011001001110001",
106  "00100101010001100110110101001010010100001011000011",
107  "00011101100100001010100000000011000010100110011100",
108  "11001001011000000101111111000000110010001101101110",
109  "10101010110110010000010011001100110101110100111011",
110  "01101001010111010001101000100011101001110101000110",
111  "00110101101110110001110101110010100100110000101101",
112  "11010101000111010011110011000001101111010011110011",
113  "10010000010001110011011101001110110010001100011100",
114  "00111101110001001100101001110100110010100110110000",
115  "00010011011000101000100001101110111100100000010100",
116  "01101110001101000001001000001011101010011101011110",
117  "00000100110011001011101011110011011101100001110111",
118  "00110011110000011001011100001110101010100110010110",
119  "00111001010011011111010100000100100000101101110001",
120  "10101101101110111110000011111011001011100011110001",
121  "00101110010101111000001010110100001110111011100011",
122  "01111110010100111010110001111000111101110100111011"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 12; verify_case(7, Arg1, MaxArea(Arg0)); }
123 
124 // END CUT HERE
125 
126 };
127 // BEGIN CUT HERE
128 int main(){
129     TheMatrix ___test;
130     ___test.run_test(-1);
131     return 0;
132 }
133 // END CUT HERE
250pt

相关文章: