简体   繁体   中英

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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