Skip to content

GH-42: Add codeforces 1592B #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions problems/anhv_practice.csv
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ anhv;codeforces50A;2022-12-06;practice;success
anhv;codeforces118A;2022-12-06;practice;success
anhv;codeforces492B;2022-12-05;practice;editorial-success
anhv;codeforces520B;2022-12-05;practice;fail
anhv;codeforces1592B;2022-12-05;practice;not-understand
1 change: 1 addition & 0 deletions problems/codeforces1592B/.gitginore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
solution
19 changes: 19 additions & 0 deletions problems/codeforces1592B/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Problem

## Usage

Run program with an example

```
bazel run src/main:solution < tests/data/1.in
```

Test program

```
# Run all tests
bazel test --test_output=all tests:solution_test
bazel test --test_output=all --cache_test_results=no tests:solution_test
# Run test with pecific test id
bazel test --test_output=all tests:solution_test --test_arg=1
```
4 changes: 4 additions & 0 deletions problems/codeforces1592B/run_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
for f in $(cd tests/; ls *.in); do
test_id=$(echo $f | cut -d'.' -f1)
./solution < tests/$test_id.in | diff -aur tests/$test_id.out -
done
7 changes: 7 additions & 0 deletions problems/codeforces1592B/src/main/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load("@rules_cc//cc:defs.bzl", "cc_binary")

cc_binary(
name = "solution",
srcs = ["solution.cpp"],
visibility = ["//visibility:public"],
)
60 changes: 60 additions & 0 deletions problems/codeforces1592B/src/main/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <iostream>
#include <algorithm>

using namespace std;

#define long long long
#define Integer_MAX_VALUE 0x7fffffff
#define Integer_MIN_VALUE 0x80000000
#define Long_MAX_VALUE 0x7fffffffffffffffL
#define Long_MIN_VALUE 0x8000000000000000L

using namespace std;

#define max_m 200000
#define small 450

int cycle[small][small];
int manual[max_m]{0};
int record[max_m + 1];
int x[max_m + 1];
int y[max_m + 1];

struct Solution {
void run() {
int n, x;
cin >> n >> x;
int a[n];
int order[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
order[i] = a[i];
}
sort(order, order + n);

for (int i = 0; i < n; i++) {
if (order[i] == a[i]) {
continue;
}

if (i < x && n - 1 - i < x) {
cout << "NO" << endl;
return;
}
}
cout << "YES" << endl;
}
};

int main() {
ios_base::sync_with_stdio(false);

Solution solution = Solution();

int t;
cin >> t;
for (int i = 0; i < t; i++) {
solution.run();
}
return 0;
}
11 changes: 11 additions & 0 deletions problems/codeforces1592B/tests/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_test")

cc_test(
name = "solution_test",
srcs = ["solution_test.cpp"],
deps = [
"//problems/codeforces1592B/src/main:solution",
"@com_google_googletest//:gtest_main",
],
data = ["data"]
)
9 changes: 9 additions & 0 deletions problems/codeforces1592B/tests/data/1.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
4
3 3
3 2 1
4 3
1 2 3 4
5 2
5 1 2 3 4
5 4
1 2 3 4 4
4 changes: 4 additions & 0 deletions problems/codeforces1592B/tests/data/1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
NO
YES
YES
YES
97 changes: 97 additions & 0 deletions problems/codeforces1592B/tests/solution_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <string_view>
#include <vector>

#include "gtest/gtest.h"

namespace fs = std::filesystem;

using namespace std;

std::string TEST_ID = "";

vector<string> split(string s, string delimiter) {
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
string token;
vector<string> res;

while ((pos_end = s.find(delimiter, pos_start)) != string::npos) {
token = s.substr(pos_start, pos_end - pos_start);
pos_start = pos_end + delim_len;
res.push_back(token);
}

res.push_back(s.substr(pos_start));
return res;
}

std::string GetHello(std::string_view in) {
if (in.size() == 0) {
return std::string("hello, word");
} else {
return std::string("Hello, ") + in.data();
}
}

string ReadFile(const std::string &filename) {
std::ifstream f(filename);
string s((std::istreambuf_iterator<char>(f)),
std::istreambuf_iterator<char>());
return s;
}

vector<string> RunProgram(string fileid) {
vector<string> v;
string program = "problems/codeforces1592B/src/main/solution";
string test_folder = "problems/codeforces1592B/tests/data/";
string command = program + " < " + test_folder + fileid + ".in > output.txt";
std::system(command.c_str());
string actual = ReadFile("output.txt");
string expectedOutputFile = test_folder + fileid + ".out";
string expected = ReadFile(expectedOutputFile);
v.push_back(actual);
v.push_back(expected);
return v;
}

TEST(RunTest, AllTestCases) {
if (TEST_ID != "") {
std::cout << "TEST_ID = " << TEST_ID << std::endl;
} else {
std::cout << "RUN ALL TESTS" << std::endl;
}

std::string test_data_folder = "problems/codeforces1592B/tests/data";
for (const auto &entry : fs::directory_iterator(test_data_folder)) {
string filename = split(entry.path(), "/").back();
vector<string> v = split(filename, ".");
string fileid = v[0];
string extension = v[1];
if (extension != "in"){
continue;
}
if (TEST_ID != ""){
if (fileid != TEST_ID){
continue;
}
}
// Run Test Case
vector<string> output = RunProgram(fileid);
string actual = output[0];
string expected = output[1];
string message = "❌ FAIL CASE: " + fileid;
ASSERT_EQ(actual, expected) << message;
}
}

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
if (argc == 2) {
TEST_ID = argv[1];
}
return RUN_ALL_TESTS();
}