这是我通过的数据透视表(关于Kattis的问题:https://open.kattis.com/problems/pivot),但我想知道你们会如何提高效率。我最初使用向量(仍然有效),但认为数组会更好,因此我将其更改为该向量。
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
int main() {
set <int> s;
int n;
cin >> n;
int v[n];
signed int val;
for (int i = 0; i < n; i++){
cin >> val;
v[i] = val;
s.insert(val);
}
int count = 0;
int i = 0;
int max = v[0];
for (int x : s){
if (v[i] == x && max <= x) {
count++;
}
if (v[i] > max){
max = v[i];
}
i++;
}
cout << count << endl;
}
您可以使用数组提高代码效率,但会限制您的边界。如果将其设为动态,则会在向量中占用更多空间。因此,如果您需要效率,请使用数组。但是根据您的要求,可以在需要的地方添加数组和向量。尝试使用较少的向量