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 ...
随机推荐
- day2_Jmeter压测
1.线程组各项设置的意思 2.压测结果查看各指标意思 备注:tps:每秒钟系统能够处理的交易或事务的数量.它是衡量系统处理能力的重要指标.tps越高说明服务器处理能力越好. 3.在一台电脑上做一个简单 ...
- adb devices offline的解决办法
手机通过USB线连接电脑后,输入adb devices 识别为 offline,如图: 解决办法如下: (0) 换数据线.换主机后面的USB口先试一下 (1) 重启adb服务 adb kill-ser ...
- 洛谷P4495 奇怪的背包 [HAOI2018] 数论
正解:数论+dp 解题报告: 传送门! 首先看到这题,跳无数次,自然而然可以想到之前考过好几次了的一个结论——如果只考虑无限放置i,它可以且仅可以跳到gcd(p,v[i]) 举一反三一下,如果有多个i ...
- 洛谷P3295 萌萌哒 [SCOI2016] 倍增+并查集
正解:倍增+并查集 解题报告: 传送门! 首先不难想到暴力?就考虑把区间相等转化成对应点对相等,然后直接对应点连边,最后求有几个连通块就好辣 然后看下复杂度,修改是O(n2)查询是O(n),就比较容易 ...
- 数据库使用:sql server/mysql/sqlite
本科学过sql server数据库,上研刚开始在做研究时自己想点子自己做,为了简便使用了论文中看到的一个简易数据库sqlite存储使用数据.后来随着数据量的增长,以及数据处理的需求sqlite速度明显 ...
- laravel项目出现Non-static method Redis::hGet() cannot be called statically的解决方法
早上ytkah在配置laravel项目中出现Non-static method Redis::hGet() cannot be called statically错误提示,很显然这是redis出问题了 ...
- SQL assistant
SQL assistant取消自动生成别名 SQL assistant-->Options-->DB option -->SQL Servers-->Auto Complete ...
- discuz论坛 模板修改
门户首页 template/rtj1009_007/portal/index.php 头部二级导航 template/rtj1009_007/common/toubu.php 底部内容文件 templ ...
- 虚函数后面的const=0
const 和 =0要分开理解. 成员函数后面用 const 修饰,const表示this是一个指向常量的指针,即对象成为一个常量,即它的成员不能够变化.(默认情况下,this的类型是指向类类型非常量 ...
- ionic3 读写权限申请
This plugin is designed to support Android new permissions checking mechanism. 1.安装插件 $ ionic cordov ...