BZOJ3316: JC loves Mkk

BZOJ3316: JC loves Mkk

二分答案·优先队列

题解:

http://blog.csdn.net/ws_yzy/article/details/50560213

PoPoQQQ大爷:

看到平均值最大果断二分答案
看到长度[L,R]果断单调队列

把原数组复制一遍,断环为链。
二分一个答案x,求a[i]-x的前缀和。
问题变成了是否有两个距离满足要求的数,之差大于0.
维护一个前缀和的单调递增队列,枚举到i把i-L进队i-R之前的出队。
要求长度为偶数,所以分开维护两个单调队列即可。

注意用long double和long long。

跑的比较慢QwQ

Code:

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#define D(x) cout<<#x<<" = "<<x<<"  "
#define E cout<<endl
using namespace std;
typedef long long ll;
typedef long double ld;
const ll N = 200005;

ld sum[N]; ll n,L,R,up,down,a[N];


struct Que{
    ll d[N]; ll l,r;
    void clear(){ l=r=0; }
    void autopop(ll lim){
//      printf("autopop: "); D(lim); E;
        while(l<r && d[l]<lim) l++;
    }
    bool empty(){ return l==r; }
    ll front(){ return d[l]; }
    void push(ll p){
        if(p<0) return;
//      printf("push: "); D(p); D(sum[p]); E; 
        while(l<r && sum[d[r-1]]>sum[p]) r--;
        d[r++]=p;
    }
} q1, q2, *q;

ll gcd(ll a,ll b){ return b?gcd(b,a%b):a; }

bool check(ld x){
    for(ll i=1;i<=n*2;i++) sum[i]=sum[i-1]+a[i]-x;
    q1.clear(); q2.clear();
    for(ll i=1;i<=n*2;i++){
//      D(i); E;
        if(i&1) q=&q1; else q=&q2;
        if((i-L)&1)q1.push(i-L); else q2.push(i-L); 
        q->autopop(i-R);
        if(!q->empty()){
//          D(q->front()); D(sum[i]-sum[q->front()]); E;
            if(sum[i]-sum[q->front()]>=0){
                down=i-q->front();
                return true;
            }
        }
    }
    return false;
}

int main(){
    freopen("a.in","r",stdin);
    scanf("%lld%lld%lld",&n,&L,&R);
    for(ll i=1;i<=n;i++){
        scanf("%lld",a+i); a[n+i]=a[i];
    } 
    ld l=0,r=1e9,mid;
    for(ll i=1;i<=100;i++){
        mid=(l+r)/2;
        if(check(mid)) l=mid;
        else r=mid;
//      D(mid); E;
    }
    up=mid*down+0.5; 
//  D(up); D(down); E;
    ll g=gcd(up,down);
    up/=g; down/=g;
    if(down==1) printf("%lld\n",up);
    else printf("%lld/%lld\n",up,down);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值