1 条题解
-
0
1 6 2 4 8 5 6 7 2
只需要找三个数即可
暴力算法
枚举中间的数 m,枚举公差 d,如果前面存在 m-d 后面存在 m+d 则成功
前后是否存在指定的值,注意到数的范围不大,可以设置前后两个桶快速判断。
复杂度 O(n^2)
如何优化?
枚举中间的数 m 无法优化
枚举公差 d 呢?
设三个数为 p, m, s
则 m - p = s - m
s = 2m - p
枚举中间的数 m ,如果前面存在 p 后面存在 2m-p 则成功
把原先的前后两个桶用两个 bitset:pre 和 suf
#include<bits/stdc++.h> using namespace std; const int N=60005,C=20005; bitset<N>pre,suf; int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int T; cin>>T; while(T--) { int n; cin>>n; pre.reset(), suf.reset(); bool flag=0; for(int i=1;i<=n;i++) { int x; cin>>x; if(suf[x])flag=1; suf|=(pre<<(x+x))>>C; pre[C-x]=1; } puts(flag?"Y":"N"); } return 0; }
- 1
信息
- ID
- 614
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- (无)
- 递交数
- 6
- 已通过
- 4
- 上传者