XTUOJ1247 Pair-Pair 预处理+暴力
2016-06-18 09:42
591 查看
分析:开个1000*1000的数组,预处理矩阵和,然后分类讨论就好
时间复杂度:O(n)
View Code
时间复杂度:O(n)
#include <cstdio> #include <iostream> #include <ctime> #include <vector> #include <cmath> #include <map> #include <queue> #include <algorithm> #include <cstring> using namespace std; typedef long long LL; const int N=1e5+5; const int INF=0x3f3f3f3f; const int mod=1e9+7; struct Node{ int x,y; }p ; int a[1005][1005]; int high[1005],low[1005],mid[1005]; LL ret[5]; int main() { int n,m; while(~scanf("%d%d",&n,&m)){ memset(a,0,sizeof(a)); memset(ret,0,sizeof(ret)); memset(high,0,sizeof(high)); memset(low,0,sizeof(low)); memset(mid,0,sizeof(mid)); for(int i=1;i<=n;++i){ scanf("%d%d",&p[i].x,&p[i].y); ++a[p[i].x][p[i].y]; if(p[i].y>p[i].x)++high[p[i].x]; else if(p[i].y==p[i].x)++mid[p[i].x]; else ++low[p[i].x]; } for(int i=1;i<=m;++i){ low[i]+=low[i-1]; mid[i]+=mid[i-1]; high[i]+=high[i-1]; } for(int i=1;i<=m;++i) for(int j=1;j<=m;++j) a[i][j]+=a[i-1][j]+a[i][j-1]-a[i-1][j-1]; for(int i=1;i<=n;++i){ if(p[i].y>p[i].x){ int tmp=0,t; t=high[m]-high[p[i].y]; tmp+=t;ret[4]+=t; t=low[m]-low[p[i].y]+mid[m]-mid[p[i].y]; tmp+=t;ret[3]+=t; t=high[p[i].y]-high[p[i].y-1]; tmp+=t;ret[3]+=t; t=high[p[i].y-1]-high[p[i].x]; tmp+=t;ret[3]+=t; t=a[p[i].x][m]-a[p[i].x][p[i].y]; tmp+=t;ret[3]+=t; ret[2]+=1ll*(n-tmp); } else if(p[i].x==p[i].y){ int tmp=0,t; t=high[m]-high[p[i].x]; tmp+=t;ret[3]+=t; t=low[p[i].x]+mid[p[i].x]; tmp+=t;ret[1]+=t; ret[2]+=1ll*(n-tmp); } else { int tmp=0,t; t=low[p[i].y]+mid[p[i].y]; tmp+=t;ret[1]+=t; t=high[m]-high[p[i].y]; tmp+=t;ret[3]+=t; ret[2]+=1ll*(n-tmp); } } printf("%I64d %I64d %I64d %I64d\n",ret[1],ret[2],ret[3],ret[4]); } return 0; }
View Code
相关文章推荐
- 统计学、人工智能、机器学习、数据挖掘的区别与联系之(1) 概念
- rails安装
- hdu 1023 Train Problem II 卡特兰数
- Painy's World 重写以及 杀人速写重写
- Tensorflow 深度学习分布式实现方式
- AFN3.0 PATCH请求报错Error Domain=NSURLErrorDomain Code=-1012 "(null)"
- "围观"设计模式(22)--行为型之职责链模式(Chain Of Responsibility Pattern)
- http://blog.csdn.net/liuhe688/article/details/6733407
- 磁盘阵列RAID原理、种类及性能优缺点对比
- 服务器TIME_WAIT和CLOSE_WAIT详解和解决办法
- wait3,wait4 用法
- hdu 1021 Fibonacci Again(斐波那契数列,取余)
- 手贱装了win10之后用hotmail的邮箱登录了小娜
- 使用dd工具对磁盘RAID5和10进行I/O性能测试
- Lock wait timeout exceeded; try restarting transaction
- rails安装:gem更换成淘宝源
- LeetCode-217 Contains Duplicate
- Keychain简单存储(不使用官方封装)
- crossdomain.xml的配置详解
- 535 5.7.8 Error: authentication failed: generic failure