Problem p03030 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p;
const int INF = (1 << 28);
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1},
          Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
int n;
vector<pair<string, pair<int, int>>> s;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s1;
    int a;
    cin >> s1 >> a;
    s.push_back(make_pair(s1, make_pair(a, i + 1)));
  }
  sort(s.begin(), s.end());
  int temp;
  bool c = false;
  for (int i = 0; i < n; i++) {
    if (s[i].first == s[i + 1].first && c == false) {
      temp = i;
      c = true;
    } else if (s[i].first == s[i + 1].first && c == true) {
    } else if (s[i].first != s[i + 1].first && c == true) {
      for (int j = i; j >= temp; j--) {
        cout << s[j].second.second << "\n";
      }
      c = false;
    } else {
      cout << s[i].second.second << "\n";
    }
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p;
const int INF = (1 << 28);
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1},
          Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
int n;
vector<pair<string, pair<int, int>>> s;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s1;
    int a;
    cin >> s1 >> a;
    s.push_back(make_pair(s1, make_pair(a, i + 1)));
  }
  sort(s.begin(), s.end());
  int temp;
  bool c = false;
  for (int i = 0; i < n; i++) {
    if (i == n - 1 && c == true) {
      for (int j = n - 1; j >= temp; j--) {
        cout << s[j].second.second << "\n";
      }
    } else if (i == n - 1 && c == false) {
      cout << s[i].second.second << "\n";
    } else if (s[i].first == s[i + 1].first && c == false) {
      temp = i;
      c = true;
    } else if (s[i].first == s[i + 1].first && c == true) {
    } else if (s[i].first != s[i + 1].first && c == true) {
      for (int j = i; j >= temp; j--) {
        cout << s[j].second.second << "\n";
      }
      c = false;
    } else {
      cout << s[i].second.second << "\n";
    }
  }
}