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

#include <cstdio>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  /* 2014 ??????1
  int a[2],ans;
  cin >> a[0] >> a[1];
  ans = a[0] * a[1];
  cout << ans << endl;
  */
  /* 2014 ??????2
  int a[6],ans = 0;
  cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4] >> a[5];
  if(a[0] + a[1] * 5 + a[2] * 10 + a[3] * 50 + a[4] * 100 + a[5] * 500 >= 1000)
  ans = 1; cout << ans << endl;
  */
  /* 2014 ??????3
  int a,i = 0;
  cin >> a;
  int b[a],c[a],ans[a];
  while(a > i){
          cin >> b[i] >> c[i];
          ans[i] =  b[i] % c[i];
          if(ans[i] == 0) ans[i] = c[i];
          i++;
  }
  i = 0;
  while(a > i){
  cout << ans[i] << endl;
  i++;
  }
  */
  /* 2014 ??????4*/
  int a[3], i = 1, keta;
  cin >> a[0];
  int n[a[0]], m[a[0]];
  while (a[0] >= i) {
    cin >> n[i] >> m[i];
    i++;
  }
  i = 1;
  cin >> a[1];
  int o[a[1]], p[a[1]];
  while (a[1] >= i) {
    cin >> o[i] >> p[i];
    i++;
  }
  i = 1;
  int ii, c, aa, iii, aaa;
  aa = a[0] + a[1];
  a[3] = a[1];
  aaa = aa;
  int ans[aa][2], hh[aa], mm[aa], chk, count;
  count = 1;
  while (aa >= i) {
    ii = 1;
    chk = 1;
    if (a[0] >= i) {
      while (i - 1 >= ii) {
        if (n[i] == hh[ii] and m[i] == mm[ii]) {
          chk = 0;
          // cout << n[i] << hh[ii] << m[i] << mm[ii];
        }
        ii++;
      }
      if (chk == 1) {
        hh[count] = n[i];
        mm[count] = m[i];
        count++;
      }
    } else {
      while (i - 1 >= ii) {
        if (o[i - a[0]] == hh[ii] and p[i - a[0]] == mm[ii]) {
          chk = 0;
          // cout << o[i] << hh[ii] << p[i] << mm[ii];
        }
        ii++;
      }
      if (chk == 1) {
        hh[count] = o[i - a[0]];
        mm[count] = p[i - a[0]];
        count++;
      }
    }
    // cout << hh[i] << " " << hh[ii] << " " << mm[i] << " " << mm[ii] << " " <<
    // chk << endl;
    i++;
  }
  aa = count - 1;

  i = 1;
  while (aa >= i) {
    ii = 1;
    c = 1;
    while (aa >= ii) {
      if (hh[i] > hh[ii] or hh[i] == hh[ii] and mm[i] > mm[ii] and i != ii) {
        c++;
      }
      ii++;
    }
    ans[c][0] = hh[i];
    ans[c][1] = mm[i];
    // cout << c  << endl;
    i++;
  }
  i = 1;
  while (aa >= i) {
    if (aa == i) {
      if (ans[i][1] < 10) {
        cout << ans[i][0] << ":"
             << "0" << ans[i][1] << endl;
        ;
      } else {
        cout << ans[i][0] << ":" << ans[i][1] << endl;
        ;
      }

    } else {
      if (ans[i][1] < 10) {
        cout << ans[i][0] << ":"
             << "0" << ans[i][1] << " ";
      } else {
        cout << ans[i][0] << ":" << ans[i][1] << " ";
      }
    }
    i++;
  }

  /*int N,M,p,i = 1,min,ii = 1;
  cin >> M >> N >> p;
  int d[M],ans = 0,a;
  while(M > i){
          cin >> d[i];
          i++;
  }
  i = 1;
  while(M > i){
  min = N + 1;
          while(M > ii){
                  if(d[ii] <= N){
                          a = d[ii] + N -  p ;
                          if(min > p - d[ii] and 0 < p - d[ii]){
                                  min = p - d[ii];
                                  d[ii] = N + 1;
                          }
                          if(min > d[ii] - p and 0 < d[ii] - p){
                                  min = p - d[ii];
                                  d[ii] = N + 1;
                          }
                          if(min > a and 0 >= a){
                                  min = a;
                                  d[ii] = N + 1;
                          }
                  }
                  ii++;
          }
          ans = ans + 100 * min;
  i++;

  }
  cout << ans << endl;*/
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  /* 2014 ??????1
  int a[2],ans;
  cin >> a[0] >> a[1];
  ans = a[0] * a[1];
  cout << ans << endl;
  */
  /* 2014 ??????2
  int a[6],ans = 0;
  cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4] >> a[5];
  if(a[0] + a[1] * 5 + a[2] * 10 + a[3] * 50 + a[4] * 100 + a[5] * 500 >= 1000)
  ans = 1; cout << ans << endl;
  */
  /* 2014 ??????3
  int a,i = 0;
  cin >> a;
  int b[a],c[a],ans[a];
  while(a > i){
          cin >> b[i] >> c[i];
          ans[i] =  b[i] % c[i];
          if(ans[i] == 0) ans[i] = c[i];
          i++;
  }
  i = 0;
  while(a > i){
  cout << ans[i] << endl;
  i++;
  }
  */
  /* 2014 ??????4*/
  int a[3], i = 1, keta;
  cin >> a[0];
  int n[a[0]], m[a[0]];
  while (a[0] >= i) {
    cin >> n[i] >> m[i];
    i++;
  }
  i = 1;
  cin >> a[1];
  int o[a[1]], p[a[1]], oo[a[1]], pp[a[1]], nn[a[0]], mm[a[0]];
  while (a[1] >= i) {
    cin >> o[i] >> p[i];
    i++;
  }
  i = 1;
  int ii, c, aa, iii, aaa, ix, check;
  aa = a[0] + a[1];
  aaa = aa;
  int ans[aa][2];
  a[3] = a[0];
  while (aa >= i) {
    if (a[0] >= i) {
      nn[i] = n[i];
      mm[i] = m[i];
    } else {
      oo[i - a[0]] = o[i - a[0]];
      pp[i - a[0]] = p[i - a[0]];
    }
    i++;
  }
  i = 1;
  while (a[0] >= i) {
    ii = 1;

    while (aa >= ii) {
      // cout << "while "<< ii << endl;
      if (a[0] >= ii) { // n,m n,m
        if (n[i] == n[ii] and m[i] == m[ii] and i != ii and n[i] != 1000) {
          // cout << ii << "[N1]" << n[i] << "[N2]" << n [ii] << "[M1]" << m[i]
          // << "[M2]" << m [ii] << endl;
          iii = 1;
          while (a[0] >= iii) {
            if (iii > ii) {
              n[iii - 1] = n[iii];
              m[iii - 1] = m[iii];
            }
            if (iii == a[0]) {
              n[a[0]] = 1000;
              m[a[0]] = 1000;
            }
            iii++;
          }
          aaa = aaa - 1;
          ii--;
          a[3] = a[0] - 1;
        }
      } else { // n,m o,p

        // cout << n[i] << " " <<  << " " << m[i] << " " << p[ii - a[0]] <<
        // endl;
        if (n[i] == o[ii - a[0]] and m[i] == p[ii - a[0]] and n[i] != 1000) {
          // cout << ii << "[N1]" << n[i] << "[O2]" << o[ii - a[0]] << "[M1]" <<
          // m[i] << "[P2]" << p[ii - a[0]] << endl;
          iii = 1;
          while (a[1] >= iii) {
            // cout << "OK";
            if (iii > ii - a[0]) {
              o[iii - 1] = o[iii];
              p[iii - 1] = p[iii];
            }
            if (iii == a[1]) {
              o[a[1]] = 1000;
              p[a[1]] = 1000;
            }
            iii++;
          }
          aaa = aaa - 1;
          ii--;
        }
      }
      ii++;
    }
    i++;
  }
  i = 1;
  while (a[1] >= i) {
    ii = 1;
    while (aa >= ii) {
      // cout << "while "<< ii << endl;
      ix = 1;
      check = 0;
      while (a[0] >= ix) {
        if (o[i] == n[ix] and p[i] == m[ix]) {
          check = 1;
        }
        ix++;
      }
      if (o[i] == o[ii] and p[i] == p[ii] and i != ii and check == 0 and
          o[i] != 1000) {
        // cout << ii << "[O]" << o[i]  << "[O2]" << o [ii] << "[P1]" << p[i] <<
        // "[P2]" << p [ii] << i << ii <<  endl;
        iii = 1;
        while (a[1] >= iii) {
          if (iii > ii) {
            o[iii - 1] = p[iii];
            p[iii - 1] = p[iii];
          }
          if (iii == a[1]) {
            o[a[1]] = 1000;
            p[a[1]] = 1000;
          }
          iii++;
        }
        aaa = aaa - 1;
        ii--;
        // cout << "hehe" << endl;
      }

      ii++;
    }
    i++;
  }

  // cout << oo[3] << "CKH" << pp[3]<< endl;
  aa = aaa;
  a[0] = a[3];
  i = 1;
  // cout << aa << endl;
  // cout << a[0] << endl;

  /*2 0 0 21 0
  3 7 30 21 0 23 7*/
  while (aa >= i) {
    ii = 1;
    c = 0;
    if (a[0] >= i) {
      while (aa >= ii) {
        if (a[0] >= ii) {
          if (n[i] > n[ii] or n[i] == n[ii] and m[i] > m[ii]) {
            c++;
          }
        } else {
          if (n[i] > o[ii - a[0]] or
              n[i] == o[ii - a[0]] and m[i] > p[ii - a[0]]) {
            c++;
          }
        }
        ii++;
      }
      ans[c][0] = n[i];
      ans[c][1] = m[i];
      // cout << c << "a" << endl;
    } else {
      while (aa >= ii) {
        if (a[0] >= ii) {
          if (o[i - a[0]] > n[ii] or
              o[i - a[0]] == n[ii] and p[i - a[0]] > m[ii]) {
            c++;
            // cout << c << "ka" << endl;
          }
        } else {
          if (o[i - a[0]] > o[ii - a[0]] or
              o[i - a[0]] == o[ii - a[0]] and p[i - a[0]] > p[ii - a[0]]) {
            c++;
            // cout << c << "kc" << endl;
          }
        }
        ii++;
      }
      // cout << c << "b" << endl;
      ans[c][0] = o[i - a[0]];
      ans[c][1] = p[i - a[0]];
    }
    // cout << c << endl;

    i++;
  }
  i = 0;
  while (aa > i) {
    keta = 0;
    if (aa - 1 == i) {
      if (ans[i][1] < 10) {
        cout << ans[i][0] << ":" << keta << ans[i][1] << endl;
        ;
      } else {
        cout << ans[i][0] << ":" << ans[i][1] << endl;
      }
    } else {
      if (ans[i][1] < 10) {
        cout << ans[i][0] << ":" << keta << ans[i][1] << " ";
      } else {
        cout << ans[i][0] << ":" << ans[i][1] << " ";
      }
    }
    i++;
  }

  /*int N,M,p,i = 1,min,ii = 1;
  cin >> M >> N >> p;
  int d[M],ans = 0,a;
  while(M > i){
          cin >> d[i];
          i++;
  }
  i = 1;
  while(M > i){
  min = N + 1;
          while(M > ii){
                  if(d[ii] <= N){
                          a = d[ii] + N -  p ;
                          if(min > p - d[ii] and 0 < p - d[ii]){
                                  min = p - d[ii];
                                  d[ii] = N + 1;
                          }
                          if(min > d[ii] - p and 0 < d[ii] - p){
                                  min = p - d[ii];
                                  d[ii] = N + 1;
                          }
                          if(min > a and 0 >= a){
                                  min = a;
                                  d[ii] = N + 1;
                          }
                  }
                  ii++;
          }
          ans = ans + 100 * min;
  i++;

  }
  cout << ans << endl;*/
}