bc2785b91d 2012-08-17 kinaba: #include <iostream> bc2785b91d 2012-08-17 kinaba: #include <sstream> bc2785b91d 2012-08-17 kinaba: #include <iomanip> bc2785b91d 2012-08-17 kinaba: #include <vector> bc2785b91d 2012-08-17 kinaba: #include <string> bc2785b91d 2012-08-17 kinaba: #include <map> bc2785b91d 2012-08-17 kinaba: #include <set> bc2785b91d 2012-08-17 kinaba: #include <algorithm> bc2785b91d 2012-08-17 kinaba: #include <numeric> bc2785b91d 2012-08-17 kinaba: #include <iterator> bc2785b91d 2012-08-17 kinaba: #include <functional> bc2785b91d 2012-08-17 kinaba: #include <complex> bc2785b91d 2012-08-17 kinaba: #include <queue> bc2785b91d 2012-08-17 kinaba: #include <stack> bc2785b91d 2012-08-17 kinaba: #include <cmath> bc2785b91d 2012-08-17 kinaba: #include <cassert> bc2785b91d 2012-08-17 kinaba: using namespace std; bc2785b91d 2012-08-17 kinaba: typedef long long LL; bc2785b91d 2012-08-17 kinaba: typedef long double LD; bc2785b91d 2012-08-17 kinaba: typedef complex<LD> CMP; bc2785b91d 2012-08-17 kinaba: bc2785b91d 2012-08-17 kinaba: class FoxAndFlowerShopDivOne { public: bc2785b91d 2012-08-17 kinaba: int theMaxFlowers(vector <string> flowers, int maxDiff) bc2785b91d 2012-08-17 kinaba: { bc2785b91d 2012-08-17 kinaba: int H = flowers.size(); bc2785b91d 2012-08-17 kinaba: int W = flowers[0].size(); bc2785b91d 2012-08-17 kinaba: vector< vector<int> > s(H+1, vector<int>(W+1)); bc2785b91d 2012-08-17 kinaba: vector< vector<int> > d(H+1, vector<int>(W+1)); bc2785b91d 2012-08-17 kinaba: for(int y=H-1; y>=0; --y) bc2785b91d 2012-08-17 kinaba: for(int x=W-1; x>=0; --x) bc2785b91d 2012-08-17 kinaba: { bc2785b91d 2012-08-17 kinaba: s[y][x] = s[y+1][x] + s[y][x+1] - s[y+1][x+1] + (flowers[y][x]!='.'); bc2785b91d 2012-08-17 kinaba: d[y][x] = d[y+1][x] + d[y][x+1] - d[y+1][x+1] + (flowers[y][x]=='L') - (flowers[y][x]=='P'); bc2785b91d 2012-08-17 kinaba: } bc2785b91d 2012-08-17 kinaba: bc2785b91d 2012-08-17 kinaba: int answer = -1; bc2785b91d 2012-08-17 kinaba: bc2785b91d 2012-08-17 kinaba: for(int X=1; X<W; ++X) bc2785b91d 2012-08-17 kinaba: { bc2785b91d 2012-08-17 kinaba: map<int, int> d2s_L, d2s_R; bc2785b91d 2012-08-17 kinaba: bc2785b91d 2012-08-17 kinaba: // left bc2785b91d 2012-08-17 kinaba: for(int y=0; y<H; ++y) bc2785b91d 2012-08-17 kinaba: for(int x=0; x<X; ++x) bc2785b91d 2012-08-17 kinaba: for(int yy=y+1; yy<=H; ++yy) bc2785b91d 2012-08-17 kinaba: for(int xx=x+1; xx<=X; ++xx) bc2785b91d 2012-08-17 kinaba: { bc2785b91d 2012-08-17 kinaba: int ss = s[y][x] - s[yy][x] - s[y][xx] + s[yy][xx]; bc2785b91d 2012-08-17 kinaba: int dd = d[y][x] - d[yy][x] - d[y][xx] + d[yy][xx]; bc2785b91d 2012-08-17 kinaba: d2s_L[dd] = max(d2s_L[dd], ss); bc2785b91d 2012-08-17 kinaba: } bc2785b91d 2012-08-17 kinaba: bc2785b91d 2012-08-17 kinaba: // right bc2785b91d 2012-08-17 kinaba: for(int y=0; y<H; ++y) bc2785b91d 2012-08-17 kinaba: for(int x=X; x<W; ++x) bc2785b91d 2012-08-17 kinaba: for(int yy=y+1; yy<=H; ++yy) bc2785b91d 2012-08-17 kinaba: for(int xx=x+1; xx<=W; ++xx) bc2785b91d 2012-08-17 kinaba: { bc2785b91d 2012-08-17 kinaba: int ss = s[y][x] - s[yy][x] - s[y][xx] + s[yy][xx]; bc2785b91d 2012-08-17 kinaba: int dd = d[y][x] - d[yy][x] - d[y][xx] + d[yy][xx]; bc2785b91d 2012-08-17 kinaba: d2s_R[dd] = max(d2s_R[dd], ss); bc2785b91d 2012-08-17 kinaba: } bc2785b91d 2012-08-17 kinaba: bc2785b91d 2012-08-17 kinaba: for(map<int,int>::iterator it=d2s_L.begin(); it!=d2s_L.end(); ++it) bc2785b91d 2012-08-17 kinaba: for(map<int,int>::iterator jt=d2s_R.begin(); jt!=d2s_R.end(); ++jt) bc2785b91d 2012-08-17 kinaba: if(abs(it->first + jt->first) <= maxDiff) bc2785b91d 2012-08-17 kinaba: answer = max(answer, it->second + jt->second); bc2785b91d 2012-08-17 kinaba: } bc2785b91d 2012-08-17 kinaba: bc2785b91d 2012-08-17 kinaba: for(int Y=1; Y<H; ++Y) bc2785b91d 2012-08-17 kinaba: { bc2785b91d 2012-08-17 kinaba: map<int, int> d2s_L, d2s_R; bc2785b91d 2012-08-17 kinaba: bc2785b91d 2012-08-17 kinaba: // left bc2785b91d 2012-08-17 kinaba: for(int y=0; y<Y; ++y) bc2785b91d 2012-08-17 kinaba: for(int x=0; x<W; ++x) bc2785b91d 2012-08-17 kinaba: for(int yy=y+1; yy<=Y; ++yy) bc2785b91d 2012-08-17 kinaba: for(int xx=x+1; xx<=W; ++xx) bc2785b91d 2012-08-17 kinaba: { bc2785b91d 2012-08-17 kinaba: int ss = s[y][x] - s[yy][x] - s[y][xx] + s[yy][xx]; bc2785b91d 2012-08-17 kinaba: int dd = d[y][x] - d[yy][x] - d[y][xx] + d[yy][xx]; bc2785b91d 2012-08-17 kinaba: d2s_L[dd] = max(d2s_L[dd], ss); bc2785b91d 2012-08-17 kinaba: } bc2785b91d 2012-08-17 kinaba: bc2785b91d 2012-08-17 kinaba: // right bc2785b91d 2012-08-17 kinaba: for(int y=Y; y<H; ++y) bc2785b91d 2012-08-17 kinaba: for(int x=0; x<W; ++x) bc2785b91d 2012-08-17 kinaba: for(int yy=y+1; yy<=H; ++yy) bc2785b91d 2012-08-17 kinaba: for(int xx=x+1; xx<=W; ++xx) bc2785b91d 2012-08-17 kinaba: { bc2785b91d 2012-08-17 kinaba: int ss = s[y][x] - s[yy][x] - s[y][xx] + s[yy][xx]; bc2785b91d 2012-08-17 kinaba: int dd = d[y][x] - d[yy][x] - d[y][xx] + d[yy][xx]; bc2785b91d 2012-08-17 kinaba: d2s_R[dd] = max(d2s_R[dd], ss); bc2785b91d 2012-08-17 kinaba: } bc2785b91d 2012-08-17 kinaba: bc2785b91d 2012-08-17 kinaba: for(map<int,int>::iterator it=d2s_L.begin(); it!=d2s_L.end(); ++it) bc2785b91d 2012-08-17 kinaba: for(map<int,int>::iterator jt=d2s_R.begin(); jt!=d2s_R.end(); ++jt) bc2785b91d 2012-08-17 kinaba: if(abs(it->first + jt->first) <= maxDiff) bc2785b91d 2012-08-17 kinaba: answer = max(answer, it->second + jt->second); bc2785b91d 2012-08-17 kinaba: } bc2785b91d 2012-08-17 kinaba: bc2785b91d 2012-08-17 kinaba: return answer; bc2785b91d 2012-08-17 kinaba: } bc2785b91d 2012-08-17 kinaba: }; bc2785b91d 2012-08-17 kinaba: bc2785b91d 2012-08-17 kinaba: // BEGIN CUT HERE bc2785b91d 2012-08-17 kinaba: #include <ctime> bc2785b91d 2012-08-17 kinaba: double start_time; string timer() bc2785b91d 2012-08-17 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } bc2785b91d 2012-08-17 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) bc2785b91d 2012-08-17 kinaba: { os << "{ "; bc2785b91d 2012-08-17 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) bc2785b91d 2012-08-17 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } bc2785b91d 2012-08-17 kinaba: void verify_case(const int& Expected, const int& Received) { bc2785b91d 2012-08-17 kinaba: bool ok = (Expected == Received); bc2785b91d 2012-08-17 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; bc2785b91d 2012-08-17 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } bc2785b91d 2012-08-17 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); bc2785b91d 2012-08-17 kinaba: #define END verify_case(_, FoxAndFlowerShopDivOne().theMaxFlowers(flowers, maxDiff));} bc2785b91d 2012-08-17 kinaba: int main(){ bc2785b91d 2012-08-17 kinaba: bc2785b91d 2012-08-17 kinaba: CASE(0) bc2785b91d 2012-08-17 kinaba: string flowers_[] = {"LLL", bc2785b91d 2012-08-17 kinaba: "PPP", bc2785b91d 2012-08-17 kinaba: "LLL"}; bc2785b91d 2012-08-17 kinaba: vector <string> flowers(flowers_, flowers_+sizeof(flowers_)/sizeof(*flowers_)); bc2785b91d 2012-08-17 kinaba: int maxDiff = 1; bc2785b91d 2012-08-17 kinaba: int _ = 7; bc2785b91d 2012-08-17 kinaba: END bc2785b91d 2012-08-17 kinaba: CASE(1) bc2785b91d 2012-08-17 kinaba: string flowers_[] = {"LLL", bc2785b91d 2012-08-17 kinaba: "PPP", bc2785b91d 2012-08-17 kinaba: "LLL"}; bc2785b91d 2012-08-17 kinaba: vector <string> flowers(flowers_, flowers_+sizeof(flowers_)/sizeof(*flowers_)); bc2785b91d 2012-08-17 kinaba: int maxDiff = 0; bc2785b91d 2012-08-17 kinaba: int _ = 6; bc2785b91d 2012-08-17 kinaba: END bc2785b91d 2012-08-17 kinaba: CASE(2) bc2785b91d 2012-08-17 kinaba: string flowers_[] = {"...", bc2785b91d 2012-08-17 kinaba: "...", bc2785b91d 2012-08-17 kinaba: "..."}; bc2785b91d 2012-08-17 kinaba: vector <string> flowers(flowers_, flowers_+sizeof(flowers_)/sizeof(*flowers_)); bc2785b91d 2012-08-17 kinaba: int maxDiff = 3; bc2785b91d 2012-08-17 kinaba: int _ = 0; bc2785b91d 2012-08-17 kinaba: END bc2785b91d 2012-08-17 kinaba: CASE(3) bc2785b91d 2012-08-17 kinaba: string flowers_[] = {"LLPL.LPP", bc2785b91d 2012-08-17 kinaba: "PLPPPPLL", bc2785b91d 2012-08-17 kinaba: "L.P.PLLL", bc2785b91d 2012-08-17 kinaba: "LPL.PP.L", bc2785b91d 2012-08-17 kinaba: ".LLL.P.L", bc2785b91d 2012-08-17 kinaba: "PPLP..PL"}; bc2785b91d 2012-08-17 kinaba: vector <string> flowers(flowers_, flowers_+sizeof(flowers_)/sizeof(*flowers_)); bc2785b91d 2012-08-17 kinaba: int maxDiff = 2; bc2785b91d 2012-08-17 kinaba: int _ = 38; bc2785b91d 2012-08-17 kinaba: END bc2785b91d 2012-08-17 kinaba: CASE(4) bc2785b91d 2012-08-17 kinaba: string flowers_[] = {"LLLLLLLLLL", bc2785b91d 2012-08-17 kinaba: "LLLLLLLLLL", bc2785b91d 2012-08-17 kinaba: "LLLLLLLLLL", bc2785b91d 2012-08-17 kinaba: "LLLLLLLLLL", bc2785b91d 2012-08-17 kinaba: "LLLLLLLLLL"}; bc2785b91d 2012-08-17 kinaba: vector <string> flowers(flowers_, flowers_+sizeof(flowers_)/sizeof(*flowers_)); bc2785b91d 2012-08-17 kinaba: int maxDiff = 0; bc2785b91d 2012-08-17 kinaba: int _ = -1; bc2785b91d 2012-08-17 kinaba: END bc2785b91d 2012-08-17 kinaba: CASE(5) bc2785b91d 2012-08-17 kinaba: string flowers_[] = {"LLLP..LLP.PLL.LL..LP", bc2785b91d 2012-08-17 kinaba: "L.PL.L.LLLL.LPLLPLP.", bc2785b91d 2012-08-17 kinaba: "PLL.LL.LLL..PL...L..", bc2785b91d 2012-08-17 kinaba: ".LPPP.PPPLLLLPLP..PP", bc2785b91d 2012-08-17 kinaba: "LP.P.PPL.L...P.L.LLL", bc2785b91d 2012-08-17 kinaba: "L..LPLPP.PP...PPPL..", bc2785b91d 2012-08-17 kinaba: "PP.PLLL.LL...LP..LP.", bc2785b91d 2012-08-17 kinaba: "PL...P.PPPL..PLP.L..", bc2785b91d 2012-08-17 kinaba: "P.PPPLPLP.LL.L.LLLPL", bc2785b91d 2012-08-17 kinaba: "PLLPLLP.LLL.P..P.LPL", bc2785b91d 2012-08-17 kinaba: "..LLLPLPPPLP.P.LP.LL", bc2785b91d 2012-08-17 kinaba: "..LP..L..LLPPP.LL.LP", bc2785b91d 2012-08-17 kinaba: "LPLL.PLLPPLP...LL..P", bc2785b91d 2012-08-17 kinaba: "LL.....PLL.PLL.P....", bc2785b91d 2012-08-17 kinaba: "LLL...LPPPPL.PL...PP", bc2785b91d 2012-08-17 kinaba: ".PLPLLLLP.LPP...L...", bc2785b91d 2012-08-17 kinaba: "LL...L.LL.LLLPLPPPP.", bc2785b91d 2012-08-17 kinaba: "PLPLLLL..LP.LLPLLLL.", bc2785b91d 2012-08-17 kinaba: "PP.PLL..L..LLLPPL..P", bc2785b91d 2012-08-17 kinaba: ".LLPL.P.PP.P.L.PLPLL"}; bc2785b91d 2012-08-17 kinaba: vector <string> flowers(flowers_, flowers_+sizeof(flowers_)/sizeof(*flowers_)); bc2785b91d 2012-08-17 kinaba: int maxDiff = 9; bc2785b91d 2012-08-17 kinaba: int _ = 208; bc2785b91d 2012-08-17 kinaba: END bc2785b91d 2012-08-17 kinaba: CASE(6) bc2785b91d 2012-08-17 kinaba: string flowers_[] = {"L","P"}; bc2785b91d 2012-08-17 kinaba: vector <string> flowers(flowers_, flowers_+sizeof(flowers_)/sizeof(*flowers_)); bc2785b91d 2012-08-17 kinaba: int maxDiff = 0; bc2785b91d 2012-08-17 kinaba: int _ = 2; bc2785b91d 2012-08-17 kinaba: END bc2785b91d 2012-08-17 kinaba: CASE(7) bc2785b91d 2012-08-17 kinaba: string flowers_[] = {"LLLP..LLP.PLL.LL..LPPLL.LL..LP", bc2785b91d 2012-08-17 kinaba: "L.PL.L.LLLL.LPLLPLP.PLL.LL..LP", bc2785b91d 2012-08-17 kinaba: "PLL.LL.LLL..PL...L..PLL.LL..LP", bc2785b91d 2012-08-17 kinaba: ".LPPP.PPPLLLLPLP..PPPLL.LL..LP", bc2785b91d 2012-08-17 kinaba: "LP.P.PPL.L...P.L.LLLPLL.LL..LP", bc2785b91d 2012-08-17 kinaba: "L..LPLPP.PP...PPPL..PLL.LL..LP", bc2785b91d 2012-08-17 kinaba: "PP.PLLL.LL...LP..LP.PLL.LL..LP", bc2785b91d 2012-08-17 kinaba: "PL...P.PPPL..PLP.L..PLL.LL..LP", bc2785b91d 2012-08-17 kinaba: "P.PPPLPLP.LL.L.LLLPLPLL.LL..LP", bc2785b91d 2012-08-17 kinaba: "PLLPLLP.LLL.P..P.LPLPLL.LL..LP", bc2785b91d 2012-08-17 kinaba: "..LLLPLPPPLP.P.LP.LLPLL.LL..LP", bc2785b91d 2012-08-17 kinaba: "..LP..L..LLPPP.LL.LPPLL.LL..LP", bc2785b91d 2012-08-17 kinaba: "LPLL.PLLPPLP...LL..PPLL.LL..LP", bc2785b91d 2012-08-17 kinaba: ".LPPP.PPPLLLLPLP..PPPLL.LL..LP", bc2785b91d 2012-08-17 kinaba: "LP.P.PPL.L...P.L.LLLPLL.LL..LP", bc2785b91d 2012-08-17 kinaba: "L..LPLPP.PP...PPPL..PLL.LL..LP", bc2785b91d 2012-08-17 kinaba: "PP.PLLL.LL...LP..LP.PLL.LL..LP", bc2785b91d 2012-08-17 kinaba: "PL...P.PPPL..PLP.L..PLL.LL..LP", bc2785b91d 2012-08-17 kinaba: "P.PPPLPLP.LL.L.LLLPLPLL.LL..LP", bc2785b91d 2012-08-17 kinaba: "PLLPLLP.LLL.P..P.LPLPLL.LL..LP", bc2785b91d 2012-08-17 kinaba: "..LLLPLPPPLP.P.LP.LLPLL.LL..LP", bc2785b91d 2012-08-17 kinaba: "..LP..L..LLPPP.LL.LPPLL.LL..LP", bc2785b91d 2012-08-17 kinaba: "LPLL.PLLPPLP...LL..PPLL.LL..LP", bc2785b91d 2012-08-17 kinaba: "LL.....PLL.PLL.P....PLL.LL..LP", bc2785b91d 2012-08-17 kinaba: "LLL...LPPPPL.PL...PPPLL.LL..LP", bc2785b91d 2012-08-17 kinaba: ".PLPLLLLP.LPP...L...PLL.LL..LP", bc2785b91d 2012-08-17 kinaba: "LL...L.LL.LLLPLPPPP.PLL.LL..LP", bc2785b91d 2012-08-17 kinaba: "PLPLLLL..LP.LLPLLLL.PLL.LL..LP", bc2785b91d 2012-08-17 kinaba: "PP.PLL..L..LLLPPL..PPLL.LL..LP", bc2785b91d 2012-08-17 kinaba: ".LLPL.P.PP.P.L.PLPLLPLL.LL..LP"}; bc2785b91d 2012-08-17 kinaba: vector <string> flowers(flowers_, flowers_+sizeof(flowers_)/sizeof(*flowers_)); bc2785b91d 2012-08-17 kinaba: int maxDiff = 0; bc2785b91d 2012-08-17 kinaba: int _ = -1; bc2785b91d 2012-08-17 kinaba: END bc2785b91d 2012-08-17 kinaba: } bc2785b91d 2012-08-17 kinaba: // END CUT HERE