题解:P11232 [CSP-S 2024] 超速检测
niuqichongtian · · 题解
Blog推荐(~这个入不要脸~)
题目简述
一条长度为
- 一共有多少辆车被判定为超速;
- 在不漏掉已经被判定为超速的车的情况下,尽可能多的关闭超速检测装置,并输出最多可以关闭多少个装置。
所有车都不会发生意外事故,且
已知公式
- 初始速度
v_0 - 匀加速度
a_0 - 目标速度
v_1 - 达到目标速度的地点
\frac{v_1^2-v_0^2}{2a}
~不要问我怎么推导的,我也不会~
思路简述
总体思路
对于每一辆车,我们可以以某种神秘方式(
- 存在有实际意义的
x 以及y 的车辆一定会在不节省能源的情况下被判定为超速; - 每辆车都会在
[x,\ y] 中取一个或多个装置作为最终剩下来的装置,而没有被选上的就会计入答案。
细想
那么怎么决策应该选哪个呢?
首先,我们发现在
已知了算法,但是怎么把区间进行一个排序呢?通过上面的思路,我们每一次的选择一定是最靠右的测速区间,按照 Ta 排序好了。正确性证明?来!~来财!~
- 一共两种排序方法:
- 按照
x 升序 - 按照
y 升序
- 按照
- 给出示例:
- -------|-------+----------+-|--------
- 按照
x 升序:| + - 按照
y 升序:+ |
- 有何区别?先遍历到 | 的话,我们就会让下一个检测装置跳到 |
_y 处。此时 + 的区间内没有任何测速装置。会\color{red}{WA} with 0pts。- 这里大家可能会说:但是这里的计算最终没问题啊!
- 但是其实这里的计算逻辑是有问题的
- 当然可能亦可以凑巧过
- 但是建议大家弄清楚逻辑,毕竟做题不是刷 AC 率,是提升自我的过程
- 若先遍历到 +,则我们就先把 + 所代表的区间的最右端的测试装置打开,此时两个区间使用一个测速装置即可。
x 和 y 的求解
--- 那设的区间最左端的测速仪编号为
方法 1
使用二分求解。容易出边界问题,所以不太支持使用。
方法 2
建一个长度 MAXL 的数组,存储每一个值右边最近的测速仪。同理左边最近的。可用
上代码!
细节提示
- 每次一组数据运行完之后一定要记得 memset!
- 记得排序时写一个 cmp 函数来自定义排序方式,并且使用
x.second < y.second作为返回值 - 本题数据量
20\times4\times10^5=8\times10^6 ,所以推荐大家使用快读快写以防卡常 - 边界问题,也即循环的起始、结束值的定义
- 一定一定不要用
i <= 测速仪区间端点数组.size()-1,因为.size()函数的返回值类型是 unsigned int,所以0-1=4294967295=2^{64}-1
Code
马蜂差+无用模板多,请见谅 大家可以找找别的 dalao 的代码
#include <bits/stdc++.h> #define I using #define AK namespace #define IOI std #undef int #define int long long #undef INT_MIN #define INT_MIN (LONG_LONG_MIN + 1145) #undef INT_MAX #define INT_MAX (LONG_LONG_MAX - 1145) #define inf (int)(1e18) #define loop(i, j, k, s) for (register int i = j; i <= k; i += s) #define relp(i, j, k, s) for (register int i = j; i >= k; i -= s) #define P1 "¡À¨º??¦Ì?1" #define P2 "¡À¨º??¦Ì?2" #define P3 "¡À¨º??¦Ì?3" #define P4 "¡À¨º??¦Ì?4" #define P5 "¡À¨º??¦Ì?5" #define P6 "¡À¨º??¦Ì?6" #define P7 "¡À¨º??¦Ì?7" #define P8 "¡À¨º??¦Ì?8" #define P9 "¡À¨º??¦Ì?9" #define P0 "¡À¨º??¦Ì?0" #define PI acos(-1.0) #define pii pair<int, int> #define l first #define r second #define pb push_back I AK IOI;
namespace CSP { const char endline = '\n'; const char space = ' '; template<typename type> inline void readint ( type &x ) { x = 0; char c = getchar(); int pn = 1; while ( !isdigit ( c ) ) { if ( c == '-' ) { pn = -1; } c = getchar(); } while ( isdigit ( c ) ) { x = x 10 + c - 48; c = getchar(); } x = pn; return; } template<typename type> inline void writeint ( type x ) { if ( x < 0 ) { putchar ( '-' ), x = -x; } if ( x >= 10 ) { writeint ( x / 10 ); } putchar ( x % 10 + 48 ); return; } struct inputstream { template<typename type> inputstream operator>> ( type &x ) { if ( typeid ( type ).name() == typeid ( char ).name() ) { x = getchar(); while ( x == ' ' || x == '\n' ) { x = getchar(); } return this; } readint ( x ); return this; } } in; struct outputstream { template<typename type> outputstream operator<< ( type x ) { if ( typeid ( type ).name() == typeid ( char ).name() ) { putchar ( x ); } else { if (x == INT_MIN) { putchar('-'); putchar('i'); putchar('n'); putchar('f'); } else if (x == INT_MAX) { putchar('i'); putchar('n'); putchar('f'); } else writeint ( x ); } return *this; } } out; } I AK CSP;
int const maxn = 2e5+7, maxl = 2e6+7; inline bool cmp(pii x, pii y) { return x.r < y.r; } class Solution { private: int N, M, L, V; // N cars, M cameras, with length L road, and V speed limit int d[maxn], v[maxn], a[maxn], l[maxl], r[maxl]; vector<pii> sp; inline void init(void) { in >> N >> M >> L >> V; loop (i, 1, N, 1) { in >> d[i] >> v[i] >> a[i]; } loop (i, 1, M, 1) { int u; in >> u; l[u] = r[u] = u; } loop (i, 1, L, 1) { if (l[i]) continue; l[i] = l[i - 1]; } relp (i, L, 0, 1) { if (r[i]) continue; r[i] = r[i + 1]; } loop (i, 1, N, 1) { if (!r[d[i]]) continue; if (a[i] < 0) { if (v[i] > V) { int tmp = d[i] + (VV - v[i]v[i]) / (2 a[i]); if ( (VV-v[i]v[i])%(2a[i]) == 0 ) tmp--; tmp = min(tmp, L); if (r[d[i]] > l[tmp]) continue; sp.pb({r[d[i]], l[tmp]}); } } else if (a[i] > 0) { if (v[i] <= V) { int tmp = d[i] + (VV-v[i]v[i])/(2a[i])+1; // in the upper line, we gave tmp a // plus one in the end of the line, and why is that? // because we want tmp to be // d[i] + ceil( (v[i]v[i]-VV)/(2a[i])+1e-6 ) // and it is equal to tmp if (tmp>L || !r[tmp]) continue; sp.pb({r[tmp], l[L]}); } else { sp.pb({r[d[i]], l[L]}); } } else if (a[i] == 0) { if (v[i] > V) { sp.pb({r[d[i]], l[L]}); } } } sort(sp.begin(), sp.end(), cmp); return; } public: inline void run(void) { sp.clear(); memset(d, 0, sizeof d); memset(v, 0, sizeof v); memset(a, 0, sizeof a); memset(l, 0, sizeof l); memset(r, 0, sizeof r); init(); int p = -1, ret = 0; out << sp.size() << ' '; for (int i = 0; i < sp.size(); i++) { if (i && sp[i].l <= sp[i-1].l || p >= sp[i].l) continue; p = sp[i].r; ret++; } out << M-ret << endline; return; } } P11232;
signed main(signed argc, char const *argv[]) { int T; in >> T; while (T--) P11232.run(); return 0; }