0

我正在解决一个问题,但我遇到了一个奇怪的错误。在 eval 函数中,我收到分数作为对 2d 向量的引用。对于 eval 的前多次调用,会保留 score 的内容。然而,在某些时候vector<int>,分数中的子向量(问题,但显然代码会变慢。像这样:

...
int eval(vector<int>&team, vector<vector<int>> scores){
...
void rec(int idx, int d, vector<int>& team, vector<vector<int>> scores){
...

以下测试数据会出现问题。

22 6 2000
0 0 0 1 0 0
0 0 0 0 0 1
0 9 0 0 0 0
6 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 10 0
0 6 0 0 0 0
0 0 1 0 0 0
2 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 10
0 0 10 0 0 0
0 0 0 0 1 0
0 0 0 10 0 0
6 0 0 0 0 0
1 0 0 0 0 0
0 5 0 0 0 0
0 6 0 0 0 0
1 0 0 0 0 0
10 0 0 0 0 0
0 10 0 0 0 0
0 4 0 0 0 0

这是所有的代码。我希望你能帮助 med 解决这个问题,并提供一些关于为什么会发生这个错误的见解。这也是我在 stackoverflow 上的第一篇文章(如果我能以任何方式改进这篇文章,请告诉我 :))。

编辑:我最初将分数作为全局变量(没有将其作为参考传递),但这给出了完全相同的错误。

#include<bits/stdc++.h>
using namespace std;
int N, K, C;
vector<int> teams;
int eval(vector<int>&team, vector<vector<int>>& scores){
  int s = 0;
  for(int i = 0; i < K; i++){
    int m = 0;
    for(int j = 0; j < K; j++){
      cout << team[j] << scores[team[j]].size() << " " << i << endl;
      m = max(scores[team[j]][i], m);
    }
    s += m;
  }
  return s;
}
void rec(int idx, int d, vector<int>& team, vector<vector<int>>& scores){
  if(d == K){
    teams.push_back(eval(team, scores));
  }
  for(int i = idx; i < N; i++){
    team[d] = i;
    rec(i+1, d+1, team, scores);
  }
}
int main(){
  cin >> N >> K >> C;
  vector<vector<int>> scores = vector<vector<int>> (N, vector<int> (K, 0));
  for(int i = 0; i < N; i++){
    for(int j = 0; j < K; j++){
      cin >> scores[i][j];
    }
  }
  //for(int i = 0; i < N; i++) cout << i << " "<< scores[i].size() << endl;
  vector<int> team (K);
  rec(0, 0, team, scores);
  sort(teams.rbegin(), teams.rend());
  cout << teams[C-1] << endl;
}
4

0 回答 0