Problem p00120 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main(void) {

  int N;

  string s;
  while (getline(cin, s)) {
    long long int D = 10000;
    vector<double> A, B;
    istringstream ss(s);
    int t;
    ss >> N;
    while (ss >> t)
      A.push_back(t);

    // for(int i = 0;i < A.size();i++)cout<<A[i]<<endl;
    double best = 1000000;
    double ans = 1000000;
    while (D--) {
      vector<double> V1, V2;
      srand(time(NULL) + D);
      V1 = A;
      double sum = 0;
      int x = rand() % A.size();
      int y = rand() % A.size();
      swap(V1[x], V1[y]);

      for (int i = 0; i < V1.size() - 1; i++) {
        double R1 = V1[i] + V1[i + 1];
        double R2 = V1[i] - V1[i + 1];
        sum += sqrt((R1 * R1) - (R2 * R2));
      }

      sum += V1[0] + V1[V1.size() - 1];
      double e = pow((sum / ans), 75);
      double z = (double)((int)(rand() % 1000000)) / 1000000;
      if (z >= (1 - (1 / e)))
        ans = sum, A = V1; /*
if(D%10000 == 0){
for(int i = 0;i < A.size();i++){
  cout<<A[i]<<" ";
}
cout<<" : ";
for(int i = 0;i < B.size();i++){
  cout<<B[i]<<" ";
}
cout<<endl;
}*/
      if (sum < best)
        best = sum, B = V1;
    }
    int siz = 0;
    for (int i = 0; i < 1000; i++) {
      if (best <= (double)i) {
        siz = i;
        // cout<<siz<<endl;
        break;
      }
    }
    if (siz <= N)
      cout << "OK" << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main(void) {

  int N;

  string s;
  while (getline(cin, s)) {
    long long int D = 100000;
    vector<double> A, B;
    istringstream ss(s);
    int t;
    ss >> N;
    while (ss >> t)
      A.push_back(t);

    // for(int i = 0;i < A.size();i++)cout<<A[i]<<endl;
    double best = 1000000;
    double ans = 1000000;
    while (D--) {
      vector<double> V1, V2;
      srand(time(NULL) + D);
      V1 = A;
      double sum = 0;
      int x = rand() % A.size();
      int y = rand() % A.size();
      swap(V1[x], V1[y]);

      for (int i = 0; i < V1.size() - 1; i++) {
        double R1 = V1[i] + V1[i + 1];
        double R2 = V1[i] - V1[i + 1];
        sum += sqrt((R1 * R1) - (R2 * R2));
      }

      sum += V1[0] + V1[V1.size() - 1];
      double e = pow((sum / ans), 75);
      double z = (double)((int)(rand() % 1000000)) / 1000000;
      if (z >= (1 - (1 / e)))
        ans = sum, A = V1; /*
if(D%10000 == 0){
for(int i = 0;i < A.size();i++){
  cout<<A[i]<<" ";
}
cout<<" : ";
for(int i = 0;i < B.size();i++){
  cout<<B[i]<<" ";
}
cout<<endl;
}*/
      if (sum < best)
        best = sum, B = V1;
    }
    int siz = 0;
    for (int i = 0; i < 1000; i++) {
      if (best <= (double)i) {
        siz = i;
        // cout<<siz<<endl;
        break;
      }
    }
    if (siz <= N)
      cout << "OK" << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}