天天看点

QDU暑训1 最小生成树,树的直径,单调栈A - 1B - 2C - 3D - 4E - 5F - 6G - 7H - 8I - 9J - 10总结

A - 1

QDU暑训1 最小生成树,树的直径,单调栈A - 1B - 2C - 3D - 4E - 5F - 6G - 7H - 8I - 9J - 10总结

The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensive to maintain. The Council of Elders must choose to stop maintaining some roads. The map above on the left shows all the roads in use now and the cost in aacms per month to maintain them. Of course there needs to be some way to get between all the villages on maintained roads, even if the route is not as short as before. The Chief Elder would like to tell the Council of Elders what would be the smallest amount they could spend in aacms per month to maintain roads that would connect all the villages. The villages are labeled A through I in the maps above. The map on the right shows the roads that could be maintained most cheaply, for 216 aacms per month. Your task is to write a program that will solve such problems.

Input

The input consists of one to 100 data sets, followed by a final line containing only 0. Each data set starts with a line containing only a number n, which is the number of villages, 1 < n < 27, and the villages are labeled with the first n letters of the alphabet, capitalized. Each data set is completed with n-1 lines that start with village labels in alphabetical order. There is no line for the last village. Each line for a village starts with the village label followed by a number, k, of roads from this village to villages with labels later in the alphabet. If k is greater than 0, the line continues with data for each of the k roads. The data for each road is the village label for the other end of the road followed by the monthly maintenance cost in aacms for the road. Maintenance costs will be positive integers less than 100. All data fields in the row are separated by single blanks. The road network will always allow travel between all the villages. The network will never have more than 75 roads. No village will have more than 15 roads going to other villages (before or after in the alphabet). In the sample input below, the first data set goes with the map above.

Output

The output is one integer per line for each data set: the minimum cost in aacms per month to maintain a road system that connect all the villages. Caution: A brute force solution that examines every possible set of roads will not finish within the one minute time limit.

Sample Input

9
A 2 B 12 I 25
B 3 C 10 H 40 I 8
C 2 D 18 G 55
D 1 E 44
E 2 F 60 G 38
F 0
G 1 H 35
H 1 I 35
3
A 2 B 10 C 40
B 1 C 20
0
           

Sample Output

216
30
           
最少生成树模板提

AC代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<stack>
using namespace std;
typedef long long ll;
const int N=110;
int n;
int g[N][N];
int dist[N];
bool st[N];

int prim()
{
	int res=0;
	memset(dist,0x3f,sizeof dist);
	memset(st,0,sizeof st);
	for(int i=0;i<n;i++)
	{
		int t=-1;
		for(int j=1;j<=n;j++)
			if(!st[j]&&(t==-1||dist[t]>dist[j]))
				t=j;
		if(i) res+=dist[t];
		for(int j=1;j<=n;j++) dist[j]=min(dist[j],g[t][j]);
		st[t]=true;
		//cout<<res<<endl;
	}
	return res;
}

int main()
{
	while(cin>>n&&n)
	{
		memset(g,0x3f,sizeof g);
		for(int i=0;i<n-1;i++)
		{
			char a,b;
			int x,y;
			cin>>a>>x;
			for(int j=0;j<x;j++)
			{
				cin>>b>>y;
				g[a-64][b-64]=g[b-64][a-64]=y;
			}
		}
//		for(int i=0;i<n;i++)
//			for(int j=0;j<n;j++)
//			{
//				if(g[i][j]!=0x3f3f3f3f) cout<<i<<" "<<j<<" "<<g[i][j]<<endl;
//			}
		int ans=prim();
		cout<<ans<<endl;
	}
	return 0;
}
           

B - 2

You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible routes for the cables that may connect pairs of points. For each possible route between two points, you are given the length of the cable that is needed to connect the points over that route. Note that there may exist many possible routes between two given points. It is assumed that the given possible routes connect (directly or indirectly) each two points in the area.

Your task is to design the network for the area, so that there is a connection (direct or indirect) between every two points (i.e., all the points are interconnected, but not necessarily by a direct cable), and that the total length of the used cable is minimal.

Input

The input file consists of a number of data sets. Each data set defines one required network. The first line of the set contains two integers: the first defines the number P of the given points, and the second the number R of given routes between the points. The following R lines define the given routes between the points, each giving three integer numbers: the first two numbers identify the points, and the third gives the length of the route. The numbers are separated with white spaces. A data set giving only one number P=0 denotes the end of the input. The data sets are separated with an empty line.

The maximal number of points is 50. The maximal length of a given route is 100. The number of possible routes is unlimited. The nodes are identified with integers between 1 and P (inclusive). The routes between two points i and j may be given as i j or as j i.

Output

For each data set, print one number on a separate line that gives the total length of the cable used for the entire designed network.

Sample Input

1 0

2 3
1 2 37
2 1 17
1 2 68

3 7
1 2 19
2 3 11
3 1 7
1 3 5
2 3 89
3 1 91
1 2 32

5 7
1 2 5
2 3 7
2 4 8
4 5 11
3 5 10
1 5 6
4 2 12

0
           

Sample Output

0
17
16
26
           

AC代码

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=1010;
int g[N][N];
int dist[N];
bool st[N];
int n,m;

int prim()
{
	memset(dist,0x3f,sizeof dist);
	memset(st,0,sizeof st);
	int res=0;
	for(int i=0;i<n;i++)
	{
		int t=-1;
		for(int j=1;j<=n;j++)
			if(!st[j]&&(t==-1||dist[t]>dist[j]))
				t=j;
		if(i) res+=dist[t];
		for(int j=1;j<=n;j++) dist[j]=min(dist[j],g[t][j]);
		st[t]=true;
	}
	return res;
}

int main()
{
	while(cin>>n&&n&&cin>>m)
	{
		memset(g,0x3f,sizeof g);
		for(int i=0;i<m;i++)
		{
			int x,y,z;
			cin>>x>>y>>z;
			g[x][y]=g[y][x]=min(g[x][y],z);
		}
		int ans=prim();
		cout<<ans<<endl;
	}
	return 0;
}

           

C - 3

There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected.

We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.

Input

The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.

Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.

Output

You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.

Sample Input

3
0 990 692
990 0 179
692 179 0
1
1 2
           

Sample Output

179
           

AC代码

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
const int N=300;
int n,m;
int g[N][N];
int f[N];
struct P{
	int st,ed;
	int l;
}p[N*N];
int k;

bool cmp(P a,P b)
{
	return a.l<b.l;
}

int find(int op)
{
	if(f[op]!=op) return find(f[op]);
	return f[op];
}

void ion(int x,int y)
{
	f[find(x)]=find(y);
}

int kruskal()
{
	sort(p,p+k,cmp);
	int res=0;
	for(int i=1;i<=n;i++) f[i]=i;
	
	cin>>m;
	for(int i=0;i<m;i++)
	{
		int a,b;
		cin>>a>>b;
		if(find(a)!=find(b))
			ion(a,b);
	}
	int cnt=n-1;
	for(int i=0;i<k;i++)
	{
		int x=p[i].st;
		int y=p[i].ed;
		int k=p[i].l;
		if(find(x)!=find(y))
		{
			res+=k;
			cnt--;
			ion(x,y);
		}
	}
	return res;
}

int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
		{
			p[k].st=i;
			p[k].ed=j;
			cin>>p[k].l;
			k++;
		}
	int ans=kruskal();
	cout<<ans<<endl;
}

           

D - 4

某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小。请计算最小的公路总长度。

Input

测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( < 100 );随后的N(N-1)/2行对应村庄间的距离,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间的距离。为简单起见,村庄从1到N编号。

当N为0时,输入结束,该用例不被处理

Output

对每个测试用例,在1行里输出最小的公路总长度。

Sample Input

3
1 2 1
1 3 2
2 3 4
4
1 2 1
1 3 4
1 4 1
2 3 3
2 4 2
3 4 5
0
           

Sample Output

3
5
           

AC代码

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
const int N=10010;
int n,m;
struct P{
	int st,ed;
	int l;
}p[N];

int f[N];

bool cmp(P a,P b)
{
	return a.l<b.l;
}

int find(int op)
{
	if(f[op]!=op) return find(f[op]);
	return f[op];
}

void ion(int x,int y)
{
	f[find(x)]=find(y);
}

int kruskal()
{
	sort(p,p+m,cmp);
	int res=0;
	int cnt=n-1;
	for(int i=1;i<=n;i++) f[i]=i;
	for(int i=0;i<m&&cnt;i++)
	{
		int x=p[i].st;
		int y=p[i].ed;
		int k=p[i].l;
		if(find(x)!=find(y))
		{
			res+=k;
			cnt--;
			ion(x,y);
		}
	//	cout<<res<<endl;
	}
	return res;
}

int main()
{
	while(cin>>n&&n)
	{
		m=n*(n-1)/2;
		for(int i=0;i<m;i++)
		{
			cin>>p[i].st>>p[i].ed>>p[i].l;
		}
		
		int ans=kruskal();
		cout<<ans<<endl;
	}
}
           

E - 5

有一个正整数的数组,化为直方图,求此直方图包含的最大矩形面积。例如 2,1,5,6,2,3,对应的直方图如下:

QDU暑训1 最小生成树,树的直径,单调栈A - 1B - 2C - 3D - 4E - 5F - 6G - 7H - 8I - 9J - 10总结

面积最大的矩形为5,6组成的宽度为2的矩形,面积为10。

Input

第1行:1个数N,表示数组的长度(0 <= N <= 50000) 第2 - N+1行:数组元素A。(1 <= A <= 10^9)

Output

输出最大的矩形面积

Sample Input

6
2
1
5
6
2
3
           

Sample Output

10
           
单调栈的应用,维护一个单调递增的栈,对于每个矩形分别向两边扩展,求出最大的面积

AC代码

#include<iostream>
#include<stack>
using namespace std;
typedef long long ll;
const int N=50010;
ll p[N];
int n;
ll ans,cnt;

int main()
{
	cin>>n;
	for(int i=0;i<n;i++) cin>>p[i];
	p[n]=-1;
	stack<int> st;
	for(int i=0;i<=n;i++)
	{
		if(st.empty()||p[i]>=p[st.top()]) st.push(i);
		else
		{
			int top;
			while(!st.empty()&&p[i]<p[st.top()])
			{
				top=st.top();
				st.pop();
				cnt=p[top]*(i-top);
				if(cnt>ans) ans=cnt;
			}
			st.push(top);
			p[top]=p[i];
		}
	}
	cout<<ans<<endl;
}
           

F - 6

Description

N个矩形,排成一排. 现在希望用尽量少的矩形海报Cover住它们.

Input

第一行给出数字N,代表有N个矩形.N在[1,250000] 下面N行,每行给出矩形的长与宽.其值在[1,1000000000]2 1/2 Postering

Output

最少数量的海报数.

Sample Input

5 
1 2 
1 3 
2 2 
2 5 
1 4
           
QDU暑训1 最小生成树,树的直径,单调栈A - 1B - 2C - 3D - 4E - 5F - 6G - 7H - 8I - 9J - 10总结

Sample Output

4
           
QDU暑训1 最小生成树,树的直径,单调栈A - 1B - 2C - 3D - 4E - 5F - 6G - 7H - 8I - 9J - 10总结
单调栈的应用,每次维护一个单调递增的栈,当两张海报之间没有小于他们高度的海报时,最优的覆盖方法是覆盖最大的面积,当两张海报高度相同时,则可以用一张海报完全覆盖掉

AC代码

#include<iostream>
#include<stack>
using namespace std;
typedef long long ll;
int n;
int ans;

int main()
{
	stack<int> st;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		int x,y;
		cin>>x>>y;
		while(!st.empty()&&y<st.top())
		{
			st.pop();
		}
		while(!st.empty()&&st.top()==y)
		{
			ans++;
			st.pop();
		}
		st.push(y);
	}
	cout<<n-ans<<endl;
}
           

G - 7

After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exercise, so he has committed to create a bovine marathon for his cows to run. The marathon route will include a pair of farms and a path comprised of a sequence of roads between them. Since FJ wants the cows to get as much exercise as possible he wants to find the two farms on his map that are the farthest apart from each other (distance being measured in terms of total length of road on the path between the two farms). Help him determine the distances between this farthest pair of farms.

Input

  • Lines 1…: Same input format as “Navigation Nightmare”.

Output

  • Line 1: An integer giving the distance between the farthest pair of farms.

Sample Input

7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
           

Sample Output

52
           

Hint

The longest marathon runs from farm 2 via roads 4, 1, 6 and 3 to farm

5 and is of length 20+3+13+9+7=52.

AC代码

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=100100;
int n,m;
int f1[N],f2[N];
int ans;
struct Node{
	int e;
	int w;
	int ne;
}node[N];
int head[N],idx;

void init()
{
	memset(head,-1,sizeof head);
}

void add(int a,int b,int c)
{
	node[idx].e=b;
	node[idx].w=c;
	node[idx].ne=head[a];
	head[a]=idx++;
}

void dp(int x,int father)
{  
	for(int i=head[x];i!=-1;i=node[i].ne)
	{
		int j=node[i].e;
		if(j==father) continue;
		dp(j,x);
		if(f1[x]<f1[j]+node[i].w)
		{
			f2[x]=f1[x];
			f1[x]=f1[j]+node[i].w;
		}
		else if(f2[x]<f1[j]+node[i].w)
			f2[x]=f1[j]+node[i].w;
		ans=max(f1[x]+f2[x],ans);
	}
	return ;
}

int main()
{
	cin>>n>>m;
	init();
	for(int i=0;i<n-1;i++)
	{
		int x,y,z;
		char op;
		cin>>x>>y>>z>>op;
		add(x,y,z);
		add(y,x,z);
	}
	dp(1,-1);
	cout<<ans<<endl;
	return 0; 
}
           

H - 8

Some of Farmer John’s N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-conscious about her messy hairstyle, FJ wants to count the number of other cows that can see the top of other cows’ heads.

Each cow i has a specified height hi (1 ≤ hi ≤ 1,000,000,000) and is standing in a line of cows all facing east (to the right in our diagrams). Therefore, cow i can see the tops of the heads of cows in front of her (namely cows i+1, i+2, and so on), for as long as these cows are strictly shorter than cow i.

Consider this example:

=
           

= =

= - = Cows facing right -->

= = =

= - = = =

= = = = = =

1 2 3 4 5 6

Cow#1 can see the hairstyle of cows #2, 3, 4

Cow#2 can see no cow’s hairstyle

Cow#3 can see the hairstyle of cow #4

Cow#4 can see no cow’s hairstyle

Cow#5 can see the hairstyle of cow 6

Cow#6 can see no cows at all!

Let ci denote the number of cows whose hairstyle is visible from cow i; please compute the sum of c1 through cN.For this example, the desired is answer 3 + 0 + 1 + 0 + 1 + 0 = 5.

Input

Line 1: The number of cows, N.

Lines 2…N+1: Line i+1 contains a single integer that is the height of cow i.

Output

Line 1: A single integer that is the sum of c1 through cN.

Sample Input

6
10
3
7
4
12
2
           

Sample Output

5
           

题意:题目给出一排牛的身高,所有的牛都向右看,对于每头牛来说,若右边的牛的身高小于当前牛的身高则可以看到,反之不可以看到

维护一个单调递减的单调栈,对于每头牛来说,他可以被他左边的牛看到

AC代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<stack>
using namespace std;
typedef long long ll;
const int N=80010;
ll n;
ll x;
ll ans;

int main()
{
	cin>>n;
	stack<int> st;
	for(int i=0;i<n;i++)
	{
		int x;
		cin>>x;
		while(!st.empty()&&x>=st.top()) st.pop();
		ans+=st.size();
		st.push(x);
	}
	cout<<ans<<endl;
}
           

I - 9

A school bought the first computer some time ago(so this computer’s id is 1). During the recent years the school bought N-1 new computers. Each new computer was connected to one of settled earlier. Managers of school are anxious about slow functioning of the net and want to know the maximum distance Si for which i-th computer needs to send signal (i.e. length of cable to the most distant computer). You need to provide this information.

QDU暑训1 最小生成树,树的直径,单调栈A - 1B - 2C - 3D - 4E - 5F - 6G - 7H - 8I - 9J - 10总结

Hint: the example input is corresponding to this graph. And from the graph, you can see that the computer 4 is farthest one from 1, so S1 = 3. Computer 4 and 5 are the farthest ones from 2, so S2 = 2. Computer 5 is the farthest one from 3, so S3 = 3. we also get S4 = 4, S5 = 4.

Input

Input file contains multiple test cases.In each case there is natural number N (N<=10000) in the first line, followed by (N-1) lines with descriptions of computers. i-th line contains two natural numbers - number of computer, to which i-th computer is connected and length of cable used for connection. Total length of cable does not exceed 10^9. Numbers in lines of input are separated by a space.

Output

For each case output N lines. i-th line must contain number Si for i-th computer (1<=i<=N).

Sample Input

5
1 1
2 1
3 1
1 1
           

Sample Output

3
2
3
4
4
           
//不会做,在网上看了好多题解,但对树形DP又加上DFS(或BFS)后就不怎么理解了
//这里给上一份网上的代码吧。。。

//DFS

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
const int MAX=1e9+7;
int vist[10020];
int dp[10020];
vector<pair<int,int> > v[10020];
int m,n,ans,point;
void dfs(int x,int fx,int sum)
{
    if(ans<=sum)
    {
        ans=sum;
        point=x;
    }
    pair<int,int> t;
    for(int i=0;i<v[x].size();i++)
    {
        t=v[x][i];
        if(t.first==fx) continue;
        dfs(t.first,x,sum+t.second);
        dp[t.first]=max(dp[t.first],sum+t.second);
    }
}
int main()
{
    int n;
    int x,y,z;
    while(~scanf("%d",&n)&&n)
    {
        for(int i=1;i<n;i++)
        {
            scanf("%d %d",&x,&z);
            y=i+1;
            v[x].push_back(make_pair(y,z));//我采用邻接表来储存点和权值;
            v[y].push_back(make_pair(x,z));
        }
        memset(dp,0,sizeof dp);
        ans=0,point=0;
        dfs(1,-1,0);  //第一次从1开始,找到距离最远的点;
        dfs(point,-1,0);//第二次从上一次找到的端点开始dfs,目的找到每台电脑离这个端点的距离最大值;
        dfs(point,-1,0);//上一步又找到了这条线的另一端,这次从这个端点开始dfs,目的为找到每台电脑离这个端点的最大值
        for(int i = 1; i <= n; ++i)
        {
            printf("%d\n", dp[i]);
        }
        for(int i=0;i<=n;i++)
            v[i].clear();
    }
    return 0;
}

// BFS

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
const int MAX=1e9+7;
int dis[10020];
int vist[10020];
int dp[10020];
vector<pair<int,int> > v[10020];
int ans;
int bfs(int x)
{
    memset(vist,0,sizeof vist);
    memset(dis,0,sizeof dis);
    vist[x]=1;
    queue<int> q;
    q.push(x);
    int point=0;
    ans=0;
    int F=x;
    while(!q.empty())
    {

        F=q.front();
        q.pop();
        if(dis[F]>=ans)
        {
            ans=dis[F];
            point=F;
        }
        pair<int,int> t;
        for(int i=0;i<v[F].size();i++)
        {
            t=v[F][i];
            if(!vist[t.first])
            {
                vist[t.first]=1;
                dis[t.first]=t.second+dis[F];
                dp[t.first]=max(dp[t.first],dis[t.first]);
                q.push(t.first);
            }
        }
    }
    return point;
}
int main()
{
    int n;
    int x,y,z;
    while(~scanf("%d",&n))
    {
        for(int i=1;i<n;i++)
        {
            scanf("%d %d",&x,&z);
            y=i+1;
            v[x].push_back(make_pair(y,z));
            v[y].push_back(make_pair(x,z));
        }
        memset(dp,0,sizeof dp);
        int p=bfs(1);
        int pp=bfs(p);
        bfs(pp);
        for(int i=1;i<=n;i++)
            printf("%d\n",dp[i]);
        for(int i=0;i<=n;i++)
            v[i].clear();
    }
    return 0;
}


           

J - 10

Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated to studying how good or bad days influent people’s memories about some period of life.

A new idea Bill has recently developed assigns a non-negative integer value to each day of human life.

Bill calls this value the emotional value of the day. The greater the emotional value is, the better the daywas. Bill suggests that the value of some period of human life is proportional to the sum of the emotional values of the days in the given period, multiplied by the smallest emotional value of the day in it. This schema reflects that good on average period can be greatly spoiled by one very bad day.

Now Bill is planning to investigate his own life and find the period of his life that had the greatest value. Help him to do so.

Input

The first line of the input contains n - the number of days of Bill’s life he is planning to investigate(1 <= n <= 100 000). The rest of the file contains n integer numbers a1, a2, … an ranging from 0 to 106 - the emotional values of the days. Numbers are separated by spaces and/or line breaks.

Output

Print the greatest value of some period of Bill’s life in the first line. And on the second line print two numbers l and r such that the period from l-th to r-th day of Bill’s life(inclusive) has the greatest possible value. If there are multiple periods with the greatest possible value,then print any one of them.

Sample Input

6
3 1 6 4 5 2
           

Sample Output

60
3 5
           

AC代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<stack>
using namespace std;
typedef long long ll;
const int N=100010;
ll sum[N];
ll n;
ll p[N];
ll l[N],r[N];
ll ans,cnt;

int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		scanf("%lld",&p[i]);
		sum[i]=sum[i-1]+p[i];
	}
	stack<int> st;
	for(int i=1;i<=n;i++)
	{
		while(!st.empty()&&p[i]<=p[st.top()]) st.pop();
		if(st.empty()) l[i]=0;
		else l[i]=st.top();
		st.push(i);
	}
	while(!st.empty()) st.pop();
	for(int i=n;i>=1;i--)
	{
		while(!st.empty()&&p[i]<=p[st.top()]) st.pop();
		if(st.empty()) r[i]=n;
		else r[i]=st.top()-1;
		st.push(i);
	}
	int k;
	for(int i=1;i<=n;i++)
	{
		cnt=p[i]*(sum[r[i]]-sum[l[i]]);
//		cout<<cnt<<endl;
		if(cnt>=ans)
		{
			ans=cnt;
			k=i;
		}
	}
	cout<<ans<<endl;
	cout<<l[k]+1<<" "<<r[k]<<endl;
	return 0;
}
           

总结

这些题总体难度来说不算大,多数为模板题,继续加油!!!

继续阅读