TOPCODER/SRM565 DIVII 250、500pt(500pt无递归解法)
2012-12-31 12:28
471 查看
250pt:
#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:
#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怪物。
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 | ||||||||||||
| ||||||||||||
Constraints | ||||||||||||
- | values will contain between 1 and 50 elements, inclusive. | |||||||||||
- | Each element of values will be between 0 and 9, inclusive. | |||||||||||
Examples | ||||||||||||
0) | ||||||||||||
| ||||||||||||
1) | ||||||||||||
| ||||||||||||
2) | ||||||||||||
| ||||||||||||
3) | ||||||||||||
| ||||||||||||
4) | ||||||||||||
|
#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 | ||||||||||||
| ||||||||||||
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) | ||||||||||||
| ||||||||||||
1) | ||||||||||||
| ||||||||||||
2) | ||||||||||||
| ||||||||||||
3) | ||||||||||||
|
#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怪物。
相关文章推荐
- TC SRM 655 DIV1 250,500pt
- SRM 497(DIVII) 250 Filtering
- LeetCode path Sum i ,ii递归和迭代解法
- SRM 440 DIVII 250中文翻译及源码(C#)
- TOPCODER/SRM 566 DIVII(250、500、1000题)(1000PT暂未附上代码)
- leetcode [Best Time to Buy and Sell Stock II]//待整理多种解法
- 01背包问题(当有的背包重量是非整数时)的递归(优化成动态规划+再用滚动数组优化)解法+一些动态规划(递归,搜索)的高级技巧
- Leetcod-219_Contains Duplicate II-滚动滑块解法-【C++】
- PKU 1191 棋盘分割 递归解法
- TopCoder 250 points 3-SRM 145 DIV 1 84.71/250 33.88%
- Codeforces Round #449 (Div. 2) C. Nephren gives a riddle 递归
- TopCoder 250 points 18-SRM 152 DIV 2 167.05/250 66.82%
- Binary Tree Maximum Path Sum Java 递归解法
- 最大子数组和(DP和递归解法)与最大子矩阵和
- HDU---Children's queue(递归解法)
- SRM 399 DIV2 [250]
- SRM 583 Div II Level Three:GameOnABoard,Dijkstra最短路径算法
- SRM 571 250 DIV2
- 记第一次TopCoder, 练习SRM 583 div2 250
- SRM 509 DIV1 500pt(DP)