主题:[原创]PKU 1050 To the Max
#include <iostream>
using namespace std;
int a[101][101];
template <typename T>
inline T const& max (T const& a, T const& b)
{
return a < b ? b : a;
}
int maxsum0(int x[], int n)
{
int maxsofar = 0, maxendinghere = 0;
int i;
for (i = 0; i < n; ++i)
{
// invariant: maxendinghere and maxsofar are accurate for x[0..i-1]
maxendinghere = max(maxendinghere + x[i], 0);
maxsofar = max(maxsofar, maxendinghere);
}
return maxsofar;
}
int maximumSubsequenceSum(int x[], int n, int &seqStart, int &seqEnd)
{
int maxSum = 0, thisSum = 0;
for (int i = 0, j = 0; j < n; ++j) {
thisSum += x[j];
if (thisSum > maxSum) {
maxSum = thisSum;
seqStart = i;
seqEnd = j;
}
else if (thisSum < 0) {
i = j + 1;
thisSum = 0;
}
//cout << seqStart << ", " << seqEnd << endl;
}
return maxSum;
}
const int N = 101;
int array[N][N];
int maxsum2D(int n, int m, int &s, int &t, int &u, int &v)
{
int maxsofar = 0, maxendinghere = 0;
int *tmpArr = new int[m + 1];
for (int i = 0; i < n; ++i) {
// void *memset(void *s, int c, size_t n);
memset(tmpArr, 0, sizeof(int)*n);
for (int j = i; j < m; ++j) {
for (int k = 0; k < m; ++k) { tmpArr[k] += array[j][k]; }
//printArray(tmpArr, m);
int col1, col2;
maxendinghere = maximumSubsequenceSum(tmpArr, m, col1, col2);
if (maxendinghere > maxsofar) {
maxsofar = maxendinghere;
s = i;
t = j;
u = col1;
v = col2;
}
}
}
delete tmpArr;
return maxsofar;
}
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >> array[i][j];
}
}
int s, t, u, v;
cout << maxsum2D(n, n, s, t, u, v)<<endl;
cout << " " << u << ".." << v << endl;
cout << s << endl << t << endl;
return 0;
}
using namespace std;
int a[101][101];
template <typename T>
inline T const& max (T const& a, T const& b)
{
return a < b ? b : a;
}
int maxsum0(int x[], int n)
{
int maxsofar = 0, maxendinghere = 0;
int i;
for (i = 0; i < n; ++i)
{
// invariant: maxendinghere and maxsofar are accurate for x[0..i-1]
maxendinghere = max(maxendinghere + x[i], 0);
maxsofar = max(maxsofar, maxendinghere);
}
return maxsofar;
}
int maximumSubsequenceSum(int x[], int n, int &seqStart, int &seqEnd)
{
int maxSum = 0, thisSum = 0;
for (int i = 0, j = 0; j < n; ++j) {
thisSum += x[j];
if (thisSum > maxSum) {
maxSum = thisSum;
seqStart = i;
seqEnd = j;
}
else if (thisSum < 0) {
i = j + 1;
thisSum = 0;
}
//cout << seqStart << ", " << seqEnd << endl;
}
return maxSum;
}
const int N = 101;
int array[N][N];
int maxsum2D(int n, int m, int &s, int &t, int &u, int &v)
{
int maxsofar = 0, maxendinghere = 0;
int *tmpArr = new int[m + 1];
for (int i = 0; i < n; ++i) {
// void *memset(void *s, int c, size_t n);
memset(tmpArr, 0, sizeof(int)*n);
for (int j = i; j < m; ++j) {
for (int k = 0; k < m; ++k) { tmpArr[k] += array[j][k]; }
//printArray(tmpArr, m);
int col1, col2;
maxendinghere = maximumSubsequenceSum(tmpArr, m, col1, col2);
if (maxendinghere > maxsofar) {
maxsofar = maxendinghere;
s = i;
t = j;
u = col1;
v = col2;
}
}
}
delete tmpArr;
return maxsofar;
}
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >> array[i][j];
}
}
int s, t, u, v;
cout << maxsum2D(n, n, s, t, u, v)<<endl;
cout << " " << u << ".." << v << endl;
cout << s << endl << t << endl;
return 0;
}