ferinの競プロ帳

競プロについてのメモ

ABC084 C - Special Trains

問題ページ
C - Special Trains

考えたこと

  • 乗れる電車のうち一番はやい電車に貪欲に乗っていけばよさそう
  • t秒目に駅に来た時 s + k*f >= t を満たす最小のkの電車に乗ればいい
  • 変形すると k >= (t-s)/f
  • k = max(0, ceil*1と決定できるのであとは貪欲にえい

割り算をdoubleに変換しないアホミスで1WA出してア

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define int ll
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a<=x&&x<b)
#define MP make_pair
#define PB push_back
#ifdef int
const ll INF = (1LL<<60);
#else
const int INF = (1LL<<30);
#endif
const double PI = 3.14159265359;
const double EPS = 1e-12;
const int MOD = 1000000007;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a){
  out<<'('<<a.first<<','<<a.second<<')';
  return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

int c[505], s[505], f[505];
signed main(void)
{
  int n;
  cin >> n;
  REP(i, n-1) cin >> c[i] >> s[i] >> f[i];

  // 駅i→駅i+1 c[i]秒かかる s[i] + k*f[i] 秒目に発車 s[i]%f[i]==0
  REP(i, n) {
    int t = 0;
    FOR(j, i, n-1) {
      // jからj+1に移動
      // t以上で最も早く到達する電車に乗る
      // t >= s[j] && t%f[j] == 0
      int k = max(0.0, ceil((double)(t-s[j])/f[j]));
      t = s[j] + k*f[j];
      t += c[j];
    }
    cout << t << endl;
  }

  return 0;
}

*1:t-s)/f