QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#636671 | #5414. Stop, Yesterday Please No More | q_zhs | WA | 1ms | 3512kb | C++17 | 4.2kb | 2024-10-13 01:43:53 | 2024-10-13 01:43:54 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while(T--){
int n, m;
long long k;
string s;
cin >> n >> m >> k;
cin >> s;
int l = s.length();
// Compute cumulative row and column movements
vector<long long> dr(l + 1, 0), dc(l +1, 0);
for(int t=1; t<=l; t++){
dr[t] = dr[t-1];
dc[t] = dc[t-1];
if(s[t-1] == 'U') dr[t] -=1;
else if(s[t-1] == 'D') dr[t] +=1;
else if(s[t-1] == 'L') dc[t] -=1;
else if(s[t-1] == 'R') dc[t] +=1;
}
// Determine min and max deltas
long long min_dr = *min_element(dr.begin(), dr.end());
long long max_dr = *max_element(dr.begin(), dr.end());
long long min_dc = *min_element(dc.begin(), dc.end());
long long max_dc = *max_element(dc.begin(), dc.end());
// Compute valid starting positions [U, D] x [L, R}
int U = 1 - (int)min_dr;
int D = n - (int)max_dr;
int L = 1 - (int)min_dc;
int R = m - (int)max_dc;
// Check if no kangaroos can remain
if(U > D || L > R){
if(k ==0){
cout << (long long)n * m << "\n";
}
else{
cout << "0\n";
}
continue;
}
// Total valid kangaroos without considering the hole
long long total_valid = (long long)(D - U +1) * (R - L +1);
long long delta = total_valid - k;
if(delta <0){
cout << "0\n";
continue;
}
// Count frequency of each (dr, dc) pair
// Using a map with a unique key for (dr, dc)
unordered_map<long long, int> shift_freq;
for(int t=1; t<=l; t++){
// Shifted by 2000000 to handle negative values
long long key = (dr[t] + 2000000) * 4000001 + (dc[t] + 2000000);
shift_freq[key]++;
}
// Initialize 2D difference array
// Using (n+2) x (m+2) to handle edge cases
// grid[i][j] will store the number of times (i,j) is covered by shifted rectangles
vector<vector<long long>> grid(n+2, vector<long long>(m+2, 0));
// Apply each unique (dr, dc) shift with its frequency to the grid
for(auto it : shift_freq){
long long key = it.first;
int freq = it.second;
// Decode (dr, dc) from key
int dr_val = (key / 4000001) - 2000000;
int dc_val = (key % 4000001) - 2000000;
// Determine the shifted rectangle [x1, x2] x [y1, y2}
int x1 = U + dr_val;
int x2 = D + dr_val;
int y1 = L + dc_val;
int y2 = R + dc_val;
// Clamp the rectangle to the grid boundaries
x1 = max(x1, 1);
y1 = max(y1, 1);
x2 = min(x2, n);
y2 = min(y2, m);
if(x1 > x2 || y1 > y2){
continue; // No valid positions for this shift
}
// Apply the frequency to the difference array
grid[x1][y1] += freq;
grid[x1][y2 +1] -= freq;
grid[x2 +1][y1] -= freq;
grid[x2 +1][y2 +1] += freq;
}
// Compute prefix sums to get the coverage count for each cell
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
grid[i][j] += grid[i][j-1];
}
}
for(int j=1; j<=m; j++) {
for(int i=1; i<=n; i++) {
grid[i][j] += grid[i-1][j];
}
}
// Count the number of hole positions with exactly delta coverage
long long ans =0;
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
if(grid[i][j] == delta){
ans++;
}
}
}
cout << ans << "\n";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3512kb
input:
3 4 5 3 ULDDRR 4 5 0 UUUUUUU 4 5 10 UUUUUUU
output:
4 20 0
result:
wrong answer 1st numbers differ - expected: '2', found: '4'