当前位置: 首页 > 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.gsyq.cn/news/20457.html

相关文章:

  • MongoDB安装及使用
  • 从Gemini Robotics看通用机器人的科技路径
  • Windows7 隐藏用户
  • 10 月记录
  • 网络安全基础--第五课:跨站脚本攻击XSS - 实践
  • 2025.10.13总结 - A
  • 洛谷版自我介绍
  • P8186 [USACO22FEB] Redistributing Gifts S 题解 - 符星珞
  • 深入解析:个人用云计算学习笔记 --17(DNS 服务器)
  • 继续学习,争取早日找到实习 - Irving11
  • 悟空原创:零门槛编程?实现了!拖拉流程,支持窗口界面设计支持生成独立可执行程序
  • 详细介绍:用于水管和污水管道巡检机器人的同步定位与建图综述
  • 1013日总结
  • 2025公众号排版效率榜:5款AI工具实测对比,从排版到分发一站搞定
  • 完整教程:R语言——离群点检测应用
  • 中国联通重要突破!正式获得开展eSIM手机运营服务商用试验批复
  • 071_尚硅谷_十进制转其它进制
  • 联考の记录
  • 实验1_CPP
  • 08 数组
  • CF2153 Codeforces Round 1057 (Div. 2) 游记
  • 面向新质生产力,职业院校“人工智能”课程教学解决方案 - 教程
  • 从《花果山》到《悬鉴》:一首诗的蜕变与AI元人文理论的建构历程
  • java语法(switch)
  • 朱世乐的 Johnson 算法笔记
  • 实用指南:JVM栈溢出时如何dump栈信息?
  • Python的typing模块:类型提示 (Type Hinting) - 详解
  • MyEclipse 2017 激活教程
  • 插入 dp
  • 【C++】AVL详解 - 教程