Problem p03688 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#define gc getchar()
#define pc putchar
#define rep                                                                    \
  for (a, b, c)                                                                \
    for (int a = b; a <= c; ++a)
#define per                                                                    \
  for (a, b, c)                                                                \
    for (int a = b; a >= c; --a)
inline int read() {
  int x = 0, f = 1;
  char c = gc;
  while (c < '0' || c > '9')
    c = gc;
  while (c <= '9' && c >= '0')
    x = x * 10 + c - '0', c = gc;
  return x * f;
}
void print(int x) {
  if (x < 0) {
    pc('-');
    x = -x;
  }
  if (x >= 10)
    print(x / 10);
  pc(x % 10 + '0');
}
const int maxn = 1000007;
bool solve() {
  static int a[maxn];
  int n = read();
  int mx = 0, mn = 0x3f3f3f3f;
  for (int i = 1; i <= n; ++i) {
    a[i] = read();
    mx = std::max(mx, a[i]);
    mn = std::min(mn, a[i]);
  }
  if (mx - mn >= 2)
    return true;
  //	if(mx == n - 1 && mn != n - 1) return true;
  int c1 = 0, c2 = 0;
  for (int i = 1; i <= n; ++i) {
    if (a[i] == mx)
      c1++;
    else
      c2++;
  }
  int sum = mx;
  if (mx != mn) {
    int res = sum - c2;
    if (res <= 0)
      return true;
    if (c1 < res * 2)
      return true;
  }
  if (mx == mn) {
    sum++;
    if (mx == n - 1)
      return false;
    if (mx * 2 > n) {
      return true;
    }
  }
  return false;
}
int main() {

  freopen("hat.in", "r", stdin);
  freopen("hat.out", "w", stdout);
  puts(solve() ? "No" : "Yes");
}
/*
3
3
1 2 2
3
1 1 2
5
4 3 4 3 4
*/


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#define gc getchar()
#define pc putchar
#define rep                                                                    \
  for (a, b, c)                                                                \
    for (int a = b; a <= c; ++a)
#define per                                                                    \
  for (a, b, c)                                                                \
    for (int a = b; a >= c; --a)
inline int read() {
  int x = 0, f = 1;
  char c = gc;
  while (c < '0' || c > '9')
    c = gc;
  while (c <= '9' && c >= '0')
    x = x * 10 + c - '0', c = gc;
  return x * f;
}
void print(int x) {
  if (x < 0) {
    pc('-');
    x = -x;
  }
  if (x >= 10)
    print(x / 10);
  pc(x % 10 + '0');
}
const int maxn = 1000007;
bool solve() {
  static int a[maxn];
  int n = read();
  int mx = 0, mn = 0x3f3f3f3f;
  for (int i = 1; i <= n; ++i) {
    a[i] = read();
    mx = std::max(mx, a[i]);
    mn = std::min(mn, a[i]);
  }
  if (mx - mn >= 2)
    return true;
  //	if(mx == n - 1 && mn != n - 1) return true;
  int c1 = 0, c2 = 0;
  for (int i = 1; i <= n; ++i) {
    if (a[i] == mx)
      c1++;
    else
      c2++;
  }
  int sum = mx;
  if (mx != mn) {
    int res = sum - c2;
    if (res <= 0)
      return true;
    if (c1 < res * 2)
      return true;
  }
  if (mx == mn) {
    sum++;
    if (mx == n - 1)
      return false;
    if (mx * 2 > n) {
      return true;
    }
  }
  return false;
}
int main() { puts(solve() ? "No" : "Yes"); }
/*
3
3
1 2 2
3
1 1 2
5
4 3 4 3 4
*/