topcoder srm 552
div1 250pt:
题意:用RGB三种颜色的球摆N层的三角形,要求相邻的不同色,给出RGB的数量,问最多能摆几个
解法:三种颜色的数量要么是全一样,要么是两个一样,另外一个比他们多一个,于是可以分类讨论:对于数量全一样的,直接算;对于另外的,我们可以先不考虑多出来的那一个,也是正常放,然后看最后剩下的位置能不能放完所有多出来的那一个,这个可以二分。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 // BEGIN CUT HERE 2 3 // END CUT HERE 4 #line 5 "FoxPaintingBalls.cpp" 5 #include<cstdio> 6 #include<cstring> 7 #include<cstdlib> 8 #include<ctime> 9 #include<cmath> 10 #include<cassert> 11 #include<iostream> 12 #include<string> 13 #include<sstream> 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 FoxPaintingBalls 24 { 25 public: 26 long long theMax(long long R, long long G, long long B, int N){ 27 //$CARETPOSITION$ 28 if(N == 1)return R+G+B; 29 ll total = 1LL*N*(N + 1) / 2; 30 ll least = total / 3; 31 ll maxn = min(min(R,G),B) / least; 32 if(total % 3 == 0)return maxn; 33 ll answer = 0,left = 0,right = maxn; 34 while(left <= right){ 35 // cout << "L: "<<l<<" R: "<<r<<endl; 36 ll mid = (left + right) >> 1; 37 ll r = R - mid * least,g = G - mid * least,b = B - mid * least; 38 if((r + g + b) >= mid){ 39 answer = mid; 40 left = mid + 1; 41 }else{ 42 right = mid - 1; 43 } 44 } 45 return answer; 46 47 48 49 50 } 51 52 // BEGIN CUT HERE 53 public: 54 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(); } 55 private: 56 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(); } 57 void verify_case(int Case, const long long &Expected, const long long &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } 58 void test_case_0() { long long Arg0 = 2LL; long long Arg1 = 2LL; long long Arg2 = 2LL; int Arg3 = 3; long long Arg4 = 1LL; verify_case(0, Arg4, theMax(Arg0, Arg1, Arg2, Arg3)); } 59 void test_case_1() { long long Arg0 = 1LL; long long Arg1 = 2LL; long long Arg2 = 3LL; int Arg3 = 3; long long Arg4 = 0LL; verify_case(1, Arg4, theMax(Arg0, Arg1, Arg2, Arg3)); } 60 void test_case_2() { long long Arg0 = 8LL; long long Arg1 = 6LL; long long Arg2 = 6LL; int Arg3 = 4; long long Arg4 = 2LL; verify_case(2, Arg4, theMax(Arg0, Arg1, Arg2, Arg3)); } 61 void test_case_3() { long long Arg0 = 7LL; long long Arg1 = 6LL; long long Arg2 = 7LL; int Arg3 = 4; long long Arg4 = 2LL; verify_case(3, Arg4, theMax(Arg0, Arg1, Arg2, Arg3)); } 62 void test_case_4() { long long Arg0 = 100LL; long long Arg1 = 100LL; long long Arg2 = 100LL; int Arg3 = 4; long long Arg4 = 30LL; verify_case(4, Arg4, theMax(Arg0, Arg1, Arg2, Arg3)); } 63 void test_case_5() { long long Arg0 = 19330428391852493LL; long long Arg1 = 48815737582834113LL; long long Arg2 = 11451481019198930LL; int Arg3 = 3456; long long Arg4 = 5750952686LL; verify_case(5, Arg4, theMax(Arg0, Arg1, Arg2, Arg3)); } 64 void test_case_6() { long long Arg0 = 1LL; long long Arg1 = 1LL; long long Arg2 = 1LL; int Arg3 = 1; long long Arg4 = 3LL; verify_case(6, Arg4, theMax(Arg0, Arg1, Arg2, Arg3)); } 65 66 // END CUT HERE 67 68 }; 69 // BEGIN CUT HERE 70 int main(){ 71 FoxPaintingBalls ___test; 72 ___test.run_test(-1); 73 return 0; 74 } 75 // END CUT HERE
div 500pt:
题意:N*N的格子里有两种颜色的花,L,P给出一个maxDiff,要求找出来两个不相交的矩形,使得他们两个中L,P的数量之差<=maxDiff,并且数量最多
解法:枚举分割线,然后预处理,L[i][j]表示i条线左侧差为j的最大数量,其余同理,最后枚举算一下。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 // BEGIN CUT HERE 2 3 // END CUT HERE 4 #line 5 "FoxAndFlowerShopDivOne.cpp" 5 #include<cstdio> 6 #include<cstring> 7 #include<cstdlib> 8 #include<ctime> 9 #include<cmath> 10 #include<cassert> 11 #include<iostream> 12 #include<string> 13 #include<sstream> 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 const int N = 35; 24 inline void checkmax(int &a,int b){ 25 if(a == -1 || a < b)a = b; 26 } 27 int L[N][N*N*2],R[N][N*N*2],U[N][N*N*2],D[N][N*N*2]; 28 class FoxAndFlowerShopDivOne 29 { 30 public: 31 int theMaxFlowers(vector <string> flowers, int maxDiff){ 32 //$CARETPOSITION$ 33 memset(L,-1,sizeof(L)); 34 memset(R,-1,sizeof(R)); 35 memset(U,-1,sizeof(U)); 36 memset(D,-1,sizeof(D)); 37 int n=flowers.size(),m=flowers[0].size(); 38 for(int top=0;top<n;top++){ 39 for(int bottom=top;bottom<n;bottom++){ 40 for(int left=0;left<m;left++){ 41 for(int right=left;right<m;right++){ 42 int total = 0,d = 0; 43 for(int i=top;i<=bottom;i++){ 44 for(int j=left;j<=right;j++){ 45 total += flowers[i][j]!='.'; 46 d += flowers[i][j]=='L'; 47 d -= flowers[i][j]=='P'; 48 } 49 } 50 for(int i=right+1;i<m;i++) 51 checkmax(L[i][d+n*m],total); 52 for(int i=left;i>=0;i--) 53 checkmax(R[i][d+n*m],total); 54 for(int i=bottom+1;i<n;i++) 55 checkmax(U[i][d+n*m],total); 56 for(int i=top;i>=0;i--) 57 checkmax(D[i][d+n*m],total); 58 } 59 } 60 } 61 } 62 int answer = -1; 63 for(int i=1;i<n;i++){ 64 for(int j=0;j<=n*m*2;j++){ 65 for(int k=0;k<=n*m*2;k++){ 66 if(abs(j+k-n*m*2)<=maxDiff && U[i][j]!=-1 && D[i][k] !=-1) 67 checkmax(answer,U[i][j]+D[i][k]); 68 } 69 } 70 } 71 for(int i=1;i<m;i++){ 72 for(int j=0;j<=n*m*2;j++){ 73 for(int k=0;k<n*m*2;k++){ 74 if(abs(j+k-n*m*2)<=maxDiff && L[i][j] != -1 && R[i][k] != -1) 75 checkmax(answer,L[i][j]+R[i][k]); 76 } 77 } 78 } 79 return answer; 80 } 81 82 // BEGIN CUT HERE 83 public: 84 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(); } 85 private: 86 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(); } 87 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; } } 88 void test_case_0() { string Arr0[] = {"LLL", 89 "PPP", 90 "LLL"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; int Arg2 = 7; verify_case(0, Arg2, theMaxFlowers(Arg0, Arg1)); } 91 void test_case_1() { string Arr0[] = {"LLL", 92 "PPP", 93 "LLL"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 0; int Arg2 = 6; verify_case(1, Arg2, theMaxFlowers(Arg0, Arg1)); } 94 void test_case_2() { string Arr0[] = {"...", 95 "...", 96 "..."}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 3; int Arg2 = 0; verify_case(2, Arg2, theMaxFlowers(Arg0, Arg1)); } 97 void test_case_3() { string Arr0[] = {"LLPL.LPP", 98 "PLPPPPLL", 99 "L.P.PLLL", 100 "LPL.PP.L", 101 ".LLL.P.L", 102 "PPLP..PL"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2; int Arg2 = 38; verify_case(3, Arg2, theMaxFlowers(Arg0, Arg1)); } 103 void test_case_4() { string Arr0[] = {"LLLLLLLLLL", 104 "LLLLLLLLLL", 105 "LLLLLLLLLL", 106 "LLLLLLLLLL", 107 "LLLLLLLLLL"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 0; int Arg2 = -1; verify_case(4, Arg2, theMaxFlowers(Arg0, Arg1)); } 108 void test_case_5() { string Arr0[] = {"LLLP..LLP.PLL.LL..LP", 109 "L.PL.L.LLLL.LPLLPLP.", 110 "PLL.LL.LLL..PL...L..", 111 ".LPPP.PPPLLLLPLP..PP", 112 "LP.P.PPL.L...P.L.LLL", 113 "L..LPLPP.PP...PPPL..", 114 "PP.PLLL.LL...LP..LP.", 115 "PL...P.PPPL..PLP.L..", 116 "P.PPPLPLP.LL.L.LLLPL", 117 "PLLPLLP.LLL.P..P.LPL", 118 "..LLLPLPPPLP.P.LP.LL", 119 "..LP..L..LLPPP.LL.LP", 120 "LPLL.PLLPPLP...LL..P", 121 "LL.....PLL.PLL.P....", 122 "LLL...LPPPPL.PL...PP", 123 ".PLPLLLLP.LPP...L...", 124 "LL...L.LL.LLLPLPPPP.", 125 "PLPLLLL..LP.LLPLLLL.", 126 "PP.PLL..L..LLLPPL..P", 127 ".LLPL.P.PP.P.L.PLPLL"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 9; int Arg2 = 208; verify_case(5, Arg2, theMaxFlowers(Arg0, Arg1)); } 128 129 // END CUT HERE 130 131 }; 132 // BEGIN CUT HERE 133 int main(){ 134 FoxAndFlowerShopDivOne ___test; 135 ___test.run_test(-1); 136 return 0; 137 } 138 // END CUT HERE