现在的位置: 首页 > 综合 > 正文

topcoder srm607 div2

2017年12月16日 ⁄ 综合 ⁄ 共 4677字 ⁄ 字号 评论关闭
文章目录

250pt:

题目:

Problem Statement

 

Andrew drew a bunch of points on a sheet of graph paper. You are given the coordinates of these points in two vector <int>s:
X and Y. That is, for each valid i, there is a point at the coordinates (X[i],
Y[i]).

Now Andrew wants to draw a rectangle. The sides of the rectangle must be parallel to the coordinate axes. (In other words, each side of the rectangle must be either horizontal or vertical.) Additionally, each of Andrew's points must be inside the rectangle
or on its boundary.

Return the area of the smallest possible rectangle Andrew can draw.

Definition

 
Class: BoundingBox
Method: smallestArea
Parameters: vector <int>, vector <int>
Returns: int
Method signature: int smallestArea(vector <int> X, vector <int> Y)
(be sure your method is public)

Limits

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

Constraints

- X will have between 2 and 50 elements, inclusive.
- X and Y will have the same number of elements.
- Each element of X and Y will be between -100 and 100, inclusive.
- The points described by X and Y will not be in a straight line horizontally or vertically. That is, the smallest rectangle will have a positive area.

Examples

0)  
 
{0,1}
{1,0}
Returns: 1
1)  
 
{0,-2,-1}
{-1,-1,-2}
Returns: 2
2)  
 
{0,0,1,0,-1,2}
{0,1,2,-2,0,-1}
Returns: 12
3)  
 
{9,-88,-40,98,-55,41,-38}
{-65,56,-67,7,-58,33,68}
Returns: 25110

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.
    

分析:找出x之间差的最大值,y之间差的最大值即可

代码:

#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 BoundingBox {
public:
	int smallestArea(vector <int>, vector <int>);
};

int BoundingBox::smallestArea(vector <int> X, vector <int> Y) {
	int xmax=-10000;
	int xmin = 10000;
	int ymax = -10000;
	int ymin = 10000;
	for(int i=0;i<X.size();i++)
	{
		xmax = max(X[i],xmax);
		xmin = min(X[i],xmin);
	}
	for(int i=0;i<Y.size();i++)
	{
		ymax = max(Y[i],ymax);
		ymin = min(Y[i],ymin);
	}
	return (xmax-xmin)*(ymax-ymin);
}


//Powered by [KawigiEdit] 2.0!

500pt:

题目:

Problem Statement

 

Marco recently learned about palindromic strings. A palindromic string is a string that reads the same forwards and backwards. For example, "radar" and "racecar" are palindromic strings.

Now Marco is excited about palindromic strings. In particular, he likes strings that have a lot of palindromic substrings. For example, he really likes the string "aaa" because it has 6 palindromic substrings: "a" occurs three times, "aa" occurs twice, and
"aaa" occurs once.

Right now, Marco has a string S composed of lowercase letters. You are to reconstruct
S from the given vector <string>s S1 and S2 as follows:

  1. Concatenate in order all elements of S1 to make a string
    A
    .
  2. Concatenate in order all elements of S2 to make a string
    B
    .
  3. Finally, concatenate A and B to get S.

Return the number of palindromic substrings in S.

Definition

 
Class: PalindromicSubstringsDiv2
Method: count
Parameters: vector <string>, vector <string>
Returns: int
Method signature: int count(vector <string> S1, vector <string> S2)
(be sure your method is public)

Limits

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

Constraints

- S1 and S2 will each contain no more than 50 elements.
- Each element of S1 and S2 will contain no more than 50 characters.
- S will contain at least 1 character.
- S will contain only lowercase letters ('a' - 'z').

Examples

0)  
 
{"a","a",""}
{"a"}
Returns: 6
This is the example given in the statement, "aaa".
1)  
 
{"zaz"}
{}
Returns: 4
2)  
 
{"top"}
{"coder"}
Returns: 8
3)  
 
{}
{"daata"}
Returns: 7
There are five palindromic substrings of length 1, one of length 2 ("aa" starting at index 1), and one of length 3 ("ata" starting at index 2).

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.
    

分析:如果暴力遍历的话,复杂度是N的三次,N=5000,明显复杂度太高,故用动态规划把复杂度降到N的平方

代码:

#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 PalindromicSubstringsDiv2 {
public:
	int count(vector <string>, vector <string>);
};

int PalindromicSubstringsDiv2::count(vector <string> S1, vector <string> S2) {
	string s = "";
	for(int i=0;i<S1.size();i++)
		s+=S1[i];
	for(int i=0;i<S2.size();i++)
		s+=S2[i];
	int n = s.length();
	vector<vector<bool> > dp(n,vector<bool>(n,false));
	for(int len = 1;len<=n;len++)
	{
		for(int i=0;i<=n-len;i++)
		{
			int j = i+len-1;
			if(i==j)
				dp[i][j]=true;
			else if(j==i+1)
			{
				if(s[i]==s[j])
					dp[i][j]=true;
			}
			else if(dp[i+1][j-1]&&s[i]==s[j])
				dp[i][j]=true;
		}
	}
	int ret = 0;
	for(int i=0;i<n;i++)
		for(int j=i;j<n;j++)
			if(dp[i][j])
				ret++;
	return ret;
}


//Powered by [KawigiEdit] 2.0!

1000pt的不会做。。。。。。

终于再次爬回div1了。。。再接再厉!

抱歉!评论已关闭.