【笔试刷题】网易-2026.04.02-改编真题
✅ 春招备战指南 ✅
💡 学习建议:
- 先尝试独立解题
- 对照解析查漏补缺
🧸 题面描述背景等均已深度改编,做法和题目本质基本保持一致。
🍹 感谢各位朋友们的订阅,你们的支持是我们创作的最大动力
🌸 目前本专栏已经上线200+套真题改编解析,后续会持续更新的
春秋招笔试机考招合集 -> 互联网必备刷题宝典🔗
网易-2026.04.02
题目一:边境排阵
看起来是田忌赛马换皮,本质就是“最多能赢几场”。把双方战力排序后,用最小可行的己方单位去吃掉当前最弱的对手,最后检查最大胜场是否严格超过一半。
难度:Low
题目二:背包格整理
先按题目给的三层关键字排序,再把每个物品按“行优先、列次之”的规则塞进网格。难点不在算法,而在把放置规则翻译成稳定的逐格扫描。
难度:Mid
题目三:索桥火炮清场
真正的关键不是模拟每一炮,而是反过来问:打了 炮之后,哪些坐标组无论怎么推都不可能被边界淘汰。那些还卡在中间的坐标组只能靠直接命中解决,于是可以二分答案。
难度:Mid
题目四:贴图驻留调度
整题就是按规则做状态机。只要先看透“同一贴图当前已加载 mip 始终是一段连续后缀,且过期时间一致”,实现就能收敛到按贴图维护 minMip + expire + usedVRAM 的模拟。
难度:High
1. 边境排阵
问题描述
边境演练开始后,对手已经提前公开了这一轮的出阵顺序。
一共会进行 轮对抗。第
轮里,对手会派出一支战力为
的小队。你手里也有
支小队,它们的战力分别是
,并且每支小队只能出战一次。
每轮规则如下:
- 如果你派出的小队战力严格大于对手,这一轮记为你方获胜。
- 如果双方战力相同,或者你方更小,都算你方失利。
你可以自由安排己方 支小队的出战顺序。请判断,是否存在一种安排方式,使得最终你方的胜场数严格大于败场数。
保证 是奇数。
输入格式
第一行输入一个整数 ,表示测试数据组数。
对于每组数据:
- 第一行输入一个奇数
,表示双方小队数量。
- 第二行输入
个整数
,表示己方小队战力。
- 第三行输入
个整数
,表示对手按出战顺序给出的战力。
输出格式
对于每组数据输出一行:
- 若可以做到胜场数严格大于败场数,输出
YES。 - 否则输出
NO。
样例输入
2
5
2 5 7 1 6
3 5 6 2 7
3
3 3 3
3 3 3
样例输出
YES
NO
数据范围
为奇数
- 所有测试数据中,
的总和不超过
| 样例 | 解释说明 |
|---|---|
| 样例 1 | 第一组可以把己方战力排成一种更优的对位方式,最终拿到超过一半的胜场,所以输出 YES。第二组所有人战力都相同,而平局也算失利,因此不可能赢过对手。 |
题解
题目真正要问的是:己方最多能赢多少场。
只要最大胜场数都不超过 ,那就不可能让胜场严格大于败场;反过来,如果最大胜场数大于
,答案就是
YES。
所以核心只剩一句话:
- 怎么求“最多能赢几场”。
这就是标准的贪心配对。
先把己方战力数组和对手战力数组都从小到大排序。随后用一个指针 j 指向当前还没有被击败的最弱对手:
- 依次查看排好序的己方战力
a。 - 如果当前这支队伍
a[i]能击败b[j],就让它去拿下这一分,然后j往后移动。 - 如果
a[i]连当前最弱的b[j]都打不过,那么它拿去对别的对手也不会更好,直接放弃这支队伍,继续看下一支更强的己方队伍。
为什么这个贪心是对的?
- 如果某支己方队伍已经可以赢下当前最弱的可击败对手,那就没有必要把它浪费到更强的对手身上。
- 让它吃掉当前最弱的那一个,能把更强的己方队伍留给后面更难啃的对手。
- 如果它连当前最弱的都赢不了,那它无论换谁都不可能拿分,跳过不会损失最优解。
因此,这个过程统计出来的胜场数就是最大胜场数。
最后因为 是奇数,所以“胜场严格大于败场”恰好等价于:
直接判断即可。
复杂度分析
- 排序复杂度是
。
- 双指针线性扫描复杂度是
。
所以单组数据总复杂度为 ,空间复杂度为
。在所有测试数据总规模不超过
的条件下完全可以通过。
参考代码
- Python
import sys
input = lambda: sys.stdin.readline().strip()
def max_win(a, b):
# 排序后,始终尝试用当前最小的可行己方战力去击败最弱的未击败对手。
a.sort()
b.sort()
j = 0
win = 0
n = len(a)
for x in a:
# 只有严格大于时才算获胜。
if j < n and x > b[j]:
win += 1
j += 1
return win
def solve():
t = int(input())
out = []
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
win = max_win(a, b)
out.append("YES" if win > n // 2 else "NO")
sys.stdout.write("\n".join(out))
if __name__ == "__main__":
solve()
- Cpp
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int maxWin(vector<long long>& a, vector<long long>& b) {
// 贪心原则:用最小的可行己方战力去吃掉当前最弱的可击败对手。
sort(a.begin(), a.end());
sort(b.begin(), b.end());
int n = static_cast<int>(a.size());
int j = 0;
int win = 0;
for (long long x : a) {
if (j < n && x > b[j]) {
++win;
++j;
}
}
return win;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<long long> a(n), b(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
for (int i = 0; i < n; ++i) {
cin >> b[i];
}
int win = maxWin(a, b);
cout << (win > n / 2 ? "YES" : "NO") << '\n';
}
return 0;
}
- Java
import java.io.InputStream;
import java.util.Arrays;
public class Main {
private static int maxWin(long[] a, long[] b) {
// 先排序,再做一次双指针统计最大胜场。
Arrays.sort(a);
Arrays.sort(b);
int j = 0;
int win = 0;
int n = a.length;
for (long x : a) {
if (j < n && x > b[j]) {
win++;
j++;
}
}
return win;
}
public static void main(String[] args) throws Exception {
FastScanner fs = new FastScanner(System.in);
StringBuilder sb = new StringBuilder();
int t = fs.nextInt();
while (t-- > 0) {
int n = fs.nextInt();
long[] a = new long[n];
long[] b = new long[n];
for (int i = 0; i < n; i++) {
a[i] = fs.nextLong();
}
for (int i = 0; i < n; i++) {
b[i] = fs.nextLong();
}
int win = maxWin(a, b);
sb.append(win > n / 2 ? "YES" : "NO").append('\n');
}
System.out.print(sb);
}
private static class FastScanner {
private final InputStream in;
private final byte[] buf = new byte[1 << 16];
private int ptr = 0;
private int len = 0;
FastScanner(InputStream is) {
in = is;
}
private int read() throws Exception {
if (ptr >= len) {
len = in.read(buf);
ptr = 0;
if (len <= 0) {
return -1;
}
}
return buf[ptr++];
}
long nextLong() throws Exception {
int c;
do {
c = read();
} while (c <= 32);
long sign = 1;
if (c == '-') {
sign = -1;
c = read();
}
long val = 0;
while (c > 32) {
val = val * 10 + (c - '0');
c = read();
}
return val * sign;
}
int nextInt() throws Exception {
return (int) nextLong();
}
}
}
2. 背包格整理
问题描述
小基 在整理一个带网格的仓库背包。
背包一共有 行
列,行号从上到下递增,列号从左到右递增。现在有
个物品,需要先按固定规则排序,再按顺序依次尝试放入背包。
每个物品都有下面这些属性:
id:物品唯一编号,所有物品的id互不相同。height、width:物品占用的高和宽,不能旋转。quality:品质,数值越大越优先。type:类别,只会是equip、weapon、item、other之一。
排序规则如下:
- 先按
type排序,优先级为equip>weapon>item>other。 - 若
type相同,则quality更高的排在前面。 - 若前两项都相同,则
id更小的排在前面。
放置规则如下:
- 按排好序后的顺序,一个一个尝试放入背包。
- 对于当前物品,优先选择行号最小的可行位置;如果同一行里有多个可行位置,再选列号最小的那个。
- 物品必须完整放进背包,且覆盖区域内所有格子都还为空。
- 如果整个背包都找不到可行位置,就跳过这个物品。
请输出:
- 最终是否放下了全部物品。
- 最终背包每个格子里放的是哪个物品的
id;若为空则输出0。
输入格式
第一行输入三个整数 ,分别表示背包的高、宽和物品数量。
接下来 行,每行输入:
id height width quality type
其中 type 只会是 equip、weapon、item、other 之一。
输出格式
第一行输出一个字符串:
- 如果所有物品都成功放入,输出
YES。 - 否则输出
NO。
接下来输出 行,每行
个整数,表示最终背包状态。
样例输入
4 3 3
1 1 3 7 other
2 3 1 2 equip
3 2 2 4 other
样例输出
YES
2 3 3
2 3 3
2 0 0
1 1 1
数据范围
type只会在equip、weapon、item、other中出现
| 样例 | 解释说明 |
|---|---|
| 样例 1 | 排序后,id=2 的装备会最先放入;随后在剩余空位中继续尝试放 id=3 和 id=1,最终三个物品都能放下,所以输出 YES。 |
题解
这题没有隐藏算法,按题意一步一步模拟就够了。
先做排序。
题目已经给出了三层比较关键字:
type优先级。quality降序。id升序。
把每个物品存下来以后,按这个规则排一次序即可。
然后是放置。
对于当前物品,题目要求:
- 行号尽量小。
- 在满足这个前提下,列号尽量小。
这句话直接翻译成程序,就是枚举左上角坐标 (r,c):
r从上到下扫。c在当前行里从左到右扫。- 一旦找到一个可以完整放下的位置,就立刻放进去,不必继续找。
判断一个位置能不能放也很直接:
- 先看物品右边界和下边界会不会越出背包。
- 再检查这个
height × width矩形里的格子是否全是空格。
如果找到位置,就把这个矩形区域全部写成对应的 id;如果遍历完整个背包都找不到,就说明这个物品放不进去。
最后再看是否所有物品都成功放入:
- 全部成功则输出
YES。 - 只要有一个被跳过,就是
NO。
复杂度分析
设单个物品大小为 。
- 排序复杂度是
。
- 每个物品最多尝试所有左上角位置,共
个。
- 每次检查位置需要看
个格子,而题目保证
,这是一个很小的常数。
所以总复杂度可以写成 。在本题范围内完全足够,空间复杂度为
。
参考代码
- Python
import sys
input = lambda: sys.stdin.readline().strip()
PRI = {
"equip": 3,
"weapon": 2,
"item": 1,
"other": 0,
}
def can_put(grid, r, c, h, w):
# 只要矩形里出现一个非 0 格子,这个位置就不能放。
for i in range(r, r + h):
for j in range(c, c + w):
if grid[i][j] != 0:
return False
return True
def put_item(grid, r, c, h, w, item_id):
# 找到可行位置后,把整个占用矩形统一写成当前物品 id。
for i in range(r, r + h):
for j in range(c, c + w):
grid[i][j] = item_id
def solve():
n, m, t = map(int, input().split())
items = []
for _ in range(t):
item_id, h, w, q, typ = input().split()
items.append((int(item_id), int(h), int(w), int(q), typ))
# 先按题目给定规则排序。
items.sort(key=lambda x: (-PRI[x[4]], -x[3], x[0]))
grid = [[0] * m for _ in range(n)]
ok = True
for item_id, h, w, _, _ in items:
placed = False
for r in range(n - h + 1):
for c in range(m - w + 1):
if can_put(grid, r, c, h, w):
put_item(grid, r, c, h, w, item_id)
placed = True
break
if placed:
break
if not placed:
ok = False
print("YES" if ok else "NO")
for row in grid:
print(*row)
if __name__ == "__main__":
solve()
- Cpp
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Item {
int id;
int h;
int w;
int q;
string type;
};
int typePriority(const string& type) {
if (type == "equip") {
return 3;
}
if (type == "weapon") {
return 2;
}
if (type == "item") {
return 1;
}
return 0;
}
bool canPut(const vector<vector<int>>& grid, int r, int c, int h, int w) {
for (int i = r; i < r + h; ++i) {
for (int j = c; j < c + w; ++j) {
if (grid[i][j] != 0) {
return false;
}
}
}
return true;
}
void putItem(vector<vector<int>>& grid, int r, int c, int h, int w, int id) {
for (int i = r; i < r + h; ++i) {
for (int j = c; j < c + w; ++j) {
grid[i][j] = id;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, t;
cin >> n >> m >> t;
vector<Item> items(t);
for (int i = 0; i < t; ++i) {
cin >> items[i].id >> items[i].h >> items[i].w >> items[i].q >> items[i].type;
}
sort(items.begin(), items.end(), [](const Item& a, const Item& b) {
int pa = typePriority(a.type);
int pb = typePriority(b.type);
if (pa != pb) {
return pa > pb;
}
if (a.q != b.q) {
return a.q > b.q;
}
return a.id < b.id;
});
vector<vector<int>> grid(n, vector<int>(m, 0));
bool ok = true;
for (const Item& item : items) {
bool placed = false;
for (int r = 0; r + item.h <= n && !placed; ++r) {
for (int c = 0; c + item.w <= m; ++c) {
if (canPut(grid, r, c, item.h, item.w)) {
putItem(grid, r, c, item.h, item.w, item.id);
placed = true;
break;
}
}
}
if (!placed) {
ok = false;
}
}
cout << (ok ? "YES" : "NO") << '\n';
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (j) {
cout << ' ';
}
cout << grid[i][j];
}
cout << '\n';
}
return 0;
}
- Java
import java.io.InputStream;
import java.util.Arrays;
public class Main {
private static class Item {
int id;
int h;
int w;
int q;
String type;
}
private static int typePriority(String type) {
if ("equip".equals(type)) {
return 3;
}
if ("weapon".equals(type)) {
return 2;
}
if ("item".equals(type)) {
return 1;
}
return 0;
}
private static boolean canPut(int[][] grid, int r, int c, int h, int w) {
for (int i = r; i < r + h; i++) {
for (int j = c; j < c + w; j++) {
if (grid[i][j] != 0) {
return false;
}
}
}
return true;
}
private static void putItem(int[][] grid, int r, int c, int h, int w, int id) {
for (int i = r; i < r + h; i++) {
for (int j = c; j < c + w; j++) {
grid[i][j] = id;
}
}
}
public static void main(String[] args) throws Exception {
FastScanner fs = new FastScanner(System.in);
int n = fs.nextInt();
int m = fs.nextInt();
int t = fs.nextInt();
Item[] items = new Item[t];
for (int i = 0; i < t; i++) {
Item item = new Item();
item.id = fs.nextInt();
item.h = fs.nextInt();
item.w = fs.nextInt();
item.q = fs.nextInt();
item.type = fs.next();
items[i] = item;
}
Arrays.sort(items, (a, b) -> {
int pa = typePriority(a.type);
int pb = typePriority(b.type);
if (pa != pb) {
return pb - pa;
}
if (a.q != b.q) {
return b.q - a.q;
}
return a.id - b.id;
});
int[][] grid = new int[n][m];
boolean ok = true;
for (Item item : items) {
boolean placed = false;
for (int r = 0; r + item.h <= n && !placed; r++) {
for (int c = 0; c + item.w <= m; c++) {
if (canPut(grid, r, c, item.h, item.w)) {
putItem(grid, r, c, item.h, item.w, item.id);
placed = true;
break;
}
}
}
if (!placed) {
ok = false;
}
}
StringBuilder sb = new StringBuilder();
sb.append(ok ? "YES" : "NO").append('\n');
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (j > 0) {
sb.append(' ');
}
sb.append(grid[i][j]);
}
sb.append('\n');
}
System.out.print(sb);
}
private static class FastScanner {
private final InputStream in;
private final byte[] buf = new byte[1 << 16];
private int ptr = 0;
private int len = 0;
FastScanner(InputStream is) {
in = is;
}
private int read() throws Exception {
if (ptr >= len) {
len = in.read(buf);
ptr = 0;
if (len <= 0) {
return -1;
}
}
return buf[ptr++];
}
String next() throws Exception {
int c;
do {
c = read();
} while (c <= 32);
StringBuilder sb = new StringBuilder();
while (c > 32) {
sb.append((char) c);
c = read();
}
return sb.toString();
}
long nextLong() throws Exception {
int c;
do {
c = read();
} while (c <= 32);
long sign = 1;
if (c == '-') {
sign = -1;
c = read();
}
long val = 0;
while (c > 32) {
val = val * 10 + (c - '0');
c = read();
}
return val * sign;
剩余60%内容,订阅专栏后可继续查看/也可单篇购买
互联网刷题笔试宝典,这里涵盖了市面上大部分的笔试题合集,希望助大家春秋招一臂之力