当前位置: 首页 > 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.hskmm.com/?act=detail&tid=30352

相关文章:

  • SSL证书批量申请终极指南:一次搞定所有域名
  • npm install creat-vue命令使用报错解决方法
  • PDF转图片工具:基于PyQt5的完整实现与深度解析 - 详解
  • MongoDB安装及使用
  • 从Gemini Robotics看通用机器人的科技路径
  • 张量的基本操作
  • Windows7 隐藏用户
  • 10 月记录
  • 统计学习方法学习Day01
  • gpt-5-codex vs gpt-5
  • Jenkins Share Library开发入门(一)
  • 第十三篇
  • 虚树
  • 网络安全基础--第五课:跨站脚本攻击XSS - 实践
  • 成员内部类
  • 用 Fortran 进行英文数字验证码识别
  • webpack优化前端性能
  • 2025.10.13总结 - A
  • 洛谷版自我介绍
  • Windows五次shift漏洞复现
  • P8186 [USACO22FEB] Redistributing Gifts S 题解 - 符星珞
  • Windows续
  • uml九类例图详解
  • 继续学习,争取早日找到实习 - Irving11
  • Keil MDK 将不同文件中的特定数据链接到同一位置
  • 1013日总结
  • 数据流图
  • 2025公众号排版效率榜:5款AI工具实测对比,从排版到分发一站搞定
  • OpenLayers地图交互 -- 章节十六:双击缩放交互详解 - 教程
  • CF1935E Distance Learning Courses in MAC