当前位置: 首页 > news >正文

CSP-S 2025 提高级模拟赛 Day6 复盘 A.选择方案

题面

给出 \(n\) 个数 \(a_i\),求出 \(a_i+a_j\geq s\)\(i,j\) 总数。

赛时想法

从前往后考虑所有在 \(i\) 之前的,大于 \(s-i\) 的数,\(i\) 可以与这些数配对。自然而然就想到用pbds里的平衡树维护。
预估复杂度 \(\mathcal{O}(n \log n)\)\(n\leq2\times10^5\) 完全没有问题。
7min就敲完了。

#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
int n,s,x,ans,v;
using T=tree<pair<int,int>,null_type,less<pair<int,int>>,rb_tree_tag,tree_order_statistics_node_update>;
T tr;
int main(){freopen("count.in","r",stdin);freopen("count.out","w",stdout);ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin >> n >> s;while(n--){cin >> x;T t1;tr.split({s-x,998244353},t1);ans+=t1.size();tr.join(t1);tr.insert({x,++v});}cout << ans;
}

结果:炸了,30pts。

赛后回顾

仔细研究了一下自己原来的代码,想起来split好像不是 \(\mathcal{O}(\log n)\) 的,改成了order_of_key。改后测得70pts。

#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
int n,s,x,ans,v;
using T=tree<pair<int,int>,null_type,less<pair<int,int>>,rb_tree_tag,tree_order_statistics_node_update>;
T tr;
int main(){freopen("count.in","r",stdin);freopen("count.out","w",stdout);ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin >> n >> s;while(n--){cin >> x;ans+=tr.size()-tr.order_of_key({s-x,998244353});tr.insert({x,++v});}cout << ans;
}

检查了一下,发现方案数可能达到 \(n^2\) 级别,需要开long long。测得100pts。

#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
#define int long long
int n,s,x,ans,v;
using T=tree<pair<int,int>,null_type,less<pair<int,int>>,rb_tree_tag,tree_order_statistics_node_update>;
T tr;
int32_t main(){freopen("count.in","r",stdin);freopen("count.out","w",stdout);ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin >> n >> s;while(n--){cin >> x;ans+=tr.size()-tr.order_of_key({s-x,998244353});tr.insert({x,++v});}cout << ans;
}

反思

  1. tree的split操作复杂度不对,容易变 \(\mathcal{O}(n^2)\)
  2. 再也不删#define int long long了,删了不知道下次哪道题就炸了。
http://www.jsqmd.com/news/13088/

相关文章:

  • “不要通过共享内存来通信”——深入理解Golang并发模型与CSP理论
  • SSL证书批量申请终极指南:一次搞定所有域名
  • 详细介绍:百度C++实习生面试题深度解析(下篇)
  • PDF转图片工具:基于PyQt5的完整实现与深度解析 - 详解
  • MongoDB安装及使用
  • 从Gemini Robotics看通用机器人的科技路径
  • 张量的基本操作
  • Windows7 隐藏用户
  • 10 月记录
  • 统计学习方法学习Day01
  • gpt-5-codex vs gpt-5
  • Jenkins Share Library开发入门(一)
  • 第十三篇
  • 成员内部类
  • 用 Fortran 进行英文数字验证码识别
  • 2025.10.13总结 - A
  • 洛谷版自我介绍
  • Windows五次shift漏洞复现
  • P8186 [USACO22FEB] Redistributing Gifts S 题解 - 符星珞
  • 继续学习,争取早日找到实习 - Irving11
  • 悟空原创:零门槛编程?实现了!拖拉流程,支持窗口界面设计支持生成独立可执行程序
  • Keil MDK 将不同文件中的特定数据链接到同一位置
  • 详细介绍:用于水管和污水管道巡检机器人的同步定位与建图综述
  • 1013日总结
  • C语言自学--自定义类型:结构体 - 指南
  • 2025公众号排版效率榜:5款AI工具实测对比,从排版到分发一站搞定
  • 完整教程:R语言——离群点检测应用
  • OpenLayers地图交互 -- 章节十六:双击缩放交互详解 - 教程
  • 中国联通重要突破!正式获得开展eSIM手机运营服务商用试验批复
  • 071_尚硅谷_十进制转其它进制