codeforce 251 A(二分或者尺取法或者单调队列)
来源:互联网 发布:python rfm模型 编辑:程序博客网 时间:2024/05/21 14:57
二分
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define LL long longconst int maxn = 1e5+100;LL a[maxn];int main(){ LL n, d; while(scanf("%I64d%I64d", &n, &d) != EOF){ for(int i = 0; i < n; i++) scanf("%I64d", &a[i]); LL ans = 0; for(int i = 1; i < n; i++){ LL j = i - (lower_bound(a, a+i, a[i]-d)-a); ans += (j-1)*j/2; } printf("%I64d\n", ans); } return 0;}
尺取法
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define LL long longconst int maxn = 1e5+100;LL a[maxn];//尺取法int main(){ LL n, d; while(scanf("%I64d%I64d", &n, &d) != EOF){ for(int i = 0; i < n; i++) scanf("%I64d", &a[i]); LL ans = 0; LL j = 0; for(int i = 0; i < n; i++){ while(j < n && a[j] - a[i] <= d) j++; // printf("j = %I64d\n", j); ans += (j-i-1)*(j-i-2)/2; } printf("%I64d\n", ans); } return 0;}
单调队列
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define LL long longconst int maxn = 1e5+100;LL a[maxn], que[maxn];int main(){ LL n, d; while(scanf("%I64d%I64d", &n, &d) != EOF){ for(int i = 0; i < n; i++) scanf("%I64d", &a[i]); if(n < 3){ printf("0\n"); continue;} LL ans = 0; LL head = 0, tail = 0; while(head <= tail){ while(a[tail] - a[head] <= d && tail < n) que[tail] = a[tail], tail++; head++; // printf("head = %I64d, tail = %I64d\n", head, tail); if(head < n) ans += (tail-head)*(tail-head-1)/2; } printf("%I64d\n", ans); } return 0;}
1 0
- codeforce 251 A(二分或者尺取法或者单调队列)
- POJ 3061 Subsequence 二分或者尺取法
- POJ 3061 Subsequence 二分或者尺取法
- poj3061(二分前缀和或者尺取法)
- codeforce 439D 二分或者三分
- BZOJ 1012[jsoi2008] 最大数maxnumber 线段树(或者是单调队列+二分)
- Codeforces6E Exposition (尺取法 + 单调队列)
- codeforces 251A Points on Line 二分 or 单调队列
- hdu 4123 Bob’s Race(单调队列或者rmq)
- NYOJ-258/POJ-2559/HDU-1506 Largest Rectangle in a Histogram,最大长方形,dp或者单调队列!
- codeforces 251A(普通队列or单调队列or二分)
- C. An impassioned circulation of affection-尺取法或者DP
- 【二分\单调队列】绿色通道
- 二分查找单调队列
- Moo University - Financial Aid (poj 2010 优先队列 或者 二分)
- 二分 或者 指针
- 【codeforces 676C】 【尺取法】【从一个ab序列中最多改变k个字符找到最长的连续a子串或者b子串长度】
- 从一个ab序列中最多改变k个字符找到最长的连续a子串或者b子串长度.【尺取法】
- 变相的欧拉方程
- selector设置的属性没效果
- hdu2036改革春风吹满地
- MFC程序提示 0xC0000005: 读取位置 0x00000020 时发生访问冲突。
- 编程言之父
- codeforce 251 A(二分或者尺取法或者单调队列)
- Java类和异常处理
- Best engine and tools for a TA and GAMDEVs
- 1st homework of OS
- logstash+grok+json+elasticsearch解析复杂日志数据(二)
- 使用servlet处理http响应
- selenium 3.0 beta2 初体验
- c语言指针的易错点
- 检察官外传 검사외전 (2016)