zoj3955:Saddle Point(想法题)
传送门
题意
给出n*m的矩阵,询问所有子矩阵中鞍点的个数
鞍点定义:在行唯一最小,在列唯一最大
分析
我们遍历每个点,计算该点对于答案的贡献即可。
每个点的贡献为\((2^{numa[i][j]})*(2^{numb[i][j]})\)
numa[i][j]记录第i行大于该点的数个数
numb[i][j]记录第j列小于该点的数个数
trick
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const long long mod = 1e9+7;
int mp[1010][1010];
long long ans;
int numa[1010][1010],numb[1010][1010];
int t,n,m,a[1010];
long long quick_mod(int a,int p)
{
long long ret=a,ans=1;
if(p<0) return 1;
for(;p;p>>=1,(ret*=ret)%=mod) if(p&1) (ans*=ret)%=mod;
return ans;
}
int main()
{
for(scanf("%d",&t);t--;)
{
scanf("%d %d",&n,&m);
for(int i=1;i<=n;++i)for(int j=1;j<=m;++j) scanf("%d",&mp[i][j]);
memset(numa,0,sizeof(numa));
memset(numb,0,sizeof(numb));
for(int i=1;i<=n;++i)
{
for(int j=1;j<=m;++j) a[j]=mp[i][j];
sort(a+1,a+1+m);
for(int j=1;j<=m;++j) numa[i][j]=m-(upper_bound(a+1,a+1+m,mp[i][j])-a-1);
}
// for(int i=1;i<=n;++i)for(int j=1;j<=m;++j) printf("%d%c",numa[i][j],j==m?'\n':' ');
for(int j=1;j<=m;++j)
{
for(int i=1;i<=n;++i) a[i]=mp[i][j];
sort(a+1,a+1+n);
for(int i=1;i<=n;++i) numb[i][j]=(lower_bound(a+1,a+1+n,mp[i][j])-a-1);
}
// for(int i=1;i<=n;++i)for(int j=1;j<=m;++j) printf("%d%c",numb[i][j],j==m?'\n':' ');
ans=0;
for(int i=1;i<=n;++i)for(int j=1;j<=m;++j) (ans+=quick_mod(2,numa[i][j])*quick_mod(2,numb[i][j]))%=mod;
printf("%lld\n",ans);
}
}
zoj3955:Saddle Point(想法题)的更多相关文章
- HDU 4972 Bisharp and Charizard 想法题
Bisharp and Charizard Time Limit: 1 Sec Memory Limit: 256 MB Description Dragon is watching NBA. He ...
- CodeForces 111B - Petya and Divisors 统计..想法题
找每个数的约数(暴力就够了...1~x^0.5)....看这约数的倍数最后是哪个数...若距离大于了y..统计++...然后将这个约数的最后倍数赋值为当前位置...好叼的想法题.... Program ...
- HDU - 5806 NanoApe Loves Sequence Ⅱ 想法题
http://acm.hdu.edu.cn/showproblem.php?pid=5806 题意:给你一个n元素序列,求第k大的数大于等于m的子序列的个数. 题解:题目要求很奇怪,很多头绪但写不出, ...
- HDU - 5969 最大的位或 想法题
http://acm.hdu.edu.cn/showproblem.php?pid=5969 (合肥)区域赛签到题...orz 题意:给你l,r,求x|y的max,x,y满足l<=x<=y ...
- HDU 4193 Non-negative Partial Sums(想法题,单调队列)
HDU 4193 题意:给n个数字组成的序列(n <= 10^6).求该序列的循环同构序列中,有多少个序列的随意前i项和均大于或等于0. 思路: 这题看到数据规模认为仅仅能用最多O(nlogn) ...
- CodeForces - 156B Suspects 逻辑 线性 想法 题
题意:有1~N,n(1e5)个嫌疑人,有m个人说真话,每个人的陈述都形如X是凶手,或X不是凶手.现在给出n,m及n个陈述(以+x/-X表示)要求输出每个人说的话是true ,false or notd ...
- 2016华中农业大学预赛 E 想法题
Problem E: Balance Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 205 Solved: 64[Submit][Status][We ...
- codeforces 657C - Bear and Contribution [想法题]
题目链接: http://codeforces.com/problemset/problem/657/C ----------------------------------------------- ...
- POJ 1066 Treasure Hunt [想法题]
题目链接: http://poj.org/problem?id=1066 --------------------------------------------------------------- ...
随机推荐
- POJ 1144 割点
题意 :求割点的数量 #include<iostream> #include<stdio.h> #include<vector> #include<strin ...
- 洛谷—— P1977 出租车拼车
https://www.luogu.org/problem/show?pid=1977 题目背景 话说小 x 有一次去参加比赛,虽然学校离比赛地点不太远,但小 x 还是想坐 出租车去.大学城的出租车总 ...
- ZOJ 3471 【状态压缩DP】
题意: 有n种化学物质,他们彼此反应会有一种消失并释放出能量. 给出矩阵,第i行j列代表i和j反应j消失释放的能量. 求最大释放多少能量. 思路: 状态压缩DP,我是这么想的. 利用二进制0代表该物质 ...
- 【纯净版windows系统】U盘启动制作图文教程
无废话,按照步骤来就可以. 1.一个大于4G的U盘(格式化)准备好U盘,请注意制作过程中对U盘有格式化操作,有用的东西请先备份 2.UltraISO(软碟通软件)下载安装百度“软碟通”,或者访问 ht ...
- FM算法及FFM算法
转自:http://tech.meituan.com/deep-understanding-of-ffm-principles-and-practices.html http://blog.csdn. ...
- leetCode 67.Add Binary (二进制加法) 解题思路和方法
Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...
- cocos2dx 制作单机麻将(五)
cocos2dx 制作单机麻将(五) 麻将逻辑6 最基础的4人麻将逻辑(轮流循环出牌, 之前学的都能用上 跑起来了!!!) 最基础的麻将逻辑 依据自己须要 设置麻将人数GAME_PLAYER 基本流 ...
- Android Activity与远程Service的通信学习总结
当一个Service在androidManifest中被声明为 process=":remote", 或者是还有一个应用程序中的Service时,即为远程Service, 远程的意 ...
- E. Dreamoon and Strings(Codeforces Round #272)
E. Dreamoon and Strings time limit per test 1 second memory limit per test 256 megabytes input stand ...
- Python的字符串和列表和字典的方法/函数
字符串 S.find()#可指定范围查找字串,返回索引值,否则返回-1 S.index()#同find,只是找不到的之后返回异常 S.count()#返回找到字串的个数 S.lower()#转小写 S ...