Skip to content

Commit fce6197

Browse files
authored
GH-42: Add codeforces 1592B (#95)
1 parent 8486867 commit fce6197

File tree

10 files changed

+213
-0
lines changed

10 files changed

+213
-0
lines changed

problems/anhv_practice.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ anhv;codeforces50A;2022-12-06;practice;success
1717
anhv;codeforces118A;2022-12-06;practice;success
1818
anhv;codeforces492B;2022-12-05;practice;editorial-success
1919
anhv;codeforces520B;2022-12-05;practice;fail
20+
anhv;codeforces1592B;2022-12-05;practice;not-understand

problems/codeforces1592B/.gitginore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
solution

problems/codeforces1592B/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Problem
2+
3+
## Usage
4+
5+
Run program with an example
6+
7+
```
8+
bazel run src/main:solution < tests/data/1.in
9+
```
10+
11+
Test program
12+
13+
```
14+
# Run all tests
15+
bazel test --test_output=all tests:solution_test
16+
bazel test --test_output=all --cache_test_results=no tests:solution_test
17+
# Run test with pecific test id
18+
bazel test --test_output=all tests:solution_test --test_arg=1
19+
```

problems/codeforces1592B/run_tests.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
for f in $(cd tests/; ls *.in); do
2+
test_id=$(echo $f | cut -d'.' -f1)
3+
./solution < tests/$test_id.in | diff -aur tests/$test_id.out -
4+
done
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_binary")
2+
3+
cc_binary(
4+
name = "solution",
5+
srcs = ["solution.cpp"],
6+
visibility = ["//visibility:public"],
7+
)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
4+
using namespace std;
5+
6+
#define long long long
7+
#define Integer_MAX_VALUE 0x7fffffff
8+
#define Integer_MIN_VALUE 0x80000000
9+
#define Long_MAX_VALUE 0x7fffffffffffffffL
10+
#define Long_MIN_VALUE 0x8000000000000000L
11+
12+
using namespace std;
13+
14+
#define max_m 200000
15+
#define small 450
16+
17+
int cycle[small][small];
18+
int manual[max_m]{0};
19+
int record[max_m + 1];
20+
int x[max_m + 1];
21+
int y[max_m + 1];
22+
23+
struct Solution {
24+
void run() {
25+
int n, x;
26+
cin >> n >> x;
27+
int a[n];
28+
int order[n];
29+
for (int i = 0; i < n; i++) {
30+
cin >> a[i];
31+
order[i] = a[i];
32+
}
33+
sort(order, order + n);
34+
35+
for (int i = 0; i < n; i++) {
36+
if (order[i] == a[i]) {
37+
continue;
38+
}
39+
40+
if (i < x && n - 1 - i < x) {
41+
cout << "NO" << endl;
42+
return;
43+
}
44+
}
45+
cout << "YES" << endl;
46+
}
47+
};
48+
49+
int main() {
50+
ios_base::sync_with_stdio(false);
51+
52+
Solution solution = Solution();
53+
54+
int t;
55+
cin >> t;
56+
for (int i = 0; i < t; i++) {
57+
solution.run();
58+
}
59+
return 0;
60+
}

problems/codeforces1592B/tests/BUILD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_test")
2+
3+
cc_test(
4+
name = "solution_test",
5+
srcs = ["solution_test.cpp"],
6+
deps = [
7+
"//problems/codeforces1592B/src/main:solution",
8+
"@com_google_googletest//:gtest_main",
9+
],
10+
data = ["data"]
11+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
4
2+
3 3
3+
3 2 1
4+
4 3
5+
1 2 3 4
6+
5 2
7+
5 1 2 3 4
8+
5 4
9+
1 2 3 4 4
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
NO
2+
YES
3+
YES
4+
YES
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include <cstdlib>
2+
#include <filesystem>
3+
#include <fstream>
4+
#include <iostream>
5+
#include <string>
6+
#include <string_view>
7+
#include <vector>
8+
9+
#include "gtest/gtest.h"
10+
11+
namespace fs = std::filesystem;
12+
13+
using namespace std;
14+
15+
std::string TEST_ID = "";
16+
17+
vector<string> split(string s, string delimiter) {
18+
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
19+
string token;
20+
vector<string> res;
21+
22+
while ((pos_end = s.find(delimiter, pos_start)) != string::npos) {
23+
token = s.substr(pos_start, pos_end - pos_start);
24+
pos_start = pos_end + delim_len;
25+
res.push_back(token);
26+
}
27+
28+
res.push_back(s.substr(pos_start));
29+
return res;
30+
}
31+
32+
std::string GetHello(std::string_view in) {
33+
if (in.size() == 0) {
34+
return std::string("hello, word");
35+
} else {
36+
return std::string("Hello, ") + in.data();
37+
}
38+
}
39+
40+
string ReadFile(const std::string &filename) {
41+
std::ifstream f(filename);
42+
string s((std::istreambuf_iterator<char>(f)),
43+
std::istreambuf_iterator<char>());
44+
return s;
45+
}
46+
47+
vector<string> RunProgram(string fileid) {
48+
vector<string> v;
49+
string program = "problems/codeforces1592B/src/main/solution";
50+
string test_folder = "problems/codeforces1592B/tests/data/";
51+
string command = program + " < " + test_folder + fileid + ".in > output.txt";
52+
std::system(command.c_str());
53+
string actual = ReadFile("output.txt");
54+
string expectedOutputFile = test_folder + fileid + ".out";
55+
string expected = ReadFile(expectedOutputFile);
56+
v.push_back(actual);
57+
v.push_back(expected);
58+
return v;
59+
}
60+
61+
TEST(RunTest, AllTestCases) {
62+
if (TEST_ID != "") {
63+
std::cout << "TEST_ID = " << TEST_ID << std::endl;
64+
} else {
65+
std::cout << "RUN ALL TESTS" << std::endl;
66+
}
67+
68+
std::string test_data_folder = "problems/codeforces1592B/tests/data";
69+
for (const auto &entry : fs::directory_iterator(test_data_folder)) {
70+
string filename = split(entry.path(), "/").back();
71+
vector<string> v = split(filename, ".");
72+
string fileid = v[0];
73+
string extension = v[1];
74+
if (extension != "in"){
75+
continue;
76+
}
77+
if (TEST_ID != ""){
78+
if (fileid != TEST_ID){
79+
continue;
80+
}
81+
}
82+
// Run Test Case
83+
vector<string> output = RunProgram(fileid);
84+
string actual = output[0];
85+
string expected = output[1];
86+
string message = "❌ FAIL CASE: " + fileid;
87+
ASSERT_EQ(actual, expected) << message;
88+
}
89+
}
90+
91+
int main(int argc, char **argv) {
92+
::testing::InitGoogleTest(&argc, argv);
93+
if (argc == 2) {
94+
TEST_ID = argv[1];
95+
}
96+
return RUN_ALL_TESTS();
97+
}

0 commit comments

Comments
 (0)