做優(yōu)惠券網(wǎng)站賺錢嗎seo整站怎么優(yōu)化
更多 CSP 認證考試題目題解可以前往:CSP-CCF 認證考試真題題解
原題鏈接: 202406-2 矩陣重塑(其二)
時間限制: 1.0 秒
空間限制: 512 MiB
題目背景
矩陣轉(zhuǎn)置操作是將矩陣的行和列交換的過程。在轉(zhuǎn)置過程中,原矩陣 A \mathbf{A} A 的元素 a i j a_{ij} aij?? 會移動到轉(zhuǎn)置后的矩陣 A T \mathbf{A}^T AT 的 a j i a_{ji} aji?? 的位置。這意味著 A \mathbf{A} A 的第 i i i 行第 j j j 列的元素在 A T \mathbf{A}^T AT 中成為了第 j j j 行第 i i i 列的元素。
例如,有矩陣 A \mathbf{A} A 如下:
A = [ a b c d e f ] \mathbf{A} = \begin{bmatrix} a & b & c \\ d & e & f \end{bmatrix} A=[ad?be?cf?]
它的轉(zhuǎn)置矩陣 A T \mathbf{A}^T AT 會是:
A T = [ a d b e c f ] \mathbf{A}^T = \begin{bmatrix} a & d \\ b & e \\ c & f \end{bmatrix} AT= ?abc?def? ?
矩陣轉(zhuǎn)置在線性代數(shù)中是一個基本操作,廣泛應用于各種數(shù)學和工程領域。
題目描述
給定 n × m n \times m n×m 的矩陣 M \mathbf{M} M,試編寫程序支持以下查詢和操作:
-
重塑操作 p p p、 q q q:將當前矩陣重塑為 p × q p \times q p×q 的形狀(重塑的具體定義見上一題);
-
轉(zhuǎn)置操作:將當前矩陣轉(zhuǎn)置;
-
元素查詢 i i i、 j j j:查詢當前矩陣第 i i i 行 j j j 列的元素( 0 ≤ i < n 0 \le i < n 0≤i<n 且 0 ≤ j < m 0 \le j <m 0≤j<m)。
依次給出 t t t 個上述查詢或操作,計算其中每個查詢的結(jié)果。
輸入格式
從標準輸入讀入數(shù)據(jù)。
輸入共 n + t + 1 n + t + 1 n+t+1 行。
輸入的第一行包含三個正整數(shù) n n n、 m m m 和 t t t。
接下來依次輸入初始矩陣 M \mathbf{M} M 的第 0 0 0 到第 n ? 1 n - 1 n?1 行,每行包含 m m m 個整數(shù),按列下標從 0 0 0 到 m ? 1 m - 1 m?1 的順序依次給出。
接下來輸入 t t t 行,每行包含形如 op a b
的三個整數(shù),依次給出每個查詢或操作。具體輸入格式如下:
-
重塑操作:
1 p q
-
轉(zhuǎn)置操作:
2 0 0
-
元素查詢:
3 i j
輸出格式
輸出到標準輸出。
每個查詢操作輸出一行,僅包含一個整數(shù)表示查詢結(jié)果。
樣例1輸入
3 2 3
1 2
3 4
5 6
3 0 1
1 2 3
3 1 2
樣例1輸出
2
6
樣例2輸入
3 2 5
1 2
3 4
5 6
3 1 0
2 0 0
3 1 0
1 3 2
3 1 0
樣例2輸出
3
2
5
初始矩陣: [ 1 2 3 4 5 6 ] \begin{bmatrix} 1 & 2\\ 3 & 4\\ 5 & 6 \end{bmatrix} ?135?246? ?, ( 1 , 0 ) (1, 0) (1,0) 位置元素為 3 3 3;
轉(zhuǎn)置后: [ 1 3 5 2 4 6 ] \begin{bmatrix} 1 & 3 & 5\\ 2 & 4 & 6 \end{bmatrix} [12?34?56?], ( 1 , 0 ) (1, 0) (1,0) 位置元素為 2 2 2;
重塑后: [ 1 3 5 2 4 6 ] ? \begin{bmatrix} 1 & 3\\ 5 & 2\\ 4 & 6 \end{bmatrix}? ?154?326? ????, ( 1 , 0 ) (1, 0) (1,0) 位置元素為 5 5 5。
子任務
80 80% 80 的測試數(shù)據(jù)滿足:
- t ≤ 100 t \le 100 t≤100;
全部的測試數(shù)據(jù)滿足:
-
t ≤ 1 0 5 t \le 10^{5} t≤105 且其中轉(zhuǎn)置操作的次數(shù)不超過 100 100 100;
-
n n n、 m m m 和所有重塑操作中的 p p p、 q q q 均為正整數(shù)且 n × m = p × q ≤ 1 0 4 n \times m = p \times q \le 10^{4} n×m=p×q≤104;
-
輸入矩陣中每個元素的絕對值不超過 1000 1000 1000。
提示
-
對于 n × m n \times m n×m 的矩陣,雖然轉(zhuǎn)置和重塑操作都可以將矩陣形態(tài)變?yōu)? m × n m \times n m×n,但這兩種操作通常會導致不同的結(jié)果。
-
評測環(huán)境僅提供各語言的標準庫,特別地,不提供任何線性代數(shù)庫(如
numpy
、pytorch
等)。
題解
待補
時間復雜度: O ( t + 100 n m ) \mathcal{O}(t+100nm) O(t+100nm)。
參考代碼(21ms,3912KB)
/*Created by Pujx on 2024/6/20.
*/
#pragma GCC optimize(2, 3, "Ofast", "inline")
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
//#define int long long
//#define double long double
using i64 = long long;
using ui64 = unsigned long long;
//using i128 = __int128;
#define inf (int)0x3f3f3f3f3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define yn(x) cout << (x ? "yes" : "no") << endl
#define Yn(x) cout << (x ? "Yes" : "No") << endl
#define YN(x) cout << (x ? "YES" : "NO") << endl
#define mem(x, i) memset(x, i, sizeof(x))
#define cinarr(a, n) for (int _ = 1; _ <= n; _++) cin >> a[_]
#define cinstl(a) for (auto& _ : a) cin >> _
#define coutarr(a, n) for (int _ = 1; _ <= n; _++) cout << a[_] << " \n"[_ == n]
#define coutstl(a) for (const auto& _ : a) cout << _ << ' '; cout << endl
#define all(x) (x).begin(), (x).end()
#define md(x) (((x) % mod + mod) % mod)
#define ls (s << 1)
#define rs (s << 1 | 1)
#define ft first
#define se second
#define pii pair<int, int>
#ifdef DEBUG#include "debug.h"
#else#define dbg(...) void(0)
#endifconst int N = 2e5 + 5;
//const int M = 1e5 + 5;
const int mod = 998244353;
//const int mod = 1e9 + 7;
//template <typename T> T ksm(T a, i64 b) { T ans = 1; for (; b; a = 1ll * a * a, b >>= 1) if (b & 1) ans = 1ll * ans * a; return ans; }
//template <typename T> T ksm(T a, i64 b, T m = mod) { T ans = 1; for (; b; a = 1ll * a * a % m, b >>= 1) if (b & 1) ans = 1ll * ans * a % m; return ans; }int a[N];
int n, m, t, k, q;void work() {cin >> n >> m >> t;for (int i = 0; i < n * m; i++) cin >> a[i];while (t--) {int op, x, y;cin >> op >> x >> y;if (op == 1) n = x, m = y;else if (op == 2) {vector<int> b(a, a + n * m);swap(n, m);for (int i = 0; i < n; i++)for (int j = 0; j < m; j++)a[i * m + j] = b[j * n + i];}else cout << a[x * m + y] << endl;}
}signed main() {
#ifdef LOCALfreopen("C:\\Users\\admin\\CLionProjects\\Practice\\data.in", "r", stdin);freopen("C:\\Users\\admin\\CLionProjects\\Practice\\data.out", "w", stdout);
#endifios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int Case = 1;//cin >> Case;while (Case--) work();return 0;
}
/*_____ _ _ _ __ __| _ \ | | | | | | \ \ / /| |_| | | | | | | | \ \/ /| ___/ | | | | _ | | } {| | | |_| | | |_| | / /\ \|_| \_____/ \_____/ /_/ \_\
*/
關于代碼的億點點說明:
- 代碼的主體部分位于
void work()
函數(shù)中,另外會有部分變量申明、結(jié)構體定義、函數(shù)定義在上方。#pragma ...
是用來開啟 O2、O3 等優(yōu)化加快代碼速度。- 中間一大堆
#define ...
是我習慣上的一些宏定義,用來加快代碼編寫的速度。"debug.h"
頭文件是我用于調(diào)試輸出的代碼,沒有這個頭文件也可以正常運行(前提是沒定義DEBUG
宏),在程序中如果看到dbg(...)
是我中途調(diào)試的輸出的語句,可能沒刪干凈,但是沒有提交上去沒有任何影響。ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
這三句話是用于解除流同步,加快輸入cin
輸出cout
速度(這個輸入輸出流的速度很慢)。在小數(shù)據(jù)量無所謂,但是在比較大的讀入時建議加這句話,避免讀入輸出超時。如果記不下來可以換用scanf
和printf
,但使用了這句話后,cin
和scanf
、cout
和printf
不能混用。- 將
main
函數(shù)和work
函數(shù)分開寫純屬個人習慣,主要是為了多組數(shù)據(jù)。