codeforces Round 572 F. Array Beauty

F. Array Beauty

time limit per test5 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output

Let’s call beauty of an array b 1 , b 2 , ⋯   , b n ( n > 1 ) — min ⁡ 1 ≤ i ≤ j ≤ n ∣ b i − b j ∣ b_1,b_2,\cdots,b_n (n>1) — \underset{1\le i\le j\le n}{\min} |b_i-b_j| b1,b2,,bn(n>1)1ijnminbibj
You’re given an array a 1 , a 2 , ⋯   , a n a_1,a_2,\cdots, a_n a1,a2,,an and a number k. Calculate the sum of beauty over all subsequences of the array of length exactly k. As this number can be very large, output it modulo 998244353.
A sequence a is a subsequence of an array b b b if a can be obtained from b b b by deletion of several (possibly, zero or all) elements.

Input

The first line contains integers n , k ( 2 ≤ k ≤ n ≤ 1000 ) n,k (2≤k≤n≤1000) n,k(2kn1000).
The second line contains n integers a 1 , a 2 , ⋯   , a n ( 0 ≤ a i ≤ 1 0 5 ) a_1,a_2,\cdots,a_n (0≤ai≤10^5) a1,a2,,an(0ai105).

Output

Output one integer — the sum of beauty over all subsequences of the array of length exactly k k k. As this number can be very large, output it modulo 998244353.

Example

input
4 3
1 7 3 5
output
8
input
5 5
1 10 100 1000 10000
output
9

Note

In the first example, there are 4 subsequences of length 3 — [1,7,3], [1,3,5], [7,3,5], [1,7,5], each of which has beauty 2, so answer is 8.

In the second example, there is only one subsequence of length 5 — the whole array, which has the beauty equal to |10−1|=9.

题解

给定一个数组,对于长度为 k k k的子序列(任意组合),定义序列的值为 min ⁡ 1 ≤ i ≤ j ≤ n ∣ b i − b j ∣ \underset{1\le i\le j\le n}{\min} |b_i-b_j| 1ijnminbibj,就是序列中相连数字差最小的位序列的值,问所有长度为 k k k的子序列的值的和为多少。
对于给定的数组,由于不是有序的,所有相连的值最小,肯定要先进行排序(从小到大),得到一个有序的数组,这样得到的子序列也是有序的。
假设一个序列的为 x x x,那么意味着 a r r [ i ] − a r r [ i − 1 ] ≥ x arr[i]-arr[i-1]\ge x arr[i]arr[i1]x,即所有的相连数字之差都大于等于 x x x
如果一个我们要找序列的值为1的个数为 N N N,那么和就是 1 × N 1\times N 1×N,如果序列的的值为 2 2 2的个数为 M M M,那么和就是 2 × M 2\times M 2×M,但是在 N N N个序列中肯定包含值为2的 M M M个序列,那么总的和就是 N + 2 × M − M × 1 = N + M N+2\times M -M\times 1=N+M N+2×MM×1=N+M M × 1 M\times 1 M×1就是在值为1中重复的个数),这样我们就可以发现我们从小到大连续 的序列的值,就是将他们的序列个数相加。
用动态规划的思想。
d p [ i n d e x ] [ l e n ] dp[index][len] dp[index][len]
其中:** i n d e x index index**表示第index数组加入序列, l e n len len表示此时序列的长度为len,那么 d p [ i n d e x ] [ l e n ] dp[index][len] dp[index][len]就表示第index个数字加入,此时长度为len的子序列的个数。
假设此时序列的值为 x x x
那么对于第index个数字,如果它不加入序列,那么此时就可以表示为
d p [ i n d e x ] [ l e n ] + = d p [ i n d e x − 1 ] [ l e n ] dp[index][len] += dp[index-1][len] dp[index][len]+=dp[index1][len]
如果加入序列,那么我们需要找到一个位置让 a r r [ i n d e x ] − a r r [ i ] ≥ x 1 ≤ i ≤ n o w arr[index]-arr[i]\ge x\quad 1\le i\le now arr[index]arr[i]x1inow
d p [ i n d e x ] [ l e n ] + = d p [ n o w ] [ l e n − 1 ] dp[index][len] += dp[now][len-1] dp[index][len]+=dp[now][len1]
综合起来就是
d p [ i n d e x ] [ l e n [ = d p [ i n d e x − 1 ] [ l e n ] + d p [ n o w ] [ l e n − 1 ] dp[index][len[ = dp[index-1][len] + dp[now][len-1] dp[index][len[=dp[index1][len]+dp[now][len1]
最后对于序列值为 x x x,得到的序列个数就是 d p [ n ] [ k ] dp[n][k] dp[n][k],由于这个值和 x x x有关,记为 f ( x ) f(x) f(x)
那么最终的结构就是
a n s = ∑ x = 1 1 0 5 k − 1 f ( x ) ans = \sum_{x=1}^{\frac{10^5}{k-1}}f(x) ans=x=1k1105f(x)

#include <bits/stdc++.h>

using namespace std;

const int MaxM = 1e5+100;

const int MaxN = 1100;

const int MOD = 998244353;

int dp[MaxN][MaxN];

int arr[MaxN];

int n,k;

int Solve(int x) {
    dp[0][0] = 1;
    int now = 0;
    for(int i = 1;i<=n;i++) {
        while(arr[i]-arr[now+1] >= x) now++;
        dp[i][0] = dp[i-1][0];
        for(int j = 1;j<=k;j++) {
            dp[i][j] = (dp[i-1][j]+dp[now][j-1])%MOD;
        }
    }
    return dp[n][k];
}

int main() {
    scanf("%d %d",&n,&k);
    for(int i = 1;i<=n;i++) {
        scanf("%d",&arr[i]);
    }
    sort(arr+1,arr+n+1);
    int ans = 0;
    for(int i = 1;i*(k-1)<=MaxM;i++) {
        ans = (ans+Solve(i))%MOD;
    }
    printf("%d\n",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,Codeforces Round 511 (Div. 1)是一个比赛的名称。然而,引用内容中没有提供与这个比赛相关的具体信息或问题。因此,我无法回答关于Codeforces Round 511 (Div. 1)的问题。如果您有关于这个比赛的具体问题,请提供更多的信息,我将尽力回答。 #### 引用[.reference_title] - *1* [Codeforces Round 860 (Div. 2)题解](https://blog.csdn.net/qq_60653991/article/details/129802687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces Round 867 (Div. 3)(A题到E题)](https://blog.csdn.net/wdgkd/article/details/130370975)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Codeforces Round 872 (Div. 2)(前三道](https://blog.csdn.net/qq_68286180/article/details/130570952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值