이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
#include "seats.h"
using namespace std;
const int maxn = 1e6 + 10;
vector <vector <int>> a;
vector <int> row, col;
int h, w;
int arr[maxn];
bool inside(int x, int y) {
return (0 <= x && x < h) && (0 <= y && y < w);
}
bool isOkay(int R) {
int total = 0;
for(int i = 0; i <= R; i++) {
for(int dx : {-1, 1}) {
for(int dy : {-1, 1}) {
int x = row[i];
int y = col[i];
int cnt = 0;
if(!inside(x + dx, y) || a[x + dx][y] > R) ++cnt;
if(!inside(x, y + dy) || a[x][y + dy] > R) ++cnt;
if(cnt == 2) ++total;
}
}
}
for(int i = R + 1; i < h * w; i++) {
for(int dx : {-1, 1}) {
for(int dy : {-1, 1}) {
int x = row[i];
int y = col[i];
int cnt = 0;
if(inside(x + dx, y) && a[x + dx][y] <= R) ++cnt;
if(inside(x, y + dy) && a[x][y + dy] <= R) ++cnt;
if(cnt == 2) ++total;
}
}
}
return (total == 4);
}
void add(int x, int y, int val) {
for(int i = x; i <= y; i++) arr[i] += val;
}
void process(int x, int y, int val) {
for(int dx : {-1, 1}) {
for(int dy : {-1, 1}) {
int r = h * w;
if(inside(x + dx, y)) r = min(r, a[x + dx][y]);
if(inside(x, y + dy)) r = min(r, a[x][y + dy]);
add(a[x][y], r - 1, val);
r = 0;
if(inside(x + dx, y)) r = max(r, a[x + dx][y]);
else r = h * w;
if(inside(x, y + dy)) r = max(r, a[x][y + dy]);
else r = h * w;
add(r, a[x][y] - 1, val);
}
}
}
void give_initial_chart(int H, int W, std::vector<int> R, std::vector<int> C) {
h = H;
w = W;
a = vector <vector <int>> (H, vector <int> (W, 0));
row = R;
col = C;
for(int i = 0; i < H * W; i++) {
a[row[i]][col[i]] = i;
}
fill(arr, arr + (h * w), 0);
for(int i = 0; i < h; i++) {
for(int j = 0; j < w; j++) {
process(i, j, 1);
}
}
}
const int dx[] = {0, 0, -1, 1};
const int dy[] = {-1, 1, 0, 0};
int swap_seats(int p, int q) {
int ans = 0;
set <pair <int, int>> change;
for(int i = 0; i < 4; i++) {
int x = row[p] + dx[i];
int y = col[p] + dy[i];
if(inside(x, y)) change.insert(make_pair(x, y));
}
for(int i = 0; i < 4; i++) {
int x = row[q] + dx[i];
int y = col[q] + dy[i];
if(inside(x, y)) change.insert(make_pair(x, y));
}
for(auto i : change) {
process(i.first, i.second, -1);
}
swap(a[row[p]][col[p]], a[row[q]][col[q]]);
swap(row[p], row[q]);
swap(col[p], col[q]);
for(auto i : change) {
process(i.first, i.second, 1);
}
return count(arr, arr + (h * w), 4);
}
컴파일 시 표준 에러 (stderr) 메시지
seats.cpp: In function 'int swap_seats(int, int)':
seats.cpp:80:7: warning: unused variable 'ans' [-Wunused-variable]
80 | int ans = 0;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |