這題在學什麼
輸入先給查詢清單,需先保留,之後才讀R、C與貨架。讀貨架時建立編號到(列+1,欄+1)的映射;貨架編號皆唯一,因此沒有覆寫歧義。
逐筆查詢映射,有編號就輸出層數及層中位置,沒有則只輸出-1。重複查詢仍重複輸出,不能把查詢去重或排序。範例42出現兩次,第一與第五行同為1 2。
Step-by-step animation
範例香料反查
步驟 1 / 3
建立貨架映射
第一層17、42、6,第二層31、12、8。
42→1 2
8→2 3
31→2 1
- 本步結果
- 31→2 1
#include <iostream>
#include <vector>
#include <unordered_map>
#include <utility>
using namespace std;
int main(){int n;cin>>n;vector<long long>query(n);for(auto &x:query)cin>>x;int r,c;cin>>r>>c;
unordered_map<long long,pair<int,int>>pos;for(int i=1;i<=r;++i)for(int j=1;j<=c;++j){long long x;cin>>x;pos[x]={i,j};}
for(auto x:query){auto it=pos.find(x);if(it==pos.end())cout<<-1<<'\n';else cout<<it->second.first<<' '<<it->second.second<<'\n';}
}先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。