TopCoder - SRM521 div1 500 RangeSquaredSubsets
2015-08-27 10:13
351 查看
Problem Statement | ||||||||||||
Given a real number n, a set of points P in the XY plane is called n-squared if it is not empty and there exists a square of side n in the XY plane with its sides parallel to the axes such that a point from the given set of points is in P if and only if it is contained within the square. A point lying on a side or a vertex of the square is considered to be contained in it. You will be given two ints nlow and nhigh. You will also be given two vector <int>s x and y such that the coordinates of point i are (x[i],y[i]). Return the number of subsets of the input set described by x and y that are n-squared for some n between nlow and nhigh, inclusive. | ||||||||||||
Definition | ||||||||||||
| ||||||||||||
Limits | ||||||||||||
| ||||||||||||
Constraints | ||||||||||||
- | nlow will be between 1 and 100000000 (10^8), inclusive. | |||||||||||
- | nhigh will be between nlow and 100000000 (10^8), inclusive. | |||||||||||
- | x and y will contain between 1 and 40 elements, inclusive. | |||||||||||
- | x and y will contain the same number of elements. | |||||||||||
- | Each element of x and y will be between -100000000 (-10^8) and 100000000 (10^8), inclusive. | |||||||||||
- | All described points will be different. | |||||||||||
Examples | ||||||||||||
0) | ||||||||||||
| ||||||||||||
1) | ||||||||||||
| ||||||||||||
2) | ||||||||||||
| ||||||||||||
3) | ||||||||||||
| ||||||||||||
4) | ||||||||||||
|
离散化枚举, 注意细节
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include "limits.h"
using namespace std;
class RangeSquaredSubsets{
public:
struct point{
int x, y;
point(int X, int Y) : x(X), y(Y) {}
};
int INF = 1e9;
long long countSubsets(int nlow, int nhigh, vector <int> x, vector <int> y){
int n = x.size();
set<long long> ans;
vector<point> points;
for(int i = 0; i < n; ++i){
points.push_back(point(x[i], y[i]));
}
x.push_back(INF); x.push_back(-INF);
y.push_back(INF); y.push_back(-INF);
sort(x.begin(), x.end());
sort(y.begin(), y.end());
x.erase(unique(x.begin(), x.end()), x.end());
y.erase(unique(y.begin(), y.end()), y.end());
int nx = x.size();
int ny = y.size();
for(int i = 1; i < nx - 1; ++i) for(int j = i; j < nx - 1; ++j)
for(int k = 1; k < ny - 1; ++k) for(int l = k; l < ny - 1; ++l){
if(fit(nlow, nhigh, x[j] - x[i], y[l] - y[k], x[j + 1] - x[i - 1], y[l + 1] - y[k - 1])){
long long mask = 0;
for(int a = 0; a < n; ++a){
if(inSquare(points[a], x[i], x[j], y[k], y[l])){
mask |= 1ll << a;
}
}
if(mask) ans.insert(mask);
}
}
return ans.size();
}
bool fit(int nlow, int nhigh, int sx, int sy, int lx, int ly){
int w = max(nlow, max(sx, sy));
return (w <= nhigh) && (lx > w) && (ly > w);
}
bool inSquare(point p, int left, int right, int down, int up){
return (p.x >= left) && (p.x <= right) && (p.y >= down) && (p.y <= up);
}
};
相关文章推荐
- 枚举的用法详细总结
- c#入门之枚举和结构体使用详解(控制台接收字符串以相反的方向输出)
- 理解C#中的枚举(简明易懂)
- C#枚举中的位运算权限分配浅谈
- C++基础入门教程(四):枚举和指针
- FileShare枚举的使用小结(文件读写锁)
- Java枚举类用法实例
- 枚举窗口句柄后关闭所有窗口示例
- javascript模拟枚举的简单实例
- 深入剖析JavaScript中的枚举功能
- 枚举和宏的区别详细解析
- 深入理解C#中的枚举
- C#实现获取枚举中元素个数的方法
- java中枚举的详细使用介绍
- java枚举的使用示例
- Java枚举详解及使用实例(涵盖了所有典型用法)
- 一个运用enum类型的Demo
- 枚举隐式派生于System.Enum
- 枚举的隐士转换跟显示转换
- Java 枚举7常见种用法