牛客IOI周赛18-普及组

题目链接

牛客IOI周赛18-普及组

D.能量水晶

题目描述

在MoveToEx打倒了巨龙之后获得了一颗神奇的能量水晶,他可以使用水晶中的能量来释放一些法术,但是很不幸,每一种法术他都只能释放一次.但是MoveToEx又想把水晶榨干.所以他必须把水晶用到不能再释放下一个法术为止.然后MoveToEx想知道,他有多少种方法释放法术.(注意,这里的方法仅指释放的法术种类不同而不包括释放的顺序不同).

输入描述:

第一行包含两个整数\(n \ m\),即MoveToEx能释放的法术的个数以及水晶的能量.
第二行有n个整数,表示每个法术所使用的能量值.

输出描述:

输出一个整数,表示答案.

示例1

输入

5 14
3 6 2 1 8

输出

3

解题思路

dp

先对数组从大到小排序,计算后缀和 \(s_i\),考虑 \(a_i\) 作为使能量不能再用的最小值时的贡献,此时 \(s_{i+1}\) 必选,此时选择前 \(i\) 个数和加上 \(s_{i+1}\) 在范围 \((m-a_i,m]\),即前 \(i\) 个数和范围在 \((m-a_i-s_{i+1},m-s_{i+1}]\) 的方案数,即转化为背包问题

  • 时间复杂度:\(O(nm)\)

代码

// Problem: 能量水晶
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/33254/D
// Memory Limit: 1048576 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

const int N=3005;
int n,m;
LL res,a[N],s[N],f[N];
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)cin>>a[i];
    sort(a+1,a+1+n,greater<int>());
    for(int i=n;i;i--)s[i]=s[i+1]+a[i];
    f[0]=1;
    for(int i=1;i<=n;i++)
    {
    	for(int j=0;j<=m;j++)
    		if(j>m-a[i]-s[i+1]&&j<=m-s[i+1])res+=f[j];
    	for(int j=m;j>=a[i];j--)f[j]+=f[j-a[i]];
    }
    cout<<res;
    return 0;
}
posted @ 2022-04-19 18:02  zyy2001  阅读(36)  评论(0编辑  收藏  举报