本篇博客还在持续更新润色中~
STL - vector
题单 🧑🏻💻
题目来源 | 题目名称 | 题目链接 | 备注 | 难度 |
---|---|---|---|---|
Codeforces 1537C | Challenging Cliffs | 链接🔗 | 思维 | 🟡 |
UVa - 101 | The Blocks Problem | 链接🔗 | 二维vector | 🟡 |
题解 🚀
Challenging Cliffs
题目信息 📚
【题目名称】Codeforces 1537C - Challenging Cliffs
【题目描述】
You are a game designer and want to make an obstacle course. The player will walk from left to right. You have $n$ heights of mountains already selected and want to arrange them so that the absolute difference of the heights of the first and last mountains is as small as possible.
In addition, you want to make the game difficult, and since walking uphill or flat is harder than walking downhill, the difficulty of the level will be the number of mountains $i$ $(1 \leq i < n)$ such that $h_i \leq h_{i+1}$ where $h_i$ is the height of the $i$-th mountain. You don’t want to waste any of the mountains you modeled, so you have to use all of them.
From all the arrangements that minimize $|h_1 - h_n|$, find one that is the most difficult. If there are multiple orders that satisfy these requirements, you may find any.
【输入】
The first line will contain a single integer $t$ $(1 \leq t \leq 100)$ — the number of test cases. Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ $(2 \leq n \leq 2 \times 10^5)$ — the number of mountains.
The second line of each test case contains $n$ integers $h_1, \ldots, h_n$ $(1 \leq h_i \leq 10^9)$, where $h_i$ is the height of the $i$-th mountain.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \times 10^5$.
【输出】
For each test case, output $n$ integers — the given heights in an order that maximizes the difficulty score among all orders that minimize $|h_1 - h_n|$.
If there are multiple orders that satisfy these requirements, you may output any.
【输入样例】
2
4
4 2 1 2
2
3 1
【输出样例】
2 4 1 2
1 3
【原题链接】
https://codeforces.com/problemset/problem/1537/C
题目解析 🍉
【题目分析】
思维题。
首先对所有高度从小到大排序,绝对值相差最小的两个数在排序后会处于相邻位置。
假设高度为 a < b < c < d < e < f < g(且 c 和 d 的绝对值差值最小),则最佳的排列顺序为:d e f g / a b c
PS:当 n = 2
时,排序顺序应当为:c d
【C++ 代码】
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
int n, tmp;
void solve() {
// 读入高度并且排序
cin >> n;
vector<int> a;
for (int i = 1; i <= n; i++) {
cin >> tmp;
a.push_back(tmp);
}
sort(a.begin(), a.end());
// 输出符合要求的排列
if (n == 2) cout << a[0] << " " << a[1] << endl;
else {
// 寻找绝对值相差最小的下标
int res = INT_MAX, id = -1;
for (int i = 0; i < a.size() - 1; i++) {
int dis = abs(a[i + 1] - a[i]);
if (dis < res) {
res = dis, id = i;
}
}
// 输出最佳排列
for (int i = id + 1; i < a.size(); i++)
cout << a[i] << " ";
for (int i = 0; i <= id; i++)
cout << a[i] << " ";
cout << endl;
}
}
int main() {
ios::sync_with_stdio(false); //cin读入优化
cin.tie(0);
int _ = 1;
cin >> _;
while (_--) {
solve();
}
return 0;
}
The Blocks Problem
题目信息 📚
【题目名称】UVa - 101 The Blocks Problem
【题目描述】
Many areas of Computer Science use simple, abstract domains for both analytical and empirical studies. For example, an early AI study of planning and robotics (STRIPS) used a block world in which a robot arm performed tasks involving the manipulation of blocks.
In this problem you will model a simple block world under certain rules and constraints. Rather than determine how to achieve a specified state, you will “program” a robotic arm to respond to a limited set of commands.
The problem is to parse a series of commands that instruct a robot arm in how to manipulate blocks that lie on a flat table. Initially there are n blocks on the table (numbered from $0$ to $n − 1$) with block bi adjacent to block $b_{i+1}$ for all $0 \le i \lt n − 1$ as shown in the diagram below:
The valid commands for the robot arm that manipulates blocks are:
- move a onto b
where a and b are block numbers, puts block a onto block b after returning any blocks that are stacked on top of blocks a and b to their initial positions. - move a over b
where a and b are block numbers, puts block a onto the top of the stack containing block b, after returning any blocks that are stacked on top of block a to their initial positions. - pile a onto b
where a and b are block numbers, moves the pile of blocks consisting of block a, and any blocks that are stacked above block a, onto block b. All blocks on top of block b are moved to their initial positions prior to the pile taking place. The blocks stacked above block a retain their order when moved. - pile a over b
where a and b are block numbers, puts the pile of blocks consisting of block a, and any blocks that are stacked above block a, onto the top of the stack containing block b. The blocks stacked above block a retain their original order when moved. - quit
terminates manipulations in the block world.
Any command in which $a = b$ or in which a and b are in the same stack of blocks is an illegal command. All illegal commands should be ignored and should have no affect on the configuration of blocks.
【输入】
The input begins with an integer $n$ on a line by itself representing the number of blocks in the block world. You may assume that $0 < n < 25$.
The number of blocks is followed by a sequence of block commands, one command per line. Your program should process all commands until the quit
command is encountered.
You may assume that all commands will be of the form specified above. There will be no syntactically incorrect commands.
【输出】
The output should consist of the final state of the blocks world. Each original block position numbered $i$ $(0 \le i \lt n$ where n is the number of blocks) should appear followed immediately by a colon. If there is at least a block on it, the colon must be followed by one space, followed by a list of blocks that appear stacked in that position with each block number separated from other block numbers by a space. Don’t put any trailing spaces on a line.
There should be one line of output for each block position (i.e., n lines of output where n is the integer on the first line of input).
【输入样例】
10
move 9 onto 1
move 8 over 1
move 7 over 1
move 6 over 1
pile 8 over 6
pile 8 over 5
move 2 over 1
move 4 over 9
quit
【输出样例】
0: 0
1: 1 9 2 4
2:
3: 3
4:
5: 5 8 7 6
6:
7:
8:
9:
【原题链接】
https://vjudge.net/problem/UVA-101
题目解析 🍉
【题目分析】
【C++ 代码】