欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 旅游 > 二分+模拟,CF1461D - Divide and Summarize

二分+模拟,CF1461D - Divide and Summarize

2025/4/21 21:52:33 来源:https://blog.csdn.net/EQUINOX1/article/details/139543197  浏览:    关键词:二分+模拟,CF1461D - Divide and Summarize

一、题目

1、题目描述

2、输入输出

2.1输入

2.2输出

3、原题链接

Problem - 1461D - Codeforces


二、解题报告

1、思路分析

我们发现每次分裂操作结果都是固定的

我们从初始序列分裂出两个确定的子序列,两个确定的子序列又分裂出4个确定的子序列

那么也就是说我们最终能够分裂出的子序列的数目是O(n)的

我们预处理出所有的子序列就预处理出了所有可以得到的和(当然这个和要在分裂的过程中维护)

而分裂要求我们得到小于等于mid的部分和大于的部分

所以我们需要对原序列进行排序,模拟的过程通过二分来找到分裂的位置

同时预处理前缀和以便每次分裂前都记录一下当前得到的值

值得注意的是nums[l] = nums[r]的时候说明当前子序列是相同的,我们无法继续向下分裂

2、复杂度

时间复杂度: O(NlogN)空间复杂度:O(N)

3、代码详解

#include <bits/stdc++.h>
using PII = std::pair<int, int>;
using i64 = long long;
std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());const int P = [](int x) {auto isprime = [](int x) {if (x <= 1) return false;for (int i = 2; i <= x / i; i ++ )if (x % i == 0) return false;return true;};while (!isprime(x)) x ++;return x;
}(rnd() % 900000000 + 100000000);void solve() {/*  直接模拟    */int N, Q, s;std::cin >> N >> Q;std::vector<int> nums(N);std::vector<i64> pre(N + 1);for (int i = 0; i < N; i ++ ) std::cin >> nums[i];std::sort(nums.begin(), nums.end());for (int i = 0; i < N; i ++ ) pre[i + 1] += nums[i] + pre[i];std::vector<std::array<int, 2>> segs { { 0, N - 1 } };  segs.reserve(N);std::unordered_set<i64> st;while (segs.size()) {std::vector<std::array<int, 2>> nxt;for (auto& [l, r] : segs) {st.insert(pre[r + 1] - pre[l] + P);if (nums[l] != nums[r]) {int mid = std::upper_bound(nums.begin(), nums.end(), (nums[l] + nums[r]) >> 1) - nums.begin();nxt.insert(nxt.end(), { { l, mid - 1 }, { mid, r } });}}segs = std::move(nxt);}for (int i = 0, s; i < Q; i ++) {std::cin >> s;if (st.count(1LL * s + P))std::cout << "YES\n";elsestd::cout << "NO\n";}
}int main () {std::ios::sync_with_stdio(false);   std::cin.tie(0);  std::cout.tie(0);int _ = 1;std::cin >> _;while (_ --)solve();return 0;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词