1.26-模拟测试2

· · 个人记录

T1-神奇的幻方

赛时分析

用时,思路,代码错误原因分析。

题意

把数字 1 \times N*N 依次填到二维数组上:

数字 1 填到第 1 行中间。

假设上一个数字位置是 x, y

if (x == 1 and y != n)  填到 n 行 y+1 列
else if (x != 1 and y == n) 填到 x-1 行 1 列
else if (x == 1 and y == n) 填到 x+1 行 y 列
else {
  if (a[x-1][y+1] == 0) 填到x-1行 y+1列
  else 填到 x+1 行 y 列
}

思路

定义两个变量来表示当前要填的数字的行和列:

x = 1, y = n/2+1

根据上面的规则模拟 x,y 的变化。

for (int i=2; i<=n*n; i++)

T2-地图探险

题意

一个 n \times m 的矩阵,x 表示障碍,. 表示空地。

机器人位置是 x,y ,方向是 d,0-3 表示东南西北。

- 朝着当前方向走一步,东南西北表示右下左上。 - 下一步可以走,走过去,方向不变。 - 下一步不能走,位置不变,`d = (d+1) % 4;` 问一共经过多少个不同的格子。 ### 思路 三个变量 `x, y, d` 表示位置和方向。 模拟 `k` 次操作,每次先看下一步能不能走: - 能走,改变 `x,y`,走过去,`d` 不变。 - 不能走,`d = (d+1) % 4;` 怎么样处理方向呢? 第一种方式,直接四个判断 ```cpp int nx, ny; if (d == 0) { nx = x; ny = y + 1; } ``` 第二种方式,用方向偏移数组 ```cpp int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; int nx = x + dx[d], ny = y + dy[d]; ``` 怎么判断下一步不能走? 越界、或者有障碍 ```cpp // 先判越界 if (nx < 1 or nx > n or ny < 1 or ny > m or a[nx][ny] == 'x') ``` 桶来标记每个经过的格子,记录答案。 ### 代码 ```cpp #include<bits/stdc++.h> using namespace std; int n, m, k; int x, y, d; char a[1005][1005]; int vis[1005][1005]; int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; void solve() { cin >> n >> m >> k; cin >> x >> y >> d; for (int i=1; i<=n; i++) { for (int j=1; j<=m; j++) { cin >> a[i][j]; } } vis[x][y] = 1; while (k--) { int nx = x + dx[d]; int ny = y + dy[d]; if (nx < 1 or nx > n or ny < 1 or ny > m or a[nx][ny] == 'x') { // 转向 d = (d + 1) % 4; } else { // 走路 x = nx, y = ny; } vis[x][y] = 1; } int res = 0; for (int i=1; i<=n; i++) { for (int j=1; j<=m; j++) { res += vis[i][j]; vis[i][j] = 0; } } cout << res << '\n'; } int main() { int t; cin >> t; while (t--) solve(); return 0; } ``` ## T3-瑞士轮 ### 题意 $2 \times N$ 个选手,$R$ 轮比赛。 每次比赛开始和结束都需要排序,按总分从大到小排,一样的就按编号从小到大排。 第 `i` 名和第 `i+1` 名比,`i = 1, 3, 5...`,赢的人加一分,实力值高的赢。 $R$ 轮比赛后,第 $Q$ 名选手是谁。 ### 思路 暴力解:结构体排序,每次模拟比赛后,再次 `sort`,期望分数应该是 $50pts
#include<bits/stdc++.h>
using namespace std;

struct P {
    int s, w, idx;
};

const int N = 2e5+5;

int n, r, q;
P a[N];

bool cmp(P a1, P a2) {
    if (a1.s == a2.s) return a1.idx < a2.idx;
    return a1.s > a2.s;
}

int main() {
    cin >> n >> r >> q;
    n *= 2;

    for (int i=1; i<=n; i++) {
        cin >> a[i].s;
        a[i].idx = i;
    }
    for (int i=1; i<=n; i++) cin >> a[i].w;

    sort(a+1, a+1+n, cmp);

    while (r--) {
        for (int i=1; i<=n; i+=2) {
            if (a[i].w > a[i+1].w) a[i].s++;
            else a[i+1].s++;
        }

        sort(a+1, a+1+n, cmp);
    }

    cout << a[q].idx;
    return 0;
}

正解:

每一轮,所有赢的人分数都加 1,他们是相对有序的 所有输的人,相对有序。

合并两个有序序列。

每次只看开头第一个元素,谁更小先拿谁,时间复杂度是 O(n)。

#include<bits/stdc++.h>
using namespace std;

struct P {
    int s, w, idx;
};

const int N = 2e5+5;

int n, r, q, idx1, idx2;
P a[N], win[N], lose[N];

bool cmp(P a1, P a2) {
    if (a1.s == a2.s) return a1.idx < a2.idx;
    return a1.s > a2.s;
}

int main() {
    cin >> n >> r >> q;
    n *= 2;

    for (int i=1; i<=n; i++) {
        cin >> a[i].s;
        a[i].idx = i;
    }
    for (int i=1; i<=n; i++) cin >> a[i].w;

    sort(a+1, a+1+n, cmp);

    while (r--) {
        // 有赢的人和输的人
        for (int i=1; i<=n; i+=2) {
            if (a[i].w > a[i+1].w) {
                a[i].s++;
                win[++idx1] = a[i];
                lose[++idx2] = a[i+1];
            }
            else {
                a[i+1].s++;
                win[++idx1] = a[i+1];
                lose[++idx2] = a[i];
            }
        }

        // 合并两个有序序列
        int i = 1, j = 1, t = 0;  // 表示两个序列当前第一个元素的位置
        win[n/2+1] = lose[n/2+1] = {INT_MIN};  // 哨兵
        while (i <= n/2 or j <= n/2) {
            // win[i] 和 lose[j] 谁更大,谁先拿出来
            if (cmp(win[i], lose[j]) == 1) {  // 取出 win[i]
                a[++t] = win[i];
                i++;
            }
            else {  // 取出 lose[j]
                a[++t] = lose[j];
                j++;
            }
        }
        idx1 = idx2 = 0;
    }

    cout << a[q].idx;
    return 0;
}