Problem p01415 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define REP2(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define REP(i, n) REP2(i, 0, n)
#define ALL(c) (c).begin(), (c).end()
#define ITER(c) __typeof((c).begin())
#define PB(e) push_back(e)
#define FOREACH(i, c) for (ITER(c) i = (c).begin(); i != (c).end(); ++i)
#define MP(a, b) make_pair(a, b)
#define PARITY(n) ((n)&1)

typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1000 * 1000 * 1000 + 7;
const double EPS = 1e-10;

int main() {
  int N, K, T, U, V, L;
  cin >> N >> K >> T >> U >> V >> L;
  vector<int> D(N);
  REP(i, N) cin >> D[i];

  double res = 0;
  double pos = 0;
  int carrot = 0;
  int i = 0;
  while (i < N) {
    // move to a next carrot
    res += (D[i] - pos) / U;
    pos = D[i++];
    carrot++;

    while (carrot > 0 && pos < L - EPS) {
      // eat carrot
      // cout << pos << " " << res << endl;
      carrot--;
      double next = min(pos + T * V, (double)L);
      ;

      while (i < N && D[i] < next) {
        carrot++;
        i++;
      }

      if (carrot > K) {
        carrot = K + 1;
        res += (D[i - 1] - pos) / V;
        pos = D[i - 1];
      } else {
        res += (next - pos) / V;
        pos = next;
      }
    }
    assert(carrot == 0);
  }
  cout << fixed << setprecision(20) << res + (L - pos) / U << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define REP2(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define REP(i, n) REP2(i, 0, n)
#define ALL(c) (c).begin(), (c).end()
#define ITER(c) __typeof((c).begin())
#define PB(e) push_back(e)
#define FOREACH(i, c) for (ITER(c) i = (c).begin(); i != (c).end(); ++i)
#define MP(a, b) make_pair(a, b)
#define PARITY(n) ((n)&1)

typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1000 * 1000 * 1000 + 7;
const double EPS = 1e-10;

int main() {
  int N, K, T, U, V, L;
  cin >> N >> K >> T >> U >> V >> L;
  vector<int> D(N);
  REP(i, N) cin >> D[i];

  double res = 0;
  double pos = 0;
  int carrot = 0;
  int i = 0;
  while (i < N) {
    // move to a next carrot
    res += (D[i] - pos) / U;
    pos = D[i++];
    carrot++;

    while (carrot > 0) {
      // eat carrot
      // cout << pos << " " << res << endl;
      carrot--;
      double next = min(pos + T * V, (double)L);
      ;

      while (i < N && D[i] < next) {
        carrot++;
        i++;
      }

      if (carrot > K) {
        carrot = K + 1;
        res += (D[i - 1] - pos) / V;
        pos = D[i - 1];
      } else {
        res += (next - pos) / V;
        pos = next;
      }
    }
    assert(carrot == 0);
  }
  cout << fixed << setprecision(20) << res + (L - pos) / U << endl;

  return 0;
}