gpt4 book ai didi

c++ - 如何将成对 vector 数组传递给 C++ 中的函数

转载 作者:行者123 更新时间:2023-11-30 03:25:10 24 4
gpt4 key购买 nike

我正在做一个名为 gridland metro 的 hackerrank 挑战,我已经为此努力了几个小时但没有成功。基本上,我查看了编辑器的解决方案并与我的代码进行了比较,发现我们的代码之间唯一的实质性区别是公认的解决方案不会将此数组“跟踪”传递给函数,而我这样做了。这是代码。

#include <bits/stdc++.h>

using namespace std;

int gridlandMetro(int n, int m, int k, map<int,int> mp, vector< pair<int,int> > track) {
long long total, non_emp, temp;
int sz1,sz2;
sz1 = mp.size();
for(int i=0;i<sz1;i++){
sort(track[i].begin(),track[i].end());
}
total = (long long)n*(long long)m;
non_emp = 0;
for(int i=0,p;i<sz1;i++){
p = 0;
sz2 = track[i].size();
for(int j=0;j<sz2;j++){
if(track[i][j].first <= p){
temp = track[i][j].second - p;
if(temp>0){
non_emp += temp;
}
}else{
non_emp += (track[i][j].second - track[i][j].first + 1);
}
p = max(p,track[i][j].second);
}
}
return total-non_emp;
}

vector< pair<int,int> > track[1003];
map<int,int>mp;

int main() {
int n,m,k,r,c1,c2;
cin >> n >> m >> k;
for(int track_i = 0;track_i < k;track_i++){
cin >> r >> c1 >> c2;
if(mp.find(r) == mp.end()){
mp[r] = mp.size();
}
r = mp[r];
track[r].push_back(make_pair(c1,c2));
}
int result = gridlandMetro(n, m, k, mp, track);
cout << result << endl;
return 0;
}

它适用于低输入,但在大输入时失败。我试过将 vector 作为指针传递,这似乎是人们在将数组传递给函数时所建议的。然而,这并没有奏效。我将把挑战的描述放在这里,但我认为这不是完全必要的。

The city of Gridland is represented as an matrix where the rows are numbered from to and the columns are numbered from to .

Gridland has a network of train tracks that always run in straight horizontal lines along a row. In other words, the start and end points of a train track are and , where represents the row number, represents the starting column, and represents the ending column of the train track.

The mayor of Gridland is surveying the city to determine the number of locations where lampposts can be placed. A lamppost can be placed in any cell that is not occupied by a train track.

Given a map of Gridland and its train tracks, find and print the number of cells where the mayor can place lampposts.

Note: A train track may (or may not) overlap other train tracks within the same row.

Input Format

The first line contains three space-separated integers describing the respective values of (the number of rows), (the number of columns), and (the number of train tracks). Each line of the subsequent lines contains three space-separated integers describing the respective values of , , and that define a train track.

Constraints

Output Format

Print a single integer denoting the number of cells where the mayor can install lampposts.

最佳答案

您已经在使用 std::vectorstd::pairstd::map,那为什么不用 std::数组

#include <array>

array<vector< pair<int,int> >, 1003> track;

int gridlandMetro(int n, int m, int k, map<int,int> mp, array<vector< pair<int,int> >, 1003>& track) {
//...
}

关于c++ - 如何将成对 vector 数组传递给 C++ 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49185877/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com