上课笔记(4)(2)——#572. 对抗赛(compete)

32 篇文章 1 订阅
28 篇文章 0 订阅

Description

程序设计对抗赛设有 (0<N<50 的整数)) 个价值互不相同的奖品,每个奖品的价值分别为 S1​,S2​,S3​…Sn​(均为不超过 100 的正整数)。现将它们分给甲乙两队,为了使得甲乙两队得到相同价值的奖品,必须将这 N 个奖品分成总价值相等的两组。

编程要求:对给定 N 及 N 个奖品的价值,求出将这 N 个奖品分成价值相等的两组,共有多少种分法?

例如:N=5,S1​,S2​,S3​…Sn​ 分别为 1,3,5,8,91,3,5,8,9

则可分为 {1,3,9} 与 {5,8}

仅有 1 种分法;

例如:N=7,S1​,S2​,S3​…Sn​ 分别为 1,2,3,4,5,6,7

则可分为:

{1,6,7} 与 {2,3,4,5}

{2,5,7} 与 {1,3,4,6}

{3,4,7} 与 {1,2,5,6}

{1,2,4,7} 与 {3,5,6}

有 4 种分法。

Format

Input

输入包含 N 及 S1​,S2​,S3​…Sn​。(每两个相邻的数据之间有一个空格隔开)。

Output

输出包含一个整数,表示多少种分法的答案,数据若无解,则输出 0。

Samples

输入数据 1

7
1 2 3 4 5 6 7

输出数据 1

4

Limitation

1s, 1024KiB for each test case.

题解:

货币系统,状态转移方程:

dp[j]=dp[j]+dp[j-a[i]];//状态转移方程 

 程序:

#include <bits/stdc++.h>
using namespace std;
int dp[5010],n,a[5010],ans;//ans价值总和 dp分发数  
int main(){
	dp[0]=1;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		ans+=a[i];
	}
	if(ans%2!=0){
		cout<<0;//无解
		return 0; 
	}
	ans/=2;//每一对得到物品的价值和为总和的一半
	dp[0]=1;
	for(int i=1;i<=n;i++)
		for(int j=ans;j>=a[i];j--)
			dp[j]=dp[j]+dp[j-a[i]];//状态转移方程 
	cout<<dp[ans]/2;//我也不知道为什么要除以2,哈哈 
    return 0;
}
import numpy as np import random from resourse import ICA, Country, Empire, Colony, decode,evaluate import matplotlib.pylab as plt import time start = time.time() ica = ICA() best1 = [10, 16, 17, 1, 4, 7, 13, 11, 12, 5, 8, 2, 14, 9, 15, 3, 6, 3, 4, 3, 1, 2, 2, 3, 5, 4, 1, 1, 2, 4, 4, 4, 2, 3] best2 = [1, 5, 12, 11, 14, 2, 10, 4, 6, 7, 17, 16, 3, 8, 13, 9, 15, 5, 4, 1, 4, 1, 1, 1, 3, 3, 5, 2, 4, 3, 5, 2, 4, 3] best3 = [7, 10, 17, 12, 4, 14, 1, 2, 11, 15, 13, 9, 16, 6, 5, 8, 3, 4, 4, 5, 3, 1, 3, 2, 3, 1, 3, 4, 4, 1, 4, 2, 2, 5] bestlst = [best1,best2, best3] country = ica.createCountries() # for num, blst in enumerate(bestlst): # ica.countries[num].ind = blst # ica.countries[num].cost= evaluate(blst) ica.createEmpires() empire = ica.empires # """ index = 1 plt.ion() while len(empire) != 1: empire = ica.empires ica.moveAction() ica.compare() ica.compete() ica.weedOut() ica.revolution() empiresCost = [e.getempirecost() for e in empire] print("第{}伦 -- {}".format(index, empiresCost)) print("帝国数: {} , 殖民地数: {}".format(len(ica.empires), len(ica.colonies))) minEmpiresCost = np.argmin(empiresCost) marsEmpire = empire[minEmpiresCost] # print("第{}伦 -- 战神 :{}".format(index, marsEmpire.ind)) index += 1 print() # plt.pause(0.05) # plt.show() print("cost: {} ,最终坐标: {} ".format(empire[0].cost, empire[0].getPosit())) print(decode(empire[0].getPosit())) # plt.show() # plt.ioff() # """ end = time.time() print("程序process_1的运行时间为:{}s".format(end - start))
04-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值