TopCoder SRM 727 Div2 500-point TwoDiagonals
2018-01-12 18:04
591 查看
题目
Bearland can be represented as a horizontal plane with N distinct points, denoting positions of N cities.The i-th city has coordinates (x[i], y[i]).Limak is planning to build two infinitely long roads in Bearland.One should go northwest, and the other should go northeast (both these directions are tilted exactly 45 degrees from the vertical direction).It implies that the two roads are perpendicular to each other, and that they have an intersection point (this point can be a city but not necessarily).
Roads increase the trade significantly.A city will be happy if it lies on at least one of the two roads.Help Limak and find the maximum possible number of happy cities.
Example 0
x { 1, 4, 4, 5 }y { 3, 0, 2, 3 }
Returns 4
There are four cities: (1,3), (4,0), (4,2), (5,3).It’s possible to draw two lines in such a way that all cities will be happy:
Example 1
x { 0, 1, 2, 3, 4, 5 }y { 2, 2, 2, 2, 2, 2 }
Returns 2
The new roads can go through at most two cities in this case.One of many optimal placements is:
分析
(1)题意给定坐标中一些点,在坐标系中画两条相互垂直的先,斜率为+1和-1,试问如何画这两条线,使得最终在线上的点的个数最多,求出最大点数。
(2)
k = 1时,y = x + b1 ==> b1 = y -x
k = -1时,y = -x + b2 ==> b2 = y + x
这里b1和b2分别是两条线的截距,分别对应着两条线。
(3)枚举b1和b2这两条线的所有组合,分别求出这条线上共有多少点(需要减去交点),取最大值,即为本题所求。
以Example 0这例
b1 = 4的线上有2个点,b1 = 6的线上有1个点,b1 = 8的线上有1个点
b2 = -4的线上有1个点,b2 = -2的线上有2个点,b2 = 2的线上有1个点
对于b1 = 4和b2 = -4这两条线,共有2 + 1 - 1(两条线都通过)= 2个点
对于b1 = 4和b2 = -2这两条线,共有2 + 2 = 4个点
对于b1 = 4和b2 = 2这两条线,共有2 + 1 - 1(两条线都通过)= 2个点
对于b1 = 6和b2 = -4这两条线,共有1 + 1 = 2个点
对于b1 = 6和b2 = -2这两条线,共有2 + 1 - 1(两条线都通过)= 2个点
对于b1 = 6和b2 = 2这两条线,共有1 + 1 = 2个点
对于b1 = 8和b2 = -4这两条线,共有1 + 1 = 2个点
对于b1 = 8和b2 = -2这两条线,共有2 + 1 - 1(两条线都通过)= 2个点
对于b1 = 8和b2 = 2这两条线,共有1 + 1 = 2个点
所以,最大值取b1 = 4和b2 = -2两条线,共有4个点
代码
#include <iostream> #include <vector> #include <map> #include <set> using namespace std; class TwoDiagonals { public: int maxPoints(vector<int> x, vector<int> y) { map<int, int> b1; // y = -x + b1 ==> b1 = y + x map<int, int> b2; // y = x + b2 ==> b2 = y - x set<pair<int, int> > points; int maxCities = 0; int cityCnt; double crossX; double crossY; for(unsigned int i = 0; i < x.size(); i++) { b1[y[i] + x[i]]++; b2[y[i] - x[i]]++; points.insert(make_pair(x[i], y[i])); } for(map<int, int>::iterator iter1 = b1.begin(); iter1 != b1.end(); iter1++) { for(map<int, int>::iterator iter2 = b2.begin(); iter2 != b2.end(); iter2++) { cityCnt = (*iter1).second + (*iter2).second; crossX = (double)((*iter1).first - (*iter2).first)/2; crossY = (double)((*iter1).first + (*iter2).first)/2; pair<double, double> crossPoint = make_pair(crossX, crossY); if(points.count(crossPoint)) { cityCnt--; } maxCities = max(maxCities, cityCnt); } } return maxCities; } }; int main() { TwoDiagonals t; vector<int> x; x.push_back(1); x.push_back(4); x.push_back(4); x.push_back(5); // x.push_back(0); // x.push_back(1); // x.push_back(2); // x.push_back(3); // x.push_back(4); // x.push_back(5); vector<int> y; y.push_back(3); y.push_back(0); y.push_back(2); y.push_back(3); // y.push_back(2); // y.push_back(2); // y.push_back(2); // y.push_back(2); // y.push_back(2); // y.push_back(2); cout << t.maxPoints(x, y) << endl; return 0; }
参考
http://blog.csdn.net/zarlove/article/details/79038268更多内容请关注微信公众号
相关文章推荐
- 小朋友学TopCoder(8):SRM726 DIV2 500-point
- TopCoder SRM 646 Div2 Problem 500 - TheGridDivTwo (BFS + 优先队列)
- Topcoder: Problem Statement SRM 39 DIV1 500 Point
- 小朋友学TopCoder(3):SRM144 DIV2 550-point
- TopCoder SRM 650 Div2 Problem 500 - TaroFillingAStringDiv2 (DP)
- topcoder SRM 619 DIV2 GoodCompanyDivTwo
- TopCoder SRM 633 Div2 Problem 500 - Jumping
- 小朋友学TopCoder(4):SRM144 DIV2 1100-point
- 小朋友学TopCoder(5):SRM144 DIV1 550-point
- 小朋友学TopCoder(7):SRM726 DIV2 250-point
- Topcoder SRM 503 DIV1 500 KingdomXCitiesandVillages
- topcoder SRM 548 DIV2 500
- TopCoder SRM 651 Div2 Problem 500 - FoxAndSouvenirTheNext (DP)
- 小朋友学TopCoder(6):SRM144 DIV1 1100-point
- Topcoder SRM 660 DIV1 500 Privateparty(数学,容斥)
- SRM 483 Div II Level Two: MovieSeating, TopCoder上正确率不到%10的题
- SRM 669 DIV 2 CombiningSlimes 500-point
- Topcoder Arena SRM 144 DIV2 1100 point 图遍历(非递归)+动态规划
- Topcoder SRM 619 DIv2 500 --又是耻辱的一题
- TopCoder SRM 634 Div2 Problem 500 - ShoppingSurvey