ZJ12. 编程题2
描述
给定一个数组序列, 需要求选出一个区间, 使得该区间是所有区间中经过如下计算的值最大的一个:
区间中的最小数 * 区间所有数的和最后程序输出经过计算后的最大值即可,不需要输出具体的区间。如给定序列 [6 2 1]则根据上述公式, 可得到所有可以选定各个区间的计算值:
[6] = 6 * 6 = 36;
[2] = 2 * 2 = 4;
[1] = 1 * 1 = 1;
[6,2] = 2 * 8 = 16;
[2,1] = 1 * 3 = 3;
[6, 2, 1] = 1 * 9 = 9;
从上述计算可见选定区间 [6] ,计算值为 36, 则程序输出为 36。
区间内的所有数字都在[0, 100]的范围内;
输入描述
第一行输入数组序列长度n,第二行输入数组序列。 对于 50%的数据, 1 <= n <= 10000; 对于 100%的数据, 1 <= n <= 500000;输出描述
输出数组经过计算后的最大值。示例1
输入:
3 6 2 1
输出:
36
C++ 解法, 执行用时: 16ms, 内存消耗: 4224KB, 提交时间: 2021-08-17
#include <iostream> #include <cstdio> #include <vector> using namespace std; inline int getint(){ int x=0; char ch=getchar(); while (!isdigit(ch)) ch=getchar(); while (isdigit(ch)){ x=x*10+ch-'0'; ch=getchar(); } return x; } int main(){ int n; cin>>n; vector<int> a(n+1, 0); vector<int> s(n+1, 0); for (int i=1; i<=n; i++){ a[i]=getint(); s[i]=s[i-1]+a[i]; } vector<int> stack; int result=a[1]*a[1]; stack.push_back(1); for (int i=2; i<=n; i++){ int top=stack.back(); while (a[top]>=a[i]){ int st=(stack.size()>=2)?stack[stack.size()-2]:0; result=max(result, a[top]*(s[i-1]-s[st])); stack.pop_back(); if (stack.size()==0) break; else top=stack.back(); } if (stack.size()==0) result=max(result, a[i]*s[i]); else result=max(result, a[i]*(s[i]-s[top])); stack.push_back(i); } while (stack.size()>0){ int top=stack.back(); int st=(stack.size()>=2)?stack[stack.size()-2]:0; result=max(result, a[top]*(s[n]-s[st])); stack.pop_back(); } cout<<result<<endl; return 0; }
C++ 解法, 执行用时: 19ms, 内存消耗: 5752KB, 提交时间: 2020-10-29
#include <iostream> #include <cstdio> #include <vector> using namespace std; inline int getint(){ int x=0; char ch=getchar(); while (!isdigit(ch)) ch=getchar(); while (isdigit(ch)){ x=x*10+ch-'0'; ch=getchar(); } return x; } int main(){ int n; cin>>n; vector<int> a(n+1, 0); vector<int> s(n+1, 0); for (int i=1; i<=n; i++){ a[i]=getint(); s[i]=s[i-1]+a[i]; } vector<int> stack; int result=a[1]*a[1]; stack.push_back(1); for (int i=2; i<=n; i++){ int top=stack.back(); while (a[top]>=a[i]){ int st=(stack.size()>=2)?stack[stack.size()-2]:0; result=max(result, a[top]*(s[i-1]-s[st])); stack.pop_back(); if (stack.size()==0) break; else top=stack.back(); } if (stack.size()==0) result=max(result, a[i]*s[i]); else result=max(result, a[i]*(s[i]-s[top])); stack.push_back(i); } while (stack.size()>0){ int top=stack.back(); int st=(stack.size()>=2)?stack[stack.size()-2]:0; result=max(result, a[top]*(s[n]-s[st])); stack.pop_back(); } cout<<result<<endl; return 0; }
C++ 解法, 执行用时: 19ms, 内存消耗: 5764KB, 提交时间: 2020-10-29
#include <iostream> #include <cstdio> #include <vector> using namespace std; inline int getint(){ int x=0; char ch=getchar(); while (!isdigit(ch)) ch=getchar(); while (isdigit(ch)){ x=x*10+ch-'0'; ch=getchar(); } return x; } int main(){ int n; cin>>n; vector<int> a(n+1, 0); vector<int> s(n+1, 0); for (int i=1; i<=n; i++){ a[i]=getint(); s[i]=s[i-1]+a[i]; } vector<int> stack; int result=a[1]*a[1]; stack.push_back(1); for (int i=2; i<=n; i++){ int top=stack.back(); while (a[top]>=a[i]){ int st=(stack.size()>=2)?stack[stack.size()-2]:0; result=max(result, a[top]*(s[i-1]-s[st])); stack.pop_back(); if (stack.size()==0) break; else top=stack.back(); } if (stack.size()==0) result=max(result, a[i]*s[i]); else result=max(result, a[i]*(s[i]-s[top])); stack.push_back(i); } while (stack.size()>0){ int top=stack.back(); int st=(stack.size()>=2)?stack[stack.size()-2]:0; result=max(result, a[top]*(s[n]-s[st])); stack.pop_back(); } cout<<result<<endl; return 0; }
C++ 解法, 执行用时: 20ms, 内存消耗: 4544KB, 提交时间: 2020-10-29
#include <iostream> #include <cstdio> #include <vector> using namespace std; inline int getint(){ int x=0; char ch=getchar(); while (!isdigit(ch)) ch=getchar(); while (isdigit(ch)){ x=x*10+ch-'0'; ch=getchar(); } return x; } int main(){ int n; cin>>n; vector<int> a(n+1, 0); vector<int> s(n+1, 0); for (int i=1; i<=n; i++){ a[i]=getint(); s[i]=s[i-1]+a[i]; } vector<int> stack; int result=a[1]*a[1]; stack.push_back(1); for (int i=2; i<=n; i++){ int top=stack.back(); while (a[top]>=a[i]){ int st=(stack.size()>=2)?stack[stack.size()-2]:0; result=max(result, a[top]*(s[i-1]-s[st])); stack.pop_back(); if (stack.size()==0) break; else top=stack.back(); } if (stack.size()==0) result=max(result, a[i]*s[i]); else result=max(result, a[i]*(s[i]-s[top])); stack.push_back(i); } while (stack.size()>0){ int top=stack.back(); int st=(stack.size()>=2)?stack[stack.size()-2]:0; result=max(result, a[top]*(s[n]-s[st])); stack.pop_back(); } cout<<result<<endl; return 0; }
C++ 解法, 执行用时: 20ms, 内存消耗: 5644KB, 提交时间: 2020-07-30
#include <iostream> #include <cstdio> #include <vector> using namespace std; inline int getint(){ int x=0; char ch=getchar(); while (!isdigit(ch)) ch=getchar(); while (isdigit(ch)){ x=x*10+ch-'0'; ch=getchar(); } return x; } int main(){ int n; cin>>n; vector<int> a(n+1, 0); vector<int> s(n+1, 0); for (int i=1; i<=n; i++){ a[i]=getint(); s[i]=s[i-1]+a[i]; } vector<int> stack; int result=a[1]*a[1]; stack.push_back(1); for (int i=2; i<=n; i++){ int top=stack.back(); while (a[top]>=a[i]){ int st=(stack.size()>=2)?stack[stack.size()-2]:0; result=max(result, a[top]*(s[i-1]-s[st])); stack.pop_back(); if (stack.size()==0) break; else top=stack.back(); } if (stack.size()==0) result=max(result, a[i]*s[i]); else result=max(result, a[i]*(s[i]-s[top])); stack.push_back(i); } while (stack.size()>0){ int top=stack.back(); int st=(stack.size()>=2)?stack[stack.size()-2]:0; result=max(result, a[top]*(s[n]-s[st])); stack.pop_back(); } cout<<result<<endl; return 0; }