5 solutions

  • 2
    @ 2024-2-5 20:22:09
    #include<map>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define N 21
    #define M 1000017
    using namespace std;
    map<int,int>mp;
    int n,m,t[N],d[M],dp[M];
    int main()
    {
    	int T;cin >> T;
    	while(T--)
    	{
    		cin >> n >> m;
    		for(int i = 0;i < n;i += 1)cin >> t[i];
    		mp.clear();
    		for(int i = 1;i <= m;i += 1){
    			cin >> d[i];
    			mp[d[i]] = 1;
    		}
    		memset(dp,0,sizeof(dp));
    		int ed = (1<<n)-1;
    		dp[0] = 1;
    		for(int s = 1;s <= ed;s += 1){
    			int tim = 0;
    			for(int i = 0;i < n;i += 1)
    				if((1<<i) & s){
    					dp[s] |= dp[s-(1<<i)];
    					tim += t[i];
    				}
    			if(mp[tim])dp[s] = 0;
    		}
    		if(dp[ed])cout << "YES" << endl;
    		else cout << "NO" << endl;
    	} 
    	return 0;
    }
    
    
    • 1
      @ 2025-10-2 15:45:31
      #include <bits/stdc++.h>
      //#include <inout.h>
      #define int long long
      #define endl "\n"
      using namespace std;
      map<int,int>mp;
      int n,m,dp[1000005],d[1000005],a[30];
      signed main() {
      	int T;cin>>T;
      	while(T--){
      		cin>>n>>m;
      		for(int i=1;i<=n;i++)cin>>a[i];
      		mp.clear();
      		for(int i=1;i<=m;i++)cin>>d[i],mp[d[i]]=1;
      		memset(dp,0,sizeof(dp));
      		dp[0]=1;
      		for(int s=1;s<(1<<n);s++){
      			int tim=0;
      			for(int i=0;i<n;i++){
      				if((1<<i)&s){
      					dp[s]|=dp[s-(1<<i)];
      					tim+=a[i+1];
      				}
      			}
      			if(mp[tim])dp[s]=0;
      		}
      		puts(dp[(1<<n)-1]?"YES":"NO");
      	}
      	return 0;
      }
      • 1
        @ 2024-11-8 20:33:10

        抄下面题解

        • 1
          @ 2024-2-6 10:10:31

          抄下面题解

          • 0
            @ 2024-2-5 17:56:07

            Solution

            暴力做法:

            利用algorithmalgorithm库的nextpermutation()next_permutation()函数,跑出所有全排列,并对这些排列进行检查即可

            正解:

            如果我们当前选择了kk张作弊卡片,那么使用完这k张卡片后的时间timtimti\sum t_i

            我们将选择了哪些卡片压缩设为状态s

            状态转移方程:

            dps=dpt(其中ts的最大真子集,即不选某一张)dp_s |= dp_{t}(其中t为s的最大真子集,即不选某一张)

            需要注意的是:若timtim为某个检测节点,则dpsdp_s恒为0

            若最终每张卡片都能被选取,则输出YESYES,否则输出NONO

            • 1

            Information

            ID
            305
            Time
            1000ms
            Memory
            512MiB
            Difficulty
            7
            Tags
            (None)
            # Submissions
            140
            Accepted
            27
            Uploaded By