思路:类似背包,直接搞
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<map> #include<cmath> #include<queue> #include<cstring> #include<set> #include<stack> #include<string> #include<ctime> #define LL long long #define u64 unsigned long long #define maxn 100010 #define INF 0x3f3f3f3f #define eps 1e-6 using namespace std; int a[1010]; bool vi[500010] ; vector<int>q; set<int>ss; void solve(int n) { memset(vi,0,sizeof(vi)) ; vi[0]=true; int v,Max=0; for( int i = 1 ; i <= n ;i++){ for(int j = Max ; j >= 0 ;j--)if(vi[j]) { vi[j+a[i]]=true; } Max+=a[i]; } } int main() { int L,n,m,ans1,ans2,sum,a1,a2 ; int i,j,k,T ; cin >> T ; while(T--) { scanf("%d",&n) ; sum=0; for( i = 1 ; i <= n ;i++ ){ scanf("%d",&a[i]) ; sum += a[i] ; } solve(n) ; m = sum/2; for( i = m ; i >= 0 ;i--)if(vi[i]) break ; ans1=i ; ans2=sum-i; k=0; sum=0; scanf("%d",&n) ; for( i = 1 ; i <= n ;i++ ){ scanf("%d",&a[i]) ; sum += a[i] ; } solve(n) ; m = sum/2; for( i = m ; i >= 0 ;i--)if(vi[i]) break ; a1=i ; a2=sum-i; if(a2-a1 < ans2-ans1 || (a2-a1==ans2-ans1&&a1+a2 >ans2+ans1) ) { ans1=a1; ans2=a2; k = 1; } sum=0; scanf("%d",&n) ; for( i = 1 ; i <= n ;i++ ){ scanf("%d",&a[i]) ; sum += a[i] ; } solve(n) ; m = sum/2; for( i = m ; i >= 0 ;i--)if(vi[i]) break ; a1=i ; a2=sum-i; if(a2-a1 < ans2-ans1 || (a2-a1==ans2-ans1&&a1+a2 >ans2+ans1) ) { ans1=a1; ans2=a2; k = 2; } printf("%c %d %d\n",‘A‘+k,ans1,ans2) ; } return 0 ; }
时间: 2024-10-06 16:35:12