Codeforces Round #689 (Div. 2, based on Zed Code Competition)D. Divide and Summarize(分治+dfs)

编程入门 行业动态 更新时间:2024-10-09 10:27:48

Codeforces Round #689 (Div. 2, <a href=https://www.elefans.com/category/jswz/34/1761398.html style=based on Zed Code Competition)D. Divide and Summarize(分治+dfs)"/>

Codeforces Round #689 (Div. 2, based on Zed Code Competition)D. Divide and Summarize(分治+dfs)

D. Divide and Summarize

题意

给你n个数,q次询问,问你能否具有满足和为s的序列。

思路

再求其有多少种和时需要使用 m i d = m a x + m i n > > 1 mid = max + min >> 1 mid=max+min>>1来寻找有多少种和。

然后dfs,但是需要判断一下左面或者右面全部相等情况,否则会爆栈

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 100;
typedef long long LL;
//#define int long longint a[N];int n, q;
set<LL>ans;
LL sum[N];
void dfs(int l ,int r) {if (l > r) return;ans.insert(sum[r] - sum[l - 1]);if (a[l] == a[r]) {return;}int mid = a[l] + a[r] >> 1;int idx = upper_bound(a + l, a + 1 + r, mid) - a;dfs(l, idx - 1);dfs(idx, r);
}void solve() {ans.clear();cin >> n >> q;for (int i  = 1; i <= n; ++i)cin >> a[i];sort(a + 1, a + 1 + n);for (int i = 1; i <= n; ++i) sum[i] = sum[i - 1] + a[i];dfs(1 ,n);while (q--) {LL s; cin >> s;if (ans.count(s)) cout << "YES\n";else cout << "NO\n";}
}
signed main() {ios::sync_with_stdio(0);cin.tie(0);int T = 1;cin >> T;while (T--) {solve();}}

更多推荐

Codeforces Round #689 (Div. 2, based on Zed Code Competition)D. Divide and Summa

本文发布于:2024-03-23 22:49:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1743761.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:based   Zed   Div   Codeforces   Summarize

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!