topcoder srm 610

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

 

div1 500pt:

  题意:有个人有个飞机,起初有F升油,有很多个任务,第i个任务消耗duration[i]油,完成之后得到refuel[i]升油,求最多能完成的任务数。

  解法:先按照refuel降序排列,然后背包。

    why?

    对于两个任务,refuel大的在前面做一定要优于在后面做。假设现在剩余F升油,有两个任务a,b,其中duration[a] > duration[b],如果a先做,那么做完之后会剩余F - duration[a] + refuel[a]升油,如果b先做,会剩余F - duration[b] + refuel[b]升油,也就是说,如果两个都能做完的话,至少要保证F-duration[a]-duraion[b]+refuel[a/b]>=0,

显然先做refuel大的更可能完成。

  

 1 // BEGIN CUT HERE
 2 
 3 // END CUT HERE
 4 #line 5 "AlbertoTheAviator.cpp"
 5 #include<cstdio>
 6 #include<cstring>
 7 #include<cstdlib>
 8 #include<ctime>
 9 #include<cmath>
10 #include<sstream>
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 int dp[55][5050];
24 pii hs[55];
25 bool cmp(pii a,pii b){
26     return a.first > b.first;
27 }
28 class AlbertoTheAviator
29 {
30     public:
31     int MaximumFlights(int F, vector <int> duration, vector <int> refuel){
32     //$CARETPOSITION$
33         int n = duration.size();
34         for(int i = 0;i < n;i++){
35             hs[i].first = refuel[i];
36             hs[i].second = duration[i];
37         }
38         sort(hs,hs+n,cmp);
39         memset(dp,-1,sizeof(dp));
40         dp[0][F] = 0;
41         for(int i = 0;i < n;i++){
42             for(int j=0;j<=5000;j++)dp[i+1][j]=dp[i][j];
43             for(int j=hs[i].second;j <= 5000;j++){
44                 if(dp[i][j] == -1)continue;
45                 dp[i+1][j-hs[i].second+hs[i].first]=max(dp[i+1][j-hs[i].second+hs[i].first],dp[i][j]+1);
46             }
47         }
48 
49 
50         int answer = 0;
51         for(int i = 0;i <= 5000;i++)
52             answer = max(answer,dp[n][i]);
53         return answer;
54 
55 
56 
57     }
58 
59 // BEGIN CUT HERE
60     public:
61     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(); }
62     private:
63     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(); }
64     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; } }
65     void test_case_0() { int Arg0 = 10; int Arr1[] = {10}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {0}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 1; verify_case(0, Arg3, MaximumFlights(Arg0, Arg1, Arg2)); }
66     void test_case_1() { int Arg0 = 10; int Arr1[] = {8, 4}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {0, 2}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 2; verify_case(1, Arg3, MaximumFlights(Arg0, Arg1, Arg2)); }
67     void test_case_2() { int Arg0 = 12; int Arr1[] = {4, 8, 2, 1}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {2, 0, 0, 0}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 3; verify_case(2, Arg3, MaximumFlights(Arg0, Arg1, Arg2)); }
68     void test_case_3() { int Arg0 = 9; int Arr1[] = {4, 6}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {0, 1}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 2; verify_case(3, Arg3, MaximumFlights(Arg0, Arg1, Arg2)); }
69     void test_case_4() { int Arg0 = 100; int Arr1[] = {101}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {100}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 0; verify_case(4, Arg3, MaximumFlights(Arg0, Arg1, Arg2)); }
70     void test_case_5() { int Arg0 = 1947; int Arr1[] = {2407, 2979, 1269, 2401, 3227, 2230, 3991, 2133, 3338, 356, 2535, 3859, 3267, 365}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {2406, 793, 905, 2400, 1789, 2229, 1378, 2132, 1815, 355, 72, 3858, 3266, 364}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 3; verify_case(5, Arg3, MaximumFlights(Arg0, Arg1, Arg2)); }
71 
72 // END CUT HERE
73 
74 };
75 // BEGIN CUT HERE
76 int main(){
77     AlbertoTheAviator ___test;
78     ___test.run_test(-1);
79     return 0;
80 }
81 // END CUT HERE
500pt

 

 

  

posted @ 2014-03-04 19:48  silver__bullet  阅读(554)  评论(0编辑  收藏  举报