cf1461 Round #689 Div2-D【二分查找+前缀和】

本文介绍了一种通过递归地将序列分成两半,并利用前缀和来计算所有可能的子序列之和的方法。该方法使用二分查找确定分割点,并通过递归遍历所有可能的和,最终解决询问是否能找到特定和的问题。

Date:2022.01.12
题意:长度为n的序列,每次可将当前序列分为两半,规则:mid=(a[max]+a[min])/2mid=(a[max]+a[min])/2mid=(a[max]+a[min])/2,所有<=mid的放到左边一类,>mid的放到右边一边,每次永久舍弃一边。m个询问,问能不能找到和为当前值的,能输出Yes,不能No。

思路:求所有可能的和,先处理前缀和。因为每次都最多选<=mid或>mid的一半,因此估算期望大概lognlognlogn层,自然考虑打表。每次二分查找出<=mid的最后一个位置,以此为界分左右两边,把两部分的和都加入set中,再由此递归约logn层即可求出所有和的组合加入set中。除此之外注意递归的弹出条件为a[max]==a[min]a[max]==a[min]a[max]==a[min],无论如何这组和都没法再分,(举个例子:当前3个元素为1、1、1,那么mid=1,二分查找总会找到最后一个1,导致死循环)否则会死循环。
代码如下:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
typedef long long LL;
LL n,m,t;
LL a[N],sum[N];
set<LL>s;
void init(LL l,LL r)
{
    if(a[l]==a[r]) {s.insert(sum[r+1]-sum[l]);return;}
    LL x=(a[l]+a[r])/2;
    LL mid=upper_bound(a,a+n,x)-1-a;
    s.insert(sum[mid+1]-sum[l]);init(l,mid);
    s.insert(sum[r+1]-sum[mid+1]);init(mid+1,r);
}
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>t;
    while(t--)
    {
        s.clear();
        memset(sum,0,sizeof sum);
        cin>>n>>m;
        for(int i=0;i<n;i++) {cin>>a[i];}
        sort(a,a+n);
        for(int i=1;i<=n;i++) {sum[i]=sum[i-1]+a[i-1];}
        init(0,n-1);s.insert(sum[n]);
        //for(auto it:s) cout<<it<<' ';
        //cout<<endl;
        while (m -- )
        {
            LL q;cin>>q;
            if(s.count(q)) cout<<"Yes"<<endl;
            else cout<<"No"<<endl;
        }
    }
    return 0;
}
#8 688.7 + cd /tmp/build/opentelemetry-cpp-contrib-8933841f0a7f8737f61404cf0a64acf6b079c8a5/instrumentation/nginx #8 688.7 + mkdir -p build #8 688.7 + cd build #8 688.7 + cmake -DCMAKE_BUILD_TYPE=Release -G Ninja -DCMAKE_CXX_STANDARD=17 -DCMAKE_INSTALL_PREFIX=/tmp/otel -DBUILD_SHARED_LIBS=ON -DNGINX_VERSION=1.25.5 .. #8 688.9 -- The C compiler identification is GNU 8.5.0 #8 689.0 -- The CXX compiler identification is GNU 8.5.0 #8 689.1 -- Detecting C compiler ABI info #8 689.2 -- Detecting C compiler ABI info - done #8 689.3 -- Check for working C compiler: /usr/bin/cc - skipped #8 689.3 -- Detecting C compile features #8 689.3 -- Detecting C compile features - done #8 689.3 -- Detecting CXX compiler ABI info #8 689.5 -- Detecting CXX compiler ABI info - done #8 689.5 -- Check for working CXX compiler: /usr/bin/c++ - skipped #8 689.5 -- Detecting CXX compile features #8 689.5 -- Detecting CXX compile features - done #8 689.5 -- Performing Test CMAKE_HAVE_LIBC_PTHREAD #8 689.7 -- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed #8 689.7 -- Looking for pthread_create in pthreads #8 689.7 -- Looking for pthread_create in pthreads - not found #8 689.7 -- Looking for pthread_create in pthread #8 689.9 -- Looking for pthread_create in pthread - found #8 689.9 -- Found Threads: TRUE #8 689.9 CMake Error at /usr/share/cmake/Modules/CMakeFindDependencyMacro.cmake:76 (find_package): #8 689.9 Found package configuration file: #8 689.9 #8 689.9 /usr/local/lib/cmake/grpc/gRPCConfig.cmake #8 689.9 #8 689.9 but it set gRPC_FOUND to FALSE so package "gRPC" is considered to be NOT #8 689.9 FOUND. Reason given by package: #8 689.9 #8 689.9 The following imported targets are referenced, but are missing: #8 689.9 protobuf::libprotobuf protobuf::libprotoc #8 689.9 #8 689.9 Call Stack (most recent call first): #8 689.9 /usr/lib64/cmake/opentelemetry-cpp/opentelemetry-cpp-config.cmake:105 (find_dependency) #8 689.9 CMakeLists.txt:5 (find_package) #8 689.9 #8 689.9 #8 689.9 -- Configuring incomplete, errors occurred! #8 ERROR: process "/bin/sh -c /build.sh" did not complete successfully: exit code: 1
最新发布
06-14
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值