zoukankan      html  css  js  c++  java
  • 【BZOJ 1563】 (四边形优化、决策单调性)

    1563: [NOI2009]诗人小G

    Time Limit: 100 Sec  Memory Limit: 64 MB
    Submit: 2611  Solved: 840

    Description

    Input

    Output

    对于每组数据,若最小的不协调度不超过1018,则第一行一个数表示不协调度若最小的不协调度超过1018,则输出"Too hard to arrange"(不包含引号)。每个输出后面加"--------------------"

    Sample Input

    4
    4 9 3
    brysj,
    hhrhl.
    yqqlm,
    gsycl.
    4 9 2
    brysj,
    hhrhl.
    yqqlm,
    gsycl.
    1 1005 6
    poet
    1 1004 6
    poet

    Sample Output

    108
    --------------------
    32
    --------------------
    Too hard to arrange
    --------------------
    1000000000000000000
    --------------------

    【样例说明】
    前两组输入数据中每行的实际长度均为6,后两组输入数据每行的实际长度均为4。一个排版方案中每行相邻两个句子之间的空格也算在这行的长度中(可参见样例中第二组数据)。每行末尾没有空格。

    HINT

    总共10个测试点,数据范围满足:

    测试点 T N L P
    1 ≤10 ≤18 ≤100 ≤5
    2 ≤10 ≤2000 ≤60000 ≤10
    3 ≤10 ≤2000 ≤60000 ≤10
    4 ≤5 ≤100000 ≤200 ≤10
    5 ≤5 ≤100000 ≤200 ≤10
    6 ≤5 ≤100000 ≤3000000 2
    7 ≤5 ≤100000 ≤3000000 2
    8 ≤5 ≤100000 ≤3000000 ≤10
    9 ≤5 ≤100000 ≤3000000 ≤10
    10 ≤5 ≤100000 ≤3000000 ≤10
    所有测试点中均满足句子长度不超过30。

    Source

    【分析】

      BZOJ1010玩具装箱的加强版。这里是^p不是平方。

      这个是经典的1D/1D形式?【所谓1D/1D动态规划,指的是状态数为O(n),每一个状态决策量为O(n)的动态规划方程。

      可以看:1D/1D动态规划优化初步这题是经典的模型一

      

      证明自己化式子啊。。

      然后就是决策单调的意思,最优取值点不断右移。

      

      

       这个为什么我觉得写栈有点尴尬【要二分两次?】,双向链表就很好啊~~

      st[i]表示i这个点的决策区间的起始位置,结束位置为nt的起始的前一位或n。

     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<cstring>
     4 #include<iostream>
     5 #include<algorithm>
     6 using namespace std;
     7 #define LL long long
     8 #define LD long double
     9 #define Maxn 100010
    10 const long double INF=1e18;
    11 
    12 char s[50];
    13 LD a[Maxn],f[Maxn],len[Maxn],sm[Maxn],L;
    14 int lt[Maxn],nt[Maxn],st[Maxn],P;
    15 
    16 LD qpow(LD x,int b)
    17 {
    18     if(x<0) x=-x;
    19     LD ans=1.0;
    20     while(b)
    21     {
    22         if(b&1) ans*=x;
    23         x*=x;
    24         b>>=1;
    25     }
    26     return ans;
    27 }
    28 
    29 LD cal(int i,int j)
    30 {
    31     return f[j]+qpow(sm[i]-sm[j]+(LD)i-(LD)j-1.0-L,P);
    32 }
    33 
    34 bool check(int mid,int x,int y)
    35 {
    36     return cal(mid,x)>=cal(mid,y);
    37 }
    38 
    39 int n;
    40 int ffind(int l,int r,int x,int y)
    41 {
    42     int ans=n+1;
    43     while(l<=r)
    44     {
    45         int mid=(l+r)>>1;
    46         if(check(mid,x,y)) ans=mid,r=mid-1;
    47         else l=mid+1;
    48     }
    49     return ans;
    50 }
    51 
    52 int main()
    53 {
    54     int T;
    55     scanf("%d",&T);
    56     while(T--)
    57     {
    58         cin>>n>>L>>P;sm[0]=0;
    59         for(int i=1;i<=n;i++)
    60         {
    61             scanf("%s",s);len[i]=(LD)strlen(s);
    62             sm[i]=sm[i-1]+len[i];
    63         }
    64         for(int i=0;i<=n;i++) f[i]=INF+1,st[i]=n+1,lt[i]=i-1,nt[i]=i+1;
    65         f[0]=0;st[0]=1;
    66         int now=0;
    67         for(int i=1;i<=n;i++)
    68         {
    69             while(st[nt[now]]<=i) now=nt[now];
    70             f[i]=cal(i,now);
    71             while(st[lt[i]]>i)
    72             {
    73                 if(check(st[lt[i]],lt[i],i))
    74                 {
    75                     lt[i]=lt[lt[i]];
    76                     nt[lt[i]]=i;
    77                 }
    78                 else break;
    79             }
    80             st[i]=ffind(st[lt[i]],n,lt[i],i);
    81             if(st[i]>n) nt[lt[i]]=nt[i],lt[nt[i]]=lt[i];
    82         }
    83         if(f[n]>INF) printf("Too hard to arrange
    ");
    84         else cout<<(LL)f[n]<<endl;
    85         printf("--------------------
    ");
    86     }
    87     return 0;
    88 }
    View Code

    2017-04-26 10:06:39

  • 相关阅读:
    faster with MyISAM tables than with InnoDB or NDB tables
    w-BIG TABLE 1-toSMALLtable @-toMEMORY
    Indexing and Hashing
    MEMORY Storage Engine MEMORY Tables TEMPORARY TABLE max_heap_table_size
    controlling the variance of request response times and not just worrying about maximizing queries per second
    Variance
    Population Mean
    12.162s 1805.867s
    situations where MyISAM will be faster than InnoDB
    1920.154s 0.309s 30817
  • 原文地址:https://www.cnblogs.com/Konjakmoyu/p/6767180.html
Copyright © 2011-2022 走看看