Sol

决策单调性+二分

传说中的四边形不等式...其实做了这道题还是不会...

证明简直吃屎//// 贴个传送门这里有部分分做法还有决策单调性的证明 byvoid

ISA tell me that these problem could make a list to find DanDiaoXing.

(由于...导致我可以放个表上来...)

打表程序

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std; #define debug(a) cout<<#a<<"="<<a<<" "
const int N = 105;
typedef long long LL; LL T,n,l,p;
LL sum[N],f[N],g[N]; LL pow(LL x){
if(x<0) x=-x;LL res=1;
for(int i=1;i<=p;i++) res=res*x;
return res;
}
LL F(int i,int j){ return f[j]+pow(sum[i]-sum[j]+i-j-1-l); }
int main(){
freopen("in.in","r",stdin);
freopen("out.out","w",stdout);
for(cin>>T;T--;){
cin>>n>>l>>p;
for(int i=1;i<=n;i++) sum[i]=sum[i-1]+2;
memset(f,0x7f,sizeof(f));f[0]=0;
for(int i=1;i<=n;i++) for(int j=0;j<i;j++) if(f[i]>F(i,j)) g[i]=j,f[i]=F(i,j);
debug(n),debug(l),debug(p);cout<<endl<<"f[i]"<<endl;
for(int i=1;i<=n;i++) cout<<f[i]<<" ";cout<<endl;
cout<<"/////////////////////"<<endl;
cout<<endl<<"g[i]"<<endl;
for(int i=1;i<=n;i++) cout<<g[i]<<" ";
// cout<<"/////////////////////"<<endl;
cout<<endl<<"**************************************************"<<endl;
}
return 0;
}

输入文件

10
10 3 1
10 3 2
10 3 3
10 3 4
10 3 5
10 3 6
10 3 7
10 3 8
10 3 9
10 3 10

输出文件

n=10 l=3 p=1
f[i]
1 2 3 4 5 6 7 8 9 10
///////////////////// g[i]
0 0 1 2 3 4 5 6 7 8
**************************************************
n=10 l=3 p=2
f[i]
1 2 3 4 5 6 7 8 9 10
///////////////////// g[i]
0 1 2 3 4 5 6 7 8 9
**************************************************
n=10 l=3 p=3
f[i]
1 2 3 4 5 6 7 8 9 10
///////////////////// g[i]
0 1 2 3 4 5 6 7 8 9
**************************************************
n=10 l=3 p=4
f[i]
1 2 3 4 5 6 7 8 9 10
///////////////////// g[i]
0 1 2 3 4 5 6 7 8 9
**************************************************
n=10 l=3 p=5
f[i]
1 2 3 4 5 6 7 8 9 10
///////////////////// g[i]
0 1 2 3 4 5 6 7 8 9
**************************************************
n=10 l=3 p=6
f[i]
1 2 3 4 5 6 7 8 9 10
///////////////////// g[i]
0 1 2 3 4 5 6 7 8 9
**************************************************
n=10 l=3 p=7
f[i]
1 2 3 4 5 6 7 8 9 10
///////////////////// g[i]
0 1 2 3 4 5 6 7 8 9

我们可以发现每个点的最优决策点g[i]是单调递增的...

但是我一开始天真的认为点i的能作为最优决策点的起始位置也是单调递增的,显然他是一个凹或凸函数,并不具有单调性,意思就是我们需要维护一个双端队列用来作为最优决策点的转移.

每次入队都需要对于队尾元素进行出队操作,就是判断该点作为最优决策点的起始位置是否比队尾元素最优决策点的起始位置更靠左,如果是,就进行出队操作.

然后二分一下,它作为最优决策的位置.

PS:long long存不下,double也存不下,需要long double,亲测.

Code

#include<cstdio>
#include<utility>
#include<cstring>
#include<iostream>
using namespace std; #define mpr(a,b) make_pair(a,b)
const int N = 100005;
const double lim = 1e18;
typedef long long LL; int n,l,p,h,t;
LL sum[N];
int len[N],q[N],L[N],R[N];
long double f[N]; inline int in(int x=0,char ch=getchar()){ while(ch>'9'||ch<'0') ch=getchar();
while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x; }
long double MyPow(long double x){
long double a=1;if(x<0) x=-x;
for(int i=1;i<=p;i++) a=a*x;
return a;
}
long double F(int i,int j){ return f[j]+MyPow(sum[i]-sum[j]+i-j-1-l); }
int main(){
// freopen("in.in","r",stdin);
for(int T=in();T--;puts("--------------------")){
n=in(),l=in(),p=in();
char tmp[50];
for(int i=1;i<=n;i++){
scanf("%s",tmp);
len[i]=strlen(tmp);
sum[i]=sum[i-1]+len[i];
}
h=t=1,q[t]=0;L[h]=1,R[h]=n;
for(int i=1;i<=n;i++){
while(R[h]<i) h++;
f[i]=F(i,q[h]);
while(L[t]>i&&F(L[t],q[t])>F(L[t],i)) R[t-1]=R[t],t--;
int ll=L[t],rr=R[t],mm;
while(ll<=rr){
mm=(ll+rr)>>1;
if(F(mm,q[t])<=F(mm,i)) ll=mm+1;
else rr=mm-1;
}if(ll<=R[t]){ L[++t]=ll,R[t]=R[t-1],R[t-1]=ll-1,q[t]=i; }
}
if(f[n]>lim) puts("Too hard to arrange");
else printf("%lld\n",(long long)f[n]);
}
return 0;
}

  

NOI2009 诗人小G的更多相关文章

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

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

  2. 1563: [NOI2009]诗人小G

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

  3. [NOI2009]诗人小G --- DP + 决策单调性

    [NOI2009]诗人小G 题目描述: 小G是一个出色的诗人,经常作诗自娱自乐. 但是,他一直被一件事情所困扰,那就是诗的排版问题. 一首诗包含了若干个句子,对于一些连续的短句,可以将它们用空格隔开并 ...

  4. P1912 [NOI2009]诗人小G

    P1912 [NOI2009]诗人小G 思路: 平行四边形不等式优化dp 因为f(j, i) = abs(sum[i]-sum[j]+i-j-1-l)^p 满足平行四边形不等式 j < i f( ...

  5. LG1912 [NOI2009]诗人小G

    题意 题目描述 小G是一个出色的诗人,经常作诗自娱自乐.但是,他一直被一件事情所困扰,那就是诗的排版问题. 一首诗包含了若干个句子,对于一些连续的短句,可以将它们用空格隔开并放在一行中,注意一行中可以 ...

  6. [NOI2009] 诗人小G [题解]

    诗人小G 题目大意 给出 \(n\) 个长度不超过 \(30\) 的句子,要求你对其进行排版. 对于每一行,有一个规定的行标准长度 \(L\) ,每一行的不协调度等于该行的实际长度与行标准长度差的绝对 ...

  7. 不失一般性和快捷性地判定决策单调(洛谷P1912 [NOI2009]诗人小G)(动态规划,决策单调性,单调队列)

    洛谷题目传送门 闲话 看完洛谷larryzhong巨佬的题解,蒟蒻一脸懵逼 如果哪年NOI(放心我这样的蒟蒻是去不了的)又来个决策单调性优化DP,那蒟蒻是不是会看都看不出来直接爆\(0\)?! 还是要 ...

  8. 【BZOJ 1563】 [NOI2009]诗人小G

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

  9. bzoj1563: [NOI2009]诗人小G

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

随机推荐

  1. 安装vim的ycm

    环境centos 6.7 vim 7.3 安装vundle Vundle(Vim bundle)是一个Vim的插件管理器.它是把git操作整合进去,用户需要做的只是去GitHub上找到自己想要的插件的 ...

  2. win 2012 修改盘符

           开始--运行   diskmgmt.msc    ........

  3. Spring MVC学习笔记——Controller接口

  4. Set接口

    Set接口也是Collection接口的子接口,Set接口中不能加入重复的元素 Set接口的常用子类 1.散列的存放:HashSet HashSet是Set接口的一个子类,主要的特点是:里面不能存放重 ...

  5. delphi---控件使用

    1.TBitBtn控件 属性:Glyph,指定要显示的位图:    Layout ,设置位图在按钮的位置:Kind,要想用自设位图,这个属性要设置bkCustom; 2.TTreeView TTree ...

  6. MathML + MathJax在网页中插入公式

    http://www.mathjax.org/download/ http://www.w3.org/Math/Software/mathml_software_cat_editors.html ht ...

  7. Quartz.NET总结(二)CronTrigger和Cron表达式

    Quartz.NET的任务调度,主要就是依靠CronTrigger和Cron表达式.Cron是已经在UNIX存在了很长一段时间,它有着强大和可靠的调度能力.CronTrigger类也正是是基于Cron ...

  8. 用淘宝ip地址库查ip

    这是一个通过调用淘宝ip地址库实现ip地址查询的功能类 using System; using System.Collections.Generic; using System.Linq; using ...

  9. Yii 框架里数据库操作详解-[增加、查询、更新、删除的方法 'AR模式']

    public function getMinLimit () {        $sql = "...";        $result = yii::app()->db-& ...

  10. 一次插入多条记录 [mysql]

    调用多次INSERT语句不就可以插入多条记录了吗?但使用这种方法要增加服务器的负荷,因为,执行每一次SQL服务器都要同样对SQL进行分析.优化等操作.幸好MySQL提供了另一种解决方案,就是使用一条I ...