div1 250pt:

  题意:用RGB三种颜色的球摆N层的三角形,要求相邻的不同色,给出RGB的数量,问最多能摆几个

  解法:三种颜色的数量要么是全一样,要么是两个一样,另外一个比他们多一个,于是可以分类讨论:对于数量全一样的,直接算;对于另外的,我们可以先不考虑多出来的那一个,也是正常放,然后看最后剩下的位置能不能放完所有多出来的那一个,这个可以二分。

 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
250pt

相关文章: