# JZOJ5709. 【北大夏令营2018模拟5.13】数列

5
50 30 40 10 20

2

## code

#include <queue>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <time.h>
#define ll long long
#define N 100003
#define M 103
#define db double
#define P putchar
#define G getchar
#define inf 998244353
#define pi 3.1415926535897932384626433832795
using namespace std;
char ch;
{
n=0;
ch=G();
while((ch<'0' || ch>'9') && ch!='-')ch=G();
ll w=1;
if(ch=='-')w=-1,ch=G();
while('0'<=ch && ch<='9')n=(n<<3)+(n<<1)+ch-'0',ch=G();
n*=w;
}

int max(int a,int b){return a>b?a:b;}
int min(int a,int b){return a<b?a:b;}
ll abs(ll x){return x<0?-x:x;}
ll sqr(ll x){return x*x;}
void write(ll x){if(x>9) write(x/10);P(x%10+'0');}

int n,m,a[N],mi[N],ans,t,sum,mx,nxt,s[N];

int main()
{
freopen("sequence.in","r",stdin);
freopen("sequence.out","w",stdout);

for(register int i=1;i<=n;i++)
{
mi[i]=min(mi[i-1],a[i]);
mx=max(mx,a[i]-mi[i]);
}
for(int i=1;i<=n;i=nxt)
{
s[i]=1;t=2147483647;m=mi[i]+mx;sum=0;
for(nxt=i+1;mi[nxt]==mi[i] && nxt<=n;nxt++)s[nxt]=s[nxt-1]+(a[nxt]==mi[nxt]?1:0);
for(register int j=nxt-1;j>=i;j--)
{
t=min(t,sum+s[j]);
if(a[j]==m)sum++;
}
ans+=min(t,sum);
}
printf("%d",ans);
return 0;
}


• 评论

• 上一篇
• 下一篇