传送门

首先可以注意到序列里面元素的顺序对答案是没有影响的,所以二话不说先排序再看看怎么搞

考虑枚举每种子序列可能产生的贡献并算一下产生这个贡献的子序列有多少

考虑设 $F(x)$ 表示选择的元素差值至少为 $x$ 的长度为 $k$ 的子序列的方案数

那么最终如果直接把每个 $F(x),x \in [1,max(a)]$ 加起来会发现,对于任意一种差值为 $t$ 的方案,它都被所有 $x<=t$ 的 $F(x)$ 各计算到了一次,那么总贡献即为 $t$,所以只要求出 $F$ 然后加起来就是我们要的答案

先不考虑复杂度,那么这个显然是可以 $dp$ 的,设 $f[i][j]$ 表示长度为 $i$ ,考虑了前 $j$ 个位置,第 $j$ 个位置强制选择时的子序列方案数

那么有转移 $f[i][j]+=f[i-1][k]$ 其中 $k<j$ 并且 $a_j-a_k>=x$ (注意这时 $a$ 已经按从小到大排序了)

注意到随着 $j$ 的增加,合法的 $k$ 一定是越来越大的并且是一段前缀区间,那么转移显然可以维护一个指针和一个前缀和加速到 $O(1)$

现在单次 $dp$ 的复杂度就是优秀的 $nk$ 了,那么再考虑一下枚举 $x$ 的复杂度,很好,复杂度达到了优秀的 $max(a)nk$

然后注意到一个看似微不足道的优化:枚举 $x$ 的时候如果 $x>max(a)/k+1$ ,那么一定不存在合法的长度为 $k$ 的序列了(显然吧)

发现复杂度就变成了 $\frac {max(a)} {k+1}nk$ 即 $max(a)n$ ...

然后就过了......

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=,mo=;
inline int fk(int x) { return x>=mo ? x-mo : x; }
int n,m,A[N];
int f[N][N],ans;
int main()
{
n=read(),m=read();
for(int i=;i<=n;i++) A[i]=read();
sort(A+,A+n+); int mx=A[n];
for(int I=;I<=mx/(m-);I++)
{
for(int i=;i<=n;i++) f[][i]=;
for(int i=;i<=m;i++)
{
int sum=,p=;
for(int j=;j<=n;j++)
{
while(A[p]+I<=A[j])
sum=fk(sum+f[i-][p++]);
f[i][j]=sum;
}
}
for(int i=;i<=n;i++) ans=fk(ans+f[m][i]);
}
printf("%d\n",ans);
return ;
}

Codeforces 1189F. Array Beauty的更多相关文章

  1. Array Beauty

    Array Beauty 给出一个长度为n的序列\(\{a_i\}\),定义一个序列的权值为其中元素两两之差的绝对值的最小值,询问\(\{a_i\}\)长度为K的子序列的权值之和\(\% 998244 ...

  2. Array Beauty CodeForces - 1189F (dp,好题)

    大意: 定义$n$元素序列$a$的美丽度为 $\min\limits_{1\le i<j\le n}|a_i-a_j|$. 给定序列$a$, 求$a$的所有长为$k$的子序列的美丽度之和. 记 ...

  3. Codeforces 797E - Array Queries

    E. Array Queries 题目链接:http://codeforces.com/problemset/problem/797/E time limit per test 2 seconds m ...

  4. Codeforces 1108E2 Array and Segments (Hard version) 差分, 暴力

    Codeforces 1108E2 E2. Array and Segments (Hard version) Description: The only difference between eas ...

  5. CodeForces 300A Array

    http://codeforces.com/problemset/problem/300/A 题意 :给你n个数字,让你分成3组,第一组各个数之积要小于0,第二组要大于0,第三组要等于0,符合要求的答 ...

  6. 网络流(最大流):CodeForces 499E Array and Operations

    You have written on a piece of paper an array of n positive integers a[1], a[2], ..., a[n] and m goo ...

  7. Codeforces 1108E2 Array and Segments (Hard version)(差分+思维)

    题目链接:Array and Segments (Hard version) 题意:给定一个长度为n的序列,m个区间,从m个区间内选择一些区间内的数都减一,使得整个序列的最大值减最小值最大. 题解:利 ...

  8. Codeforces 623B Array GCD

    Array GCD 最后的序列里肯定有a[1], a[1]-1, a[1]+1, a[n], a[n]-1, a[n]+1中的一个,枚举质因子, dp去check #include<bits/s ...

  9. CodeForces 57C Array 组合计数+逆元

    题目链接: http://codeforces.com/problemset/problem/57/C 题意: 给你一个数n,表示有n个数的序列,每个数范围为[1,n],叫你求所有非降和非升序列的个数 ...

随机推荐

  1. 使用linux中,最让人无语的是软件源

    使用linux的最大的障碍是软件源的配置和系统的安装,这两个搞定了,坚持使用下去都不是问题,如果实在不行,还可以win10下的linux子系统可以作为基本的使用.下面记录两个软件源: Found 2 ...

  2. RuntimeException异常处理汇总

    Java中所有异常的父类是Throwable类,在Throwable类下有两大子类: 一个是Error类,指系统错误异常,例如:VirtualMachineError 虚拟机错误,ThreadDeat ...

  3. the requested PHP extension dom is missing from your system

    composer  出错 the requested PHP extension dom is missing from your system 解决办法    yum install  php70w ...

  4. kvm安装及简单使用

    1 cat /etc/redhat-release      CentOS release 6.4 (Final)2 egrep ‘vmx|svm’ /proc/cpuinfo3 yum -y ins ...

  5. UNIX 历史问题 分布式系统的Thundering Herd效应 惊群效应

    https://uwsgi-docs.readthedocs.io/en/latest/articles/SerializingAccept.html One of the historical pr ...

  6. 分布式系统的应用程序性能监视工具,专为微服务、云本机架构和基于容器(Docker、K8s、Mesos)架构而设计。 SkyWalking

    Apache SkyWalking™ | SkyWalking Teamhttp://skywalking.apache.org/zh/ Application performance monitor ...

  7. python flask url参数

    python flask url参数 常见 url 传参中都是 xxx?xxx=xxx 问题来了 flask中我没有找到 关于xx? 问号的使用方式 是不是flask就不支持这种方式 如果有 rout ...

  8. java 枚举和数值的相互转换

    枚举简介 enum 的全称为 enumeration, 是 JDK 1.5  中引入的新特性,存放在 java.lang 包中 在实际编程中,往往存在着这样的“数据集”,它们的数值在程序中是稳定的,而 ...

  9. ubuntu报错解决和注意事项

    1.在容器中安装expect报错 [root@kube-node3 target]# docker exec -it 36563e55c42b sh$ sudo apt-get install exp ...

  10. Win10系统,Jmeter 字体调整方法

    找到jmeter所在目录--->bin--->jmeter.properties, 1.搜索jsyntaxtextarea.font.size,去掉#,把14改成更大的数字 2.修改右侧参 ...