【uva】12325 - Zombie's Treasure Chest

一开始以为是动规,后来发现不是,算是贪心吧。

思路:输入N,S1,V1,S2,V2,分别代表背包总容量,一号宝藏的体积和价格,二号宝藏的体积和价格。

那么 N / S1 和 N / S2 可以分别代表 N容量最多能装 一号,二号宝藏的数量,那么如果这2个数其中一个比较小的时候,如果N / S1 小 直接枚举一号宝藏的数量 进行 二号宝藏数量的计算,反之亦然。

但是还存在一个比较特殊的情况:如果N / S1 和 N / S2 都很大呢? 也就是说S1,S2都很小。

我们不妨假设下表:

                                                                                

  宝藏一 宝藏二
单位体积 S1 S2
单位价格 V1 V2
数量 S2 S1
总价格 S2*V1 S1*V2
总体积 S1*S2 S1*S2

我们不妨设一号宝藏有S2个,二号宝藏有S1个,那么一号宝藏总体积和二号宝藏总体积就一样了,如果S2 *V1 > S1 * V2的话,说明宝藏二的数量肯定在[0,S1)内,因为

如果体积大于了S1,那么,宝藏二的总价格 就为 S * V2(S > S1),那么肯定体积就大于S1*S2了,S1*S2的体积下,拿宝藏二肯定不如拿宝藏一。


以上就是分析,还有一个比较坑人的地方就是由于输入为32位,所以需要使用Long Long 否则会被后台数据卡死的(亲身经历~ TAT)

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<stack>
#include<queue>
#include<map>
#include<vector>
typedef long long LL;
using namespace std;
LL n,V1,S1,V2,S2;
#define INF 5000
LL solve(int choice){
    LL MAX = -1;
    if(choice == 0){
        for(LL i = 0 ; i * S1 <= n ; i++){
            LL temp = n - i * S1;
            LL ans  = temp / S2 * V2 + i * V1;
            MAX = max(MAX,ans);
        }
    }
    else if(choice == 1){
        for(LL i = 0 ; i * S2 <= n ; i++){
            LL temp = n - i * S2;
            LL ans  = temp / S1 * V1 + i * V2;
            MAX = max(MAX,ans);
        }
    }
    else {
        LL Sum1 = S2 * V1;
        LL Sum2 = S1 * V2;
        if(Sum1 > Sum2){
            for(LL i = 0 ; i < S1 ; i++){
                LL temp = n - i * S2;
                LL ans  = temp / S1 * V1 + i * V2;
                MAX = max(MAX,ans);
            }
        }
        else {
            for(LL i = 0 ; i < S2 ; i++){
                LL temp = n - i * S1;
                LL ans  = temp / S2 * V2 + i * V1;
                MAX = max(MAX,ans);
            }
        }
    }
    return MAX;
}
int main(){
    int T,Case = 1;
    scanf("%d",&T);
    while(T--){
        scanf("%lld%lld%lld%lld%lld",&n,&S1,&V1,&S2,&V2);
        /*分别输入宝物一和宝物二的体积 价值*/
        LL ans;
        if((n / S1) < INF)
            ans = solve(0);
        else if((n / S2) < INF)
            ans = solve(1);
        else {
            ans = solve(2);
        }
        printf("Case #%d: %lld\n",Case ++, ans);
    }
    return 0;
}


阅读更多

There's Treasure Everywhere!

03-10

Finding buried treasures is simple: all you need is a map! The pirates in the Caribbean were famous for their enormous buried treasures and their elaborate maps. The maps usually read like ``Start at the lone palm tree. Take three steps towards the forest, then seventeen step towards the small spring, . . . blahblah . . . , finally six steps toward the giant rock. Dig right here, and you will find my treasure!'' Most of these directions just boil down to taking the mentioned number of steps in one of the eight principal compass directions (depicted in the left of the figure).nObviously, following the paths given by these maps may lead to an interesting tour of the local scenery, but if one is in a hurry, there is usually a much faster way: just march directly from your starting point to the place where the treasure is buried. Instead of taking three steps north, one step east, one step north, three steps east, two steps south and one step west (see figure), following the direct route (dashed line in figure) will result in a path of about 3.6 steps.nnYou are to write a program that computes the location of and distance to a buried treasure, given a `traditional' map.nnnnInputnnThe input contains several strings, each one on a line by itself, and each one consisting of at most 200 characters. The last string will be END, signaling the end of the input. All other strings describe one treasure map each, according to the following format: nnThe description is a comma-separated list of pairs of lengths (positive integers less than 1000) and directions (N (north), NE (northeast), E (east), SE (southeast), S (south), SW (southwest), W (west) or NW (northwest)). For example, 3W means 3 steps to the west, and 17NE means 17 steps to the northeast. A full stop (.) terminates the description, which contains no blanks.nnnOutput nnFor every map description in the input, first print the number of the map, as shown in the sample output. Then print the absolute coordinates of the treasure, in the format ``The treasure is located at (x,y).''. The coordinate system is oriented such that the x-axis points east, and the y-axis points north. The path always starts at the origin (0,0). nnOn the next line print the distance to that position from the point (0,0), in the format ``The distance to the treasure is d.''. The fractional values x, y, d must be printed exact to three digits to the right of the decimal point.nnPrint a blank line after each test case.nnnSample Input nn3N,1E,1N,3E,2S,1W.n10NW.nENDnnnSample Output nnMap #1nThe treasure is located at (3.000,2.000).nThe distance to the treasure is 3.606.nnMap #2nThe treasure is located at (-7.071,7.071).nThe distance to the treasure is 10.000.

The Treasure

05-04

We have arrived at the age of the Internet. Many software applications have transformed from stand-alone to online applications. Computer games are following this trend as well. Online games are becoming more and more popular, not only because they are more intelligent, but also because they can bring great profits. "The computer game industry is developing rapidly in China. Online game revenues amounted to 1.3 billion Yuan last year and are expected to reach 6.7 billion Yuan by 2007." reported by China Daily in 2004.nnHowever, good games originate from good programmers. We take for example that there is a RPG (Role Playing Game) and your boss asks you to implement some tasks. For simplicity's sake, we assume there are two kinds of roles in this game: one is player and the other is monster. You should help the player to achieve the goal: reach the place where treasure is positioned as early as possible and get the treasure.nnThe map of the game is a matrix of N * M identical cells. Some cells are passable blocks, and others are non-passable rocks. At any time, there is at most one role occupying a block. At the beginning, the time is set to 0, and the player is at a certain block. He then moves towards the treasure. At each turn, we have some rules:nnThe player can stay in the same block during the next one-second time duration, or he can walk or run towards the east, south, west, north, northeast, northwest, southeast, and southwest.nnnWith walking, the player can arrive at the corresponding passable blocks around him (See Fig.1). Each move takes 1 second.nWith running, the player can arrive at the corresponding passable blocks 2 cells away from him (See Fig.2). Each run takes 1 second. As demonstrated in Fig.3, if a neighbor cell is not passable, the player cannot run in that direction. For example, if cell 2 is a rock, running from 1 to 3 is impossible.nThe monsters are classified into aggressive and non-aggressive. If a monster occupies a cell, the player cannot move into that cell or run through that cell. In addition, the player cannot move into the cells surrounding an aggressive monster, because it will attack the player near it. For example, in Fig.4, if there is an aggressive monster in 5, then the cell 1, 2, 3, 4, 6, 7, 8 and 9 are in its attacking region, so the player cannot stay in or pass through these cells.nMonsters change their positions each turn. Each monster appears by its position sequence iteratively. That's to say, given the position sequence of monster i: (x1, y1), (x2, y2), ..., (xs, ys), its initial position is (x1, y1) at time 0, then it appears in (x2, y2) at time 1, and so on. When monster i arrives at (xs, ys) at time s-1, it will arrive in (x1, y1) at time s, and start to repeat.nAt the start of each turn, all the monsters change their positions first (the way of changing is given above). If a monster appears in the player's cell, or if an aggressive monster appears near the player to put him in its attacking region, the player will die, and the goal cannot be achieved. After all the monsters change their positions, the player makes a move or stays in the same cell. In his move, the moving path should not be occupied by any rocks or monsters or in the attacking region of any aggressive monsters. When counting the total time, we can neglect the time between monsters' position change and the player's move. Given the map of the game, the player's starting position, the treasure position and all the monsters' positions in every second, your task is to write a program to find the minimum time that the player gets the treasure.nInputnnThe input consists of several test cases. The first line of each case contains two integers N and M (1 <= N, M <= 100), where N is the height of the map and M is the width of the map. This is followed by N lines each containing M characters representing the map. A '#' represents a rock, a '.' is a free block, 'p' is the starting position of the player, 't' is the position of the treasure, 'n' is the initial position of a non-aggressive monster, and an 'a' stands for the initial position of an aggressive monster.nnThe cell (i, j) is the j-th cell on the i-th row counting from left to right. The rows are counted from 1 to N starting from the first line of the matrix. We can number all the monsters as 1, 2, 3�� according to their initial position, sorting first by row, then by column. The (n+2)-th line contains an integer p (0 <= p <= 100), which is the total number of monsters (i.e. the total number of 'n's and 'a's in the matrix). It is followed by p lines each specifying a monster's position sequence in the following format: the i-th (1 <= i <= p) line corresponds to monster i, which begins with an integer s (1 <= s <= 100), meaning the length of position sequence. Then s pairs of integers x1, y1, x2, y2, ��, xs, ys are followed, separated by blanks. Each pair is a free block in the map, (i.e. a monster never goes to a rock cell).nnIt is assured that none of the aggressive monsters' initial position is around the player. Two consecutive cases are separated by a blank line. The input is terminated by a line containing a pair of zeros.nnOutputnnFor each test case, output the minimum total time required for the player to get the treasure, in seconds. If it's not possible to get the treasure, or the minimum required time is greater than 100 seconds, please print a line just containing the string "impossible". Two consecutive cases should be separated by a blank line.nnSample Inputnn7 8n#.#####.n#.t#..p.n#..#....n..#a.#.#n#...##.nn.#......n........n2n2 4 4 5 4n3 5 8 6 8 5 7nn3 3np#.n##.nt..n0nn2 2n#tnp#n0nn0 0nnSample Outputnn8nnimpossiblenn1

没有更多推荐了,返回首页