밑에 SET 질문한 사람입니다.

  • kun452
    kun452
     while(num--){
            int n;
            cin >> n;
    
            set < pair<int, int> > S;
            set < pair<int, int> >::iterator it;
            set < pair<int, int> >::iterator it2;
    
            vector <int> x;
            vector <int> y;
    
            int a, b;
            for(int i=0; i<n; i++){
                scanf("%d%d", &a, &b);
                x.push_back(a);
                y.push_back(b);
                S.insert(make_pair(a, b));
            }
    
            int temp_x;
            int temp_y;
            int MAX = 0;
    
            for(int i=0; i<n; i++){
                for(int j=i+1; j<n; j++){
                    temp_x = x[j] - x[i];
                    temp_y = y[j] - y[i];
    
                    it = S.find(make_pair(x[j]-temp_y, y[j] + temp_x));
                    it2 = S.find(make_pair(x[j]-temp_y -temp_x, y[j] + temp_x -temp_y));
                    //it = S.find(make_pair(3, 10));
                    if(it != S.end() && it2 != S.end()){
                        MAX = max(MAX, (int)pow((double)(temp_x), 2) + (int)pow((double)(temp_y), 2));
                        continue;
                    }
    
                    it = S.find(make_pair(x[j]+temp_y, y[j] -temp_x));
                    it2 = S.find(make_pair(x[j]+temp_y-temp_x, y[j]-temp_x-temp_y));
                    if(it != S.end() && it2 != S.end())
                        MAX = max(MAX, (int)pow((double)(temp_x), 2) + (int)pow((double)(temp_y), 2));
    
                }
            }
    
            cout << MAX << endl;
        }
    

    이 코드의 시간복잡도가 제가 생각할때는 n^2 * log(n) 으로 계산되었는데 n이 최대 3000인데 시간초과가 나더라구요;;
    제가 생각한게 틀린가요?


    8년 전
1개의 댓글이 있습니다.
  • iyaa
    iyaa

    N이 3천일때, N*N*log(N)>=1억
    1억은 얼추 1초로 잡는데, num번 만큼 테스트를 수행하니, 테스트케이스가 1이 아닌 이상은 시간초과가 날 가능성이 충분합니다.


    8년 전 link
  • 정회원 권한이 있어야 커멘트를 다실 수 있습니다. 정회원이 되시려면 온라인 저지에서 5문제 이상을 푸시고, 가입 후 7일 이상이 지나셔야 합니다. 현재 문제를 푸셨습니다.