bzoj1563: [NOI2009]诗人小G
Description

Input

Output
Sample Input
4 9 3
brysj,
hhrhl.
yqqlm,
gsycl.
4 9 2
brysj,
hhrhl.
yqqlm,
gsycl.
1 1005 6
poet
1 1004 6
poet
Sample Output
--------------------
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。
#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的更多相关文章
- bzoj1563: [NOI2009]诗人小G 决策单调性(1D1D)
目录 题目链接 题解 代码 题目链接 bzoj1563: [NOI2009]诗人小G 题解 \(n^2\) 的dp长这样 \(f_i = min(f_j + (sum_i - sum_j - 1 - ...
- [BZOJ1563][NOI2009]诗人小G(决策单调性优化DP)
模板题. 每个决策点都有一个作用区间,后来的决策点可能会比先前的优.于是对于每个决策点二分到它会比谁在什么时候更优,得到新的决策点集合与区间. #include<cstdio> #incl ...
- BZOJ1563 NOI2009诗人小G(动态规划+决策单调性)
设f[i]为前i行的最小不协调度,转移枚举这一行从哪开始,显然有f[i]=min{f[j]+abs(s[i]-s[j]+i-j-1-m)p}.大胆猜想有决策单调性就好了.证明看起来很麻烦,从略.注意需 ...
- 2018.09.28 bzoj1563: [NOI2009]诗人小G(决策单调性优化dp)
传送门 决策单调性优化dp板子题. 感觉队列的写法比栈好写. 所谓决策单调性优化就是每次状态转移的决策都是在向前单调递增的. 所以我们用一个记录三元组(l,r,id)(l,r,id)(l,r,id)的 ...
- BZOJ1563:[NOI2009]诗人小G(决策单调性DP)
Description Input Output 对于每组数据,若最小的不协调度不超过1018,则第一行一个数表示不协调度若最小的不协调度超过1018,则输出"Too hard to arr ...
- BZOJ1563 NOI2009 诗人小G【决策单调性优化DP】
LINK 因为是图片题就懒得挂了 简要题意:有n个串,拼接两个串需要加一个空格,给你l和p,问你拼接后每个串的总长减l的绝对值的p次方的最小值 首先打表发现一下这题是决策单调的对于所有数据都成立就当他 ...
- 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 ...
- [bzoj1563][NOI2009]诗人小G(决策单调性优化)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1563 分析: 首先可得朴素的方程:f[i]=min{f[j]+|s[j]-j-s[i] ...
- 1563: [NOI2009]诗人小G
1563: [NOI2009]诗人小G https://lydsy.com/JudgeOnline/problem.php?id=1563 分析: 直接转移f[i]=f[j]+cost(i,j),co ...
随机推荐
- JDBC基本知识
JDBC的作用 JDBC为java访问数据库提供通用的API,可以为多种关系数据库提供统一访问.因为SQL是关系式数据库管理系统的标准语言,只要我们遵循SQL规范,那么我们写的代码既可以访问MySQL ...
- JBoss 系列十一:JBoss Cluster Framework Demo 介绍
内容概要 JBoss Cluster Framework Demo包括JGruops.JBossCache.Infinispan,我们在随后的系列中会使用和运行这些示例来说明JGroups.JBoss ...
- [Unity3D]Unity3D游戏开发之自己主动寻路与Mecanim动画系统的结合
大家好,欢迎大家关注我的博客,我是秦元培,我的博客地址是blog.csdn.net/qinyuanpei. 这段时间博主将大部分的精力都放在了研究官方演示样例项目上,主要是希望能够从中挖掘出有价值的东 ...
- STL 六大组件 功能与运用
STL 提供六大组件,彼此可以组合套用: 1 容器(containers):各种数据结构,如vector,list,deque,set,map,用来存放数据,从实现的角度来看,STL容器是一种clas ...
- Java基础知识强化之集合框架笔记36:List练习之键盘录入多个数据在控制台输出最大值
1. 键盘录入多个数据,以0结束,要求在控制台输出这多个数据中的最大值 分析: • 创建键盘录入数据对象 • 键盘录入多个数据,我们不知道多少个,所以用集合存储 • 以0结束,这个简单,只要键盘 ...
- oracle在linux配置信息
这两天在linux中给已有的oracle添加新实例,其中涉及数据库服务.监听配置,oracle服务是否正常.监听是否成功等操作,特此记录存档,以备后用. oracle服务启动操作命令 1.查看orac ...
- spring验证事务的代码,用到了mockito
package *.withdraw; import javax.annotation.Resource; import org.junit.Before; import org.junit.Test ...
- TypeScript笔记[5]泛型+Dictionary 转
TypeScript笔记[5]泛型 在C++.C#.Java等主流编程语言中,一般对泛型编程提供了支持.合理利用泛型,可以提高开发效率.提升代码质量. 例如在C++编程语言中,常常利用下面的结构表 ...
- 关键词:CodeSmith工具、Money类型、__UNKNOWN__
问题描述: 当数据库列类型有Money类型的时候,CodeSmith生成数据访问层会出错.有不能识别的类型.解决方法: 通过查找资料得知,数据库中的Money类型在DbType中是Currency(货 ...
- angularjs-ngTable select filter
jsp <td title="'Status'" filter="{status: 'select'}" filter-data="fn.sta ...