Big Event in HDU (母函数, 玄学AC)
The splitting is absolutely a big event in HDU! At the same time,
it is a trouble thing too. All facilities must go halves. First, all
facilities are assessed, and two facilities are thought to be same if
they have the same value. It is assumed that there is N (0<N<1000)
kinds of facilities (different value, different kinds).
with a number N (0 < N <= 50 -- the total number of different
facilities). The next N lines contain an integer V (0<V<=50
--value of facility) and an integer M (0<M<=100 --corresponding
number of the facilities) each. You can assume that all V are different.
A test case starting with a negative integer terminates input and this test case is not to be processed.
OutputFor each case, print one line containing two integers A and B
which denote the value of Computer College and Software College will
get respectively. A and B should be as equal as possible. At the same
time, you should guarantee that A is not less than B.
Sample Input
2
10 1
20 1
3
10 1
20 2
30 1
-1
Sample Output
20 10
40 40 母函数模板题
#include<bits/stdc++.h> using namespace std;
#define maxn 1250000 int ans[maxn], temp[maxn];
int v[],num[];
int n, sum; void init()
{
// memset(a,0,sizeof(a));
int mid = sum/;
ans[]=;
int i, j, k;
for(i=; i<=num[]; i++)
ans[i*v[]] = ;
for(i=; i<n; i++)
{
for(j=; j<=mid; j++)
for(k=; (k*v[i]+j)<=mid&&k<=num[i]; k++)
{
temp[j+k*v[i]] +=ans[j];
}
for(j=; j<=mid; j++)
{
ans[j] = temp[j];
temp[j] = ;
}
}
} int main()
{
int n;
while(cin >> n && n!= -)
{
sum = ;
memset(ans, , sizeof(ans));
for(int i = ; i < n; i++)
{
cin >> v[i] >> num[i];
sum += v[i]*num[i];
}
init();
for(int i = sum/; i>=; i--)
{
if(ans[i]) {cout << sum-i << " " << i << endl;break;}
}
}
return ;
}
上面的代码连样例都过不了,但我们只要把 上面的 void init() 函数写到主函数里面去, 就能AC, 楼主不太懂, 大佬能说一下吗???
AC代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#define MAXN 1001000
using namespace std; int value[200],number[200],ans[MAXN],temp[MAXN];
int main()
{
int n,i,j,k;
while(cin >> n&&n>=0)
{
int max = 0;
for(i=0; i<n; i++)
{
cin >> value[i] >> number[i];
max += value[i] * number[i];
}
int mid = max/2 ;
memset(ans,0,sizeof(int)*mid+10);
memset(temp,0,sizeof(int)*mid+10);
for(i=0; i<=number[0]; i++)
ans[i*value[0]] = 1;
for(i=1; i<n; i++)
{
for(j=0; j<=mid; j++)
for(k=0; (k*value[i]+j)<=mid&&k<=number[i]; k++)
{
temp[j+k*value[i]] +=ans[j];
}
for(j=0; j<=mid; j++)
{
ans[j] = temp[j];
temp[j] = 0;
}
}
for(i=mid; i>=0; i--)
if(ans[i]!=0)
break;
cout << max - i<<" "<< i << endl;
}
return 0;
}
Big Event in HDU (母函数, 玄学AC)的更多相关文章
- HDU1171——Big Event in HDU(母函数)
Big Event in HDU DescriptionNowadays, we all know that Computer College is the biggest department in ...
- HDU 1171 Big Event in HDU 母函数
欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory ...
- Big Event in HDU(HDU1171)可用背包和母函数求解
Big Event in HDU HDU1171 就是求一个简单的背包: 题意:就是给出一系列数,求把他们尽可能分成均匀的两堆 如:2 10 1 20 1 结果是:20 10.才最均匀! 三 ...
- 组合数学 - 母函数的变形 --- hdu 1171:Big Event in HDU
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Big Event in HDU(杭电1171)(多重背包)和(母函数)两种解法
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- 杭电1171 Big Event in HDU(母函数+多重背包解法)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu 1171 Big Event in HDU (01背包, 母函数)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Big Event in HDU(多重背包套用模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1171 Big Event in HDU Time Limit: 10000/5000 MS (Java/Othe ...
- hdoj1171 Big Event in HDU
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
随机推荐
- Swift中"#"的用法
配置外部参数名 在函数(或者方法)的参数名前添加"#",可以使该参数拥有相同的本地参数名和外部参数名. 注:在方法中,第二个及后续的参数,默认是具有和内部参数一致的外部参数名的,只 ...
- 【JMeter】【微信好文收藏】Jmeter接口测试实战-有趣的cookie
场景: 接口测试时常都需要登录,请求方式(post), 登录常用的方法有通过获取token, 获取session, 获取cookie, 等等. 这几种都有一个共同的特点, 有效期(expires). ...
- (4.20)sql server中 len 与datalength 的区别
len是任意字符均为一个占位符字节.datalength是根据字符集不同判断占用,如一个中文占用2个字节.
- RestFramework——API基本实现及dispatch基本源码剖析
基于Django实现 在使用RestFramework之前我们先用Django自己实现以下API. API完全可以有我们基于Django自己开发,原理是给出一个接口(URL),前端向URL发送请求以获 ...
- Github安全开源工具集合
Scanners-Box是来自github平台的开源扫描仪的集合,包括子域枚举,数据库漏洞扫描程序,弱密码或信息泄漏扫描仪,端口扫描仪,指纹扫描仪和其他大型扫描仪,模块化扫描仪等.对于其他众所周知的扫 ...
- POJ1611:The Suspects(模板题)
http://poj.org/problem?id=1611 Description Severe acute respiratory syndrome (SARS), an atypical pne ...
- CentOS6.5安装zookeeper-3.4.5(单机)
1.下载 下载链接:http://archive.apache.org/dist/zookeeper/ 本文下载版本:zookeeper-3.4.5.tar.gz 2.安装 安装目录:/usr/loc ...
- 初尝Web API《转》
HTTP 并不是只能用在网页中.它其实还是一个强大的平台,可以用来生成一些API,暴露服务和数据.HTTP很简单灵活,还非常普及.几乎所有你能想到的平台都有HTTP库,所以HTTP服务可以囊括很大范围 ...
- linux 编译 'aclocal-1.14' is missing on your system
centos编译出现:类似情况: $tar -xvf libpcap-1.0.0.tar.gz $cd libpcap-1.0.0.tar.gz $./configure ...
- 获取 Google USB 驱动程序
获取 Google USB 驱动程序 另请参阅 安装 USB 驱动程序 使用硬件设备 使用任何 Google Nexus 设备进行 ADB 调试时,只有 Windows 需要 Google ...