Pagini recente » Cod sursa (job #537404) | Cod sursa (job #1081703)
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;
const char infile[] = "dreptpal.in";
const char outfile[] = "dreptpal.out";
ifstream fin(infile);
ofstream fout(outfile);
const int MAXN = 1005;
const int oo = 0x3f3f3f3f;
typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;
const inline int min(const int &a, const int &b) { if( a > b ) return b; return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b; return a; }
const inline void Get_min(int &a, const int b) { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b) { if( a < b ) a = b; }
int A[MAXN][MAXN], N, M, dp[MAXN][MAXN], Stack[MAXN];
inline void buildManacher(int *p, int *DP) {
int ind = 0, best = 0;
for(int i = 1 ; i <= M ; ++ i) {
if(best >= i)
DP[i] = min(best - i, DP[2*ind - 1]);
while(i - DP[i] - 1 >= 0 && i + DP[i] + 1 <= M && p[i - DP[i] - 1] == p[i + DP[i] + 1])
++DP[i];
if(i + DP[i] > best) {
best = i + DP[i];
ind = i;
}
}
}
inline void Solve(void) {
#ifdef debug
for(int i = 1 ; i <= N ; ++ i, fout << '\n')
for(int j = 1 ; j <= M ; ++ j)
fout << A[i][j] << ' ';
fout << "\n";
for(int i = 1 ; i <= N ; ++ i, fout << '\n')
for(int j = 1 ; j <= M ; ++ j)
fout << dp[i][j] << ' ';
#endif
int Ans = 0;
for(int j = 1; j <= M ; ++ j) {
int top = 0;
for(int i = 1 ; i <= N + 1 ; ++ i) {
while( top > 0 && dp[i][j] <= dp[Stack[top]][j]) {
int left = Stack[top - 1] + 1;
int right = i - 1;
Ans = max(Ans, (2*dp[Stack[top]][j] + 1) * (right - left + 1));
-- top;
}
Stack[++ top] = i;
}
}
fout << Ans << '\n';
}
int main() {
fin >> N >> M;
for(int i = 1 ; i <= N ; ++ i)
for(int j = 1 ; j <= M ; ++ j)
fin >> A[i][j];
for(int i = 1 ; i <= N ; ++ i)
buildManacher(A[i], dp[i]);
Solve();
fin.close();
fout.close();
return 0;
}