总的来说这次的div1 前两题都比以前简单,我这种不碰500的都开始做500分的题了= =
250pt题目:
Problem Statement |
|||||||||||||
Magical Girl Illy uses "magical strings" to cast spells. For her, a string X is magical if and only if there exists a non-negative integer k such that X is composed of k consecutive '>' characters followed by k consecutive '<' characters. Note that the empty string is also magical (for k=0). Once Illy picked up a string S. Each character of S was either '<' or '>'. Illy can change S by removing some of its characters. (The characters she does not remove will remain in their original order.) Illy wants to change S into a magical string by removing as few of its characters as possible. You are given the string S. Compute and return the length of the magical string Illy will obtain from S. |
|||||||||||||
Definition |
|||||||||||||
|
|||||||||||||
Limits |
|||||||||||||
|
|||||||||||||
Constraints |
|||||||||||||
- | S will contain between 1 and 50 characters, inclusive. | ||||||||||||
- | Each character of S will be '<' or '>'. | ||||||||||||
Examples |
|||||||||||||
0) | |||||||||||||
|
|||||||||||||
1) | |||||||||||||
|
|||||||||||||
2) | |||||||||||||
|
|||||||||||||
3) | |||||||||||||
|
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
分析:题目说S长度最多50,那么暴力搜索页就50的平方,肯定可以解决
代码:
#include <vector> #include <list> #include <map> #include <set> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> using namespace std; class MagicalStringDiv1 { public: int getLongest(string); }; int MagicalStringDiv1::getLongest(string S) { int n = S.length(); int ret = 0; for(int i=0;i<n;i++) { int left=0; int right = 0; for(int j=0;j<=i;j++) { if(S[j]=='>') left++; } for(int j=i+1;j<n;j++) { if(S[j]=='<') right++; } ret = max(ret,min(left,right)); } return ret*2; } //Powered by [KawigiEdit] 2.0!
Problem Statement |
|||||||||||||
We have balls of K different colors. The colors are numbered 0 through K-1, and the number of balls of color i is X[i]. We want to divide the balls into as few packages as possible. Each package must contain between 1 and K balls, inclusive. Additionally, each package must be either a "normal set" (all balls in the package have the same color), or a "variety set" (no two balls have the same color). You are given the int K. You are also given ints A, B, C, and D. Use these to generate the array X using the following pseudocode: X[0] = A for i = 1 to K-1: X[i] = (X[i-1] * B + C) % D + 1 Compute and return the smallest possible number of packages. |
|||||||||||||
Definition |
|||||||||||||
|
|||||||||||||
Limits |
|||||||||||||
|
|||||||||||||
Notes |
|||||||||||||
- | You can assume that the answer will fit into a signed 32-bit integer. | ||||||||||||
Constraints |
|||||||||||||
- | K will be between 1 and 100,000, inclusive. | ||||||||||||
- | B, C and D will be between 1 and 1,000,000,000, inclusive. | ||||||||||||
- | A will be between 1 and D, inclusive. | ||||||||||||
Examples |
|||||||||||||
0) | |||||||||||||
|
|||||||||||||
1) | |||||||||||||
|
|||||||||||||
2) | |||||||||||||
|
|||||||||||||
3) | |||||||||||||
|
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
分析:
贪心,首先如果每种颜色多余K个,那么一个袋子装K个同样颜色的最划算,当所有颜色的小球的个数都小于K时,排个序,比如剩下1,2,3,那么有要考虑是每个袋子中放不一样的还是放一样的需要的袋子少,这里要明白一点,如果2代表的颜色是和其他的颜色的球放在一个袋子里,那么比它小的1肯定可以顺带进去,比2大的我们采取袋子中的球要色一样的策略,这种策略对1,2,3都算一遍即可求出最小值。
代码:
#include <vector> #include <list> #include <map> #include <set> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> using namespace std; class PackingBallsDiv1 { public: int minPacks(int, int, int, int, int); }; int PackingBallsDiv1::minPacks(int K, int A, int B, int C, int D) { vector<long long> v(K,(long long)0); v[0]=(long long)A; for(int i=1;i<K;i++) v[i] = (v[i-1] *(long long) B +(long long) C) % (long long)D + (long long)1; int sum = 0; vector<long long> v1; for(int i=0;i<K;i++) { sum+=v[i]/K; v[i]=v[i]%K; } sort(v.begin(),v.end()); int ret = sum+v.size(); for(int i=0;i<K;i++) ret = min(ret,(int)(sum+v[i]+K-i-1)); return ret ; }