# [NOI2009]诗人小G

## 输入输出样例

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


108
brysj,
hhrhl.
yqqlm,
gsycl.
--------------------
32
brysj, hhrhl.
yqqlm, gsycl.
--------------------
Too hard to arrange
--------------------
1000000000000000000
poet
--------------------


## 说明/提示

【样例说明】

https://www.luogu.com.cn/blog/83547/zong-dong-tai-gui-hua-di-ben-zhi-kan-si-bian-xing-fou-deng-shi-you-hua

 1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<algorithm>
5 using namespace std;
6 typedef long double ld;
7 ld f[100005],l,p,len[100005];
8 char w[100005][31];
9 int q[200005],R[100005],n,pre[100005];
10 ld qpow(ld x,int k)
11 {
12     ld res=1;
13     while (k)
14     {
15         if (k&1) res=res*x;
16         x=x*x;
17         k>>=1;
18     }
19     return res;
20 }
21 ld Abs(ld x)
22 {
23     if (x<0) return -x;
24     return x;
25 }
26 ld getValue(int x,int y)
27 {
28     return f[y]+qpow(Abs(len[x]-len[y]-l),p);
29 }
30 int binary(int x,int y)
31 {
32     int l=y,r=n+1,mid;
33     while (l<r)
34     {
35         int mid=(l+r)/2;
36         if (getValue(mid,y)>=getValue(mid,x)) r=mid;
37         else l=mid+1;
38     }
39     return l;
40 }
41 int main()
42 {int T,i,flag,j;
43 //freopen("P1912_2.in","r",stdin);
44 //freopen("P1912.out","w",stdout);
45     scanf("%d",&T);
46     while (T--)
47     {
48         scanf("%d%Lf%Lf",&n,&l,&p);
49         l++;
50         for (i=1;i<=n;i++)
51         {
52             scanf("%s",w[i]);
53             len[i]=strlen(w[i]);
54             len[i]+=len[i-1]+1;
55         }
56         int h=1,t=1;
57         q[1]=0;flag=1;
58         for (i=1;i<=n;i++)
59         {
60             while (h<t&&R[h]<=i) h++;
61             f[i]=getValue(i,q[h]);
62             pre[i]=q[h];
63             if (f[i]>1e18) flag=0;
64             while (h<t&&R[t-1]>=binary(i,q[t])) t--;
65             R[t]=binary(i,q[t]);
66             q[++t]=i;
67         }
68         if (f[n]>1e18) printf("Too hard to arrange\n");
69         else
70         {
71             t=0;q[0]=n;
72             printf("%.0Lf\n",f[n]);
73             for (i=pre[n];i;i=pre[i]) q[++t]=i;
74             q[++t]=0;
75             for (i=t;i;i--)
76             {
77                 for (j=q[i]+1;j<q[i-1];j++)
78                  printf("%s ",w[j]);
79                  printf("%s\n",w[j]);
80             }
81         }
82         printf("--------------------\n");
83     }
84 } 

posted @ 2020-01-29 18:39  Z-Y-Y-S  阅读(224)  评论(1编辑  收藏