-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathidentical_checker.cpp
More file actions
165 lines (141 loc) · 3.82 KB
/
identical_checker.cpp
File metadata and controls
165 lines (141 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <numeric>
#include <regex.h>
#include <stdexcept>
#include <string>
#include <vector>
namespace CheckerCodes {
constexpr int AC = 0;
constexpr int WA = 1;
constexpr int PE = 2;
constexpr int PARTIAL = 7;
} // namespace CheckerCodes
namespace regex_helpers {
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_helpers
void errorHook();
void exitWA() {
errorHook();
std::exit(CheckerCodes::WA);
}
void exitPE() {
errorHook();
std::exit(CheckerCodes::PE);
}
void assertWA(bool condition) {
if (!condition) {
exitWA();
}
}
void assertPE(bool condition) {
if (!condition) {
exitPE();
}
}
void readSpace() { assertPE(std::cin.get() == ' '); }
void readNewLine() { assertPE(std::cin.get() == '\n'); }
void readEOF() { assertPE(std::cin.get() == 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 = std::cin.get();
assertPE(!isspace(c));
while (!isspace(c) && c != EOF) {
assertWA(token.size() < MAX_TOKEN_SIZE);
assertWA(min_char <= c && c <= max_char);
token.push_back(char(c));
c = std::cin.get();
}
std::cin.unget();
return token;
}
bool validateInt(const std::string &token) {
if (token == "0") {
return true;
}
auto i = 0u;
if (i < token.size() && token[i] == '-') {
++i;
}
if (i >= token.size() || token[i] < '1' || '9' < token[i]) {
return false;
}
for (i++; i < token.size(); i++) {
if (token[i] < '0' || '9' < token[i]) {
return false;
}
}
return true;
}
long long readInt(long long lo, long long hi) {
std::string token = readToken();
assertWA(validateInt(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;
}
template <typename T>
std::vector<T> readIntArray(size_t N, long long lo, long long hi) {
std::vector<T> arr;
arr.reserve(N);
for (size_t i = 0; i < N; i++) {
if (i) {
readSpace();
}
arr.push_back(readInt(lo, hi));
}
readNewLine();
return arr;
}
void errorHook() {}
// readLine() and readFloat() removed for brevity.
int main(int argc, char **argv) {
std::ifstream judge_input(argv[1]);
std::ifstream submission_output(argv[2]);
std::cin.rdbuf(submission_output.rdbuf());
std::ifstream judge_answer(argv[3]);
int N, K;
judge_input >> N >> K;
// If any integer is greater than K, we give an immediate WA.
std::vector<int> arr = readIntArray<int>(N, 0, K);
// We can read EOF now, since we are done with the input. This makes it easier
// to remember.
readEOF();
// Note that we must store the sum in a long long, since it may overflow a
// 32-bit integer.
long long sum = std::accumulate(arr.begin(), arr.end(), 0LL);
assertWA(sum == K);
// It turns out that the minimum product is always zero, since [0, 0, ..., K]
// is always valid.
// Thus, it suffices to check for a zero in the array.
if (std::find(arr.begin(), arr.end(), 0) == arr.end()) {
// No zero found. Give partial points:
// Output to stderr for the coci contrib module to grant partial AC.
std::cerr << "partial 50/100" << std::endl;
// Output to stdout to give user feedback. Newline appears as space on
// judge, so omit it.
std::cout << "50/100 points" << std::flush;
return CheckerCodes::PARTIAL;
}
return CheckerCodes::AC;
}