CCF-CSP认证考试 202312-1 仓库规划 100分题解

更多 CSP 认证考试题目题解可以前往:CCF-CSP 认证考试真题题解


原题链接: 202312-1 仓库规划

时间限制: 1.0s
内存限制: 512.0MB

问题描述

西西艾弗岛上共有 n n n 个仓库,依次编号为 1 ⋯ n 1 \cdots n 1n。每个仓库均有一个 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 1in)包含 m m m 个整数,表示仓库 i i i 的位置编码。

输出格式

输出到标准输出。

输出共 n n n 行。

i i i 行( 1 ≤ i ≤ n 1 \le i \le n 1in)输出一个整数,表示仓库 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<m10 0 < n ≤ 1000 0 < n \le 1000 0<n1000,且位置编码中的所有元素均为绝对值不大于 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 "debug.h"
#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\\data.in", "r", stdin);
    freopen("C:\\Users\\admin\\CLionProjects\\Practice\\data.out", "w", stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int Case = 1;
    //cin >> Case;
    while (Case--) work();
    return 0;
}
/*
     _____   _   _       _  __    __
    |  _  \ | | | |     | | \ \  / /
    | |_| | | | | |     | |  \ \/ /
    |  ___/ | | | |  _  | |   }  {
    | |     | |_| | | |_| |  / /\ \
    |_|     \_____/ \_____/ /_/  \_\
*/

关于代码的亿点点说明:

  1. 代码的主体部分位于 void work() 函数中,另外会有部分变量申明、结构体定义、函数定义在上方。
  2. #pragma ... 是用来开启 O2、O3 等优化加快代码速度。
  3. 中间一大堆 #define ... 是我习惯上的一些宏定义,用来加快代码编写的速度。
  4. "debug.h" 头文件是我用于调试输出的代码,没有这个头文件也可以正常运行(前提是没定义 DEBUG 宏),在程序中如果看到 dbg(...) 是我中途调试的输出的语句,可能没删干净,但是没有提交上去没有任何影响。
  5. ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 这三句话是用于解除流同步,加快输入 cin 输出 cout 速度(这个输入输出流的速度很慢)。在小数据量无所谓,但是在比较大的读入时建议加这句话,避免读入输出超时。如果记不下来可以换用 scanfprintf,但使用了这句话后,cinscanfcoutprintf 不能混用。
  6. main 函数和 work 函数分开写纯属个人习惯,主要是为了多组数据。
  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值