More than 2019 cattle off summer school camp (second field) -F artition problem

Topic link: https: //ac.nowcoder.com/acm/contest/882/F

The meaning of problems: the 2 × n individuals into two groups of n individuals seeking a competitive value and another group owner and group owner.

Ideas:

  Wrong title of the game, that seek competitive value of each group of people and write for two hours, that would be submitted when T, the result has been WA, autistic. .

  After the read title, began the T. .

  n <= 14, using the search + pruning can be done. Search has C (2 * n, n) complexity, but each requires a complex result of the O (n * n) is calculated, the total complexity is O (n * n * C (2 * n, n)) will certainly T.

  Optimization: simplified calculation result, the first pre-processing each row and v2, v2 [i] of the i-th i.e. contention value for all other individual and, as vector memory had been selected people, the individual selects the next x time, with v2 [i] by subtracting 2 × (all v1 [x] [i]), i is the number of people has been selected from flies, Save by 2 because there is no time prior to addition of i. Such complexity is reduced to O (n * C (2 * n, n)), gave the title as 4s, it is off.

     In addition, the first directly elected individual, because the individual is certainly the first to be on a team where the election results, as this can reduce the complexity of the 1-fold.

 AC Code:

#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;

typedef long long LL;
int n;
LL v1[30][30],v2[30],ans;
vector<int> vc;

void dfs(int x,LL sum){
    if(vc.size()==n){
        if(sum>ans) ans=sum;
        return;
    }
    if(x>2*n) return;
    LL tmp=v2[x];
    for(int i=0;i<vc.size();++i)
        tmp-=2*v1[x][vc[i]];
    vc.push_back(x);
    dfs(x+1,sum+tmp);
    vc.pop_back();
    dfs(x+1,sum);
}

int main(){
    scanf("%d",&n);
    for(int i=1;i<=2*n;++i)
        for(int j=1;j<=2*n;++j)
            scanf("%lld",&v1[i][j]),v2[i]+=v1[i][j];
    vc.push_back(1);
    dfs(2,v2[1]);
    printf("%lld\n",ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/FrankChen831X/p/11221767.html