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 ...
随机推荐
- cf 702B
You are given n integers a1, a2, ..., an. Find the number of pairs of indexes i, j (i < j) that a ...
- poj3624背包问题(一维数组)
Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd ...
- myeclipse svn
打开myeclipse的help---install from site 点击add弹出对话框 在输入框中输入对应内容 http://subclipse.tigris.org/update_1.10. ...
- [置顶] AFNetworking 2.0 新特性讲解之AFHTTPSessionManager
AFNetworking 2.0 相比1.0 API 接口改动还是很大的. 其中一个便是 AFURLSessionManager,当然如果你不太熟悉,或者为了兼容低版本,你依然可以选择AFHTTPRe ...
- 如何彻底删除SVN中的文件和文件夹(附恢复方法)
在SVN中如果删除某个文件或文件夹也可以在历史记录中进行找回,有的时候需要彻底删除某些文件,即不希望通过历史记录进行恢复,需要在服务器上对SVN的数据进行重新整理 这里假设SVN项目的目录为 /dat ...
- Android 颜色渲染(二) 颜色区域划分原理与实现思路
版权声明:本文为博主原创文章,未经博主允许不得转载. 上一篇讲到颜色选择器,该demo不能选择黑白或者具体区间颜色,这是为什么呢,还是要从原理部分讲起,首先看一下两张图: 图1 ...
- 移动端 设置 小于12px 字体 初探
1.移动端字号规范 2. 百度字号调研 3. 绕过12px 限制 4. 缩放 5. chrome 字号
- css考核点整理(十)-响应式开发经验,响应式页面的三种核心技术是什么
响应式开发经验,响应式页面的三种核心技术是什么
- 如何自定义echarts主题
上一篇,选择echarts原有的主题样式,那么如何自定义自己的主题 与选择原有主题类似 1.echarts官网地址http://echarts.baidu.com/echarts2/doc 在工具中 ...
- 2进制,16进制,BCD,ascii,序列化对象相互转换
public final static char[] BToA = "0123456789abcdef".toCharArray() ; 1.16进制字符串转为字节数组 /** * ...