32 lines
1.4 KiB
C++
32 lines
1.4 KiB
C++
// concept: complexity-budget-estimation
|
|
// Rough operations budget for C++: <= 1e8 simple ops per second is safe,
|
|
// up to ~1e9/s only for very tight loops, beyond that expect TLE.
|
|
struct TimeBudget {
|
|
double seconds; // time limit of the problem
|
|
explicit TimeBudget(double s = 1.0) : seconds(s) {}
|
|
|
|
// 0 = safe, 1 = risky (needs small constant), 2 = TLE. O(1).
|
|
int verdict(double ops) const {
|
|
if (ops <= 1e8 * seconds) return 0;
|
|
if (ops <= 1e9 * seconds) return 1;
|
|
return 2;
|
|
}
|
|
|
|
// Cost models for common complexities; n is a size (count), not an index. O(1) each.
|
|
static double lg(double n) { return max(1.0, log2(n)); }
|
|
static double nLogN(double n) { return n * lg(n); }
|
|
static double nSqrtN(double n) { return n * sqrt(n); }
|
|
static double subsets(double n) { return pow(2.0, n) * n; } // 2^n * n bitmask DP
|
|
static double perms(int n) { double f = 1; for (int i = 2; i <= n; i++) f *= i; return f * n; } // n! * n
|
|
};
|
|
|
|
// Wall-clock timer: time a max-size test locally, or stop a search before the limit.
|
|
struct Timer {
|
|
chrono::steady_clock::time_point st = chrono::steady_clock::now();
|
|
// Milliseconds since construction or last reset(). O(1).
|
|
double ms() const {
|
|
return chrono::duration<double, milli>(chrono::steady_clock::now() - st).count();
|
|
}
|
|
void reset() { st = chrono::steady_clock::now(); }
|
|
};
|