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

相关推荐
Asize38 分钟前
146. LRU 缓存
算法
Asize40 分钟前
543. 二叉树的直径
算法
lemon_sjdk1 小时前
ObjectProperty
java·开发语言·算法
Zentceh1 小时前
AI-ISP在夜视机芯中的应用:从传统ISP到PixelClean全彩夜视的进化
人工智能·科技·算法·计算机视觉·车载系统·视频·智能硬件
「QT(C++)开发工程师」1 小时前
C++ std::move 详解
开发语言·c++
xier_ran1 小时前
【infra之路】AWQ 详解:激活感知权重保护,让 W4A16 量化精度超越 GPTQ
线性代数·算法·机器学习·量化·infra
亦皓ai2 小时前
AI时代后端工程(二):AI把代码写得越来越快,我却越来越不敢让它直接开工了
人工智能·算法·机器学习·搜索引擎·transformer
玖玥拾3 小时前
LeetCode 392 判断子序列
笔记·算法·leetcode
豆沙沙包?4 小时前
C++~~~set容器、map容器(p57-P69)
开发语言·c++
qeen874 小时前
【数据结构】哈希表的C++实现与封装
数据结构·c++·散列表·哈希表