QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#606638#6422. Evil CoordinateLJY_ljyAC ✓39ms4008kbC++114.3kb2024-10-03 11:08:442024-10-03 11:08:45

Judging History

你现在查看的是最新测评结果

  • [2024-10-03 11:08:45]
  • 评测
  • 测评结果:AC
  • 用时:39ms
  • 内存:4008kb
  • [2024-10-03 11:08:44]
  • 提交

answer

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std;

const int MAXN = 1e5 + 10;
char str[MAXN];
int mx, my;
bool used[MAXN];

int main() {
	int t; scanf("%d", &t);
	for (int u = 1; u <= t; u++) {
		scanf("%d %d", &mx, &my);
		scanf("%s", str + 1);
		bool flag = true;
		int n = strlen(str + 1);
		for (int i = 1; i <= n; i++) used[i] = false;
		for (int i = 1; i <= n; i++) {
			if (str[i] != 'U' && str[i] != 'D') {
				flag = false;
				break;
			}
		}
		if (flag) { //UD
			if (mx != 0) printf("%s\n", str + 1);
			else {
				int End = 0;
				for (int i = 1; i <= n; i++) {
					if (str[i] == 'U') End++;
					else End--;
				}
				if (my > 0) {
					if (End >= my) printf("Impossible\n");
					else {
						for (int i = 1; i <= n; i++) {
							if (str[i] == 'D') printf("D");
						}
						for (int i = 1; i <= n; i++) {
							if (str[i] == 'U') printf("U");
						}
						printf("\n");
					}
				} else if (my < 0) {
					if (End <= my) printf("Impossible\n");
					else {
						for (int i = 1; i <= n; i++) {
							if (str[i] == 'U') printf("U");
						} 
						for (int i = 1; i <= n; i++) {
							if (str[i] == 'D') printf("D");
						}
						printf("\n");
					}
				} else 
					printf("Impossible\n");
			}
		} else {
			flag = true;
			for (int i = 1; i <= n; i++) {
				if (str[i] != 'L' && str[i] != 'R') {
					flag = false;
					break;
				}
			}
			if (flag) { //LR 
				if (my != 0) printf("%s\n", str + 1);
				else {
					int End = 0;
					for (int i = 1; i <= n; i++) {
						if (str[i] == 'L') End--;
						else End++;
					}
					if (mx < 0) {
						if (End <= mx) printf("Impossible\n");
						else {
							for (int i = 1; i <= n; i++) {
								if (str[i] == 'R') printf("R");
							}
							for (int i = 1; i <= n; i++) {
								if (str[i] == 'L') printf("L");
							}
							printf("\n");
						}
					} else if (mx > 0) {
						if (End >= mx) printf("Impossible\n");
						else {
							for (int i = 1; i <= n; i++) {
								if (str[i] == 'L') printf("L");
							}
							for (int i = 1; i <= n; i++) {
								if (str[i] == 'R') printf("R");
							}
							printf("\n");
						}
 					} else 
						printf("Impossible\n");
				}
			} else {
				int End1 = 0, End2 = 0;
				for (int i = 1; i <= n; i++) {
					if (str[i] == 'U') End1++;
					if (str[i] == 'D') End1--;
					if (str[i] == 'L') End2--;
					if (str[i] == 'R') End2++;
				} 
				if (my == End1 && mx == End2) printf("Impossible\n");
				else if (mx == 0 && my == 0) printf("Impossible\n");
				else {
					if (my != End1) {
						if (mx != 0) {
							for (int i = 1; i <= n; i++) {
								if (str[i] == 'U' || str[i] == 'D')
									printf("%c", str[i]);
							}
							for (int i = 1; i <= n; i++) {
								if (str[i] == 'L' || str[i] == 'R') 
									printf("%c", str[i]);
							}
							printf("\n");
						} else {
							for (int i = 1; i <= n; i++) {
								if (str[i] == 'L' || str[i] == 'R')  {
									printf("%c", str[i]);
									used[i] = true;
									break;
								}
							}
							for (int i = 1; i <= n; i++) {
								if (str[i] == 'U' || str[i] == 'D')
									printf("%c", str[i]);
							}
							for (int i = 1; i <= n; i++) {
								if (str[i] == 'L' || str[i] == 'R') 
									if (!used[i])
										printf("%c", str[i]);
							}
							printf("\n"); 
						}
					} else {
						if (my != 0) {
							for (int i = 1; i <= n; i++) {
								if (str[i] == 'L' || str[i] == 'R') 
									printf("%c", str[i]);
							}
							for (int i = 1; i <= n; i++) {
								if (str[i] == 'U' || str[i] == 'D')
									printf("%c", str[i]);
							}
							printf("\n");
						} else {
							for (int i = 1; i <= n; i++) {
								if (str[i] == 'U' || str[i] == 'D') {
									printf("%c", str[i]);
									used[i] = true;
									break;
								}
							}
							for (int i = 1; i <= n; i++) {
								if (str[i] == 'L' || str[i] == 'R') 
									printf("%c", str[i]);
							}
							for (int i = 1; i <= n; i++) {
								if (str[i] == 'U' || str[i] == 'D')
									if (!used[i])
										printf("%c", str[i]);
							}
							printf("\n");
						}
					}
				} 
			}
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3764kb

input:

5
1 1
RURULLD
0 5
UUU
0 3
UUU
0 2
UUU
0 0
UUU

output:

RRLLUUD
UUU
Impossible
Impossible
Impossible

result:

ok 5 cases

Test #2:

score: 0
Accepted
time: 23ms
memory: 4008kb

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:

URRDUD
UUR
Impossible
Impossible
Impossible
RRUUUDUUU
DDRRRLR
UD
Impossible
LLLLRRDD
LRRLDUDD
Impossible
DUDDDULLR
LL
Impossible
UDUURLR
Impossible
Impossible
Impossible
LLRLLRRLDD
Impossible
RL
Impossible
Impossible
Impossible
Impossible
Impossible
LRRRLLRRUU
UDLLL
Impossible
ULUDDDU
UUDDRR
Impossi...

result:

ok 11109 cases

Test #3:

score: 0
Accepted
time: 39ms
memory: 3928kb

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:

ULLRLRRLR
LLRR
R
LLLLRRRR
URL
DU
URLRL
DDDDUUUU
URRRLLRLL
LLRR
ULR
R
RDDUUUD
DDDDUUUU
DDDUUU
LLLLLRRRRR
LLLRRR
ULR
U
LLLLRRRR
RDUUDDUDU
RDUUDDU
LLLRRR
DDDDUUUU
R
DDDUUU
DDDUUU
LLLLLRRRRR
DDDDUUUU
DDUU
URLLRRLLR
DDUU
LLLRRR
ULR
ULR
U
ULR
LLLRRR
LLLLLRRRRR
U
DDDUUU
R
LLLRRR
RDUUDUUDD
RUDDUUDUD
LLLRRR
...

result:

ok 11107 cases