CF-567C - Geometric Progression
来源:互联网 发布:网络语gg是什么意思 编辑:程序博客网 时间:2024/05/18 04:29
<span style="font-size: 1em; line-height: 1.4em; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: rgb(255, 255, 255);">这道题,我用一个结构体储存数值和位置,然后二次排序</span>
我最初的时候枚举起点x,然后二分查找数值为x*k区间中的位置位于x之后的区间,然后在枚举区间的y(=x*k),用二分再求出y*k切在其之后的区间
这样写时间会t
后面发现枚举起点效率低,没有枚举中点效率高,因为枚举中点一可以剔除x%k!=0的
并且可以在lgn的时间内 查找到x/k和x*k的区间长度
#include<iostream>#include<algorithm>using namespace std;const int maxn=2e5+100;long long n,k,ans;struct node{ long long num; int pos; node(long long num,int pos):num(num),pos(pos){} node():num(0),pos(0){ } bool operator < (const node& rhy)const{ return num<rhy.num||(num==rhy.num&&pos<rhy.pos); }}a[maxn];long long findcnt(long long x,int s,int e){ return upper_bound(a,a+n,node(x,e-1))-lower_bound(a,a+n,node(x,s+1));}int main(){ cin>>n>>k; for(int i=0;i<n;i++){ cin>>a[i].num; a[i].pos=i; } ans=0; sort(a,a+n); if(n>=3){ for(int i=0;i<n;i++){ long long num=a[i].num; int pos=a[i].pos; if(num%k==0) ans+=findcnt(num/k,-1,pos)*findcnt(num*k,pos,n+1); } } cout<<ans<<endl; return 0;}
看别人代码发现更加机智的写法,用两个map分别记录下第i位之前和之后的每个数字出现次数
然后~~~~~
map<int,int> s, t; rep(i, n) ++ t[a[i]]; ll ans = 0; rep(i, n) { int x = a[i]; -- t[x]; if(x % k == 0 && abs((ll)x * k) <= (ll)1e9) ans += (ll)s[x / k] * t[x * k]; ++ s[x]; }
0 0
- CF-567C - Geometric Progression
- CF 567C. Geometric Progression
- CF 567C(Geometric Progression-map)
- codeforces(567C)-- Geometric Progression
- codeforces 567C Geometric Progression
- Codeforces 567C Geometric Progression
- CodeForces 567C Geometric Progression
- CodeForces 567C Geometric Progression
- codeforces#314C&567C Geometric Progression
- codeforce 567c Geometric Progression(map)
- codeforces 567C Geometric Progression (map维护)
- Codeforces 567C Geometric Progression (离散 + DP)
- CodeForces 567C Geometric Progression【思维+map】
- CodeForces #Pi C.Geometric Progression
- CF Geometric Progression (DP和map)
- codeforces 567E C. Geometric Progression(dp+STL)
- CodeForces 567C. Geometric Progression(map 数学啊)
- CodeForces 567C. - Geometric Progression(解题报告)
- linux下mysql中文输出乱码
- A. Lineland Mail-水题-Codeforces Round #Pi (Div. 2)
- B. Berland National Library-水题- Codeforces Round #Pi (Div. 2)
- JAVA学习笔记(二)
- 虚拟机中的Ubuntu14.04启动时自动设置分辨率为1920x1080
- CF-567C - Geometric Progression
- Linux-C网络编程之select函数
- 科技湃|推特移动端内测新闻功能,对媒体是不是好事?
- Android 应用开发相关下载资源(2015/05/06更新)
- C语言的表达式与语句
- SAP各模块介绍
- Node.js开发入门—HTTP文件服务器
- 软件开发方法
- python基础学习笔记