4 条题解

  • 1
    @ 2025-7-18 19:55:58
    #include<algorithm>
    #include<iostream>
    using namespace std;
    const int MAXN = 114514;
    int n,w,ans,c[MAXN],cab[MAXN];
    void dfs(int now,int cnt) {
    	if (cnt >= ans) {
    		return ;
    	}
    	if (now == n + 1) {
    		ans = min(ans,cnt);
    		return ;
    	}
    	for (int i = 1;i <= cnt;i++) {
    		if (cab[i] + c[now] <= w) {
    			cab[i] += c[now];
    			dfs(now + 1,cnt); 
    			cab[i] -= c[now];
    		}
    	}
    	cab[cnt + 1] = c[now];
    	dfs(now + 1,cnt + 1);
    	cab[cnt + 1] = 0;
    }
    int main() {
    	cin >> n >> w;
    	for (int i = 1;i <= n;i++) {
    		cin >> c[i];
    	}
    	sort(c + 1,c + n + 1);
    	reverse(c + 1,c + n + 1);
    	ans = n;
    	dfs(1,0);
    	cout << ans << endl;
    	return 0;
    }
    
    • 1
      @ 2021-8-7 21:05:12

      C++ :

      #include <iostream>
      #include <ctime>
      #include <algorithm>
      using namespace std;
      int n,w;
      int c[19];
      int use[19];
      int minn = 20;
      bool cmp(int x,int y)
      {
      	return x>y;
      }
      void dfs(int pos,int tot)
      {
      	if(tot>minn)
      		return ;
      	if(pos == n+1)
      	{
      		minn = min(minn,tot);
      		return ;
      	}
      	for(int i=1;i<=tot;i++)
      	{
      		if(c[pos] + use[i] <= w)
      		{
      			use[i] += c[pos];
      			dfs(pos+1,tot);
      			use[i] -= c[pos];
      		}
      	}
      	use[tot+1] = c[pos];
      	dfs(pos+1,tot+1);
      	use[tot+1] = 0;
      }
      int main()
      {
      	cin>>n>>w;
      	for(int i=1;i<=n;i++)
      		cin>>c[i];
      	// clock_t start = clock();
      	sort(c+1,c+1+n,cmp);
      	dfs(1,0);
      	// clock_t end = clock();
      	cout<<minn<<endl;
      	// cout<<end-start<<endl;
      	return 0;
      }
      

      Python :

      # coding=utf-8
      a,b=map(int,input().split())
      n=0
      cnt=0
      for x in range(a):
          n=int(input())
          cnt+=n
      if(int(cnt/b)==cnt/b):
          print(int(cnt/b))
      else:
          print(int(cnt/b)+1)
      
      
      • @ 2023-8-26 21:02:19

        您的Python程序只能得84分:

        84 Wrong Answer

        # 状态 耗时 内存占用
        ---------------------------------
        #1 Accepted 7ms 3 MiB
        -
        #2 Accepted 6ms 3.1 MiB
        #3 7ms 3 MiB
        #4 6ms
        #5
        #6 3.1 MiB
        #7
        #8 3 MiB
        #9
        #10 3.1 MiB
        #11 Wrong Answer 读取到 8,应为 10。 7ms 3 MiB
        #12 Accepted 6ms
        #13 Wrong Answer 读取到 9,应为 11。 3.1 MiB
    • -6
      @ 2021-11-29 17:48:36

      11

      • -6
        @ 2021-11-29 17:36:29
        # coding=utf-8
        a,b=map(int,input().split())
        n=0
        cnt=0
        for x in range(a):
            n=int(input())
            cnt+=n
        if(int(cnt/b)==cnt/b):
            print(int(cnt/b))
        else:
            print(int(cnt/b)+1)
        • 1

        信息

        ID
        76
        时间
        1000ms
        内存
        128MiB
        难度
        7
        标签
        递交数
        410
        已通过
        100
        上传者