-2
I created the code below to solve the problem Vault, of the 2017 OBI. It works, but in 60% of cases it exceeds the time limit, as I do to optimize it?
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int N, M, pAnt, pAtual, resp[10]={0};
cin >> N >> M;
int seq[N];
for(int i=0;i<N;i++){
cin >> seq[i];
}
cin >> pAnt;
resp[seq[pAnt-1]]++;
for(int i=1;i<M;i++){
cin >> pAtual;
if(pAnt<pAtual)
for(int j=0;j<10;j++)
resp[j]+=count(seq+pAnt, seq+pAtual, j);
else
for(int j=0;j<10;j++)
resp[j]+=count(seq+pAtual-1, seq+pAnt-1, j);
pAnt = pAtual;
}
cout << resp[0] << " " << resp[1] << " " << resp[2] << " " << resp[3] << " " << resp[4] << " ";
cout << resp[5] << " " << resp[6] << " " << resp[7] << " " << resp[8] << " " << resp[9] << endl;
return 0;
}
Instead of for each digit you count how many times it occurs in the range it would not be more practical to sweep the range once and increment each occurrence of the digit?
– anonimo