在本機上測試OK 但是丟到 OJ就會
記憶體區段錯誤!Segmentation fault (core dumped)
反覆測試 下面是Code
求大神幫忙看
#include <bits/stdc++.h>
using namespace std;
int main(){
int m,n;
cin >> m >> n;
int arr[m];
map<int,int> color_count;
int beautiful_value = 0;
for(int i = 0 ; i < m-1 ; i++ ){
cin >> arr[i];
// Segmentation fault (core dumped) 記憶體區段錯誤!開始的地方
if(color_count.find(arr[i]) == color_count.end()){ //no this value
color_count.insert(std::pair<int, int> (arr[i],0)) ;
}
color_count[arr[i]] = color_count[arr[i]]+1;
//到這裡 這一段程式會出現 記憶體區段錯誤!Segmentation fault (core dumped)
}
cin >> arr[m-1];
if(color_count.find(arr[m-1]) == color_count.end()) //no this value
color_count[arr[m-1]] = 1 ;
else
color_count[arr[m-1]]++;
if(color_count.size() == m)
beautiful_value++;
int v = (color_count[arr[0]] - 1 );
color_count[arr[0]] = v;
if(!v)
color_count.erase(arr[0]);
for(int i = m ; i < n ; i++ ){
cin >> arr[i];
if(color_count.find(arr[i]) == color_count.end()) //no this value
color_count[arr[i]] = 1 ;
else
color_count[arr[i]]++;
if(color_count.size() == m){
beautiful_value++;
}
int v = (color_count[arr[i-m+1]] - 1 );
color_count[arr[i-m+1]] = v;
if(!v)
color_count.erase(arr[i-m+1]);
}
cout << beautiful_value << endl;
return 0 ;
}