sdut2408 pick apples (贪心+背包)山东省第三届ACM省赛

本文出自:http://blog.csdn.net/svitter/

题意:三种苹果,每种都有对应的Size,Value,给你一个背包空间,求最大的价值。

本题目的关键就在于非常大的背包空间

依据indicates the size (1 <= S<=
100) 我们可以考虑在1000000(100^3)之外的空间放性价比最高的苹果。为什么时100^3?

要知道背包如果正好填满,而填满空间相应价值的苹果大于不填满的价值的苹果,那么就选择能填满空间而使价值最大的苹果,而非性价比最高的苹果——性价比高的苹果可能因为剩下的空间不足,而造成空间利用不充分达不到最大价值。100^3大于任何3个苹果的Size的最大公倍数,所以选择100^3这个数字。

另:之前把f[1000001]初值赋值为-1, 以此来求背包- =太水了= =完全没有考虑到最后一个f[BagSize]可能为-1的情况,然后写了一个找不是-1的最大Bag值,又水了- =当赋值-1的时候,未必最大的就是空间用的最多的。

简单举例:

BagSize 60, a[1] 59/30, a[2].size 58/31 a[3].size 57/32;

贴代码:

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;
#define lln long long int
struct Apple
{
    int Value;
    int Size;
    double Cost;
};

Apple a[3];

lln f[1001000];

void ace()
{
    int t, i, j, no, most;
    int BagSize, tempBag;
    double cost;
    lln RestNum, ans, RestValue;
    int Size;
    scanf("%d", &t);
    for(no = 1; no <= t; no++)
    {
        memset(f, 0, sizeof(f));
        f[0] = 0;

        most = 0; cost = 0;
        for(i = 0; i < 3; i++)
        {
            scanf("%d %d", &a[i].Size, &a[i].Value);
            a[i].Cost = ((double) a[i].Value) / a[i].Size;
            if(a[i].Cost > cost)//find the most
            {
                most = i;
                cost = a[i].Cost;
            }
        }
        scanf("%d", &BagSize);

        RestNum = 0;
        RestValue = 0;
        //大于1000的时候,把性价比最高的苹果填入
        if(BagSize > 1000000)
        {
            tempBag = BagSize - 1000000;
            RestNum = tempBag / a[most].Size;
            BagSize -= RestNum * a[most].Size;
            RestValue = RestNum * a[most].Value;
        }
        //剩余的苹果使用背包
        for(i = 0; i < 3; i++)
        {
            Size = BagSize - a[i].Size;
            for(j = 0; j <= Size; j++)
            {
                f[j + a[i].Size] = max(f[j + a[i].Size], f[j] + a[i].Value);
            }
        }

        ans = RestValue + f[BagSize];
        printf("Case %d: %lld\n", no, ans);
    }
}
int main()
{
    ace();
    return 0;
}
时间: 2024-12-29 07:00:23

sdut2408 pick apples (贪心+背包)山东省第三届ACM省赛的相关文章

Sdut2411 Pixel density 山东省第三届ACM省赛(输入输出字符串处理)

本文出处:http://blog.csdn.net/svitter 原题:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2411 题意:给你一个串,让你依据那个串来输出ppi.坑特别多.ppi的计算方法是dp / inches; dp = sqrt(wp*wp + hp * hp); 现在我来说说这个题目有多坑: 给你的串的格式是这样: name + inches+ "inches"

Sdut 2416 Fruit Ninja II(山东省第三届ACM省赛 J 题)(解析几何)

Time Limit: 5000MS Memory limit: 65536K 题目描写叙述 Haveyou ever played a popular game named "Fruit Ninja"? Fruit Ninja (known as Fruit Ninja HD on the iPad and Fruit Ninja THD for NvidiaTegra 2 based Android devices) is a video game developed by Hal

[2012山东省第三届ACM大学生程序设计竞赛]——Fruit Ninja II

Fruit Ninja II 题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2416 Time Limit: 5000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Have you ever played a popular game named "Fruit Ninja"? Fruit Ninja (known as Fruit Ninja

[2012山东省第三届ACM大学生程序设计竞赛]——n a^o7 !

n a^o7 ! 题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2413 Time Limit: 1000MS Memory limit: 65536K 题目描述 All brave and intelligent fighters, next you will step into a distinctive battleground which is full of sweet and hap

山东省第三届acm

#include <iostream>#include <stdio.h>using namespace std;int s[4],p[4];long long llmax(long long a,long long b){    return a>b?a:b;}int main(){    int t;    cin>>t;    int c=1;    while(t--)    {        for(int i=1; i<=3; i++)     

[2012山东省第三届ACM大学生程序设计竞赛]——Mine Number

Mine Number 题目:http://acm.sdut.edu.cn/sdutoj/problem.php? action=showproblem&problemid=2410 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描写叙述 Every one once played the game called Mine Sweeping, here I change the rule. You are given an n*m ma

sdut 2603 Rescue The Princess(算是解析几何吧)(山东省第四届ACM省赛A题)

题目地址:sdut 2603 Rescue The Princess Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Several days ago, a beast caught a beautiful princess and the princess was put in prison. To rescue the princess, a prince who wanted to marry the princess

sdut2605 A^X mod P 山东省第四届ACM省赛(打表,快速幂模思想,哈希)

本文出自:http://blog.csdn.net/svitter 题意: f(x) = K, x = 1 f(x) = (a*f(x-1) + b)%m , x > 1 求出( A^(f(1)) + A^(f(2)) + A^(f(3)) + ...... + A^(f(n)) ) modular P. 1 <= n <= 10^6 0 <= A, K, a, b <= 10^9 1 <= m, P <= 10^9 本题目的关键在于大幂的分解和..你要这样想,因

Pick apples 第三届acm省赛

Description Once ago, there is a mystery yard which only produces three kinds of apples. The number of each kind is infinite. A girl carrying a big bag comes into the yard. She is so surprised because she has never seen so many apples before. Each ki