Skip to content

Instantly share code, notes, and snippets.

@johnchen902
Last active May 26, 2017 17:14
Show Gist options
  • Save johnchen902/cb82968d2d1023ab85c8d55087f2c1df to your computer and use it in GitHub Desktop.
Save johnchen902/cb82968d2d1023ab85c8d55087f2c1df to your computer and use it in GitHub Desktop.
Problem Template
$ tree
.
├── allsolution
│   ├── ac1.cpp
│   ├── tle1.cpp
│   └── wa1.cpp
├── generator.py
├── Makefile
├── solution.cpp
├── testdata
├── tester.sh
└── verifier.cpp
$ make
g++ -std=c++11 -Wall -Wextra -fsanitize=undefined -fsanitize=undefined solution.cpp -o solution
g++ -std=c++11 -Wall -Wextra -fsanitize=undefined -fsanitize=undefined verifier.cpp -o verifier
cp generator.py generator
chmod +x generator
./generator 1 > testdata/input1.txt
./verifier < testdata/input1.txt
./generator 2 > testdata/input2.txt
./verifier < testdata/input2.txt
./generator 3 > testdata/input3.txt
./verifier < testdata/input3.txt
./solution < testdata/input1.txt > testdata/output1.txt
./solution < testdata/input2.txt > testdata/output2.txt
./solution < testdata/input3.txt > testdata/output3.txt
g++ -std=c++11 -Wall -Wextra -fsanitize=undefined -fsanitize=undefined allsolution/tle1.cpp -o allsolution/tle1
g++ -std=c++11 -Wall -Wextra -fsanitize=undefined -fsanitize=undefined allsolution/wa1.cpp -o allsolution/wa1
g++ -std=c++11 -Wall -Wextra -fsanitize=undefined -fsanitize=undefined allsolution/ac1.cpp -o allsolution/ac1
./tester.sh
allsolution/ac1 < testdata/input1.txt: AC
allsolution/ac1 < testdata/input2.txt: AC
allsolution/ac1 < testdata/input3.txt: AC
allsolution/tle1 < testdata/input1.txt: RE/TLE
allsolution/tle1 < testdata/input2.txt: RE/TLE
allsolution/tle1 < testdata/input3.txt: RE/TLE
allsolution/wa1 < testdata/input1.txt: WA
allsolution/wa1 < testdata/input2.txt: WA
allsolution/wa1 < testdata/input3.txt: WA
$ tree
.
├── allsolution
│   ├── ac1
│   ├── ac1.cpp
│   ├── tle1
│   ├── tle1.cpp
│   ├── wa1
│   └── wa1.cpp
├── generator
├── generator.py
├── Makefile
├── solution
├── solution.cpp
├── testdata
│   ├── input1.txt
│   ├── input2.txt
│   ├── input3.txt
│   ├── output1.txt
│   ├── output2.txt
│   └── output3.txt
├── tester.sh
├── verifier
└── verifier.cpp
# Of course you can edit this file as appropriate
CXXFLAGS = -std=c++11 -Wall -Wextra -O2 -fsanitize=undefined
LDFLAGS = -fsanitize=undefined
TESTS = $(shell seq 1 3)
INPUTS = $(addprefix testdata/input, $(addsuffix .txt, $(TESTS)))
OUTPUTS = $(addprefix testdata/output, $(addsuffix .txt, $(TESTS)))
ALLSOLS = $(patsubst %.cpp,%,$(wildcard allsolution/*.cpp))
.PHONY: all clean test
.DELETE_ON_ERROR:
all: solution verifier $(INPUTS) $(OUTPUTS) test
%: %.py
cp $< $@
chmod +x $@
$(INPUTS): testdata/input%.txt: generator
./generator $* > $@
./verifier < $@
$(OUTPUTS): testdata/output%.txt: testdata/input%.txt solution verifier
./solution < $< > $@
test: tester.sh $(INPUTS) $(OUTPUTS) $(ALLSOLS)
./tester.sh
clean:
$(RM) solution verifier generator $(INPUTS) $(OUTPUTS) $(ALLSOLS)
// You can use another language if appropriate,
// provided TA can run it and solution/ac1.cpp is actually in C++.
#include <cstdio>
int main() {
int t;
std::scanf("%d", &t);
while(t--) {
int x, y;
std::scanf("%d %d", &x, &y);
std::printf("%d\n", x + y);
}
}
#!/usr/bin/env python3
# If you use a program to generate your input,
# you should also present the program to TA.
# Make sure the result is reproducible and portable (so no std::rand or <random> in C++)
# Of course you can use any language for generator as long as TA can run it.
import random, sys
if len(sys.argv) != 2:
sys.exit("Usage: ./generator caseid")
caseid = int(sys.argv[1])
maxt = 1000
maxab = 1000
random.seed(1000 + caseid)
if caseid == 1:
print(3)
print(1, 2)
print(10, 20)
print(100, 200)
elif caseid == 2 or caseid == 3:
print(maxt)
for i in range(maxt):
a = random.randint(1, maxab)
b = random.randint(1, maxab)
print(a, b)
else:
sys.exit("Unknown case %d" % caseid)
// It's easy for generator to produce bad input,
// so we need to double-check it using a program with obvious no flaw.
// Make sure to check all constraint guaranteed in the problem.
// Of course you can use any language for verifier as long as TA can run it.
#include <cstdio>
#include <cstdlib>
template<typename... T>
[[noreturn]] void die(const char *msg, T... args) {
std::fprintf(stderr, msg, args...);
std::putchar('\n');
std::exit(1);
}
int readint(int min, int max) {
int c = getchar();
if(c < '0' || c > '9')
die("'0' to '9' expected. char %d found", c);
ungetc(c, stdin);
int x;
if(std::scanf("%d", &x) != 1)
die("%%d expected");
if(x < min || max < x)
die("%d is out of range [%d, %d]", x, min, max);
return x;
}
void expectchar(int c) {
int d = getchar();
if(c != d)
die("char %d expected. char %d found", c, d);
}
int main() {
int t = readint(1, 1000);
expectchar('\n');
for(int i = 0; i < t; i++) {
readint(1, 1000);
expectchar(' ');
readint(1, 1000);
expectchar('\n');
}
expectchar(EOF);
}
#!/usr/bin/env bash
# You need to make this file executable (i.e. chmod +x test.sh).
# Hopefully you won't need to change this file, except to change the timeout at line 13.
temp=$(mktemp)
trap "rm $temp" INT TERM EXIT
find allsolution/ -type f -executable | sort | while read file; do
find testdata/ -type f -name \*input\* | sort | while read input; do
echo -n "$file < $input: "
output=$(echo $input | sed s/input/output/)
timeout 1s ./$file < $input > $temp && { cmp -s $temp $output && echo AC || echo WA; } || echo "RE/TLE"
done
done
// This file is usually same as solution.cpp.
// You can put other intended solutions at allsolution/ac2.cpp, allsolution/ac3.cpp, ...
// Keep this one to make sure tester.sh is working.
#include <cstdio>
int main() {
int t;
std::scanf("%d", &t);
while(t--) {
int x, y;
std::scanf("%d %d", &x, &y);
std::printf("%d\n", x + y);
}
}
// You should put "more plausible" bad solutions at allsolution/wa2.cpp, allsolution/wa3.cpp, ...
// Keep this one to make sure tester.sh is working.
int main() { /* do nothing */ }
// You should put "more plausible" bad solutions at allsolution/tle2.cpp, allsolution/tle3.cpp, ...
// Keep this one to make sure tester.sh is working.
int main() { for(;;); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment