P2101 命运石之门的选择 题解
草了
我不得不承认我孤陋寡闻了
因为这是我见到的第一道卡MLE的题
前几次提交 \((60 - 80pts):\)
#include<algorithm>
#include<bitset>
#include<cctype>
#include<cerrno>
#include<clocale>
#include<cmath>
#include<complex>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<utility>
#include<vector>
#include<cwchar>
#include<cwctype>
#include<chrono>
#include<random>
#include<unordered_map>
using namespace std;
const int N=5001;
int n;
int h[N],f[N][N][2];
inline int read(){
int x=0,y=1;
char c=getchar();
while (c<'0'||c>'9'){
if(c=='-')
y=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
x=x*10+c-'0';
c=getchar();
}
return x*y;
}
int dfs(int l,int r,int low){
if(l>r)
return 0;
if(l==r){
if(low==h[l])
return 0;
else
return 1;
}
return min(r-l+1,f[l][r][0]-low+dfs(l,f[l][r][1]-1,f[l][r][0])+dfs(f[l][r][1]+1,r,f[l][r][0]));
}
int main(){
n=read();
for(int i=1;i<=n;i++){
h[i]=read();
f[i][i][0]=h[i];
f[i][i][1]=i;
}
for(int i=1;i<n;i++){
for(int j=i+1;j<=n;j++){
if(f[i][j-1][0]<h[j]){
f[i][j][1]=f[i][j-1][1];
f[i][j][0]=f[i][j-1][0];
}
else{
f[i][j][1]=j;
f[i][j][0]=h[j];
}
}
}
printf("%d",dfs(1,n,0));
return 0;
}
看了题解才会 \(qwq\)
\((100pts):\)
#include<algorithm>
#include<bitset>
#include<cctype>
#include<cerrno>
#include<clocale>
#include<cmath>
#include<complex>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<utility>
#include<vector>
#include<cwchar>
#include<cwctype>
#include<chrono>
#include<random>
#include<unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=5010;
int n;
ll h[N];
inline ll read(){
ll x=0,y=1;
char c=getchar();
while (c<'0'||c>'9'){
if(c=='-')
y=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
x=x*10+c-'0';
c=getchar();
}
return x*y;
}
ll dfs(int l,int r,ll high){
if(l==r)
return 1;
ll ans,m=-1,tmp=r-l+1;
for(int i=l;i<=r;i++)
if(m==-1||m>h[i])
m=h[i];
ans=m-high;
for(int i=l;i<=r;i++){
int mid;
if(h[i]==m)
continue;
else
for(int j=i;j<=r;j++){
mid=j;
if(j==r||h[j+1]==m)
break;
}
ans+=dfs(i,mid,m);
i=mid+1;
}
return min(tmp,ans);
}
int main(){
n=read();
for(int i=1;i<=n;i++)
h[i]=read();
printf("%lld",dfs(1,n,0));
return 0;
}