مؤلف المشكلة: | حسن صالح (MathModel) |
أعده: | حسن صالح (MathModel) |
حرره: | حسن صالح (MathModel) |
يمكننا الحفاظ على خريطة من الأزواج إلى الأعداد الصحيحة، أي \mathtt{map<pair<int,int>,int>}، حتى نتمكن من تتبع عدد الزيارات لكل زوج من الإحداثيات. بعد ذلك، نمر عبر قيم الخريطة ونتحقق من الحد الأقصى لعدد الزيارات (دعنا نقول أن هذا هو ). ثم نمر عبر مفاتيح الخريطة ونعد كم عدد mp[\{x,y\}]=m، وسيكون هذا هو الجواب النهائي.
التعقيد O(n \log n).
#include <bits/stdc++.h>
using namespace std;
int solve(){
int n,x,y;cin>>n>>x>>y;
map<pair<int,int>,int>mp;
string s;cin>>s;
mp[{x,y}]++;
for(auto i:s){
if(i=='R'){
x++;
}
if(i=='L'){
x--;
}
if(i=='U'){
y++;
}
if(i=='D'){
y--;
}
mp[{x,y}]++;
}
int mx=0;int ans=0;
for(auto i:mp)mx=max(mx,i.second);
for(auto i:mp){
if(i.second==mx)ans++;
}
cout<<ans<<"\n";
return 0;
}
int main(){
int t;cin>>t;
while(t--){
solve();
}
return 0;
}