zoukankan      html  css  js  c++  java
  • BZOJ 1563 诗人小G

    标准的1d/1d形式,具有决策单调性,二分栈优化nlogn。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #define maxn 100050
    #define inf 1000000000000000000LL
    using namespace std;
    int t,n,l,p,s[maxn],top=0,sum[maxn],g[maxn];
    char sj[maxn];
    long double dp[maxn];
    long double calc(int i,int j)
    {
        long double ret=sum[i]-sum[j]+i-j-l-1;ret=fabs(ret);long double ans=1.0;
        for (int i=1;i<=p;i++) ans=ans*ret;
        return ans+dp[j];
    }
    int get_pos(int x)
    {
        int l=1,r=top,ans=-1;
        while (l<=r)
        {
            int mid=(l+r)>>1;
            if (g[s[mid]]<=x) {ans=s[mid];l=mid+1;}
            else r=mid-1;
        }
        return ans;
    }
    int get_di(int l,int r,int x,int y)
    {
        int ans;
        while (l<=r)
        {
            int mid=(l+r)>>1;
            if (calc(mid,x)<calc(mid,y)) {ans=mid;l=mid+1;}
            else r=mid-1;
        }
        return ans+1;
    }
    void work()
    {
        memset(dp,0,sizeof(dp));memset(sum,0,sizeof(sum));memset(g,0,sizeof(g));
        scanf("%d%d%d",&n,&l,&p);
        for (int i=1;i<=n;i++) {scanf("%s",sj);sum[i]=sum[i-1]+strlen(sj);}
        top=1;s[1]=0;g[0]=1;
        for (int i=1;i<=n;i++)
        {
            int ret=get_pos(i);dp[i]=calc(i,ret);
            while (g[s[top]]>i && calc(g[s[top]],s[top])>calc(g[s[top]],i)) s[top--]=0;
            int pos=get_di(max(g[s[top]],i),n,s[top],i);
            if (pos==n+1) continue;
            s[++top]=i;g[s[top]]=pos;
        }
        if (dp[n]-0.5>inf) printf("Too hard to arrange
    ");
        else printf("%lld
    ",(long long)dp[n]);
        printf("--------------------
    ");
    }
    int main()
    {
        scanf("%d",&t);
        for (int i=1;i<=t;i++) work();
        return 0;
    }
  • 相关阅读:
    navigator对象及属性(userAgent)(扩展)
    最新Visual C++ 运行时
    Wakfu .pk 音频文件提取
    Flex布局学习记录
    小程序 swiper bindChange 抖动解决方法
    小程序 scroll-view 无法触发 onReachBottom 解决办法
    小程序修改按钮宽高
    db.collection(变量名)
    小程序图片轮播自适应
    微信小程序 MD5引用
  • 原文地址:https://www.cnblogs.com/ziliuziliu/p/6603529.html
Copyright © 2011-2022 走看看