POJ 2769

http://poj.org/problem?id=2796

题意:求n个数的和乘以这n个数中的最小值的积最大的数,以及其范围。

思路:求每一个数两边的比其大的数的和,再乘以这个数。还有一个范围,用单调栈找以及记录。

这个题和2559差不多,就是多了一个对数字的求和。

 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <stack>
 4
 5 #define x 100010
 6
 7 using namespace std;
 8
 9 stack<long long >s;
10 long long l[x],r[x],a[x],sum,ans,z[x],m,loc;
11
12 int main(){
13     int n;
14     scanf("%d",&n);
15     ans=0;
16     z[0]=0,r[0]=1,l[0]=1,l[1]=1,r[n]=n;     //要考虑案例是1 0 的情况。
17     for(int i=1;i<=n;i++){
18
19         scanf("%lld",&a[i]);
20         z[i]+=z[i-1]+a[i];       //z[m]-z[n-1]就是从m到n的a[i]之和。
21     }
22     a[0]=-1,a[n+1]=-1;
23     while(!s.empty()){
24
25         s.pop();
26     }
27         s.push(0);
28
29     for(int i=1;i<=n;i++){
30
31         for(m=s.top();a[m]>=a[i];m=s.top())
32
33             s.pop();
34
35             l[i]=m+1;
36
37             s.push(i);
38     }
39     while(!s.empty())
40
41         s.pop();
42
43     s.push(n+1);
44
45     for(int i=n;i>0;i--){
46             sum=0;
47
48         for(m=s.top();a[m]>=a[i];m=s.top())
49             s.pop();
50         r[i]=m-1;
51         s.push(i);
52         if((z[r[i]]-z[l[i]-1])*a[i]>ans) {ans=(z[r[i]]-z[l[i]-1])*a[i];loc=i;}
53     }
54
55     printf("%lld\n%lld %lld\n",ans,l[loc],r[loc]);
56    return 0;
57 }
时间: 2024-12-25 08:18:55

POJ 2769的相关文章

poj 2769 Reduced ID Numbers(memset使用技巧)

Description T. Chur teaches various groups of students at university U. Every U-student has a unique Student Identification Number (SIN). A SIN s is an integer in the range 0 ≤ s ≤ MaxSIN with MaxSIN = 106-1. T. Chur finds this range of SINs too larg

POJ 2769 Reduced ID Numbers

思路: 枚举 Reduced ID Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8847 Accepted: 3552 Description T. Chur teaches various groups of students at university U. Every U-student has a unique Student Identification Number (SIN). A SIN s

POJ 2769 Reduced ID Numbers 同余定理

Reduced ID Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8989 Accepted: 3610 Description T. Chur teaches various groups of students at university U. Every U-student has a unique Student Identification Number (SIN). A SIN s is an

POJ 2769 Reduced ID Numbers (同余)

Reduced ID Numbers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9153   Accepted: 3675 Description T. Chur teaches various groups of students at university U. Every U-student has a unique Student Identification Number (SIN). A SIN s is

POJ 3683(Priest John&#39;s Busiest Day-强连通分量解决2-SAT)[Template:2-SAT]

Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8144   Accepted: 2769   Special Judge Description John is the only priest in his town. September 1st is the John's busiest day in a year because there is an old le

POJ - 3186 Treats for the Cows (区间DP)

题目链接:http://poj.org/problem?id=3186 题意:给定一组序列,取n次,每次可以取序列最前面的数或最后面的数,第n次出来就乘n,然后求和的最大值. 题解:用dp[i][j]表示i~j区间和的最大值,然后根据这个状态可以从删前和删后转移过来,推出状态转移方程: dp[i][j]=max(dp[i+1][j]+value[i]*k,dp[i][j-1]+value[j]*k) 1 #include <iostream> 2 #include <algorithm&

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)

POJ——T2271 Guardian of Decency

http://poj.org/problem?id=2771 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5932   Accepted: 2463 Description Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he is

POJ——T2446 Chessboard

http://poj.org/problem?id=2446 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18560   Accepted: 5857 Description Alice and Bob often play games on chessboard. One day, Alice draws a board with size M * N. She wants Bob to use a lot of c