P3341 题解

· · 题解

找的一道数据结构题,但是感觉更像模拟。史山代码写我一上午。

首先对于第一问,直接模拟即可。模拟时需要考虑如何快速找到某个棋子周围四个方向上第一个遇到的同色棋子。这个很简单,对于每行和每列都开一个 set 即可。找棋子直接使用 lower_bound 或者 upper_bound。

主要在于第二问。你发现每种颜色的棋子就只有两个,所以说不存在同种颜色有多组可删的点对,不知道删哪个才能使最终决策更优的问题。对于单种颜色的一对显然能删就删是最优的。

那么可以弄一个可删点对的队列,初始时把能删的颜色放进去。每删掉一个颜色的点对,考虑去扩展可以删的其他颜色。不难发现可以删的其他颜色只有可能是被删的这个点对所在的行和列离这个点最近的那一些点,最多有 8 个。暴力去判断这些点是否可删接着继续加入队列即可。最后直到不能扩展入队为止,统计答案即可。输出方案是 naive 的。

最后讲一下怎么判断一种颜色的点对可删的细节。

具体细节还是很多,但是代码思路还是很清晰的。

#include<bits/stdc++.h>
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
const int N=1e5+5;
int R,C,n,Q,ans,vis[N];
set<int>sx[N],sy[N];
map<pii,int>mp;
pair<pii,pii> v[N];
pii fd(char op,int x,int y){
    if(op=='L'){
        auto it=sx[x].lower_bound(y);
        if(it==sx[x].begin())return {-1,-1};
        it--;return {x,*it};
    }
    else if(op=='R'){
        auto it=sx[x].upper_bound(y);
        if(it==sx[x].end())return {-1,-1};
        return {x,*it};
    }
    else if(op=='U'){
        auto it=sy[y].lower_bound(x);
        if(it==sy[y].begin())return {-1,-1};
        it--;return {*it,y};
    }
    else{
        auto it=sy[y].upper_bound(x);
        if(it==sy[y].end())return {-1,-1};
        return {*it,y};
    }
}
struct node{int x,y;char opx,opy;};
vector<node>res;
bool chk(pii x,pii y){
    if(x.fi==y.fi){
        if(x.se>y.se)swap(x,y);
        if(x.se+1==y.se)return 0;
        auto it=sx[x.fi].find(y.se);
        it--;
        if(*it==x.se){
            res.push_back({x.fi,x.se+1,'L','R'});
            return 1;
        }
        return 0;
    }
    if(x.se==y.se){
        if(x.fi>y.fi)swap(x,y);
        if(x.fi+1==y.fi)return 0;
        auto it=sy[x.se].find(y.fi);
        it--;
        if(*it==x.fi){
            res.push_back({x.fi+1,x.se,'U','D'});
            return 1;
        }
        return 0;
    }
    if(x.fi>y.fi)swap(x,y);
    if(x.se<y.se){
        if(sx[x.fi].find(y.se)==sx[x.fi].end()){
            auto it1=sx[x.fi].lower_bound(y.se);it1--;
            auto it2=sy[y.se].upper_bound(x.fi);
            if(*it1==x.se&&*it2==y.fi){
                res.push_back({x.fi,y.se,'L','D'});
                return 1;
            }
        }
        if(sx[y.fi].find(x.se)==sx[y.fi].end()){
            auto it1=sx[y.fi].upper_bound(x.se);
            auto it2=sy[x.se].lower_bound(y.fi);it2--;
            if(*it1==y.se&&*it2==x.fi){
                res.push_back({y.fi,x.se,'R','U'});
                return 1;
            }
        }
        return 0;
    }
    else{
        if(sx[x.fi].find(y.se)==sx[x.fi].end()){
            auto it1=sx[x.fi].upper_bound(y.se);
            auto it2=sy[y.se].upper_bound(x.fi);
            if(*it1==x.se&&*it2==y.fi){
                res.push_back({x.fi,y.se,'R','D'});
                return 1;
            }
        }
        if(sx[y.fi].find(x.se)==sx[y.fi].end()){
            auto it1=sx[y.fi].lower_bound(x.se);it1--;
            auto it2=sy[x.se].lower_bound(y.fi);it2--;
            if(*it1==y.se&&*it2==x.fi){
                res.push_back({y.fi,x.se,'L','U'});
                return 1;
            }
        }
        return 0;
    }
}
int main(){
    cin>>R>>C>>n;
    for(int i=1,a,b,c,d;i<=n;i++){
        cin>>a>>b>>c>>d;
        mp[{a,b}]=mp[{c,d}]=i;
        v[i]={{a,b},{c,d}};
        sx[a].insert(b),sx[c].insert(d);
        sy[b].insert(a),sy[d].insert(c);
    }
    cin>>Q;
    while(Q--){
        int x,y;
        char op1,op2;
        cin>>x>>y>>op1>>op2;
        if(sx[x].find(y)!=sx[x].end())continue;
        pii res1=fd(op1,x,y),res2=fd(op2,x,y);
        if(res1.fi==-1||res2.fi==-1||mp[res1]!=mp[res2])continue;
        ans++,sx[res1.fi].erase(res1.se),sy[res1.se].erase(res1.fi);
        sx[res2.fi].erase(res2.se),sy[res2.se].erase(res2.fi);
    }
    cout<<ans<<"\n";
    for(auto it:mp)sx[it.fi.fi].insert(it.fi.se),sy[it.fi.se].insert(it.fi.fi);
    queue<int>q;
    for(int i=1;i<=n;i++)if(chk(v[i].fi,v[i].se))q.push(i),vis[i]=1;
    while(!q.empty()){
        int x=q.front();q.pop();
        int a=v[x].fi.fi,b=v[x].fi.se,c=v[x].se.fi,d=v[x].se.se;
        sx[a].erase(b),sy[b].erase(a),sx[c].erase(d),sy[d].erase(c);
        auto it1=sx[a].lower_bound(b);
        if(it1!=sx[a].begin()){
            it1--;int col=mp[{a,*it1}];
            if(!vis[col]&&chk(v[col].fi,v[col].se))q.push(col),vis[col]=1;
        }
        auto it2=sx[a].upper_bound(b);
        if(it2!=sx[a].end()){
            int col=mp[{a,*it2}];
            if(!vis[col]&&chk(v[col].fi,v[col].se))q.push(col),vis[col]=1;
        }
        auto it3=sy[b].lower_bound(a);
        if(it3!=sy[b].begin()){
            it3--;int col=mp[{*it3,b}];
            if(!vis[col]&&chk(v[col].fi,v[col].se))q.push(col),vis[col]=1;
        }
        auto it4=sy[b].upper_bound(a);
        if(it4!=sy[b].end()){
            int col=mp[{*it4,b}];
            if(!vis[col]&&chk(v[col].fi,v[col].se))q.push(col),vis[col]=1;
        }
        auto it5=sx[c].lower_bound(d);
        if(it5!=sx[c].begin()){
            it5--;int col=mp[{c,*it5}];
            if(!vis[col]&&chk(v[col].fi,v[col].se))q.push(col),vis[col]=1;
        }
        auto it6=sx[c].upper_bound(d);
        if(it6!=sx[c].end()){
            int col=mp[{c,*it6}];
            if(!vis[col]&&chk(v[col].fi,v[col].se))q.push(col),vis[col]=1;
        }
        auto it7=sy[d].lower_bound(c);
        if(it7!=sy[d].begin()){
            it7--;int col=mp[{*it7,d}];
            if(!vis[col]&&chk(v[col].fi,v[col].se))q.push(col),vis[col]=1;
        }
        auto it8=sy[d].upper_bound(c);
        if(it8!=sy[d].end()){
            int col=mp[{*it8,d}];
            if(!vis[col]&&chk(v[col].fi,v[col].se))q.push(col),vis[col]=1;
        }
    }
    cout<<res.size()<<"\n";
    for(auto it:res)cout<<it.x<<" "<<it.y<<" "<<it.opx<<" "<<it.opy<<"\n";
    return 0;
}