Checker
unknown
c_cpp
a year ago
4.6 kB
26
Indexable
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <regex.h>
#include <stdexcept>
#include <string>
#include <vector>
#include <utility>
void errorHook();
void exitWA() {
errorHook();
std::exit(1);
}
void exitPE() {
errorHook();
std::exit(2);
}
void assertWA(bool condition) {
if (!condition) {
exitWA();
}
}
void assertPE(bool condition) {
if (!condition) {
// Defined for `testlib` and `coci`.
exitPE();
}
}
void readSpace() { assertPE(getchar() == ' '); }
void readNewLine() { assertPE(getchar() == '\n'); }
void readEOF() { assertPE(getchar() == EOF); }
std::string readToken(char min_char = 0, char max_char = 127) {
static constexpr size_t MAX_TOKEN_SIZE = 1e7;
std::string token;
int c = getchar();
assertPE(!isspace(c));
while (!isspace(c) && c != EOF && token.size() < MAX_TOKEN_SIZE) {
assertWA(min_char <= c && c <= max_char);
token.push_back(char(c));
c = getchar();
}
ungetc(c, stdin);
return token;
}
namespace regex_detail {
regex_t compile(const char *pattern) {
regex_t re;
if (regcomp(&re, pattern, REG_EXTENDED | REG_NOSUB) != 0) {
throw std::runtime_error("Pattern failed to compile.");
}
return re;
}
bool match(regex_t re, const std::string &text) {
return regexec(&re, text.c_str(), 0, NULL, 0) == 0;
}
} // namespace regex_detail
long long readInt(long long lo, long long hi) {
// stoll is horribly insufficient, so we use a regex.
static regex_t re = regex_detail::compile("^0|-?[1-9][0-9]*$");
std::string token = readToken();
assertWA(regex_detail::match(re, token));
long long parsedInt;
try {
parsedInt = stoll(token);
} catch (const std::invalid_argument &) {
exitWA();
} catch (const std::out_of_range &) {
exitWA();
}
assertWA(lo <= parsedInt && parsedInt <= hi);
return parsedInt;
}
double readDouble(long long lo, long long hi) {
// stoll is horribly insufficient, so we use a regex.
static regex_t re = regex_detail::compile("^0|-?(?:[1-9][0-9]*|0)?(?:\\.[0-9]+)?$"); //pasted from SO and hand tested, should be OK...
std::string token = readToken();
assertWA(regex_detail::match(re, token));
double parsedDouble;
try {
parsedDouble = stod(token);
} catch (const std::invalid_argument &) {
exitWA();
} catch (const std::out_of_range &) {
exitWA();
}
assertWA(lo <= parsedDouble && parsedDouble <= hi);
return parsedDouble;
}
template <typename T>
std::vector<T> readDoubleArray(size_t N, long long lo, long long hi) {
std::vector<T> arr;
arr.reserve(N);
for (size_t i = 0; i < N; i++) {
arr.push_back(readDouble(lo, hi));
if (i != N - 1) {
readSpace();
}
}
readNewLine();
return arr;
}
// This is a hook for when the reader functions error, including `assertWA`. Use
// it to do custom handling when an error happens, such as overriding WA with
// partials, or printing a flag like `-1` in interactors.
void errorHook() {}
// If this is a checker, use the following line in main(int argc, char** argv)
// to replace stdin with the user output. freopen(argv[2], "r", stdin);
double eval(int x, std::vector<double> coeffs) {
double res = 0;
std::reverse(coeffs.begin(), coeffs.end());
for (int power = 0; power < coeffs.size(); power++) {
res += coeffs[power] * std::pow(x, power);
}
return res;
}
int main(int argc, char** argv) {
const int MAXCOORD = 100;
const int MAXCOEFF = 1e4;
const int MAXDEG = 1e4;
const int MAXSTARS = 50;
//store coordinates of test case
freopen(argv[1], "r", stdin);
std::vector<std::pair<int, int>> stars;
int n = readInt(0, MAXSTARS); readNewLine();
for (int i = 0; i < n; i++) {
int x = readInt(-MAXCOORD, MAXCOORD); readSpace();
int y = readInt(-MAXCOORD, MAXCOORD);
(i == n-1 ? readEOF() : readNewLine());
stars.push_back({x, y});
}
//read user polynomial
freopen(argv[2], "r", stdin);
int k = readInt(0, n); readNewLine();
//will this catch and return WA if the given output arr is not of size k+1?
std::vector<double> coeffs = readDoubleArray(k+1, -MAXCOEFF, MAXCOEFF);
readEOF();
//actual polynomial evaluation and checking
for (std::pair<int, int> pos : stars) {
assertWA(abs(eval(pos.first, coeffs) - pos.second) < 0.00001);
}
std::exit(0); //return AC? guessing return code 0 here
// argv[1] is process input
// argv[2] is setup for you - it's process output
// argv[3] is judge answer
}Editor is loading...
Leave a Comment