您的位置:首页 > 运维架构

TOPCODER/SRM565 DIVII 250、500pt(500pt无递归解法)

2012-12-31 12:28 417 查看
250pt:

Problem Statement

 A histogram of a given collection of numbers graphically represents the frequency of each value in the collection. We are given several numbers ranging from 0 to 9 as a vector <int>values. The goal is to build their histogram according
to the following rules.

1) The width of the histogram should be exactly 10.

2) The height of the histogram should equal to H+1, where H is the number of times the most frequent element occurs invalues.

3) The i-th (0-based) column of the histogram corresponds to the value i. Let X(i) be the frequency of value i invalues. Then the last X(i) characters in the column should be 'X's and the other ones should be '.'s. For example, if value i was
not present invalues, the column should be filled with '.' characters. If i was present once, the last element of the column should be 'X' and and the other ones should be '.'s. If i was present twice, the last two elements should be 'X's
and and the other ones should be '.'s, and so on.

Build the histogram and return it as a vector <string>.

Definition

 
Class:ValueHistogram
Method:build
Parameters:vector <int>
Returns:vector <string>
Method signature:vector <string> build(vector <int> values)
(be sure your method is public)
 
 

Constraints

-values will contain between 1 and 50 elements, inclusive.
-Each element of values will be between 0 and 9, inclusive.

Examples

0) 
 
{2, 3, 5, 5, 5, 2, 0, 8}

Returns: {"..........", ".....X....", "..X..X....", "X.XX.X..X." }

The most frequent value is 5, which occurs 3 times. Hence the height of the histogram is 4. It looks as follows:
..........
.....X....
..X..X....
X.XX.X..X.

1) 
 
{9, 9, 9, 9}

Returns: {"..........", ".........X", ".........X", ".........X", ".........X" }

..........
.........X
.........X
.........X
.........X

2) 
 
{6, 4, 0, 0, 3, 9, 8, 8}

Returns: {"..........", "X.......X.", "X..XX.X.XX" }

..........
X.......X.
X..XX.X.XX

3) 
 
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

Returns: {"..........", "XXXXXXXXXX" }

..........
XXXXXXXXXX

4) 
 
{6, 2, 3, 3, 3, 7, 8, 1, 0, 9, 2, 2, 4, 3}

Returns: {"..........", "...X......", "..XX......", "..XX......", "XXXXX.XXXX" }

..........
...X......
..XX......
..XX......
XXXXX.XXXX

#include <deque>

#include <vector>

#include <algorithm>

#include <functional>

#include <iterator>

#include <map>

#include <memory>

#include <numeric>

#include <queue>

#include <set>

#include <utility>

#include <stack>

#include <iostream>

#include <string>

#include <cstdio>

#include <cstdlib>

#include <cmath>

#define PI 3.1415926535898

#define LL long long

 

using namespace std;

class ValueHistogram

{

 public:

 vector <string> build(vector <int> values)

 {

  int height ;

  vector <int> num(10,0);

  for(int i=0;i<values.size();i++)

  {

   num[values[i]]++;

  }

  vector <int> tp(num);

  sort(tp.rbegin(),tp.rend());

  height = tp[0]+1;

  vector <string> result(height);

  result[0] = "..........";

  for(int i=1;i<height;i++)

   result[i] = result[0];

  for(int i=0;i<10;i++)

  {

   for(int j=0;j<num[i];j++)

   {

    result[3-j][i] = 'X';

   }

  }

  return result;

 }

};

500pt:

Problem Statement

 Manao is traversing a valley inhabited by monsters. During his journey, he will encounter several monsters one by one. The scariness of each monster is a positive integer. Some monsters may be scarier than others. The i-th (0-based index) monster Manao
will meet has scariness equal to dread[i].

Manao is not going to fight the monsters. Instead, he will bribe some of them and make them join him. To bribe the i-th monster, Manao needsprice[i] gold coins. The monsters are not too greedy, therefore each value inprice
will be either 1 or 2.

At the beginning, Manao travels alone. Each time he meets a monster, he first has the option to bribe it, and then the monster may decide to attack him. A monster will attack Manao if and only if he did not bribe it and its scariness is strictly greater than
the total scariness of all monsters in Manao's party. In other words, whenever Manao encounters a monster that would attack him, he has to bribe it. If he encounters a monster that would not attack him, he may either bribe it, or simply walk past the monster.



Consider this example: Manao is traversing the valley inhabited by the Dragon, the Hydra and the Killer Rabbit. When he encounters the Dragon, he has no choice but to bribe him, spending 1 gold coin (in each test case, Manao has to bribe the first monster he
meets, because when he travels alone, the total scariness of monsters in his party is zero). When they come by the Hydra, Manao can either pass or bribe her. In the end, he needs to get past the Killer Rabbit. If Manao bribed the Hydra, the total scariness
of his party exceeds the Rabbit's, so they will pass. Otherwise, the Rabbit has to be bribed for two gold coins. Therefore, the optimal choice is to bribe the Hydra and then to walk past the Killer Rabbit. The total cost of getting through the valley this
way is 2 gold coins.

You are given the vector <int>s dread and price. Compute the m
4000
inimum price Manao will pay to safely pass the valley.

Definition

 
Class:MonstersValley2
Method:minimumPrice
Parameters:vector <int>, vector <int>
Returns:int
Method signature:int minimumPrice(vector <int> dread, vector <int> price)
(be sure your method is public)
 
 

Constraints

-dread will contain between 1 and 20 elements, inclusive.
-Each element of dread will be between 1 and 2,000,000,000, inclusive.
-price will contain between the same number of elements as
dread
.
-Each element of price will be either 1 or 2.

Examples

0) 
 
{8, 5, 10}

{1, 1, 2}

Returns: 2

The example from the problem statement.
1) 
 
{1, 2, 4, 1000000000}

{1, 1, 1, 2}

Returns: 5

Manao has to bribe all monsters in the valley.
2) 
 
{200, 107, 105, 206, 307, 400}

{1, 2, 1, 1, 1, 2}

Returns: 2

Manao can bribe monsters 0 and 3.
3) 
 
{5216, 12512, 613, 1256, 66, 17202, 30000, 23512, 2125, 33333}

{2, 2, 1, 1, 1, 1, 2, 1, 2, 1}

Returns: 5

Bribing monsters 0, 1 and 5 is sufficient to pass safely.
#include <deque>

#include <vector>

#include <algorithm>

#include <functional>

#include <iterator>

#include <map>

#include <memory>

#include <numeric>

#include <queue>

#include <set>

#include <utility>

#include <stack>

#include <iostream>

#include <string>

#include <cstdio>

#include <cstdlib>

#include <cmath>

#define PI 3.1415926535898

#define LL long long

 

using namespace std;

class MonstersValley2

{

 public:

  int minimumPrice(vector <int> dread, vector <int> price)

  {

   bool flag = true;

   int max = -1;

   int max_pos = 0; 

   int cur_dread = dread[0];

   int cur_p = price[0];

   for(int i=0;i<dread.size();i++)

   {

    if ( dread[i] > max)

    {

     max = dread[i];

     max_pos = i;

    }

   }

   vector <int> stack(max_pos,0);

   stack[0] = 1;

   for(i=1;i<max_pos;i++)

   {

    if( cur_dread < dread[i])

    {

     if( price[i] == 2)

     {

      for(int j = i-1;j>=0;j++)

      {

       if( stack[j] == 1)

        break;

       if( price[j] == 1)

       {

        if((cur_dread+dread[j])>=dread[i])

        {

         cur_dread += dread[j];

         cur_p += price[j];

         stack[j] = 1;

         flag = false;

         break;

        }

       }

      }

      if(flag)

      {

       cur_dread += dread[i];

       cur_p += price[i];

       stack[i] = 1;

      }

     }

     else

     {

      cur_dread += dread[i];

      cur_p += price[i];

      stack[i] = 1;

     }

     flag = true;

    }

   }

   if ( cur_dread >= max)

    return cur_p;

   else

   {

    if ( price[max_pos] == 1)

     return (cur_p+1);

    else

    {

     for(int i=1;i<max_pos;i++)

     {

      if((stack[i]!=1) && (price[i] == 1))

      {

       if((cur_dread+dread[i])>=max)

        return (cur_p+1);

      }

     }

     return (cur_p+2);

    }

   }

  }

};

说说我解这道题思路:

1、这一路上遇到的Monster中,考虑如果Dread 值最大的在中间,那么通过它之后,后面都可以通过。故只考虑从头开始到Dread值最大的怪物。

2、考虑情况:到dread最大怪物前一个怪物时,最少price就是不能过就收买。那么到最大怪物处时:

1>若此时dread总值大于该怪物,则通过。

2>若小于,考虑2种情况:(注意限制条件:Each element of price will be either 1 or 2.price 只可能是1or2)

第一种,若最大dread怪物price值为1,则直接收买过。因为在前面怪物中再收买一个或者多个怪物来通过这个最大dread怪物,至少花费1。

第二种,若最大dread怪物price值为2,则在前面怪物中,则在前面需要另外收买一个price为1且收买后dread值能大于最大dread怪我,若不能满足,则直接收买这个最大dread怪物。 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  250-500pt 565 SRM TOPCODER