题目链接:https://csacademy.com/contest/archive/task/dominant-free-sets/statement/
题目大意:给一个包含N个点的集合,集合中的点各不相同。对于点A,B,如果B.X >= A.X 并且 B.Y >= A.Y,那么认为B优于A。问不存在两个点A,B使得A优于B的集合共多少个。结果对1e9+7取余。10^51≤xi,yi, n≤10^5
解题思路:对于A,B两个点来说,假设A.X < B.X, 那么如果B不优于A,则必有B.Y < A.Y。所以可以将所有点按照X,Y从小到大排序,得到一个O(N^2)的递推关系:num[a[i]] = Σnum[a[j]] 1 <= j < i && a[j].x < a[i].x && a[j].y > a[i].y
但是仔细考虑一下,每个点的横纵坐标都是1e5之内,而且每次需要找的是已经访问过的点里面x小于当前x,y大于当前y的点的值。那么显然可以记录y坐标的值,利用前缀和即可判断已经出现了多少y大于当前y。而对于x来说,由于统计的是之前大于y的数量,因此只要按照x,y从小到大访问,那么就不会出现将x相同的统计进去。
需要注意的是,在取余的时候相减可能出现负数,而这个时候应当将结果加上mod然后再取余。
代码:
1 const int maxn = 1e5 + 10; 2 typedef long long ll; 3 const ll mod = 1e9 + 7; 4 int n; 5 int px[maxn], py[maxn], f[maxn]; 6 ll bit[maxn]; 7 8 int lowbit(int x){ 9 return x & (-x); 10 } 11 void add(int x, ll v){ 12 while(x <= 1e5){ 13 bit[x] = bit[x] % mod + v % mod; 14 bit[x] %= mod; 15 x += lowbit(x); 16 } 17 } 18 ll sum(int x){ 19 ll ans = 0; 20 while(x > 0){ 21 ans = ans % mod + bit[x] % mod; 22 ans %= mod; 23 x -= lowbit(x); 24 } 25 return ans % mod; 26 } 27 bool cmp(int i, int j){ 28 if(px[i] != px[j]) return px[i] < px[j]; 29 return py[i] < py[j]; 30 } 31 void solve(){ 32 for(int i = 0; i <= 1e5; i++) f[i] = i; 33 sort(f + 1, f + n + 1, cmp); 34 memset(bit, 0, sizeof(bit)); 35 for(int ii = 1; ii <= n; ii++){ 36 int i = f[ii]; 37 ll tmp = (sum(1e5) - sum(py[i]) + mod) % mod + 1; 38 tmp %= mod; 39 add(py[i], tmp); 40 } 41 ll ans = sum(1e5) % mod; 42 printf("%I64d ", ans); 43 } 44 int main(){ 45 scanf("%d", &n); 46 for(int i = 1; i <= n; i++) 47 scanf("%d %d", &px[i], &py[i]); 48 solve(); 49 return 0; 50 }
题目:
Dominant Free Sets
Memory limit: 256 MB
You are given a set SS of NN points. A point AA dominates point BB if A_x geq B_xAx≥Bx and A_y geq B_yAy≥By. Count the number of non-empty subsets of SS that don't contain two points AA and BB such that AA dominates BB.
Standard input
The first line contains a single integer NN.
Each of the next NN lines contains two integers xx and yy, representing a point at coordinates (x, y)(x,y).
Standard output
Print the answer modulo 10^9+7109+7 on the first line.
Constraints and notes
- 1 leq N leq 10^51≤N≤105
- 1 leq x_i, y_i leq 10^51≤xi,yi≤105
- The NN points are distinct
Input | Output | Explanation |
---|---|---|
4 1 1 2 2 3 3 4 4 |
4 |
The only valid sets are the ones with exactly one point. |
3 2 1 1 1 1 2 |
4 |
The sets are {(1, 1)}{(1,1)}, {(1, 2)}{(1,2)}, {(2, 1)}{(2,1)} and {(1, 2), (2, 1)}{(1,2),(2,1)} |