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

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <unordered_map>
using namespace std;

const int tablesize = 3000;

/*
class Data{
public:
  bool flag;
  //list<int> min;
  //list<int> mid;
  bool minflag[1000];
  bool midflag[1000];

};*/

// Data table[tablesize];

int main() {

  int n, p[3], count, psum;
  unordered_map<string, int> mp;
  const int defaultnum = mp["d"];
  int ok = (defaultnum == 10) ? 0 : 10;

  count = 0;

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> p[0] >> p[1] >> p[2];
    sort(p, p + 3);
    psum = p[0] + p[1] + p[2];
    string keystring = "";
    stringstream ss;
    for (int j = 0; j < 3; j++) {
      ss << p[j];
    }

    ss >> keystring;

    if (mp[keystring] == ok) {
      count++;
    } else {
      mp[keystring] = ok;
    }

    /*  Data& tmp = table[psum];
      int pmin=p[0],pmid=p[1];
      if(!tmp.flag){
        tmp.flag = true;
        tmp.minflag[pmin] = true;
        tmp.midflag[pmid] = true;
        //tmp.min.push_back(pmin);
        //tmp.mid.push_back(pmid);
      }
      else{
        bool ok = false;

        if(tmp.minflag[pmin] && tmp.midflag[pmid]){
          count++;
        }
        else{
          tmp.minflag[pmin] = true;
          tmp.midflag[pmid] = true;
        }


        /*list<int>::iterator minit;
        list<int>::iterator midit;
        for(minit = tmp.min.begin(); minit != tmp.min.end(); minit++){
          if(*minit == pmin){
            ok = true;
            break;
          }
        }

        if(ok){

          ok = false;

          for(midit = tmp.mid.begin(); minit != tmp.mid.end(); midit++){
            if(*midit == pmid){
              ok = true;
              break;
            }
          }

          if(ok){
            count++;
            continue;
          }
        }

        tmp.min.push_back(pmin);
        tmp.mid.push_back(pmid);

      }*/
  }

  cout << count << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <list>
using namespace std;

const int tablesize = 3000;

class Data {
public:
  bool flag;
  list<int> min;
  list<int> mid;
};

Data table[tablesize];

int main() {

  for (int i = 0; i < tablesize; i++) {
    table[i].flag = false;
  }

  int n, p[3], count, psum;

  count = 0;

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> p[0] >> p[1] >> p[2];
    sort(p, p + 3);
    psum = p[0] + p[1] + p[2];
    Data &tmp = table[psum];
    int pmin = p[0], pmid = p[1];
    if (!tmp.flag) {
      tmp.flag = true;
      tmp.min.push_back(pmin);
      tmp.mid.push_back(pmid);
    } else {
      bool ok = false;
      list<int>::iterator minit;
      list<int>::iterator midit;
      for (minit = tmp.min.begin(); minit != tmp.min.end(); minit++) {
        if (*minit == pmin) {
          ok = true;
          break;
        }
      }

      if (ok) {

        ok = false;

        for (midit = tmp.mid.begin(); minit != tmp.mid.end(); midit++) {
          if (*midit == pmid) {
            ok = true;
            break;
          }
        }

        if (ok) {
          count++;
          continue;
        }
      }

      tmp.min.push_back(pmin);
      tmp.mid.push_back(pmid);
    }
  }

  cout << count << endl;

  return 0;
}