题解

01背包,类似于这道题,相似度99.999999%:

01-背包 P2663 越越的组队

 
一共有4科,每科的时间独立,然后每一科做一遍 P2663越越的组队,时间之和累加得到答案
 
考虑复习每一科的最短时间
由于可以左右脑并用,所以把题目简化一下就是把所有题目分成两组,取时间最长的一组作为答案,但是要使得时间最长的一组它的时间尽量短
我们知道两组数值越接近,肯定就是最优答案了
 

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>

using namespace std;

typedef long long ll;

inline int read()
{
    int ans=0;
    char last=' ',ch=getchar();
    while(ch<'0'||ch>'9') last=' ',ch=getchar();
    while(ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar();
    if(last=='-') ans=-ans;
    return ans;
}

int cnt[5],s[25];
int f[3000];
int ans=0,tot=0,ave=0;

int main()
{
    for(int i=1;i<=4;i++) cnt[i]=read();
    for(int t=1;t<=4;t++){
        memset(s,0,sizeof(s));
        memset(f,0,sizeof(f));
        tot=0,ave=0;
        for(int i=1;i<=cnt[t];i++) s[i]=read(),tot+=s[i];
        ave=tot/2;
        for(int i=1;i<=cnt[t];i++)
            for(int j=ave;j>=s[i];j--)
                f[j]=max(f[j],f[j-s[i]]+s[i]);
        ans+=(tot-f[ave]);
    }
    printf("%d\n",ans);
    
    return 0;
}

 

 

相关文章:

  • 2022-02-03
  • 2022-12-23
  • 2022-01-18
  • 2021-08-16
  • 2022-01-29
  • 2022-02-25
  • 2021-08-31
  • 2022-02-20
猜你喜欢
  • 2022-02-10
  • 2021-08-18
  • 2021-07-14
  • 2021-06-21
  • 2021-06-10
  • 2021-07-06
  • 2021-10-18
相关资源
相似解决方案