alexjercan/bugnet huggingface dataset:
Problem p00081 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

typedef long double ld;

const ld EPS = 1e-9;
const ld INF = 1e10;
const ld PI = M_PI;

struct Point {
  ld x, y;
  Point(ld x, ld y) : x(x), y(y) {}
  Point() {}
};

Point operator+(const Point &a, const Point &b) {
  return Point(a.x + b.x, a.y + b.y);
}
Point operator-(const Point &a, const Point &b) {
  return Point(a.x - b.x, a.y - b.y);
}
Point operator*(const Point &a, const ld b) { return Point(a.x * b, a.y * b); }
Point operator*(const Point &a, const Point &b) {
  return Point(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
}
Point operator/(const Point &a, const ld b) { return Point(a.x / b, a.y / b); }

ld dot(const Point &a, const Point &b) { return a.x * b.x + a.y * b.y; }
ld norm(const Point &a) { return dot(a, a); }

struct Line : vector<Point> {
  Line(Point a = Point(0, 0), Point b = Point(0, 0)) {
    this->push_back(a);
    this->push_back(b);
  }
};

// 写像
Point projection(const Line &l, const Point &p) {
  ld t = dot(p - l[0], l[1] - l[0]) / norm(l[0] - l[1]);
  return l[0] + (l[1] - l[0]) * t;
}

// 反射
Point reflection(const Line &l, const Point &p) {
  return p + (projection(l, p) - p) * 2;
}

int main() {
  double x1, y1, x2, y2, xq, yq;
  while (scanf("%lf,%lf,%lf,%lf,%lf,%lf", &x1, &y1, &x2, &y2, &xq, &yq) !=
         EOF) {
    Line l = Line(Point(x1, y1), Point(x2, y2));
    Point p = Point(xq, yq);

    Point ans = reflection(l, p);
    printf("%.6Lf %.6Lf\\n", ans.x, ans.y);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long double ld;

const ld EPS = 1e-9;
const ld INF = 1e10;
const ld PI = M_PI;

struct Point {
  ld x, y;
  Point(ld x, ld y) : x(x), y(y) {}
  Point() {}
};

Point operator+(const Point &a, const Point &b) {
  return Point(a.x + b.x, a.y + b.y);
}
Point operator-(const Point &a, const Point &b) {
  return Point(a.x - b.x, a.y - b.y);
}
Point operator*(const Point &a, const ld b) { return Point(a.x * b, a.y * b); }
Point operator*(const Point &a, const Point &b) {
  return Point(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
}
Point operator/(const Point &a, const ld b) { return Point(a.x / b, a.y / b); }

ld dot(const Point &a, const Point &b) { return a.x * b.x + a.y * b.y; }
ld norm(const Point &a) { return dot(a, a); }

struct Line : vector<Point> {
  Line(Point a = Point(0, 0), Point b = Point(0, 0)) {
    this->push_back(a);
    this->push_back(b);
  }
};

// 写像
Point projection(const Line &l, const Point &p) {
  ld t = dot(p - l[0], l[1] - l[0]) / norm(l[0] - l[1]);
  return l[0] + (l[1] - l[0]) * t;
}

// 反射
Point reflection(const Line &l, const Point &p) {
  return p + (projection(l, p) - p) * 2;
}

int main() {
  double x1, y1, x2, y2, xq, yq;
  while (scanf("%lf,%lf,%lf,%lf,%lf,%lf", &x1, &y1, &x2, &y2, &xq, &yq)) {
    Line l = Line(Point(x1, y1), Point(x2, y2));
    Point p = Point(xq, yq);

    Point ans = reflection(l, p);
    printf("%.6Lf %.6Lf\\n", ans.x, ans.y);
  }
  return 0;
}
Problem p00081 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>
using namespace std;

#define rep(i, j) for (int i = 0; i < j; i++)

// note: theta[rad]
void rotate(double &x, double &y, const double &theta) {
  double nx, ny;
  nx = x * cos(theta) - y * sin(theta);
  ny = x * sin(theta) + y * cos(theta);
  x = nx;
  y = ny;
}

int main() {
  double p[2][2], q[2];
  while (scanf("%lf,%lf,%lf,%lf,%lf,%lf", &p[0][0], &p[0][1], &p[1][0],
               &p[1][1], &q[0], &q[1]) == 6) {
    double bias[2] = {p[0][0], p[0][1]};
    p[0][0] -= bias[0];
    p[0][1] -= bias[1];
    p[1][0] -= bias[0];
    p[1][1] -= bias[1];
    q[0] -= bias[0];
    q[1] -= bias[1];
    double theta = atan2(p[1][1] - p[0][1], p[1][0] - p[0][0]);
    rotate(q[0], q[1], -theta);
    double r[2] = {q[0], -q[1]};
    rotate(r[0], r[1], theta);
    r[0] += bias[0];
    r[1] += bias[1];
    printf("%lf %lf\\n", r[0], r[1]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
using namespace std;

#define rep(i, j) for (int i = 0; i < j; i++)

// note: theta[rad]
void rotate(double &x, double &y, const double &theta) {
  double nx, ny;
  nx = x * cos(theta) - y * sin(theta);
  ny = x * sin(theta) + y * cos(theta);
  x = nx;
  y = ny;
}

int main() {
  double p[2][2], q[2];
  while (scanf("%lf,%lf,%lf,%lf,%lf,%lf", &p[0][0], &p[0][1], &p[1][0],
               &p[1][1], &q[0], &q[1])) {
    double bias[2] = {p[0][0], p[0][1]};
    p[0][0] -= bias[0];
    p[0][1] -= bias[1];
    p[1][0] -= bias[0];
    p[1][1] -= bias[1];
    q[0] -= bias[0];
    q[1] -= bias[1];
    double theta = atan2(p[1][1] - p[0][1], p[1][0] - p[0][0]);
    rotate(q[0], q[1], -theta);
    double r[2] = {q[0], -q[1]};
    rotate(r[0], r[1], theta);
    r[0] += bias[0];
    r[1] += bias[1];
    printf("%lf %lf\\n", r[0], r[1]);
  }
  return 0;
}
Problem p00081 (C++) — buggy submission, verdict: Time Limit Exceeded

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0081
//????§£?????????
//?????????????????????

#include <complex>
#include <cstdio>
#include <iostream>
using namespace std;

typedef complex<double> xy_t;
double x[3], y[3];
double pro(xy_t a, xy_t b) { return (conj(a) * b).real(); }
int main() {
  xy_t p[3], a, b, c, d;
  char n;
  while (cin >> x[0] >> n >> y[0] >> n >> x[1] >> n >> y[1] >> n >> x[2] >> n >>
         y[2]) {
    p[0] = xy_t(x[0], y[0]);
    p[1] = xy_t(x[1], y[1]);
    p[2] = xy_t(x[2], y[2]);
    a = p[2] - p[0];
    b = p[1] - p[0];
    c = b * pro(a, b) / norm(b) + p[0];
    d = c + c - p[2];
    printf("%.6f %.6f\\n", real(d), imag(d));
  }
  return 0;
}

Error: TLE

Fixed submission:

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0081
//????§£?????????
//?????????????????????

#include <complex>
#include <iostream>
using namespace std;

typedef complex<double> xy_t;
double x[3], y[3];
double pro(xy_t a, xy_t b) { return (conj(a) * b).real(); }
int main() {
  xy_t p[3], a, b, c, d;
  while (1) {
    for (int i = 0; i < 3; i++) {
      cin >> x[i] >> y[i];
    }
    p[0] = xy_t(x[0], y[0]);
    p[1] = xy_t(x[1], y[1]);
    p[2] = xy_t(x[2], y[2]);
    a = p[1] - p[0];
    b = p[2] - p[0];
    c = (pro(a, b) / norm(a)) * a;
    d = c - b + c + p[0];
    cout << d.real() << d.imag() << endl;
  }
  return 0;
}
Problem p00081 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <complex>
#include <cstdio>
using namespace std;
typedef complex<double> xy_t;
double dot_product(xy_t a, xy_t b) { return (conj(a) * b).real(); }
int main() {
  double x[3], y[3];
  xy_t p[3], a, b, c, d;
  while (~scanf("%lf,%lf,%lf,%lf,%lf,%lf", &x[0], &y[0], &x[1], &y[1], &x[2],
                &y[2])) {
    p[0] = xy_t(x[0], y[0]);
    p[1] = xy_t(x[1], y[1]);
    p[2] = xy_t(x[2], y[2]);
    a = p[1] - p[0];
    b = p[2] - p[0];
    c = (dot_product(a, b) / norm(a)) * a;
    d = c - b + c + p[0];
    printf("%.4f %.4f\\n", d.real(), d.imag());
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <complex>
#include <cstdio>
using namespace std;
typedef complex<double> xy_t;
double dot_product(xy_t a, xy_t b) { return (conj(a) * b).real(); }
int main() {
  while (true) {
    double x[3], y[3];
    xy_t p[3], a, b, c, d;
    scanf("%lf,%lf,%lf,%lf,%lf,%lf", &x[0], &y[0], &x[1], &y[1], &x[2], &y[2]);
    p[0] = xy_t(x[0], y[0]);
    p[1] = xy_t(x[1], y[1]);
    p[2] = xy_t(x[2], y[2]);
    a = p[1] - p[0];
    b = p[2] - p[0];
    c = (dot_product(a, b) / norm(a)) * a;
    d = c - b + c + p[0];
    printf("%.6f %.6f", d.real(), d.imag());
  }
}
Problem p00081 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
string s;
void f() {
  double x1, x2, y1, y2, x3, y3, a, b, c, d;
  // input
  s += ',';
  int n = 1;
  bool b00l = 0;
  double pk = 1, pk1 = 1, p = 0;
  for (char ch : s) {
    if (ch == ',') {
      p *= pk1;
      if (n == 1)
        x1 = p;
      if (n == 2)
        y1 = p;
      if (n == 3)
        x2 = p;
      if (n == 4)
        y2 = p;
      if (n == 5)
        x3 = p;
      if (n == 6)
        y3 = p;
      n++;
      b00l = 0;
      pk = 1;
      p = 0;
      pk1 = 1;
    } else if (ch == '.') {
      b00l = 1;
      pk /= 10;
    } else if (ch == '-') {
      pk1 = -1;
    } else if (b00l) {
      p += (double)(ch - '0') * pk;
      pk /= 10;
    } else {
      p *= 10;
      p += (double)(ch - '0');
    }
  }

  a = x1 - x2;
  b = y1 - y2;
  c = b;
  d = a;
  if (d < 0)
    d *= -1;
  else
    c *= -1;
  double k1 = -100000, k2 = 100000;
  while (k2 - k1 >= 0.0000000000001) {
    double mid = (k1 + k2) / 2;
    if ((a * (y3 + k1 * d - y1) - b * (x3 + k1 * c - x1)) *
            ((a * (y3 + mid * d - y1) - b * (x3 + mid * c - x1))) >
        0) {
      k1 = mid;
    } else
      k2 = mid;
  }
  double k = k1 + k2;
  printf("%.20f %.20f\\n", x3 + k * c, y3 + k * d);
}
signed main() {
  while (cin >> s)
    f();
}


Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
  while (cin) {
    double x1, y1, x2, y2, x3, y3;
    scanf("%lf,%lf,%lf,%lf,%lf,%lf", &x1, &y1, &x2, &y2, &x3, &y3);
    cout << (x1 + x2 - x3) << ' ' << (y1 + y2 - y3) << '\\n';
  }
}
Problem p00081 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
#define rep2(x, from, to) for (int x = (from); x < (to); ++(x))
#define rep(x, to) rep2(x, 0, to)
int main() {
  double x1, y1, x2, y2, qx, qy;
  while (scanf("%lf,%lf,%lf,%lf,%lf,%lf", &x1, &y1, &x2, &y2, &qx, &qy) !=
         EOF) {
    qx -= x1, qy -= y1, x2 -= x1, y2 -= y1;
    double cos = x2 / sqrt(x2 * x2 + y2 * y2),
           sin = y2 / sqrt(x2 * x2 + y2 * y2);
    double bx = qx;
    qx = cos * qx + sin * qy, qy = -sin * bx + cos * qy;
    bx = qx;
    qx = x1 + cos * qx + sin * qy, qy = y1 + sin * bx - cos * qy;
    printf("%.6lf %.6lf\\n", qx, qy);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <complex>
#include <cstdio>
#include <iostream>
using namespace std;
#define rep2(x, from, to) for (int x = (from); x < (to); ++(x))
#define rep(x, to) rep2(x, 0, to)
int main() {
  double x1, y1, x2, y2, qx, qy;
  while (scanf("%lf,%lf,%lf,%lf,%lf,%lf", &x1, &y1, &x2, &y2, &qx, &qy)) {
    // ??????????´???°????????????
    // complex<double> p1(x1,y1),p2(x2,y2),q(qx,qy);
    //      p2-=p1; q-=p1; //?§??????????????????????
    //      q/=p2; //????????¨???????§????????±???????
    //      q=conj(q); //????????¨????±??????????R??¨??????
    //      q*=p2; q+=p1;
    // printf("%.6lf %.6lf\\n",q.real(),q.imag());

    //// ????????§?¨???§????????????
    qx -= x1, qy -= y1, x2 -= x1, y2 -= y1;
    double cos = x2 / sqrt(x2 * x2 + y2 * y2),
           sin = y2 / sqrt(x2 * x2 + y2 * y2);
    double bx = qx;
    // cf.????´???°????????????(????§????????????¶??????????????????
    // complex c = a/b;
    // c.re = (a.re * b.re + a.im * b.im)/(b.re * b.re + b.im * b.im);
    // c.im = (a.im * b.re - a.re * b.im)/(b.re * b.re + b.im * b.im);
    //  cf.????´???°????????????(????§????????????¶???????????????
    //  complex c = a*b;
    //  c.re = a.re * b.re - a.im * b.im
    //  c.im = a.re * b.im + a.im * b.re

    // ????§???????(????§???¢??°???????????????(sin,cos(q-p2)????????????????????????????????§????¨?
    qx = cos * qx + sin * qy, qy = -sin * bx + cos * qy;
    // printf("%.6lf %.6lf\\n", qx, qy);
    bx = qx;
    // cf.????´???°?????±???(conj())(imag????????????
    qx = x1 + cos * qx + sin * qy, qy = y1 + sin * bx - cos * qy;
    printf("%.6lf %.6lf\\n", qx, qy);
  }
  return 0;
}
Problem p00082 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

typedef long long ll;

const int start[] = {1, 4, 1, 4, 1, 2, 1, 2};
int now[] = {1, 4, 1, 4, 1, 2, 1, 2};
int ans[8];
int out;
int in[8];

bool isunder() {

  for (int i = 0; i < 8; i++) {
    if (ans[i] < now[i])
      return false;
    if (ans[i] > now[i])
      return true;
  }
  return false;
}

void move() {

  int end = now[7];

  for (int i = 6; i >= 0; i--)
    now[i + 1] = now[i];

  now[0] = end;
}

void check() {

  int sum = 0;

  for (int i = 0; i < 8; i++)
    sum += max(0, in[i] - now[i]);

  if (sum < out || (sum == out && isunder())) {
    for (int i = 0; i < 8; i++)
      ans[i] = now[i];
    out = sum;
  }
}

bool fin() {
  for (int i = 0; i < 8; i++)
    if (now[i] != start[i])
      return false;

  return true;
}
void solve() {

  while (1) {
    check();
    move();
    if (fin())
      break;
  }

  for (int i = 0; i < 8; i++) {
    if (i)
      cout << " ";
    cout << ans[i];
  }
  cout << endl;
}

void init() {
  ans[0] = -1;
  out = (1 << 30);
}

bool input() {
  init();
  for (int i = 0; i < 8; i++)
    if (!(cin >> in[i]))
      return false;

  return true;
}
int main() {

  while (input())
    solve();

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

const string start = "14141212";
string s, outcome;

void make() {
  outcome = "999999999999";
  string tmp = "";

  for (int i = 0; i < (int)s.length(); i++)
    if (s[i] != ' ')
      tmp += s[i];

  s = tmp;
}

void calc(string now, int &out) {

  int sum = 0;

  for (int i = 0; i < now.length(); i++) {
    sum += max(0, s[i] - now[i]);
  }
  if (out == sum)
    outcome = min(outcome, now);
  else if (out > sum) {
    out = sum;
    outcome = now;
  }
}

void solve() {
  make();
  string now = start;
  int out = (1 << 20);

  while (1) {
    calc(now, out);
    string tmp = now[now.length() - 1] + now.substr(0, s.length() - 1);
    now = tmp;
    if (now == start)
      break;
  }
  for (int i = 0; i < outcome.length(); i++) {
    if (i)
      cout << " ";
    cout << outcome[i];
  }
  cout << endl;
}

int main() {

  while (getline(cin, s))
    solve();
  return 0;
}
Problem p00082 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
int p[3] = {1, 2, 4};
int x[8], y[8];
int z[8] = {2, 0, 2, 0, 1, 0, 1, 0};
int main() {
  while (cin >> x[0]) {
    int maxn = 100000000, maxid = 0;
    for (int i = 1; i < 8; i++) {
      cin >> x[i];
    }
    for (int i = 0; i < 8; i++) {
      int sum = 0, sum2 = 0;
      for (int j = 0; j < 8; j++) {
        y[j] = z[(i + j) % 8];
      }
      for (int j = 0; j < 8; j++) {
        if (p[y[j]] < x[j]) {
          sum += x[j] - p[y[j]];
        }
      }
      for (int j = 0; j < 8; j++) {
        sum2 += p[y[j]] * (int)pow(10, 7 - j);
      }
      if (maxn > sum) {
        maxn = sum;
        maxid = sum2;
      } else if (maxn == sum && maxid > sum2) {
        maxid = sum2;
      }
    }
    for (int j = 0; j < 8; j++) {
      if (j) {
        cout << ' ';
      }
      cout << (maxid / (int)pow(10, 7 - j)) % 10;
    }
    cout << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
int p[3] = {1, 2, 4};
int x[8], y[8];
vector<pair<int, int>> vec;
int main() {
  while (cin >> x[0]) {
    for (int i = 1; i < 8; i++) {
      cin >> x[i];
    }
    for (int i = 0; i < 6561; i++) {
      int sum = 0, sum2 = 0;
      for (int j = 0; j < 8; j++) {
        y[j] = (i / (int)pow(3, j)) % 3;
      }
      for (int j = 0; j < 8; j++) {
        if (p[y[j]] < x[j]) {
          sum += x[j] - p[y[j]];
        }
      }
      for (int j = 0; j < 8; j++) {
        sum2 += p[y[j]] * (int)pow(10, 7 - j);
      }
      vec.push_back(make_pair(sum, sum2));
    }
    sort(vec.begin(), vec.end());
    for (int j = 0; j < 8; j++) {
      if (j) {
        cout << ' ';
      }
      cout << (vec[0].first / (int)pow(10, 7 - j)) % 10 << endl;
    }
  }
}
Problem p00082 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#define MAX 8

int make(int *num, int c) {
  int res = 0;
  for (int i = 0; i < MAX; i++) {
    res *= 10;
    res += num[(i + c) % MAX];
  }
  return res;
}

int main() {
  int p[MAX];
  int c[MAX] = {4, 1, 4, 1, 2, 1, 2, 1};
  int ex, mx, fl1, fl2;

  while (std::cin >> p[0] >> p[1] >> p[2] >> p[3] >> p[4] >> p[5] >> p[6] >>
         p[7]) {
    fl1 = 0;

    mx = 114514;
    fl2 = 114514810;
    for (int i = 0; i < MAX; i++) {
      int temp;
      ex = 0;
      for (int j = 0; j < MAX; j++) {
        temp = p[j] - c[(j + i) % MAX];
        if (temp > 0)
          ex += temp;
      }

      temp = make(c, i);
      if (mx > ex) {
        mx = ex;
        fl1 = i;
        fl2 = temp;
      } else if (mx == ex && temp < fl2) {
        mx = ex;
        fl1 = i;
        fl2 = temp;
      }
    }

    for (int i = 0; i < MAX; i++) {
      if (i != MAX - 1)
        std::cout << c[(fl1 + i) % MAX] << " ";
      else
        std::cout << c[(fl1 + i) % MAX] << std::endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#define MAX 8

int make(int *num, int c) {
  int res = 0;
  for (int i = 0; i < MAX; i++) {
    res *= 10;
    res += num[(i + c) % MAX];
  }
  return res;
}

int main() {
  int c[MAX] = {4, 1, 4, 1, 2, 1, 2, 1};
  int ex, mx, fl1, fl2;

  while (true) {
    int p[MAX];
    fl1 = 0;
    for (int i = 0; i < MAX; i++) {
      std::cin >> p[i];
    }

    mx = 114514;
    fl2 = 114514810;
    for (int i = 0; i < MAX; i++) {
      int temp;
      ex = 0;
      for (int j = 0; j < MAX; j++) {
        temp = p[j] - c[(j + i) % MAX];
        if (temp > 0)
          ex += temp;
      }

      temp = make(c, i);
      if (mx > ex) {
        mx = ex;
        fl1 = i;
        fl2 = temp;
      } else if (mx == ex && temp < fl2) {
        mx = ex;
        fl1 = i;
        fl2 = temp;
      }
    }

    for (int i = 0; i < MAX; i++) {
      std::cout << c[(fl1 + i) % MAX] << " ";
    }
    std::cout << std::endl;
  }

  return 0;
}
Problem p00082 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int fly[] = {4, 1, 4, 1, 2, 1, 2, 1}, peo[8], remainder, point = 0;
  while (cin >> peo[0]) {
    for (int i = 1; i < 8; i++)
      cin >> peo[i];
    for (int i = 0; i < 8; i++) {
      int num = 0;
      for (int j = 0; j < 8; j++)
        if (fly[(i + j) % 8] < peo[j])
          num += peo[j] - fly[(i + j) % 8];
      if (remainder > num || i == 0) {
        remainder = num;
        point = i;
      } else if (remainder == num) {
        for (int j = 0; j < 8 && fly[(i + j) % 8] <= fly[(point + j) % 8];
             j++) {
          if (fly[(i + j) % 8] < fly[(point + j) % 8]) {
            point = i;
            break;
          }
        }
      }
    }
    for (int i = 0; i < 7; i++)
      cout << fly[(point + i) % 8] << " ";
    cout << fly[(point + 7) % 8] << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int fly[] = {4, 1, 4, 1, 2, 1, 2, 1}, peo[8], remainder, point = 0;
  while (true) {
    for (int i = 0; i < 8; i++)
      cin >> peo[i];
    for (int i = 0; i < 8; i++) {
      int num = 0;
      for (int j = 0; j < 8; j++)
        if (fly[(i + j) % 8] < peo[j])
          num += peo[j] - fly[(i + j) % 8];
      if (remainder > num || i == 0) {
        remainder = num;
        point = i;
      } else if (remainder == num) {
        for (int j = 0; j < 8; j++) {
          if (fly[(i + j) % 8] > fly[(point + j) % 8])
            break;
          else if (fly[(i + j) % 8] < fly[(point + j) % 8]) {
            point = i;
            break;
          }
        }
      }
    }
    for (int i = 0; i < 7; i++)
      cout << fly[(point + i) % 8] << " ";
    cout << fly[(point + 7) % 8] << endl;
  }
}
Problem p00082 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  vector<pair<int, int>> vec;
  int a, p[8], q[8] = {4, 1, 4, 1, 2, 1, 2, 1};
  while (cin >> p[0]) {
    for (int i = 1; i < 8; i++)
      cin >> p[i];
    int m = 999999999;
    for (int i = 0; i < 8; i++) {
      int t = 0;
      for (int j = 0; j < 8; j++)
        if (p[j] > q[(i + j) % 8])
          t += (p[j] - q[(i + j) % 8]);
      if (t < m) {
        vec.clear();
        m = t;
      }
      if (t <= m) {
        int te = 0;
        for (int j = 0; j < 8; j++)
          te = te * 10 + q[(i + j) % 8];
        vec.push_back(make_pair(te, i));
      }
    }
    sort(vec.begin(), vec.end());
    a = vec[0].second;
    for (int i = 0; i < 7; i++)
      cout << q[(a + i) % 8] << " ";
    cout << q[(a + 7) % 8] << endl;
    vec.clear();
  }
}

Error: -11

Fixed submission:

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

int main() {
  int p[8], q[8] = {4, 1, 4, 1, 2, 1, 2, 1}, a = 0, m = 999999999;
  while (cin >> p[0]) {
    vector<pair<int, int>> vec;
    for (int i = 1; i < 8; i++)
      cin >> p[i];
    for (int i = 0; i < 8; i++) {
      int t = 0;
      for (int j = 0; j < 8; j++)
        if (p[j] > q[(i + j) % 8])
          t += (p[j] - q[(i + j) % 8]);
      if (t <= m) {
        m = t;
        int te = 0;
        for (int j = 0; j < 8; j++)
          te = te * 10 + q[(a + j) % 8];
        vec.push_back(make_pair(te, i));
      }
    }
    sort(vec.begin(), vec.end());
    a = vec[0].second;
    for (int i = 0; i < 7; i++)
      cout << q[(a + i) % 8] << " ";
    cout << q[(a + 7) % 8] << endl;
  }
}
Problem p00082 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <climits>
#include <iostream>

using namespace std;

int ArrayToInt(const int *arry, int ofs) {
  int result;
  result = arry[ofs] * 10000000 + arry[ofs + 1] * 1000000 +
           arry[ofs + 2] * 100000 + arry[ofs + 3] * 10000 +
           arry[ofs + 4] * 1000 + arry[ofs + 5] * 100 + arry[ofs + 6] * 10 +
           arry[ofs + 7];
  return result;
}

// 0082
int main() {
  const int merry[15] = {1, 2, 1, 2, 1, 4, 1, 4, 1, 2, 1, 2, 1, 4, 1};
  int human[8] = {0};

  while (1) {
    int remain[8] = {0};

    for (int i = 0; i < 8; ++i) {
      cin >> human[i];
      if (cin.eof())
        return 0;
    }

    int min_remain = INT_MAX;
    for (int start = 0; start < 8; ++start) {
      for (int i = 0; i < 8; ++i) {
        int t = human[i] - merry[start + i];
        if (t > 0) {
          remain[start] += t;
        }
      }
      min_remain = ((min_remain < remain[start]) ? min_remain : remain[start]);
    }

    int x;
    int prev = INT_MAX;
    for (int i = 0; i < 8; ++i) {
      if (remain[i] == min_remain) {
        int t = ArrayToInt(merry, i);
        if (prev > t) {
          prev = t;
          x = i;
        }
      }
    }

    cout << merry[x + 0] << " " << merry[x + 1] << " " << merry[x + 2] << " "
         << merry[x + 3] << " " << merry[x + 4] << " " << merry[x + 5] << " "
         << merry[x + 6] << " " << merry[x + 7] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <climits>
#include <iostream>

using namespace std;

int ArrayToInt(const int *arry, int ofs) {
  int result;
  result = arry[ofs] * 10000000 + arry[ofs + 1] * 1000000 +
           arry[ofs + 2] * 100000 + arry[ofs + 3] * 10000 +
           arry[ofs + 4] * 1000 + arry[ofs + 5] * 100 + arry[ofs + 6] * 10 +
           arry[ofs + 7];
  return result;
}

// 0082
int main() {
  const int merry[15] = {1, 2, 1, 2, 1, 4, 1, 4, 1, 2, 1, 2, 1, 4, 1};
  int human[8] = {0};
  int remain[8] = {0};

  while (1) {
    for (int i = 0; i < 8; ++i) {
      cin >> human[i];
      if (cin.eof())
        break;
    }

    int min_remain = INT_MAX;
    for (int start = 0; start < 8; ++start) {
      for (int i = 0; i < 8; ++i) {
        int t = human[i] - merry[start + i];
        if (t > 0) {
          remain[start] += t;
        }
      }
      min_remain = ((min_remain < remain[start]) ? min_remain : remain[start]);
    }

    int x;
    int prev = INT_MAX;
    for (int i = 0; i < 8; ++i) {
      if (remain[i] == min_remain) {
        int t = ArrayToInt(merry, i);
        if (prev > t) {
          prev = t;
          x = i;
        }
      }
    }

    cout << merry[x + 0] << " " << merry[x + 1] << " " << merry[x + 2] << " "
         << merry[x + 3] << " " << merry[x + 4] << " " << merry[x + 5] << " "
         << merry[x + 6] << " " << merry[x + 7] << endl;
  }
  return 0;
}