2025.5.16
刷题日记
板刷1600的第三天
今天课多,再加上这几天休息不好,所以没有做新的题,只是把昨天一直卡的一道1700的模拟给补了一下
Round_1012的C题,https://codeforces.com/problemset/problem/2090/C

当时就是感觉能写,于是没看题解,一直试,一直TLE。可见最终状况之惨烈
最后看了一下题解,发现思路其实是没问题的,就是在模拟的过程中一直超时
当时就是很不理解,我每次给答案的时间复杂度几乎就是O(1)啊,为什么呢?
然后对照了题解一点点改,还是一直TLE一直TLE
最后一怒之下给数组大小改小了一点,过了
无敌了孩子,这我是真绷不住了
这道题的意思就是,现在有一批人要进食堂吃饭,num = 0的人比较社恐,只能找当前离他最近的空桌子
num = 1的人就无所谓了,只要找到当前离他最近的位置即可
所有人都只能在过道上走,不能翻桌子(

那么这道题的思路其实就已经显现出来了:
咱们把座位和桌子都编上号,先按照步数排序,步数相同按x坐标排序,x坐标还相同则按y坐标排序,最后从前往后依次找空的就ok了
需要提前开个vis数组用来标记是否被用过
就这么简单个思路,但博主在实现的过程中一直在用std::vector、std::map、std::pair搁那来回套来套去的,给自己都写迷了
虽然能保证输出的答案的正确性,但是一直超时
接下来请欣赏究极屎山
点击查看代码
#include <bits/stdc++.h>
#include <iostream>
// GNU G++17 7.3.0
using std::cin, std::cout;
#define debug0(x) cout << #x << " = " << x << '\n'
#define debugp(x, y) cout << #x << " = " << x << ", " << #y << " = " << y << '\n'
#define debugt(x, y, z) cout << #x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = " << z << '\n'
#define debug1(f, a, b) cout << #f << ": "; for (int i = (a); i <= (b); i++) cout << (f)[i] << " \n"[i == (b)]
#define debug2(f, a, b, c, d) cout << #f << ":\n"; for (int i = (a); i <= (b); i++) for (int j = (c); j <= (d); j++) cout << (f)[i][j] << " \n"[j == (d)]
#define debug3(q) cout << #q << ": "; for (auto it : (q)) cout << it << ' '; cout << '\n'
#define debug4(q) cout << #q << ":\n"; for (auto [x, y] : (q)) cout << '[' << x << ", " << y << "]\n"
#define debug5(q) cout << #q << ":\n"; for (auto [x, y, z] : (q)) cout << '[' << x << ", " << y << ", " << z << "]\n"
using ll = int64_t;
const ll N = 2e5 + 10;
const ll INF = LLONG_MAX;
const ll mod = 998244353;
typedef struct node {
int x, y, num;
} node;
std::vector<node> seat;
std::vector<std::pair<int, int>> table;
std::map<std::pair<int, int>, int> ask_seat;
std::map<std::pair<int, int>, int> ask_table;
void solve () {
int n;
std::cin >> n;
std::vector<node> seat;
std::vector<std::pair<int, int>> table;
std::map<std::pair<int, int>, int> ask_seat;
std::map<std::pair<int, int>, int> ask_table;
for (int x = 0; x <= n; x++) {
for (int y = 0; y <= n; y++) {
int i = 3 * x + 1;
int j = 3 * y + 1;
int num = (x + y) * 3 + 1;
seat.push_back({ i, j, num });
seat.push_back({ i + 1, j, num + 1 });
seat.push_back({ i, j + 1, num + 1 });
seat.push_back({ i + 1, j + 1, num + 4 });
table.push_back({x, y});
}
}
std::sort(seat.begin(), seat.end(), [&] (node a, node b) {
if (a.num != b.num) return a.num < b.num;
else {
if (a.x != b.x) return a.x < b.x;
else return a.y < b.y;
}
});
std::sort(table.begin(), table.end(), [&] (std::pair<int, int> a, std::pair<int, int> b) {
if (a.first + a.second != b.first + b.second) return a.first + a.second < b.first + b.second;
else {
if (a.first != b.first) return a.first < b.first;
else return a.second < b.second;
}
});
for (int i = 0; i < seat.size(); i++) {
ask_seat[{seat[i].x, seat[i].y}] = i;
}
for (int i = 0; i < table.size(); i++) {
int X = 3 * table[i].first + 1;
int Y = 3 * table[i].second + 1;
ask_table[{X, Y}] = i;
ask_table[{X + 1, Y}] = i;
ask_table[{X, Y + 1}] = i;
ask_table[{X + 1, Y + 1}] = i;
}
std::vector<int> a(n + 1);
std::vector<int> vis_seat(N, 0);
std::vector<int> vis_table(N, 0);
for (int i = 1; i <= n; i++) {
std::cin >> a[i];
}
int p1 = 0, p2 = 0;
for (int i = 1; i <= n; i++) {
if (a[i] == 1) {
while (vis_seat[p1]) p1++;
std::cout << seat[p1].x << ' ' << seat[p1].y << '\n';
vis_seat[p1] = 1;
vis_table[ask_table[{ seat[p1].x, seat[p1].y }]] = 1;
}
else {
while (vis_table[p2]) p2++;
std::cout << table[p2].first * 3 + 1 << ' ' << table[p2].second * 3 + 1 << '\n';
vis_seat[ask_seat[{table[p2].first * 3 + 1, table[p2].second * 3 + 1}]] = 1;
vis_table[p2] = 1;
}
}
}
int main () {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t = 1;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}
u1s1这写的也太逆天了,博主的代码能力还是泰差
接下来照着题解优化了一下,把不必要的map和vector全删了
并且这里需要注意,对于n个人,最坏的情况下也就是n张桌子
所以数组不需要开那么大,只需要开 m = sqrtl(2 * n)即可
最终代码如下:
点击查看代码
#include <bits/stdc++.h>
using ll = int64_t;
const ll N = 2e5 + 10;
const ll INF = LLONG_MAX;
const ll mod = 998244353;
typedef struct node {
int x, y, num;
} node;
std::vector<node> seat;
void init () {
for (int x = 0; x <= 320; x++) {
for (int y = 0; y <= 320; y++) {
int i = 3 * x + 1;
int j = 3 * y + 1;
int num = i + j - 1;
seat.push_back({ i, j, num });
seat.push_back({ i + 1, j, num + 1 });
seat.push_back({ i, j + 1, num + 1 });
seat.push_back({ i + 1, j + 1, num + 4 });
}
}
std::sort(seat.begin(), seat.end(), [&] (node a, node b) {
if (a.num != b.num) return a.num < b.num;
else {
if (a.x != b.x) return a.x < b.x;
else return a.y < b.y;
}
});
}
void solve () {
int n;
std::cin >> n;
int m = sqrtl(2 * n) + 3;
std::vector<int> a(n + 1);
std::vector<std::vector<int>> vis_seat(m * 3, std::vector<int>(m * 3, 0));
std::vector<std::vector<int>> vis_table(m, std::vector<int>(m, 0));
for (int i = 1; i <= n; i++) {
std::cin >> a[i];
}
int p1 = 0, p2 = 0;
for (int i = 1; i <= n; i++) {
int X, Y;
if (a[i] == 1) {
while (vis_seat[seat[p1].x][seat[p1].y]) p1++;
X = seat[p1].x, Y = seat[p1].y;
}
else {
while (vis_table[seat[p2].x / 3][seat[p2].y / 3]) p2++;
X = seat[p2].x, Y = seat[p2].y;
}
vis_seat[X][Y] = 1;
vis_table[X / 3][Y / 3] = 1;
std::cout << X << ' ' << Y << '\n';
}
}
int main () {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
init();
int t = 1;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}

浙公网安备 33010602011771号