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。

题解:
https://www.byvoid.com/blog/noi-2009-poet
code:
 #include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
char ch;
bool ok;
void read(int &x){
for (ok=,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=;
for (x=;isdigit(ch);x=x*+ch-'',ch=getchar());
if (ok) x=-x;
}
typedef long double int64;
const int maxn=;
const int maxl=;
const int64 maxval=1E18;
char s[maxl];
int T,n,l,p;
int64 sum[maxn],f[maxn];
bool flag;
int64 ksm(int64 a,int b){
int64 t;
for (t=;b;a*=a,b>>=) if (b&) t*=a;
return t;
}
int64 calc(int j,int i){return f[j]+ksm(abs(sum[i]-sum[j]+i-j--l),p);}
struct Stack{
int top,pos;
struct Data{
int st,ed,id;
}s[maxn],tmp;
void init(){s[top=]=(Data){,n,},pos=;}
bool cmp(int t,int x,int y){return calc(x,t)<calc(y,t);}
int get(int id){
int l=tmp.st,r=tmp.ed,m,a=tmp.id;
while (l<r){
m=((l+r)>>)+;
if (cmp(m,a,id)) l=m; else r=m-;
}
return l;
}
void push(int id){
while (top&&!cmp(s[top].st,s[top].id,id)) top--;
tmp=s[top--];
int m=get(id);
if (tmp.st<=m) s[++top]=(Data){tmp.st,m,tmp.id};
if (m<n) s[++top]=(Data){m+,n,id};
}
int64 query(int x){
while (x>s[pos].ed) pos++;
return calc(s[pos].id,x);
}
}stack;
int main(){
for (read(T);T;T--){
read(n),read(l),read(p);
for (int i=;i<=n;i++) scanf("%s",s+),sum[i]=sum[i-]+strlen(s+);
stack.init(),flag=;
for (int i=;i<=n;i++){
f[i]=stack.query(i);
stack.push(i);
}
if (f[n]>maxval) puts("Too hard to arrange");
else printf("%lld\n",(long long)f[n]);
puts("--------------------");
}
return ;
}

bzoj1563: [NOI2009]诗人小G的更多相关文章

  1. bzoj1563: [NOI2009]诗人小G 决策单调性(1D1D)

    目录 题目链接 题解 代码 题目链接 bzoj1563: [NOI2009]诗人小G 题解 \(n^2\) 的dp长这样 \(f_i = min(f_j + (sum_i - sum_j - 1 - ...

  2. [BZOJ1563][NOI2009]诗人小G(决策单调性优化DP)

    模板题. 每个决策点都有一个作用区间,后来的决策点可能会比先前的优.于是对于每个决策点二分到它会比谁在什么时候更优,得到新的决策点集合与区间. #include<cstdio> #incl ...

  3. BZOJ1563 NOI2009诗人小G(动态规划+决策单调性)

    设f[i]为前i行的最小不协调度,转移枚举这一行从哪开始,显然有f[i]=min{f[j]+abs(s[i]-s[j]+i-j-1-m)p}.大胆猜想有决策单调性就好了.证明看起来很麻烦,从略.注意需 ...

  4. 2018.09.28 bzoj1563: [NOI2009]诗人小G(决策单调性优化dp)

    传送门 决策单调性优化dp板子题. 感觉队列的写法比栈好写. 所谓决策单调性优化就是每次状态转移的决策都是在向前单调递增的. 所以我们用一个记录三元组(l,r,id)(l,r,id)(l,r,id)的 ...

  5. BZOJ1563:[NOI2009]诗人小G(决策单调性DP)

    Description Input Output 对于每组数据,若最小的不协调度不超过1018,则第一行一个数表示不协调度若最小的不协调度超过1018,则输出"Too hard to arr ...

  6. BZOJ1563 NOI2009 诗人小G【决策单调性优化DP】

    LINK 因为是图片题就懒得挂了 简要题意:有n个串,拼接两个串需要加一个空格,给你l和p,问你拼接后每个串的总长减l的绝对值的p次方的最小值 首先打表发现一下这题是决策单调的对于所有数据都成立就当他 ...

  7. BZOJ1563: [NOI2009]诗人小G(决策单调性 前缀和 dp)

    题意 题目链接 Sol 很显然的一个dp方程 \(f_i = min(f_j + (sum_i - sum_j - 1 - L)^P)\) 其中\(sum_i = \sum_{j = 1}^i len ...

  8. [bzoj1563][NOI2009]诗人小G(决策单调性优化)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1563 分析: 首先可得朴素的方程:f[i]=min{f[j]+|s[j]-j-s[i] ...

  9. 1563: [NOI2009]诗人小G

    1563: [NOI2009]诗人小G https://lydsy.com/JudgeOnline/problem.php?id=1563 分析: 直接转移f[i]=f[j]+cost(i,j),co ...

随机推荐

  1. 深入浅出node(4) 异步编程

    一)函数式编程基础 二)异步编程的优势和难点 2.1 优势 2.2 难点 2.2.1 异常处理 2.2.2 函数嵌套过深 2.2.3 阻塞 2.2.4 多线程编程 2.2.5 异步转同步 三)异步编程 ...

  2. NIO设置SO_LINGER引发的异常

    欢迎关注Github:https://github.com/teaey/ ### 背景 银时跟我讲,想从 Netty3迁移到Netty4 . 问其原因是由于 Netty3在容器里会报错,错误堆栈: j ...

  3. MVC ASPX(webForm)视图引擎 &lt;%:%&gt; 与&lt;%=%&gt;的差别

    控制器 using System; using System.Collections.Generic; using System.Linq; using System.Web; using Syste ...

  4. MapReduce优化

    Combiner和Partitioner是用来优化MapReduce的,可以提高MapReduce的运行效率.下面我们来具体学习这两个组件 Combiner 我们以WordCount为例,首先通过下面 ...

  5. oracle重建、更新索引、索引统计信息命令

    在oracle中查找所有的表的索引的命令 select t.*,i.index_type from user_ind_columns t,user_indexes i where t.index_na ...

  6. 学点bootstrap

  7. U3D 脚本添加和获得对象

    有时候,一开始可能没有对象,而是由于某种触发,产生的一个对象,这里讲解下,如何通过脚本来创建一个对象: 这是通过脚本创建一个立方体: using UnityEngine; using System.C ...

  8. Java 最简单的批处理

    批处理Batch && PreparedStatement : import java.sql.*; public class TestBatch { public static vo ...

  9. django连接已有的数据库

    以连接postgresql为例: 1.安装psycopg2,下载地址:http://www.stickpeople.com/projects/python/win-psycopg/ 2.配置setti ...

  10. Android Parcelable Trans byte[]

    思路: http://stackoverflow.com/questions/10898116/make-custom-parcelable-containing-byte-array 谢谢, 这位外 ...