250p:暴力。。。直接O(n^5)。。n<=50。
刚开始在想O(n^3)算法。。。搞了半天没搞出,最后还是用最暴力的方法写的,下次应该记得直接超暴力!
#include <vector> #include <list> #include <map> #include <set> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> #include <string.h> using namespace std; class Painting { public: int largestBrush(vector <string>); }; int min (int a, int b) { return a > b ? b : a; } int max (int a, int b) { return a > b ? a : b; } bool vis[55][55]; int Painting::largestBrush(vector <string> p) { int i, j, k, l, o; int n = p.size(); int m = p[0].size(); int tot = min(n, m); for(i = tot;i >= 1 ;i--) { memset(vis, 0, sizeof(vis)); for(j = 0;j < n; j ++) { for(k = 0;k < m; k++) { if(j+i <= n && k+i <= m) { int ok = 1; for(l = j;l < j+i;l++) { for(o = k;o < k+i;o ++) { if(p[l][o] == 'W') ok = 0; } } if(ok) { for(l = j;l < j+i;l++) { for(o = k;o < k+i;o ++) { vis[l][o] = 1; } } } } } } int ok = 1; for(j = 0;j < n; j++) { for(k = 0;k < m; k++) { if(p[j][k] == 'B' && !vis[j][k]) ok = 0; } } if(ok) return i; } } // Powered by FileEdit // Powered by TZTester 1.01 [25-Feb-2003] // Powered by CodeProcessor
题意:定义相邻两数严格单增单减交替的数列为交替数列,定义美值为交替数列中所有相邻两数差值绝对值的总和。
现在有最多50个数,每个数的值是是个随机整数,在区间low[i], high[i],其中1<=low<=high<=1e5。如果这个数列的值组成的序列不是交替序列,则需要任意删掉一些使得其变成交替序列,求最后的序列的美值的期望是多少。
其实对于非交替数列,可以画图知道,无论怎么删除,总的美值大小还是等于所有相邻两数的差值绝对值总和,知道了这个,题目就顿时变得非常简单了。。。可以枚举前一个数的范围域,利用等差数列求和可得。
#include <cstdio> #include <cstring> #include <cctype> #include <cstdlib> #include <cmath> #include <ctime> #include <iostream> #include <map> #include <set> #include <list> #include <sstream> #include <queue> #include <deque> #include <stack> #include <vector> #include <bitset> #include <algorithm> using namespace std; #define clr(a, x) memset(a, x, sizeof(a)) #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define REP(i,a,b) for(int i=a;i<=b;i++) template <class T> void checkmax(T &t, T x) { if (x > t) t = x; } template <class T> void checkmin(T &t, T x) { if (x < t) t = x; } template <class T> void _checkmax(T &t, T x) { if (t == -1 || x > t) t = x; } template <class T> void _checkmin(T &t, T x) { if (t == -1 || x < t) t = x; } typedef long long ll; class AlternatingLane { public: double expectedBeauty(vector <int> low, vector <int> high); // BEGIN CUT HERE public: 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(); } private: 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(); } void verify_case(int Case, const double &Expected, const double &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } void test_case_0() { int Arr0[] = {1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {100}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); double Arg2 = 0.0; verify_case(0, Arg2, expectedBeauty(Arg0, Arg1)); } void test_case_1() { int Arr0[] = {1, 1, 1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {2, 2, 2}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); double Arg2 = 1.0; verify_case(1, Arg2, expectedBeauty(Arg0, Arg1)); } void test_case_2() { int Arr0[] = {1, 3, 5, 7, 9}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {2, 4, 6, 8, 10}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); double Arg2 = 8.0; verify_case(2, Arg2, expectedBeauty(Arg0, Arg1)); } void test_case_3() { int Arr0[] = {4, 3, 3, 7}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {10, 7, 7, 7}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); double Arg2 = 6.171428571428572; verify_case(3, Arg2, expectedBeauty(Arg0, Arg1)); } // END CUT HERE }; double cal(int n) { return (double)n*(n+1)/2; } double AlternatingLane::expectedBeauty(vector <int> l, vector <int> h) { int n = l.size(); double ans = 0; int i, j, k; for(i = 0;i < n-1; i++) { double cur = 0; for(j = l[i];j <= h[i]; j++) { if(j < l[i+1]) cur += cal(h[i+1]-j) - cal(l[i+1]-j-1); else if(j > h[i+1]) cur += cal(j-l[i+1]) - cal(j-h[i+1]-1); else cur += cal(h[i+1]-j) + cal(j-l[i+1]); } cur /= (h[i]-l[i]+1); cur /= (h[i+1]-l[i+1]+1); ans += cur; } cout<<ans<<endl; return ans; } // BEGIN CUT HERE int main() { AlternatingLane ___test; ___test.run_test(-1); } // END CUT HERE