题解:P8695 [蓝桥杯 2019 国 AC] 轨道炮

· · 题解

1.思路

定义变量

观察到题目标签数据范围是 N \le 1000 ,范围较小,考虑用暴力枚举做法。

定义结构体 node ,并存储 4 个变量, x,y,vx,vy 。 x,y 表示横纵坐标, vx,vy 表示在横纵方向的运动速度。

枚举时间

我们设桶 tx,ty 为某一时刻某一 x 或 y 坐标上敌人的数量,记录每个时刻的 tx,ty ,取最大值。

注意到坐标范围 -10^6 \le X_i,Y_i \le 10^6 ,用数组会出现一些奇奇怪怪的错误,比如 RE,MLE 之类,所以我们用 map 来存储。

时间枚举到 1145 足矣。(好臭的数字)

2.AC代码

仅供参考,不要抄袭,抄袭不会使你得到提升。

码风较丑见谅。

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
struct node{
    int x,y,vx,vy;
}a[1001];
signed main(){  
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i].x>>a[i].y;
        int v;
        cin>>v;
        char op;
        cin>>op;
        if(op=='U') a[i].vy=v;
        if(op=='D') a[i].vy=-v;
        if(op=='L') a[i].vx=-v;
        if(op=='R') a[i].vx=v;
    }
    int maxn=-1;
    for(int i=0;i<=1145;i++){
        map<int,int> tx,ty;
        for(int j=1;j<=n;j++){
            tx[a[j].x+a[j].vx*i]++;
            ty[a[j].y+a[j].vy*i]++;
        }
        for(auto j:tx) maxn=max(maxn,j.second);
        for(auto j:ty) maxn=max(maxn,j.second); 
    }
    cout<<maxn;
    return 0;
}