ZOJ-3955-Saddle Point【17th浙大校赛】
来源:互联网 发布:mac下载flash软件 编辑:程序博客网 时间:2024/05/17 02:41
Saddle Point
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
2
2 2
1 1
1 1
4 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
Sample Output
4
465
题目链接:ZOJ-3955
题目大意:给出一个n*m的矩阵,定义矩阵中的特殊位置(i, j)为当且仅当Aij 是这一行唯一最小的数,是这一列唯一最大的数。删去一些行和列,剩下的元素构成的矩阵一共有(2^n-1)*(2^m-1)种,求这些矩阵的特殊位置的数量之和。
题目思路:我们可以算每个数对答案的贡献。设第 i 行比a[i][j]大的数有 k1 ,设第 j 列比a[i][j]小的数有 k2 个。则a[i][j]对答案的贡献就是 2^k1*2^k2
参考博客:here
以下是代码:
#include <iostream>#include <iomanip>#include <fstream>#include <sstream>#include <cmath>#include <cstdio>#include <cstring>#include <cctype>#include <algorithm>#include <functional>#include <numeric>#include <string>#include <set>#include <map>#include <stack>#include <vector>#include <queue>#include <deque>#include <list>using namespace std;#define LL long long#define mod 1000000007LL qpow(LL a, LL n){ LL ret = 1; while (n) { if (n & 1) ret = ret * a % mod; a = a * a % mod; n>>= 1; } return ret;}long long num[1005][1005];long long row[1005][1005];long long col[1005][1005];int main() { int t; cin >> t; while(t--) { int n,m; scanf("%d%d",&n,&m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { scanf("%lld",&num[i][j]); row[i][j] = num[i][j]; col[j][i] = row[i][j]; } } for (int i = 0; i < n; i++) sort(row[i], row[i] + m); for (int i = 0; i < m; i++) sort(col[i], col[i] + n); long long ans = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { long long a = upper_bound(row[i], row[i] + m, num[i][j]) - row[i]; long long b = lower_bound(col[j], col[j] + n, num[i][j]) - col[j]; a = m - a; ans = (ans + qpow(2,a) * qpow(2,b) % mod) % mod; } } printf("%lld\n",ans); } return 0;}
- ZOJ-3955-Saddle Point【17th浙大校赛】
- zoj 3955 Saddle Point
- ZOJ 3955 Saddle Point (树状数组)
- ZOJ-3953-Intervals【贪心】【17th浙大校赛】
- ZOJ-3954-Seven-Segment Display【17th浙大校赛】
- ZOJ 3955 Saddle Point (排序/二分/树状数组)
- 【ZOJ3955 The 17th Zhejiang University Programming Contest H】【二分 贡献计数】Saddle Point 鞍点贡献
- ZOJ-3956-Course Selection System【01背包】【17th浙大校赛】
- ZOJ-3956-Course Selection System【01背包】【17th浙大校赛】
- ZOJ3955 Saddle Point 【快速幂】
- zoj 3767(浙大校赛)
- 17th浙大校赛 ZOJ3952 Fibonacci Sequence Chicken Edition【汇编】
- The 17th Zhejiang University Programming Contest Sponsored by TuSimple 浙大校赛C题 How Many Nines
- ZOJ 3866 Cylinder Candy (浙大校赛+积分应用)
- 浙大校赛 ZOJ 3956Course Selection System (01背包)
- 浙大17年校赛(ZOJ 3953) Intervals[贪心]
- ZOJ 3723 (浙大月赛)状压DP
- 浙大月赛ZOJ Monthly, August 2014
- 第七章 目录与路径
- 第七章 文件与目录管理
- 一个前端的计算器
- 第七章 文件内容查阅
- 第七章 文件与目录的默认权限与隐藏权限
- ZOJ-3955-Saddle Point【17th浙大校赛】
- 大写金额转换小写数字金额(美图2017线下笔试题)
- 第七章 命令与文件的搜寻
- 用弹性布局实现垂直左右居中
- 第七章 极重要!权限与命令间的关系
- Eddy的难题
- 第八章 认识 EXT2 文件系统
- Cookie/Session机制详解
- Java学习笔记_003_数组