[codeforces 1313C1] Skyscrapers (easy version) 问的是谷,答的是峰

Codeforces Round #622 (Div. 2)   比赛人数5752

[codeforces 1313C1] Skyscrapers (easy version)   问的是谷,答的是峰

总目录详见https://blog.csdn.net/mrcrack/article/details/103564004

在线测评地址https://codeforces.ml/contest/1313/problem/C1

Problem Lang Verdict Time Memory
C1 - Skyscrapers (easy version) GNU C++11 Accepted 31 ms 0 KB

比赛时,发现该题C1的AC率是B题的2-3倍,基本可以认为,以下算法是成熟算法。

在比赛时,也果断的放置B题,开始C1题的编码。

问的是谷,答的是峰

样例手动算法如下

Input1
5
1 2 3 2 1   没有谷
Output1
1 2 3 2 1   3是峰

Input2
3
10 6 8      6是谷

处理过程
10 6 6      10是峰   选和最大的输出,注意计算和,int要溢出
6 6 8       6是峰

Output2
10 6 6      10是峰


再造一组数据:在写这组数据的过程中,对
Also there mustn't be integers j and k such that j<i<k and aj>ai<ak. 
Plots j and k are not required to be adjacent to i.
有了更深刻的理解,j,i,k可以不相邻。
Input3
5 2 4 1 3   谷是2,1
处理后,合适数据如下:
5 2 2 1 1   5是峰   选和最大的输出,注意计算和,int要溢出
2 2 2 1 1   2是峰
2 2 4 1 1   4是峰
1 1 1 1 1   1是峰
1 1 1 1 3   3是峰
Output3
5 2 2 1 1

让输入的每个数据都成为峰,处理出新的数组,计算相应的和,and the total number of floors in all skyscrapers is the maximum possible.,选出最大的和。

AC代码如下

#include <stdio.h>
#define maxn 1010
#define LL long long
LL tot,sum;
int a[maxn],m[maxn],ans[maxn];
int min(int a,int b){
	return a<b?a:b;
}
int main(){
	int n,i,j;
	scanf("%d",&n);
	for(i=1;i<=n;i++)scanf("%d",&m[i]);
	for(i=1;i<=n;i++){
		a[i]=m[i],sum=m[i];//a[i]作为峰
		for(j=i-1;j>=1;j--)a[j]=min(m[j],a[j+1]),sum+=a[j];//峰的左侧
		for(j=i+1;j<=n;j++)a[j]=min(m[j],a[j-1]),sum+=a[j];//峰的右侧
		if(tot<sum){//找所有建筑总层数最多的情况
			tot=sum;
			for(j=1;j<=n;j++)ans[j]=a[j];//保存每个建筑楼层值
		}
	}
	for(i=1;i<n;i++)printf("%d ",ans[i]);
	printf("%d\n",ans[n]);
	return 0;
}
发布了552 篇原创文章 · 获赞 531 · 访问量 44万+

猜你喜欢

转载自blog.csdn.net/mrcrack/article/details/104472861