简体   繁体   English

有什么办法可以提高我的效率?

[英]Is there any way I could Improve my efficiency [on hold]

This is my code for pivot (Problem on Kattis: https://open.kattis.com/problems/pivot ) that passes, but I was wondering what you guys would do to improve efficiency. 这是我通过的透视代码(关于Kattis的问题: https ://open.kattis.com/problems/pivot),但是我想知道你们会如何提高效率。 I initially used a vector (still worked) but thought that an array would be better so I changed it to that. 我最初使用向量(仍然有效),但认为数组会更好,因此我将其更改为该向量。

#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;
}

You can use an array to make your code efficient but it limits your boundaries. 您可以使用数组来提高代码效率,但会限制您的边界。 If you make it dynamic it consumes more spaces in vector. 如果将其设为动态,则会在向量中占用更多空间。 So if you need efficiency so go with an array. 因此,如果您需要效率,请使用数组。 But as per your requirement add array and vector both wherever it is needed. 但是根据您的要求,可以在需要的地方添加数组和向量。 try to use less vector 尝试使用更少的向量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM