XTU 1198 Candy

Candy

     

题目描述

Henry和Lena最近买了很多各种各样的糖…他们决定把所有糖分了… 但是两个人都不希望自己糖的总重量比对方少太多, 鉴于不同的糖的味道不尽相同,所以每个糖都有一个yummy值。 Henry希望知道在两人得到的糖总质量差不大于m的时候,自己的糖yummy值之和的尽量大。

输入

有多组数据 每组数据第一行为两个整数,n,m,(1 <=n <= 100, 0 <= m <= 500) 接下来有两行,每行有n个数,第一行的第i个数表示第i颗糖的重量xi( 0 < xi <= 100), 第二行的第i个数表示第i颗糖的yummy值 yi( 0 < yi <= 100 )

输出

每行输出一组数据的结果, 一个数表示Henry的糖的总yummy值的最大值,如果不存在如题所述的分糖方案,输出-1

样例输入

1 30
43
15
2 290
89 22
76 77

样例输出

-1
153

代码:

/**
*  01背包 先全部装入再判断是否满足条件
*  条件: | A - B | <= m
*              A + B = sum
*  得到: | sum - 2*B | <= m
*
*/
#include <stdio.h>
#include <string.h>
#include <limits.h>
#define maxn 20005
int max(int a, int b)
{
	if (a > b)return a;
	return b;
}
int abs(int a)
{
	if (a > 0)return a;
	return -a;
}
int main()
{
	int n, m;
	int x[maxn], y[maxn], dp[maxn];
	while (scanf("%d%d", &n, &m) != EOF)
	{
		memset(dp, -1, sizeof(dp));
		dp[0] = 0;
		int sum = 0;
		for (int i = 1; i <= n; i++){
			scanf("%d", &x[i]);
			sum += x[i];
		}
		for (int i = 1; i <= n; i++)
			scanf("%d", &y[i]);
		for (int i = 1; i <= n; i++)
		{
			for (int v = sum; v >= x[i]; v--){
				if (dp[v - x[i]] >= 0)
					dp[v] = max(dp[v], dp[v - x[i]] + y[i]);
			}
		}
		int ans = INT_MIN;
		for (int v = 0; v <= sum; v++)
		if (abs(sum - 2 * v) <= m)
			ans = max(ans, dp[v]);
		printf("%d\n", ans);
	}
	return 0;
}

XTU 1198 Candy

时间: 2024-10-13 13:25:06

XTU 1198 Candy的相关文章

[LeetCode][Java] Candy

题目: There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more cand

1198 国王游戏

1198 国王游戏 2012年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n位大臣排成一排,国王站在队伍的最前面.排好队后,所有的大臣都会获得国王奖赏的若干金币,每位大臣获得的金币数分别是:排在该大臣前面的所有人的左手上的数的乘积除以他自

从视频文件中读入数据--&gt;将数据转换为灰度图--&gt;对图像做candy边缘检测

//从视频文件中读入数据-->将数据转换为灰度图-->对图像做candy边缘检测 //作者:sandy //时间:2015-10-10 #include <cv.h> #include <highgui.h> int main(int argc, char *argv[]){ //预备工作 CvCapture* capture=cvCreateFileCapture("E:\\Videos\\xx.avi");//让capture变量指向视频文件 i

[leet code 135]candy

1 题目 There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more can

LeetCode 笔记25 Candy (艰难的调试)

There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more candies

Candy

动态规划: There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more ca

hdu 4465 Candy (快速排列组合 )

Candy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2115    Accepted Submission(s): 910 Special Judge Problem Description LazyChild is a lazy child who likes candy very much. Despite being ve

POJ 3083 Children of the Candy Corn

Children of the Candy Corn Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 308364-bit integer IO format: %lld      Java class name: Main The cornfield maze is a popular Halloween treat. Visitors are shown the

【LeetCode】Candy 解题报告

[题目] There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more can