您的位置:首页 > 产品设计 > UI/UE

【bzoj1345】[Baltic2007]序列问题Sequence

2018-08-05 15:50 295 查看
  题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=1345

  因为合并的花费是较大数的权值,所以每个数肯定是和附近的小数合并完后才与大数合并,这样才不会造成浪费。所以我们可以用一个栈底大栈顶小的单调栈来维护序列, 每次把数压进去,被弹出的数就与要压进去的数合并。最后从上到下合并整个栈,就可以AC了。

  代码:

#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#define ll long long
#define ull unsigned long long
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define lowbit(x) (x& -x)
#define mod 1000000000
#define inf 0x3f3f3f3f
#define eps 1e-18
#define maxn 510
inline ll read(){ll tmp=0; char c=getchar(),f=1; for(;c<'0'||'9'<c;c=getchar())if(c=='-')f=-1; for(;'0'<=c&&c<='9';c=getchar())tmp=(tmp<<3)+(tmp<<1)+c-'0'; return tmp*f;}
inline ll power(ll a,ll b){ll ans=1; for(;b;b>>=1){if(b&1)ans=ans*a%mod; a=a*a%mod;} return ans;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void swap(int &a,int &b){int tmp=a; a=b; b=tmp;}
using namespace std;
int st[maxn];
int n,tp;
int main()
{
n=read();
ll ans=0;
tp=1; st[1]=read();
for(int i=2;i<=n;i++){
int k=read();
if(tp&&st[tp]<k){
while(tp&&st[tp]<k)--tp,ans+=st[tp];
ans+=k-st[tp];
}
st[++tp]=k;
}
for(int i=1;i<tp;i++)
ans+=st[i];
printf("%lld\n",ans);
}


bzoj1345
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: