QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#506360 | #6422. Evil Coordinate | Foedere0 | AC ✓ | 23ms | 3724kb | C++23 | 3.7kb | 2024-08-05 16:55:01 | 2024-08-05 16:55:01 |
Judging History
answer
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<vector>
#include<stack>
#define int long long
//#define endl '\n'
using namespace std;
typedef pair<int,int> PII;
const int N=200200;
int n;
int x,y;
string s;
int o=0,p=0;
map<char,int> m;
void solve(){
m.clear();
o=0,p=0;
cin>>x>>y>>s;
string ans;
ans.clear();
if(x==0&&y==0){
cout<<"Impossible"<<endl;
return;
}
for(int i=0;i<s.size();i++){
if(s[i]=='L') o--;
if(s[i]=='R') o++;
if(s[i]=='U') p++;
if(s[i]=='D') p--;
m[s[i]]++;
}
if(o==x&&p==y){
cout<<"Impossible"<<endl;
return;
}
if(x!=0&&y!=0){
if(x!=o){
while(m['L']){
ans+='L';
m['L']--;
}
while(m['R']){
ans+='R';
m['R']--;
}
while(m['U']) ans+='U',m['U']--;
while(m['D']) ans+='D',m['D']--;
}
else{
while(m['D']){
ans+='D';
m['D']--;
}
while(m['U']){
ans+='U';
m['U']--;
}
while(m['L']) ans+='L',m['L']--;
while(m['R']) ans+='R',m['R']--;
}
cout<<ans<<endl;
return;
}
if(x==0){
int yy=0;
bool st=0;
if(o!=0){
while(m['L']){
ans+='L';
m['L']--;
}
while(m['R']) ans+='R', m['R']--;
while(m['U']) ans+='U', m['U']--;
while(m['D']) ans+='D', m['D']--;
}
else{
if(m['L']) ans+='L',m['L']--;
else if(m['R']) ans+='R',m['R']--;
else if(!m['L']&&!m['R']) st=1;
if(y>=0){
yy=0;
while(m['D']){
ans+='D';
m['D']--;
yy--;
}
while(m['U']){
ans+='U';
yy++;
if(yy==y&&st==1){
cout<<"Impossible"<<endl;
return;
}
m['U']--;
}
}
else{
yy=0;
while(m['U']){
ans+='U';
m['U']--;
yy++;
}
while(m['D']){
ans+='D';
yy--;
if(yy==y&&st==1){
cout<<"Impossible"<<endl;
return;
}
m['D']--;
}
}
while(m['L']) ans+='L', m['L']--;
while(m['R']) ans+='R', m['R']--;
while(m['U']) ans+='U', m['U']--;
while(m['D']) ans+='D', m['D']--;
}
}
else if(y==0){
int xx=0;
bool st=0;
if(p!=0){
xx=0;
while(m['U']){
ans+='U';
m['U']--;
}
//cout<<"nmsl"<<endl;
while(m['D']) ans+='D', m['D']--;
while(m['L']) ans+='L', m['L']--;
while(m['R']) ans+='R', m['R']--;
}
else{
xx=0;
if(m['U']) ans+='U',m['U']--;
else if(m['D']) ans+='D',m['D']--;
else if(!m['U']&&!m['D']) st=1;
//cout<<ans<<endl;
if(x>=0){
while(m['L']){
ans+='L';
xx--;
m['L']--;
}
while(m['R']){
ans+='R';
xx++;
if(xx==x&&st){
cout<<"Impossible"<<endl;
return;
}
m['R']--;
}
}
else{
while(m['R']){
ans+='R';
xx++;
m['R']--;
}
while(m['L']){
ans+='L';
xx--;
if(xx==x&&st){
cout<<"Impossible"<<endl;
return;
}
m['L']--;
}
}
while(m['U']) ans+='U', m['U']--;
while(m['D']) ans+='D', m['D']--;
}
}
cout<<ans<<endl;
}
signed main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int T=1;
cin>>T;
while(T--){
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3624kb
input:
5 1 1 RURULLD 0 5 UUU 0 3 UUU 0 2 UUU 0 0 UUU
output:
LLRRUUD UUU Impossible Impossible Impossible
result:
ok 5 cases
Test #2:
score: 0
Accepted
time: 8ms
memory: 3676kb
input:
11109 6 0 RUDUDR 2 0 URU 0 0 UDRU 0 0 R -1 1 LDUUDDRUUL -1 5 RRUUUDUUU -8 4 RRDRLDR 2 0 UD 0 0 UUDD 3 -2 LDDLLLRR 3 -2 LDRURLDD 1 0 RRL -1 0 DUDDLLRDU -4 0 LL -1 -1 DLRLDLUDUR 1 4 URDULUR 0 0 DDUUDUDDDD 0 2 UU 1 0 RRULD 0 -2 LDLRLLDRRL 0 1 RLRLLRLUR -3 0 RL 0 0 D 0 0 L 0 0 DDLRRUDRUD 0 0 DULU 2 0 RR...
output:
URRUDD UUR Impossible Impossible Impossible RRUUUUUUD LRRRRDD UD Impossible LLLLRRDD LLRRUDDD Impossible UUDDDDLLR LL Impossible DUUULRR Impossible Impossible Impossible LLLLLRRRDD Impossible RL Impossible Impossible Impossible Impossible Impossible LLLRRRRRUU LLLUD Impossible ULUUDDD DDUURR Impossi...
result:
ok 11109 cases
Test #3:
score: 0
Accepted
time: 23ms
memory: 3724kb
input:
11107 1 0 LLRLRURLR 1 0 LLRR 0 1 R 1 0 LLLRLRRR 1 0 RUL 0 1 UD 1 0 RLRLU 0 1 DDDUUUDU 1 0 RURRLLRLL 1 0 LRLR 1 0 ULR 0 1 R 0 1 DDUUUDR 0 1 UUDDUDDU 0 1 DDUUDU 1 0 RRLRLLRLRL 1 0 RLRRLL 1 0 LUR 1 0 U 1 0 LRRRLLLR 0 1 DRUUDDUDU 0 1 DUUDDUR 1 0 LRLRLR 0 1 UUDDDUDU 0 1 R 0 1 UDUDDU 0 1 DUUDUD 1 0 RRLRRR...
output:
ULLLLRRRR LLRR R LLLLRRRR ULR DU ULLRR DDDDUUUU ULLLLRRRR LLRR ULR R RUUUDDD DDDDUUUU DDDUUU LLLLLRRRRR LLLRRR ULR U LLLLRRRR RUUUUDDDD RUUUDDD LLLRRR DDDDUUUU R DDDUUU DDDUUU LLLLLRRRRR DDDDUUUU DDUU ULLLLRRRR DDUU LLLRRR ULR ULR U ULR LLLRRR LLLLLRRRRR U DDDUUU R LLLRRR RUUUUDDDD RUUUUDDDD LLLRRR ...
result:
ok 11107 cases