# [bzoj1122][POI2008]账本BBB

## 1122: [POI2008]账本BBB

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 402  Solved: 202
[Submit][Status][Discuss]

## Input

The first line contains 5 integers n, p, q, x and y (1  n  1000000, 0  p;q  1000000, 1  x;y  1000), separated by single spaces and denoting respectively: the number of transactions done by Byteasar, initial and final account balance and the number of seconds needed to perform a single turn (change of sign) and move of transaction to the beginning. The second line contains a sequence of n signs (each a plus or a minus), with no spaces in-between. 1 ≤ n ≤ 1000000, 0 ≤ p ,q ≤ 1000000, 1 ≤x,y ≤ 1000)

9 2 3 2 1
---++++++

3

## Source

TAT还是不会

(要是必要的操作是减变加，因为题目有解，所以修改操作一定不在最小值位置之前)

 1
2 #include<cstdio>
3 #include<cstdlib>
4 #include<cstring>
5 #include<algorithm>
6 #define LL long long
7 int que[2000020],seq[2000020],mn[1000010];
8 char opt[1000002];
9 inline LL abs(LL a){
10     return a>0?a:-a;
11 }
12 inline LL max(LL a,LL b){
13     return a>b?a:b;
14 }
15 inline LL min(LL a,LL b){
16     return a<b?a:b;
17 }
18 int main(){
19     LL n,p,q,x,y;
20     int i,h,t;
21     scanf("%lld %lld %lld %lld %lld",&n,&p,&q,&x,&y);
22     scanf("%s",opt+1);
23     for(i=n<<1;i>n;--i)seq[i]=seq[i+1]+(opt[i-n]=='+'?1:-1);
24     for(i=n;i;--i)seq[i]=seq[i+1]+(opt[i]=='+'?1:-1);
25     que[0]=0;
26     h=1,t=0;
27     for(i=n<<1;i;--i){
28         while(h<=t&&seq[i]>seq[que[t]])--t;//seq[i]-seq[que[h]]>seq[que[t]]-seq[que[h]]
29         que[++t]=i;
30         while(h<=t&&que[h]-i>=n)++h;
32     }
33     LL all=seq[n+1],tmp=(q-p-all)/2,ans=1e16,cst;
34     for(i=0;i<n;i++){//枚举起点与枚举旋转次数不同
35         cst=x*abs(tmp)+y*(LL)i;
36         if(i==0){
37             mn[1]+=p+max(tmp,0)*2;
38             if(mn[1]<0)cst+=2*x*((1-mn[1])/2);
39         }else{
40             mn[n-i+1]+=p+max(tmp,0)*2;
41             if(mn[n-i+1]<0)cst+=2*x*((1-mn[n-i+1])/2);
42         }
43         ans=min(ans,cst);
44     }
45     printf("%lld",ans);
46     return 0;
47 }
View Code

posted @ 2016-10-15 01:17  Pumbit-Legion  阅读(572)  评论(0编辑  收藏  举报