Codeforces 1188C Array Beauty dp(看题解)

Array Beauty

最关键的一点是, 可以枚举最小值, 对于每个最小值算出大于等于它的方案数, 这个复杂度是科学的因为 i * (k - 1) <= dif

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);

using namespace std;

const int N = 1000 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-8;
const double PI = acos(-1);

template<class T, class S> inline void add(T &a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T &a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T &a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T &a, S b) {return a > b ? a = b, true : false;}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n, k, a[N];
int dp[N][N];
int sum[N];
int way[100007];

int calc(int d) {
    int ans = 0;
    for(int i = 0; i <= k; i++) {
        for(int j = 0; j <= n; j++) {
            dp[i][j] = 0;
        }
    }
    dp[0][0] = 1;
    for(int i = 0; i < k; i++) {
        sum[0] = dp[i][0];
        for(int j = 1; j <= n; j++) {
            sum[j] = (sum[j - 1] + dp[i][j]) % mod;
        }
        int p = 1;
        for(int j = 1; j <= n; j++) {
            while(a[j] - a[p] >= d) p++;
            dp[i + 1][j] = sum[p - 1];
        }
    }
    for(int j = k; j <= n; j++) {
        add(ans, dp[k][j]);
    }
    return ans;
}

int main() {
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
    }

    sort(a + 1, a + 1 + n);
    int dif = a[n] - a[1];

    int ans = 0;
    for(int i = 1; i * (k - 1) <= dif; i++) {
        way[i] = calc(i);
    }
    for(int i = 1; i * (k - 1) <= dif; i++) {
        sub(way[i], way[i + 1]);
        add(ans, 1LL * i * way[i] % mod);
    }
    printf("%d\n", ans);

    return 0;
}

/*
*/

 

转载于:https://www.cnblogs.com/CJLHY/p/11142571.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值