Codeforces Round #622 (Div. 2) C1. Skyscrapers (easy version)(简单版本暴力)
This is an easier version of the problem. In this version n≤1000n≤1000
The outskirts of the capital are being actively built up in Berland. The company "Kernel Panic" manages the construction of a residential complex of skyscrapers in New Berlskva. All skyscrapers are built along the highway. It is known that the company has already bought nn plots along the highway and is preparing to build nn skyscrapers, one skyscraper per plot.
Architects must consider several requirements when planning a skyscraper. Firstly, since the land on each plot has different properties, each skyscraper has a limit on the largest number of floors it can have. Secondly, according to the design code of the city, it is unacceptable for a skyscraper to simultaneously have higher skyscrapers both to the left and to the right of it.
Formally, let's number the plots from 11 to nn . Then if the skyscraper on the ii -th plot has aiai floors, it must hold that aiai is at most mimi (1≤ai≤mi1≤ai≤mi ). Also there mustn't be integers jj and kk such that j<i<kj<i<k and aj>ai<akaj>ai<ak . Plots jj and kk are not required to be adjacent to ii .
The company wants the total number of floors in the built skyscrapers to be as large as possible. Help it to choose the number of floors for each skyscraper in an optimal way, i.e. in such a way that all requirements are fulfilled, and among all such construction plans choose any plan with the maximum possible total number of floors.
The first line contains a single integer nn (1≤n≤10001≤n≤1000 ) — the number of plots.
The second line contains the integers m1,m2,…,mnm1,m2,…,mn (1≤mi≤1091≤mi≤109 ) — the limit on the number of floors for every possible number of floors for a skyscraper on each plot.
Print nn integers aiai — the number of floors in the plan for each skyscraper, such that all requirements are met, and the total number of floors in all skyscrapers is the maximum possible.
If there are multiple answers possible, print any of them.
5
1 2 3 2 1
1 2 3 2 1
3
10 6 8
10 6 6
大意是让你填在n个位置填数,有两个限制:1.每个位置的数不能超过那个位置的最大范围。2.填的数的左右两边不能有比它大的数。
简单版暴力就可以过,枚举1e3个位置,可以看出是一个类似尖峰的形状,分别往两边递减,找到那个能取最大值的位置,输出即可。O(n^2)复杂度对于1e3可过。
不开long long见祖宗!!!
#include <bits/stdc++.h> #define INF 0x3f3f3f3f using namespace std; int a[1005]; int out[1005]; int main() { int n,i,j,k; cin>>n; int num=-1; long long ans=0; for(i=1;i<=n;i++) { scanf("%d",&a[i]); } for(i=1;i<=n;i++) { long long cnt=0; cnt+=a[i]; out[i]=a[i]; for(j=i-1;j>=1;j--) { cnt+=min(a[j],out[j+1]); out[j]=min(a[j],out[j+1]); } for(j=i+1;j<=n;j++) { cnt+=min(a[j],out[j-1]);//在两个限制之下取最小的 out[j]=min(a[j],out[j-1]); } if(cnt>=ans) { num=i; ans=cnt; } } out[num]=a[num]; for(j=num-1;j>=1;j--) { out[j]=min(a[j],out[j+1]); } for(j=num+1;j<=n;j++) { out[j]=min(a[j],out[j-1]); } for(i=1;i<=n;i++) { cout<<out[i]<<' '; } return 0; }