更多 CSP 认证考试题目题解可以前往:CCF-CSP 认证考试真题题解
原题链接: 202312-1 仓库规划
时间限制: 1.0s
内存限制: 512.0MB
问题描述
西西艾弗岛上共有 n n n 个仓库,依次编号为 1 ⋯ n 1 \cdots n 1⋯n。每个仓库均有一个 m m m 维向量的位置编码,用来表示仓库间的物流运转关系。
具体来说,每个仓库 i i i 均可能有一个上级仓库 j j j,满足:仓库 j j j 位置编码的每一维均大于仓库 i i i 位置编码的对应元素。比如编码为 ( 1 , 1 , 1 ) (1, 1, 1) (1,1,1) 的仓库可以成为 ( 0 , 0 , 0 ) (0, 0, 0) (0,0,0) 的上级,但不能成为 ( 0 , 1 , 0 ) (0, 1, 0) (0,1,0) 的上级。如果有多个仓库均满足该要求,则选取其中编号最小的仓库作为仓库 i i i 的上级仓库;如果没有仓库满足条件,则说明仓库 i i i 是一个物流中心,没有上级仓库。
现给定 n n n 个仓库的位置编码,试计算每个仓库的上级仓库编号。
输入格式
从标准输入读入数据。
输入共 n + 1 n+1 n+1 行。
输入的第一行包含两个正整数 n n n 和 m m m,分别表示仓库个数和位置编码的维数。
接下来 n n n 行依次输入 n n n 个仓库的位置编码。其中第 i i i 行( 1 ≤ i ≤ n 1 \le i \le n 1≤i≤n)包含 m m m 个整数,表示仓库 i i i 的位置编码。
输出格式
输出到标准输出。
输出共 n n n 行。
第 i i i 行( 1 ≤ i ≤ n 1 \le i \le n 1≤i≤n)输出一个整数,表示仓库 i i i 的上级仓库编号;如果仓库 i i i 没有上级,则第 i i i 行输出 0 0 0。
样例输入
4 2
0 0
-1 -1
1 2
0 -1
样例输出
3
1
0
3
样例解释
对于仓库 2 : ( − 1 , − 1 ) 2:(-1, -1) 2:(−1,−1) 来说,仓库 1 : ( 0 , 0 ) 1:(0, 0) 1:(0,0) 和仓库 3 : ( 1 , 2 ) 3:(1, 2) 3:(1,2) 均满足上级仓库的编码要求,因此选择编号较小的仓库 1 1 1 作为其上级。
子任务
50 % 50\% 50% 的测试数据满足 m = 2 m = 2 m=2;
全部的测试数据满足 0 < m ≤ 10 0 < m \le 10 0<m≤10、 0 < n ≤ 1000 0 < n \le 1000 0<n≤1000,且位置编码中的所有元素均为绝对值不大于 1 0 6 10^6 106 的整数。
题解
对于每个仓库,由于需要寻找编号最小的上级仓库,从小到大依次判断每个仓库是否为其上级仓库,如果是结束当前循环,否则继续判断下一个仓库。
时间复杂度: O ( n 2 m ) \mathcal{O}(n^2m) O(n2m)。
参考代码(15ms,2.980MB)
/*
Created by Pujx on 2024/2/2.
*/
#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 i = 1; i <= n; i++) cin >> a[i]
#define cinstl(a) for (auto& x : a) cin >> x;
#define coutarr(a, n) for (int i = 1; i <= n; i++) cout << a[i] << " \n"[i == n]
#define coutstl(a) for (const auto& x : a) cout << x << ' '; 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 ""
#else
#define dbg(...) void(0)
#endif
const 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;
vector<vector<int>> v(n + 1, vector<int>(m));
for (int i = 1; i <= n; i++) cinstl(v[i]);
for (int i = 1, j; i <= n; i++) {
for (j = 1; j <= n; j++) {
int cnt = 0;
for (int k = 0; k < m; k++) cnt += v[i][k] < v[j][k];
if (cnt == m) break;
}
cout << (j > n ? 0 : j) << endl;
}
}
signed main() {
#ifdef LOCAL
freopen("C:\\Users\\admin\\CLionProjects\\Practice\\", "r", stdin);
freopen("C:\\Users\\admin\\CLionProjects\\Practice\\", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int Case = 1;
//cin >> Case;
while (Case--) work();
return 0;
}
/*
_____ _ _ _ __ __
| _ \ | | | | | | \ \ / /
| |_| | | | | | | | \ \/ /
| ___/ | | | | _ | | } {
| | | |_| | | |_| | / /\ \
|_| \_____/ \_____/ /_/ \_\
*/
关于代码的亿点点说明:
- 代码的主体部分位于
void work()
函数中,另外会有部分变量申明、结构体定义、函数定义在上方。#pragma ...
是用来开启 O2、O3 等优化加快代码速度。- 中间一大堆
#define ...
是我习惯上的一些宏定义,用来加快代码编写的速度。""
头文件是我用于调试输出的代码,没有这个头文件也可以正常运行(前提是没定义DEBUG
宏),在程序中如果看到dbg(...)
是我中途调试的输出的语句,可能没删干净,但是没有提交上去没有任何影响。ios::sync_with_stdio(false); (0); (0);
这三句话是用于解除流同步,加快输入cin
输出cout
速度(这个输入输出流的速度很慢)。在小数据量无所谓,但是在比较大的读入时建议加这句话,避免读入输出超时。如果记不下来可以换用scanf
和printf
,但使用了这句话后,cin
和scanf
、cout
和printf
不能混用。- 将
main
函数和work
函数分开写纯属个人习惯,主要是为了多组数据。