2019牛客暑期多校训练营(第二场)F——Partition problem(暴力搜素+思维)

链接:https://ac.nowcoder.com/acm/contest/882/F
来源:牛客网
 

时间限制:C/C++ 4秒,其他语言8秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

Given 2N people, you need to assign each of them into either red team or white team such that each team consists of exactly N people and the total competitive value is maximized.
 

Total competitive value is the summation of competitive value of each pair of people in different team.

 

The equivalent equation is ∑2Ni=1∑2Nj=i+1(vij if i-th person is not in the same team as j-th person else 0)∑i=12N∑j=i+12N(vij if i-th person is not in the same team as j-th person else 0)

输入描述:

The first line of input contains an integers N.

Following 2N lines each contains 2N space-separated integers vijvij is the j-th value of the i-th line which indicates the competitive value of person i and person j.

* 1≤N≤141≤N≤14
* 0≤vij≤1090≤vij≤109
* vij=vjivij=vji

输出描述:

Output one line containing an integer representing the maximum possible total competitive value.

示例1

输入

复制

1
0 3
3 0

输出

复制

3

题意:把2n个人分成2堆(每堆n人),求一堆里面每个人与另一堆里面每个人的竞争值的和最大的值,答案是随便一堆的最大值,因为最后两堆的值一定是一样的~~

题解:暴力搜索,一开始先在一堆里面,然后往另一堆里面拿,每一个人都有拿与不拿两种情况,最后暴力搜索出每一种情况,取一个最大值即可~~详情请看代码~~

上代码:

#include <vector>
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
const int MAX = 30;
ll a[MAX][MAX];
ll dp[MAX];
ll ans;
int n;
vector<int> v;
void dfs(ll pos,ll sum){
	if(v.size()==n){
		ans=max(ans,sum);
		return;
	}
	if(pos>2*n) return;
	ll now=dp[pos];
	for (int i = 0; i < v.size();i++) now-=2*a[pos][v[i]];//自己画图理解,这是为了,贡献的正确
	v.push_back(pos);
	dfs(pos+1,sum+now);//要pos
	v.pop_back();
	dfs(pos+1,sum);//不要pos
}
int main(){
	scanf("%d",&n);
	for (int i = 1; i <= 2*n;i++){
		for (int j = 1; j <= 2*n;j++){
			scanf("%lld",&a[i][j]);
			dp[i]+=a[i][j];
		}
	}
	dfs(1,0);
	printf("%lld\n",ans);
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心脏dance

如果解决了您的疑惑,谢谢打赏呦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值