Array Beauty

给出一个长度为n的序列\(\{a_i\}\),定义一个序列的权值为其中元素两两之差的绝对值的最小值,询问\(\{a_i\}\)长度为K的子序列的权值之和\(\% 998244353\),\(2\leq k\leq n\leq 1000,max(a_i)\leq 10^5\)。

询问权值之和,除了枚举全部情况加起来,还有一种思路,对于每个权值看其出现的次数。

于是可以枚举权值,找规律容易知道一种方案对应权值最大上限为\(max(a_i)/(k-1)\),枚举权值i,因为子序列具有明显的可递推性,考虑dp,但是不好处理权值正好为i,这种情况,显然考虑差分。

因此设\(f[j][k]\)表示长度为j,末尾以第k个元素结尾,然后权值大于等于i的方案数,然后发现子序列的选择与顺序无关,于是可以给\(\{a_i\}\)排序,然后我们有

\[f[j][k]=\sum_{l=j-1,a_k-a_l\geq i}^{k-1}f[j-1][k]
\]

边界:\(f[0][0]=1\)

最后对于每个i累加所有的\(\sum_{k=K}^nf[K][k]\)就是答案了。

时间复杂度为\(O(\max(a_i)/(k-1)\times nk=nmax(a_i))\)。

我觉得这到题目有几个地方,我没有反应过来,正好为某值不好求,应马上反应差分,数列集合问题第一反应是否能够排序。

参考代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define il inline
#define ri register
#define ll long long
#define Size 1050
#define xzz 998244353
using namespace std;
int a[Size],dp[Size][Size];
il void read(int&);
int main(){
int n,K,lim(0);read(n),read(K);
for(int i(1);i<=n;++i)
read(a[i]),lim=max(lim,a[i]);
sort(a+1,a+n+1),a[0]=-0x3f3f3f3f;
int i,j,k,l,r,ans(0);
for(i=1;i<=lim/(K-1);++i){
for(j=1,dp[0][0]=1;j<=K;++j)
for(k=j,l=j-1,r=0;k<=n;++k){
while(a[k]-a[l]>=i)
r=(r+dp[j-1][l])%xzz,++l;
dp[j][k]=r;
}
for(k=K;k<=n;++k)ans=(ans+dp[K][k])%xzz;
}printf("%d",ans);
return 0;
}
il void read(int &x){
x^=x;ri char c;while(c=getchar(),c<'0'||c>'9');
while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
}

Array Beauty的更多相关文章

  1. CF1188C Array Beauty(DP)

    日常降智. 不过还是第一次和 2700 的题正解这么近呢-- 由于排序后不影响答案,而且直觉告诉我们排序后会更好做,不妨排个序. 直觉告诉我们,变成求最小差 \(\ge v\) 的方案数会比最小差 \ ...

  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 1189F. Array Beauty

    传送门 首先可以注意到序列里面元素的顺序对答案是没有影响的,所以二话不说先排序再看看怎么搞 考虑枚举每种子序列可能产生的贡献并算一下产生这个贡献的子序列有多少 考虑设 $F(x)$ 表示选择的元素差值 ...

  4. Codeforces Round #365 (Div. 2) D 树状数组+离线处理

    D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes in ...

  5. Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum 离线+线段树

    题目链接: http://codeforces.com/contest/703/problem/D D. Mishka and Interesting sum time limit per test ...

  6. codeforces D

    D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes in ...

  7. Mishka and Interesting sum

    Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes input ...

  8. Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum 树状数组+离线

    D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes in ...

  9. codeforces 703D Mishka and Interesting sum 偶数亦或 离线+前缀树状数组

    题目传送门 题目大意:给出n个数字,m次区间询问,每一次区间询问都是询问 l 到 r 之间出现次数为偶数的数 的亦或和. 思路:偶数个相同数字亦或得到0,奇数个亦或得到本身,那么如果把一段区间暴力亦或 ...

随机推荐

  1. 转:KVM 虚拟机的克隆

    KVM 虚拟机的克隆 首先把需要克隆的源虚拟机先关闭,然后使用以下命令来进行克隆,注意我这里使用的是相对路径.   virsh shutdown VM02 virt-clone -o VM02 -n ...

  2. github 配置ssh

    https://www.cnblogs.com/superGG1990/p/6844952.html

  3. python2和python3中TestSuite().addTest的区别

    Python2中unittest.TestSuite().addTest()的参数是这样的:unittest.TestSuite().addTest(TestFun("test_nam&qu ...

  4. linux su su-的区别

    su只是切换用户. su - 切换用户并切换shell环境. su another pwd后为/home/current su - another pwd后为/home/another

  5. URAL 1057 Amount of Degrees (数位dp)

    Create a code to determine the amount of integers, lying in the set [X;Y] and being a sum of exactly ...

  6. oracle中解决中文乱码问题

    中文乱码问题解决 1.查看服务器端编码 select userenv('language') from dual; 查到结果是: USERENV('LANGUAGE') --------------- ...

  7. python 网络编程:socket(二)

    上节地址:Python网络编程:socket 一.send和sendall区别 send,sendall         ret = send('safagsgdsegsdgew') #send 发送 ...

  8. 前端每日实战:57# 视频演示如何用纯 CSS 创作一双黑暗中的眼睛

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/xzYVzO 可交互视频 此视频是可 ...

  9. 【C#、阿里云、Tomcat、XP系统】c#下使用.NET4.0中HttpWebRequest访问Tomcat中HTTPS项目时,在XP系统中超时

    情景: 1.使用Java开发的Web项目,部署在服务器Tomcat中 2.项目使用HTTPS,使用阿里云的PFX证书 阿里云推荐Tomcat配置如下 <Connector port=" ...

  10. 数据库的显示、创建、使用 、用户授权管理及忘记root用户后重置密码

    1.显示数据库 show databases; 默认的数据库及大致功能: mysql -- 用户权限 相关数据 test --用于用户测试数据 information_schema -MySQL 本身 ...