topcoder srm609 div1

总的来说这次的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

 
Class: MagicalStringDiv1
Method: getLongest
Parameters: string
Returns: int
Method signature: int getLongest(string S)
(be sure your method is public)

Limits

 
Time limit (s): 2.000
Memory limit (MB): 256

Constraints

- S will contain between 1 and 50 characters, inclusive.
- Each character of S will be '<' or '>'.

Examples

0)  
 
"<><><<>"
Returns: 4
The longest magical string Illy can produce is ">><<". Its length is 4. To change S into ">><<", Illy must remove the characters at 0-based indices 0, 2, and 6.
1)  
 
">>><<<"
Returns: 6
S is already a magical string. Therefore Illy doesn't have to remove any character.
2)  
 
"<<<>>>"
Returns: 0
Illy has to remove all characters of S.
3)  
 
"<<<<><>>><>>><>><>><>>><<<<>><>>>>><<>>>>><><<<<>>"
Returns: 24

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!

500pt题目:

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

 
Class: PackingBallsDiv1
Method: minPacks
Parameters: int, int, int, int, int
Returns: int
Method signature: int minPacks(int K, int A, int B, int C, int D)
(be sure your method is public)

Limits

 
Time limit (s): 2.000
Memory limit (MB): 256

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)  
 
3
4
2
5
6
Returns: 4
There are three colors of balls. Using the pseudocode in the problem statement, we can compute that X[0]=4, X[1]=2, and X[2]=4. As there are 10 balls and we can only put at most 3 into each package, we need at least 4 packages. One possible solution with 4 packages is {0,1,2}, {0,0}, {0,1}, and {2,2,2}. (That is, the first package contains one ball of each color, the second package contains two balls of color 0, and so on.)
1)  
 
1
58
23
39
93
Returns: 58
All the balls have the same color, and each package can only contain one ball. Thus, the number of packages is the same as the number of balls.
2)  
 
23
10988
5573
4384
100007
Returns: 47743
3)  
 
100000
123456789
234567890
345678901
1000000000
Returns: 331988732
Watch out for integer overflow when generating X.

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 ;
	
}



你可能感兴趣的:(Algorithm,C++)