1. HDU 1081 To The Max
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1081
emmm,4层for循环暴力解出来的。5*1e7的复杂度竟然没超时。
代码如下:
//include <bits/stdc++.h>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <time.h>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;
const int maxn = 1e2+5;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
template <class T>
inline bool scan_d(T &ret) {
char c; int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c<'0' || c>'9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
int n;
int a[maxn][maxn];
int sum[maxn][maxn];
int main()
{
while(scanf("%d",&n)!=EOF)
{
memset (sum,0,sizeof(sum));
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
sum[i][j]=sum[i][j-1]+a[i][j];
}
}
int ans=0;
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
for (int k=j;k<=n;k++)
{
int tans=sum[i][k]-sum[i][j-1];
ans=max(tans,ans);
for (int l=i+1;l<=n;l++)
{
tans+=sum[l][k]-sum[l][j-1];
ans=max(ans,tans);
}
}
}
}
printf("%d\n",ans);
}
return 0;
}
2. HDU 1003 Max Sum
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1003
在这里附上我的题解:
3. POJ 2593 Max Sequence
题目链接:
http://poj.org/problem?id=2593
求正向的最长子序列,倒着求剩下的最长子序列,一合就可以。
代码如下:
//include <bits/stdc++.h>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <time.h>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;
const int maxn = 1e5+5;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
template <class T>
inline bool scan_d(T &ret) {
char c; int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c<'0' || c>'9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
int n;
int a[maxn];
int dp[maxn];
int main()
{
while(scanf("%d",&n)!=EOF&&n)
{
for (int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
memset (dp,0,sizeof(dp));
int sum=0,Max=-INF;
for (int i=1;i<=n;i++)
{
sum+=a[i];
if(sum>Max) Max=sum;
dp[i]=Max;
if(sum<0) sum=0;
}
int ans=-INF;
sum=0,Max=-INF;
for (int i=n;i>=2;i--)
{
sum+=a[i];
if(sum>Max) Max=sum;
ans=max(ans,dp[i-1]+Max);
if(sum<0) sum=0;
}
printf("%d\n",ans);
}
return 0;
}
4. HDU 1506 Largest Rectangle in a Histogram
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1506
参考题解:
https://blog.csdn.net/u013852115/article/details/76943840
代码如下:
//include <bits/stdc++.h>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <time.h>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;
const int maxn = 1e5+5;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
template <class T>
inline bool scan_d(T &ret) {
char c; int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c<'0' || c>'9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
int n;
int a[maxn];
int l[maxn];
int r[maxn];
int main()
{
while(scanf("%d",&n)!=EOF&&n)
{
for (int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
l[i]=r[i]=n;
}
ll ans=0;
a[0]=a[n+1]=-1;
for (int i=1;i<=n;i++)
{
int j=i-1;
while(j>=1&&a[j]>=a[i])
{
j=l[j]-1;
}
l[i]=j+1;
}
for (int i=n;i>=1;i--)
{
int j=i+1;
while(j<=n&&a[j]>=a[i])
{
j=r[j]+1;
}
r[i]=j-1;
}
for (int i=1;i<=n;i++)
{
ll temp=(ll)(r[i]-l[i]+1)*a[i];
ans=max(ans,temp);
}
printf("%lld\n",ans);
}
return 0;
}