↓ 跳过正文
  1. Posts/

codeforces #319 B - Modulo Sum (抽屉原理,dp)

·409 字·1 分钟

背包还是理解的不够透彻..

因为每次都是用那个一维形式的.

这道题的做法类似01背包.

此外还可以有一个优化…

当n>m的时候...根绝抽屉原理..一定为yes..

复杂度可以从o(nm)优到 o(m^2)

代码实现
 1/*************************************************************************
 2	> File Name: code/cf/#319/B.cpp
 3	> Author: 111qqz
 4	> Email: rkz2013@126.com
 5	> Created Time: 2015年09月15日 星期二 19时54分44秒
 6 ************************************************************************/
 7
 8#include<iostream>
 9#include<iomanip>
10#include<cstdio>
11#include<algorithm>
12#include<cmath>
13#include<cstring>
14#include<string>
15#include<map>
16#include<set>
17#include<queue>
18#include<vector>
19#include<stack>
20#include<cctype>
21#define y1 hust111qqz
22#define yn hez111qqz
23#define j1 cute111qqz
24#define ms(a,x) memset(a,x,sizeof(a))
25#define lr dying111qqz
26using namespace std;
27#define For(i, n) for (int i=0;i<int(n);++i)
28typedef long long LL;
29typedef double DB;
30const int inf = 0x3f3f3f3f;
31const int N=1E3+7;
32int n,m;
33int a[N];
34int dp[N];
35void OneZeroPack(int cost,int value)
36{
37    for ( int v = m ; v--; v >=cost)
38    {
39	dp[v] = max(dp[v],dp[v-cost]+value);
40    }
41}
42int main()
43{
44  #ifndef  ONLINE_JUDGE
45
46  #endif
47    cin>>n>>m;
48    if (n>=m)
49    {
50	puts("YES");
51	return 0;
52    }
53    int k = 0;
54    for ( int i = 0 ; i < n ; i++)
55    {
56	int x;
57	scanf("%d",&x);
58	if (x>m) continue;  //大于背包容量的肯定直接舍去
59	k++;
60	a[k] = x;
61    }
62  //  for ( int i = 1 ; i <= k ; i++) cout<<"a[i]:"<<a[i]<<endl;
63    for ( int i = 0 ; i <= m ; i++)
64	dp[i] = -inf;
65    dp[0] = 0 ;
66    for ( int i = 1 ; i <= k ; i++)
67    {
68	OneZeroPack(a[i],a[i]);
69    }
70    for ( int i = 0 ; i <= m ; i++) cout<<i<<" "<<dp[i]<<endl;
71    if (dp[m]==-inf)
72    {
73	puts("NO");
74    }
75    else
76    {
77	puts("YES");
78    }
79
80
81 #ifndef ONLINE_JUDGE
82  fclose(stdin);
83  #endif
84	return 0;
85}

相关文章

hdu 1171 Big Event in HDU (母函数,01背包)

·1075 字·3 分钟
**Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 26534 Accepted Submission(s): 9332 ** Problem Description Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don’t know that Computer College had ever been split into Computer College and Software College in 2002. 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).

hdu 1203 I NEED A OFFER! (01背包)

·977 字·2 分钟
I NEED A OFFER! # **Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18287 Accepted Submission(s): 7320 ** Problem Description Speakless很早就想出国,现在他已经考完了所有需要的考试,准备了所有要准备的材料,于是,便需要去申请学校了。要申请国外的任何大学,你都要交纳一定的申请费用,这可是很惊人的。Speakless没有多少钱,总共只攒了n万美元。他将在m个学校中选择若干的(当然要在他的经济承受范围内)。每个学校都有不同的申请费用a(万美元),并且Speakless估计了他得到这个学校offer的可能性b。不同学校之间是否得到offer不会互相影响。“I NEED A OFFER”,他大叫一声。帮帮这个可怜的人吧,帮助他计算一下,他可以收到至少一份offer的最大概率。(如果Speakless选择了多个学校,得到任意一个学校的offer都可以)。

hdu 2546 饭卡 (01背包)

·692 字·2 分钟
饭卡 # **Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 14225 Accepted Submission(s): 4945 ** Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额。如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负),否则无法购买(即使金额足够)。所以大家都希望尽量使卡上的余额最少。 某天,食堂中有n种菜出售,每种菜可购买一次。已知每种菜的价格以及卡上的余额,问最少可使卡上的余额为多少。

poj 3370 Halloween treats (剩余类,抽屉原理)

·837 字·2 分钟
昨天那道签到的数学题没搞出来不开心. 是时候刷一波数学了 这题题意是说,从n个数中任选m个,使得m个的和为c的倍数. 如果有解,输出选的数的下标,否则输出无解字符串. 抽屉原理的原始描述是,如果有n+1个物品,有n个抽屉,那么至少有一个抽屉有2个物品.

HUST team contest #E A Mountain Road||poj 3846 (dp)

·840 字·2 分钟
比赛的时候以为是贪心… 想了好久. 不过最后没敢写,因为没办法证明正确性,只是直觉== 最后剩下的时间给队友改另一道题了.. 果然明智… 蠢的人的直觉真心不靠谱..