[CSP-S 2024] 超速检测

这题写了一份WA40的代码,然后崩溃了/ll

一下是40分代码。

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
struct node{
    int d, v, a;
}inp[N];
int T, n, m, L, V, p[N];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> T;
    while (T--){
        cin >> n >> m >> L >> V;
        for (int i = 1; i <= n; i++){
            cin >> inp[i].d >> inp[i].v >> inp[i].a;
        }
        for (int i = 1; i <= m; i++){
        	cin >> p[i];
		}
		set<int> st;
		int ans = 0;
		for (int i = 1; i <= n; i++){
			if (inp[i].a > 0 || inp[i].a == 0){
				if (inp[i].d > p[m]){
					continue;
				}
				if (inp[i].a == 0){
					if (inp[i].v > V){
						ans++;
						st.insert(p[m]);
					}
					continue;
				}
				int x = p[m] - inp[i].d;
				double tmp = sqrt(2.0 * inp[i].a * x + 1.0 * inp[i].v * inp[i].v);
				if (tmp > V * 1.0){
					ans++;
					st.insert(p[m]);
				}
				continue;
			}
            auto it = lower_bound(p + 1, p + m + 1, inp[i].d);
			int pos = *it;
			int x = pos - inp[i].d;
			double tmp2 = 2.0 * inp[i].a * x + 1.0 * inp[i].v * inp[i].v;
			if (tmp2 <= 0){
				continue;
			}
			else{
				if (sqrt(tmp2) > V){
					ans++;
					st.insert(pos);
				}
			}
		}
		cout << ans << " " << m - st.size() << '\n';
    }
}

发现错误的朋友可以在评论区里指出来。

问题就在于,如果我有一辆车减速,而在经过多个测速点时都超速,从极限的思想来说,我们只保留第一个即可。貌似是对的,其实是错的。

如果是这样,那么如果存在另一辆车,也经过了这些测速点,但他是一辆加速的车,那么我们会保留最后一个。

实际上,我们只需要保留一个,但保留了两个,故这样是错的。

所以我们就要对于每一辆车有一个检测区间,问题转化为区间覆盖问题。

OK,上正解!

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
const int N = 100005;
int n,m,L,V;
struct node{
    int x, y;
    bool operator <(const node &q) const{
        return (y == q.y ? x < q.x : y < q.y);
    }
};
vector<node> ve;
struct cs{
    int d, v, a;
}cr[N];
int dct[N];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--){
        cin >> n >> m >> L >> V;
        for (int i = 1; i <= n; i++) 
            cin >> cr[i].d >> cr[i].v >> cr[i].a;
        for (int i = 1; i <= m; i++) 
            cin >> dct[i];
        for (int i = 1; i <= n; i++){
            int st = -1, ed = -1;
            if (cr[i].a == 0){
                if (cr[i].v > V){
                    st = lower_bound(dct + 1, dct + 1 + m, cr[i].d) - dct;
                    ed = m;
                }
            }
            else if (cr[i].a > 0){
                if (cr[i].v > V){
                    st = lower_bound(dct + 1, dct + 1 + m, cr[i].d) - dct;
                    ed = m;
                }
                else if (cr[i].v == V){
                    st = upper_bound(dct + 1, dct + 1 + m, cr[i].d) - dct;
                    ed = m;
                }
                else{
                    int fz= V * V - cr[i].v * cr[i].v;
                    int fm = 2 * cr[i].a;
                    st = upper_bound(dct + 1, dct + 1 + m, cr[i].d + fz / fm) - dct;
                    ed = m;
                }
            }
            else{
                if (cr[i].v > V){
                    int fz = cr[i].v * cr[i].v - V * V;
                    int fm = -2 * cr[i].a;
                    st = lower_bound(dct + 1, dct + 1 + m, cr[i].d) - dct;
                    if (fz % fm == 0){
                        ed = lower_bound(dct + 1, dct + 1 + m, cr[i].d + fz / fm) - dct - 1;
                    }
                    else{
                        ed = lower_bound(dct + 1, dct + 1 + m, cr[i].d + fz / fm + 1) - dct - 1;
                    }
                }
            }
            if (st != -1 && st <= ed) 
                ve.push_back((node){st,ed});
        }
        sort(ve.begin(), ve.end());
        int nr = -0x3f3f3f3f, nl = 0, ans=0;
        for (int i = 0; i < ve.size(); i++){
            nl = max(nl, ve[i].x);
            if (nl > nr) 
                ans++, nr = ve[i].y, nl = ve[i].x;
        }
        cout << ve.size() << " " << m - ans << "\n";
        ve.clear();
    }
    return 0;
}

OK,考场上也要多思考!!!

相关推荐
kisshyshy1 天前
🍦 雪糕、食堂、火车厢:三幅漫画吃透栈、队列与链表
javascript·算法
众少成多积小致巨1 天前
JNI (Java Native Interface) 技术手册中文参考指南
android·java·c++
猿人谷1 天前
不只是 CPU 阈值:STAR 如何用 GAT + Transformer 做容器级自动扩缩容?
人工智能·算法
复杂网络1 天前
Stable Diffusion 视觉大模型微调技术深度调研
算法
复杂网络1 天前
基于 Stable Diffusion 架构的视觉大模型代表性工作与原理深度解析
算法
MrZhao4001 天前
Agent Loop 如何用 Hook 扩展:权限、日志与工具拦截
算法
MrZhao4001 天前
Agent 为什么需要 Skills:别把所有知识都塞进 system prompt
算法
JieE2123 天前
LeetCode 101. 对称二叉树|JS 递归 + 迭代双解法,彻底搞懂镜像判断
javascript·算法
JieE2124 天前
LeetCode 56. 合并区间|超清晰 JS 图解思路,面试高频区间题
javascript·算法·面试
Jack204 天前
HarmonyOS开发中错误处理策略:网络异常统一处理
算法