ZOJ 1985 Largest Rectangle in a Histogram

网友投稿 809 2022-11-10

ZOJ 1985 Largest Rectangle in a Histogram

ZOJ 1985 Largest Rectangle in a Histogram

题目地址:code_snippet_id="1647064" snippet_file_name="blog_20160414_1_9584762" name="code" class="cpp">#include #include #include #include #include #include #include #include #include #include #include const int inf = 0x3f3f3f3f;//1061109567typedef long long LL;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;int a[100010],left1[100010],right1[100010];int main(){ int n; while(scanf("%d",&n) && n) { /*for(int i=1; i<=n; i++) { scanf("%d",&a[i]); left1[i] = i; right1[i] = i; }*/ for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); left1[i] = i; right1[i] = i; } /*for(int i=1; i<=n; i++) { int temp = i; while(temp-1>=1 && a[temp-1] >= a[i]) temp = left1[temp-1]; left1[i] = temp; } for(int i=n; i>=1; i--) { int temp = i; while(temp + 1 <= n && a[temp+1] >= a[i]) temp = right1[temp+1]; right1[i] = temp; }*/ int i,temp; for (i = 1; i <= n; i++) { temp = i; while (temp - 1 >= 1 && a[temp - 1] >= a[i]) temp = left1[temp - 1]; left1[i] = temp; } for (i = n; i >= 1; i--) { temp = i; while (temp + 1 <= n && a[temp + 1] >= a[i]) temp = right1[temp + 1]; right1[i] = temp; } /*LL max1 = 0; for(int i=1; i<=n; i++) { LL sum = (LL)a[i] * (LL)(right1[i]-left1[i]+1); if(sum > max1) max1 = sum; } printf("%I64d\n",max1);*/ long long max = -1, res; for (i = 1; i <= n; i++) { res = (long long) a[i] * (long long) (right1[i] - left1[i] + 1); if (res > max) max = res; } printf("%I64d\n",max); } return 0;}

超时代码

#include #include #include #include #include #include #include #include #include #include #include const int inf = 0x3f3f3f3f;//1061109567typedef long long LL;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;int a[100010];int main(){ int n; while(scanf("%d",&n) && n) { int max1 = 0; for(int i=1; i<=n; i++) { scanf("%d",&a[i]); int min1 = a[i]; for(int j=i; j>=1; j--) { min1 = min(min1,a[j]); int sum = min1 * (i - j + 1); if(sum > max1) max1 = sum; } } printf("%d\n",max1); } return 0;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:2546 饭卡(01背包,挺好的)
下一篇:输入一个字符串,找出其中不含有重复字符的最长子串的长度。
相关文章

 发表评论

暂时没有评论,来抢沙发吧~