当前位置:网站首页>Complete knapsack problem 1
Complete knapsack problem 1
2022-06-12 09:04:00 【Little wish】
Milk Pails
link :https://ac.nowcoder.com/acm/contest/7163/E
source : Cattle from
The main idea of the topic : Give the capacity X Units and Y A pail of milk , Pour in after each filling M Unit barrels , Make sure there is no overflow , ask M How many units of milk can a barrel hold at most
The sample input :
17 25 77
Sample output :
76
Sample explanation :
In this example, FJ fills the pail of size 17 three times and the pail of size 25 once, accumulating a total of 76 units of milk.
Ideas :
Completely backpack , d p [ i ] = 1 dp[i]=1 dp[i]=1 It means that you can install i i i Unit milk , otherwise d p [ i ] = 0 dp[i]=0 dp[i]=0, The initial state d p [ 0 ] = 1 dp[0]=1 dp[0]=1
AC Code :
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int dp[N];
int main() {
freopen("Milk Pails.in", "r", stdin);
int x, y, m;
while (scanf("%d%d%d", &x, &y, &m) == 3) {
memset(dp, 0, sizeof(dp));
dp[0] = 1;
for (int i = x; i <= m; i++) {
dp[i] |= dp[i-x];
}
for (int i = y; i <= m; i++) {
dp[i] |= dp[i-y];
}
for (int i = m; i >= 0; i--) {
if (dp[i]) {
printf("%d\n", i);
break;
}
}
}
return 0;
}
Fruit Feast
link :https://ac.nowcoder.com/acm/contest/7163/I
source : Cattle from
The main idea of the topic : Every lemon you eat will make you feel full A, Every time you eat an orange, you feel full B, You can choose to drink water once , Satiety is halved , Rounding down , The greatest feeling of satiety is T, Ask about the maximum satiety you can achieve
The sample input :
8 5 6
The sample input :
8
Sample explanation :
Eat an orange , Satiety is 6, Drink lots of water , Satiety is 3, Eat a lemon , Satiety is 5
Ideas :
Completely backpack , d p [ i ] dp[i] dp[i] Indicates the feeling of satiety that can be obtained , d p [ i ] = 1 dp[i]=1 dp[i]=1 It means you can feel full i i i, otherwise d p [ i ] = 0 dp[i]=0 dp[i]=0, The initial state d p [ 0 ] = 1 dp[0]=1 dp[0]=1, When transferring, pay attention to halving and transferring
AC Code :
#include <bits/stdc++.h>
using namespace std;
const int N = 5e6+7;
int dp[N];
int main() {
//freopen("Fruit Feast.in", "r", stdin);
int t, a, b;
while (scanf("%d%d%d", &t, &a, &b) == 3) {
memset(dp, 0, sizeof(dp));
dp[0] = 1;
for (int i = a; i <= t; i++) {
dp[i] |= dp[i-a];
}
for (int i = b; i <= t; i++) {
dp[i] |= dp[i-b];
}
for (int i = 0; i <= t; i++) {
dp[i/2] |= dp[i];
}
for (int i = a; i <= t; i++) {
dp[i] |= dp[i-a];
}
for (int i = b; i <= t; i++) {
dp[i] |= dp[i-b];
}
for (int i = t; i >= 0; i--) {
if (dp[i]) {
printf("%d\n", i);
break;
}
}
}
return 0;
}
边栏推荐
- 分库分表会带来读扩散问题?怎么解决?
- 通俗理解时域采样与频域延拓
- (node:22344) [DEP0123] DeprecationWarning: Setting the TLS ServerName to an IP address is not permit
- 2022 low voltage electrician retraining question bank and online simulation examination
- 【字符集七】汉字的宽字符码和多字节码分别是多少
- [advanced pointer 2] array parameter transfer & pointer parameter transfer & function pointer & function pointer array & callback function
- 第四章-第一个程序
- Résoudre le problème de demander à l'élément d'être ouvert lorsque l'unit é est ouverte et que vous n'avez pas été ouvert auparavant (peut - être fermé anormalement auparavant)
- Background location case II
- Load the source code of 2D 3D virtual anchor in the web page (1: project introduction and source code)
猜你喜欢
![[character set 7] what are the wide character codes and multi byte codes of Chinese characters](/img/8c/6d375d90234e6094b6930c2cefefa1.png)
[character set 7] what are the wide character codes and multi byte codes of Chinese characters

Background position - mixed units

ERROR 1630 (42000): FUNCTION a.avg does not exist. Check the ‘Function Name Parsing and Resolution‘

Background attribute compound writing

Unittest测试框架

Priority issues

The classic dog contract of smart contract (I)

Composition of box model

Filters and listeners

Background location case II
随机推荐
(JS) three digits are separated by commas, and two decimal places are reserved (or rounded)
128. longest continuous sequence hash table
第五章-[bx]和Loop指令
mySql学习记录——三、mysql查询语句
Filters and listeners
(十五) TweenRunner
About weights exercise
node示例后台搭建
Redis installation test
重启Kubernetes Pod的几种方式
EIP-1559
Make a simple page with the websql database of HTML5:
Flink CheckPoint : Exceeded checkpoint tolerable failure threshold
【字符集九】gbk拷贝到Unicode会乱码?
2022 safety officer-c certificate special operation certificate examination question bank and simulation examination
The classic dog contract of smart contract (I)
ISCSI详解(五)——ISCSI客户端配置实战
分库分表会带来读扩散问题?怎么解决?
Solve the problem that when unity is opened, you will be prompted that the project has been opened, but you have not opened it before (it may be closed abnormally before)
Display the remaining valid days according to the valid period