【ZOJ3955 The 17th Zhejiang University Programming Contest H】【二分 贡献计数】Saddle Point 鞍点贡献
来源:互联网 发布:开罗旅游自助攻略知乎 编辑:程序博客网 时间:2024/06/06 07:14
Chiaki has an n × m matrix A. Rows are numbered from 1 to n from top to bottom and columns are numbered from 1 to m from left to right. The element in the i-th row and the j-th column is Ai, j.
Let M({i1, i2, ..., is}, {j1, j2, ..., jt}) be the matrix that results from deleting row i1, i2, ..., is and column j1, j2, ..., jt of A and f({i1, i2, ..., is}, {j1, j2, ..., jt}) be the number of saddle points in matrix M({i1, i2, ..., is}, {j1, j2, ..., jt}).
Chiaki would like to find all the value of f({i1, i2, ..., is}, {j1, j2, ..., jt}). As the output may be very large ((2n - 1)(2m - 1) matrix in total), she is only interested in the value
Note that a saddle point of a matrix is an element which is both the only largest element in its column and the only smallest element in its row.
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains four integers n and m (1 ≤ n, m ≤ 1000) -- the number of rows and the number of columns.
Each of the next n lines contains m integer Ai, 1, Ai, 2, ..., Ai, m (1 ≤ Ai, j ≤ 106), where Ai, j is the integer in the i-th row and the j-th column.
It is guaranteed that neither the sum of all n nor the sum of all m exceeds 5000.
Output
For each test case, output an integer denoting the answer.
Sample Input
22 21 11 14 51 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 20
Sample Output
4465
Author: LIN, Xi
Source: The 17th Zhejiang University Programming Contest Sponsored by TuSimple
#include<stdio.h>#include<iostream>#include<string.h>#include<string>#include<ctype.h>#include<math.h>#include<set>#include<map>#include<vector>#include<queue>#include<bitset>#include<algorithm>#include<time.h>using namespace std;void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }#define MS(x, y) memset(x, y, sizeof(x))#define ls o<<1#define rs o<<1|1typedef long long LL;typedef unsigned long long UL;typedef unsigned int UI;template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b > a)a = b; }template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b < a)a = b; }const int N = 1010, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f;template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }int casenum, casei;int n, m;vector<int>line[N], list[N];int a[N][N];int bit[N * 2];int main(){bit[0] = 1; for (int i = 1; i <= 2000; ++i)bit[i] = bit[i - 1] * 2 % Z;scanf("%d", &casenum);for (casei = 1; casei <= casenum; ++casei){scanf("%d%d", &n, &m);for (int i = 1; i <= n; ++i)line[i].clear();for (int i = 1; i <= m; ++i)list[i].clear();for (int i = 1; i <= n; ++i){for (int j = 1; j <= m; ++j){scanf("%d", &a[i][j]);list[j].push_back(a[i][j]);line[i].push_back(a[i][j]);}}for (int i = 1; i <= n; ++i)sort(line[i].begin(), line[i].end());for (int i = 1; i <= m; ++i)sort(list[i].begin(), list[i].end());int ans = 0;for (int i = 1; i <= n; ++i){for (int j = 1; j <= m; ++j){//行最小,大的随便删int line_can_del = line[i].size() - (upper_bound(line[i].begin(), line[i].end(), a[i][j]) - line[i].begin());//列最大,小的随便删int list_can_del = (lower_bound(list[j].begin(), list[j].end(), a[i][j]) - list[j].begin());gadd(ans, bit[line_can_del + list_can_del]);}}printf("%d\n", ans);}return 0;}/*【题意】找{列严格最大,行严格最小}的节点贡献之和【分析】按照贡献思想直接算即可*/
- 【ZOJ3955 The 17th Zhejiang University Programming Contest H】【二分 贡献计数】Saddle Point 鞍点贡献
- The 12th Zhejiang University Programming Contest
- The 14th Zhejiang University Programming Contest
- The 14th Zhejiang University Programming Contest
- [The 14th Zhejiang University Programming Contest]
- The 15th Zhejiang University Programming Contest
- The 15th Zhejiang University Programming Contest
- The 15th Zhejiang University Programming Contest
- The 15th Zhejiang University Programming Contest
- The 16th Zhejiang University Programming Contest-
- 【ZOJ3950 The 17th Zhejiang University Programming Contest C】【简单日期模拟】How Many Nines 区间9个数计数
- 【ZOJ3954 The 17th Zhejiang University Programming Contest G】【预处理 哈希 二分】Seven-Segment Display 矩阵列全排列的
- The 11th Zhejiang University Programming Contest / 解题报告 4.3
- The 11th Zhejiang University Programming Contest - C/ Chinese Zodiac
- The 11th Zhejiang University Programming Contest - G/ Gaussian Prime
- The 11th Zhejiang University Programming Contest C G J
- 115 - The 12th Zhejiang University Programming Contest - C
- The 14th Zhejiang University Programming Contest(未完工)
- ROS roslaunch 学习
- 在JavaScript的Array数组中调用一组Function方法
- Ireport子报表总结
- 数据结构链表的简单处理
- 我在等一个人
- 【ZOJ3955 The 17th Zhejiang University Programming Contest H】【二分 贡献计数】Saddle Point 鞍点贡献
- leetcodeOJ 51. N-Queens
- 某一模块动态+、—效果的实现
- 不借助中间页面实现JSON数据的接收
- Dataguard的Log Gap检测和处理原理
- css布局初次尝试-Layer7
- jQuery实现Select多选列表双击选中项时相互添加
- 【Linux】Centos6.8安装docker
- Centos7_install_shadwsocks