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 ...
随机推荐
- 剑指Offer题解(Python版)
https://blog.csdn.net/tinkle181129/article/details/79326023# 二叉树的镜像 链表中环的入口结点 删除链表中重复的结点 从尾 ...
- LeetCode 617 Merge Two Binary Trees 解题报告
题目要求 Given two binary trees and imagine that when you put one of them to cover the other, some nodes ...
- 《Redis 集群》
由于集群这章节内容较多,也比较重要,所以单独拉出来,做一个小章节. 1:如何搭建一个集群? - 环境为 Ubuntu16.04 - 这里我预计使用 9001 - 9006 端口,生成一个 6 台机器的 ...
- 【pyqtgraph绘图】线条,填充和颜色
解读官方API-线条,填充和颜色 参考: http://www.pyqtgraph.org/documentation/style.html 线条,填充和颜色 Qt依靠其QColor,QPen和QBr ...
- react-native-image-picker iOS设置
效果图,因为我用的是模拟器,所以拍照功能没有效果,不过可从相册选择,下面是具体的效果图 把react-native-image-picker添加到项目 yarn add react-native-im ...
- 数据库机器迁移对AlwaysON 集群影响测试
1主3从(共享文件见证) 模拟事故 AlwaysON集群 结论 主域控服务器重启 共享文件夹见证失败,SQL集群无影响 无影响 修改共享文件夹见证路径 第一次测试修改后:整个集群突然重启,查询 ...
- Python3学习之路~6.4 析构函数
析构函数是在实例释放.销毁的时候执行的,通常用于做一些收尾工作.比如说,关闭一些数据库连接.打开的临时文件等. #Author:Zheng Na class Role: # 构造函数 def __in ...
- VS2017使用Git进行源代码管理
步骤一:将解决方案添加到源代码管理 步骤二:进入团队资源管理器 双击存储库项目进入Git操作页面. 步骤三:同步本地代码到远程仓库 选择同步功能 步骤四:发布代码到Git 点击之后输入你要发布的git ...
- nohup 详解
转:https://www.cnblogs.com/jinxiao-pu/p/9131057.html nohup nohup 命令运行由 Command参数和任何相关的 Arg参数指定的命令,忽略所 ...
- 将常用的T-CODE收藏进 文件夹
1:选中文件夹,右键>insert transaction>输入相应的t-code.