[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,考场上也要多思考!!!

相关推荐
goodluckyaa2 小时前
LCR 006. 两数之和 II - 输入有序数组
算法
孤狼warrior2 小时前
YOLO目标检测 一千字解析yolo最初的摸样 模型下载,数据集构建及模型训练代码
人工智能·python·深度学习·算法·yolo·目标检测·目标跟踪
Σίσυφος19003 小时前
PCL法向量估计 之 RANSAC 平面估计法向量
算法·机器学习·平面
xhbaitxl3 小时前
算法学习day39-动态规划
学习·算法·动态规划
I_LPL3 小时前
day23 代码随想录算法训练营 回溯专题2
算法·hot100·回溯算法·求职面试
智者知已应修善业3 小时前
【洛谷P9975奶牛被病毒传染最少数量推导,导出多样例】2025-2-26
c语言·c++·经验分享·笔记·算法·推荐算法
Trouvaille ~3 小时前
【Linux】应用层协议设计实战(一):自定义协议与网络计算器
linux·运维·服务器·网络·c++·http·应用层协议
CSCN新手听安3 小时前
【linux】高级IO,I/O多路转接之poll,接口和原理讲解,poll版本的TCP服务器
linux·运维·服务器·c++·计算机网络·高级io·poll
CSCN新手听安3 小时前
【linux】网络基础(三)TCP服务端网络版本计算器的优化,Json的使用,服务器守护进程化daemon,重谈OSI七层模型
linux·服务器·网络·c++·tcp/ip·json
m0_736919103 小时前
C++中的委托构造函数
开发语言·c++·算法