Skip to content
Open
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
32 changes: 32 additions & 0 deletions AdvancedLevel_C++/1168. Prime Day (20).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
char input[10];
bool is_prime(int n) {
if (n <= 1) return false;
else if (n == 2) return true;
int max_factor = sqrt(n) + 1;
for (int factor = 2; factor <= max_factor; ++factor)
if (n % factor == 0) return false;
return true;
}

int main() {
cin.getline(input, 10);
bool all_prime = true;
const char* cur_str = input;
while (*cur_str) {
cout << cur_str << " ";
if (is_prime(atoi(cur_str)))
cout << "Yes" << endl;
else {
cout << "No" << endl;
all_prime = false;
}
++cur_str;
}
if (all_prime)
cout << "All Prime!" << endl;
return 0;
}
56 changes: 56 additions & 0 deletions AdvancedLevel_C++/1169. The Judger (25).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <iostream>
#include <cstdio>
#include <vector>
#include <unordered_set>
using namespace std;

unsigned int num1, num2; // The beginning numbers.
size_t N, M; // N players and M rounds.
unordered_set<unsigned int> nums; // The numbers that have shown up.
unsigned int plays[10][1000]; // plays[i][j] implies the i-th player's
// play in the j-th round.
int main() {
cin >> num1 >> num2;
nums.insert(num1); nums.insert(num2);
cin >> N >> M;
for (size_t i = 0; i < N; ++i)
for (size_t j = 0; j < M; ++j)
cin >> plays[i][j];

vector<bool> is_out(N, false);
for (size_t j = 0; j < M; ++j) {
for (size_t i = 0; i < N; ++i) {
if (is_out[i]) continue;
unsigned int cur_play = plays[i][j];
bool is_ok = false;
if (nums.find(cur_play) == nums.end()) {
for (unsigned int num : nums) {
if (nums.find(num + cur_play) != nums.end()) {
nums.insert(cur_play);
is_ok = true;
break;
}
}
}
if (!is_ok) {
is_out[i] = true;
printf("Round #%u: %u is out.\n", j + 1, i + 1);
}
}
}

vector<size_t> winners;
for (size_t i = 0; i < N; ++i)
if (!is_out[i]) winners.push_back(i);

if (winners.empty())
cout << "No winner." << endl;
else {
cout << "Winner(s):";
for (size_t winner : winners)
cout << " " << winner + 1;
cout << endl;
}
return 0;
}

49 changes: 49 additions & 0 deletions AdvancedLevel_C++/1170. Safari Park (25).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <iostream>
#include <vector>
#include <utility>
#include <unordered_set>
using namespace std;

size_t N; // The number of regions.
size_t R; // The number of neighboring relations.
size_t K; // The number of species of animals.
size_t M; // The number of distribution plans.
vector<pair<size_t, size_t>> relations;
vector<vector<size_t>> plans;

int main() {
cin >> N >> R >> K;
size_t r1, r2;
for (size_t i = 0; i < R; ++i) {
cin >> r1 >> r2;
relations.emplace_back(--r1, --r2);
}
cin >> M;
plans.resize(M);
for (size_t i = 0; i < M; ++i) {
plans[i].resize(N);
for (size_t j = 0; j < N; ++j) cin >> plans[i][j];
}

for (vector<size_t>& plan : plans) {
unordered_set<size_t> species;
for (size_t s : plan) species.insert(s);
if (species.size() < K)
cout << "Error: Too few species." << endl;
else if (species.size() > K)
cout << "Error: Too many species." << endl;
else {
bool is_ok = true;
for (pair<size_t, size_t> relation: relations) {
size_t r1 = relation.first;
size_t r2 = relation.second;
if (plan[r1] == plan[r2]) {
is_ok = false;
break;
}
}
cout << (is_ok ? "Yes" : "No") << endl;
}
}
return 0;
}
70 changes: 70 additions & 0 deletions AdvancedLevel_C++/1171. Replacement Selection (30).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

size_t N; // The total number of records.
size_t M; // The capacity of the internal memory.
vector<int> tape; // tape[i] indicates the i-th record.
vector<vector<int>> runs; // runs[i] indicates the i-th run.

int main() {
cin >> N >> M;
tape.resize(N);
for (size_t i = 0; i < N; ++i) cin >> tape[i];

while (!tape.empty()) {
vector<int> run;
vector<int> left;
priority_queue<int, vector<int>, greater<int>> qu;

// The number of records that stay till the next run.
size_t stay = 0;

size_t i = 0;
for (; i < tape.size() && i < M; ++i)
qu.push(tape[i]);
for (; i < tape.size(); ++i) {
if (stay == M) {
// Memory is full of staying records left to the next run.
left.push_back(tape[i]);
continue;
}
else if (qu.size() + stay == M) {
// Pop the top to spare a memory unit for tape[i].
int t = qu.top();
run.push_back(t);
qu.pop();
}

if (!run.empty() && tape[i] < run.back()) {
// tape[i] needs to stay till the next run.
left.push_back(tape[i]);
++stay;
}
else {
// tape[i] belongs to the current run.
qu.push(tape[i]);
}
}

while (!qu.empty()) {
int t = qu.top();
qu.pop();
run.push_back(t);
}

tape = left;
runs.emplace_back(run);
}

// Print runs.
for (vector<int>& run : runs) {
for (auto ri = run.cbegin(); ri != run.cend(); ++ri) {
cout << *ri;
if (ri + 1 == run.cend()) cout << endl;
else cout << " ";
}
}
return 0;
}
45 changes: 45 additions & 0 deletions BasicLevel_C++/1111. 对称日 (15).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <iostream>
#include <string>
#include <cassert>
#include <cctype>
#include <vector>
#include <unordered_map>
using namespace std;

size_t N; // The number of dates.
unordered_map<string, string> ump = {
{"Jan", "01"}, {"Feb", "02"}, {"Mar", "03"},
{"Apr", "04"}, {"May", "05"}, {"Jun", "06"},
{"Jul", "07"}, {"Aug", "08"}, {"Sep", "09"},
{"Oct", "10"}, {"Nov", "11"}, {"Dec", "12"}
};

int main() {
cin >> N; cin.get(); // Absorb the Enter('\n').
for (size_t i = 0; i < N; ++i) {
string inp;
getline(cin, inp);
vector<string> v; // Split the input to 3 strings.
string::iterator p = inp.begin(), q = p;
while (true) {
if (q == inp.end() || !isalnum(*q)) {
// *q is neither a digit nor a letter.
if (p < q) v.emplace_back(p, q);
if (q == inp.end()) break;
else p = q + 1;
}
++q;
}
assert(v.size() == 3); // month, day, year.

while (v[1].size() < 2) v[1] = "0" + v[1]; // dd
while (v[2].size() < 4) v[2] = "0" + v[2]; // yyyy
string fmt = v[2] + ump[v[0]] + v[1]; // yyyymmdd format
string rev(fmt.rbegin(), fmt.rend()); // fmt reversed

if (fmt == rev) cout << "Y ";
else cout << "N ";
cout << fmt << endl;
}
return 0;
}
50 changes: 50 additions & 0 deletions BasicLevel_C++/1112. 超标区间 (20).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <iostream>
#include <vector>
#include <utility>
using namespace std;

size_t N; // The number of data points.
int T; // The threshold.

int main() {
cin >> N >> T;
vector<int> points(N);
for (size_t i = 0; i < N; ++i)
cin >> points[i];

using Segment_t = pair<size_t, size_t>; // Segment type: [first, second]
vector<Segment_t> segments;

bool active = false; // Indicates whether a segment is on recording.
size_t start = 0, end = 0;
int max_point = -1;
for (size_t i = 0; i < N; ++i) {
if (points[i] > max_point)
max_point = points[i];
if (points[i] > T) {
if (active) continue;
else {
active = true; // Start a record.
start = i;
}
}
else {
if (active) {
active = false; // End the current active record.
end = i;
segments.emplace_back(start, end - 1);
}
else continue;
}
}
if (active) {
// End the last record.
active = false;
end = N;
segments.emplace_back(start, end - 1);
}
if (segments.empty()) cout << max_point << endl;
for (Segment_t seg : segments)
cout << "[" << seg.first << ", " << seg.second << "]" << endl;
return 0;
}
55 changes: 55 additions & 0 deletions BasicLevel_C++/1113. 钱串子的假发 (20).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <iostream>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;
const size_t MAX_DIGIT = 100002; // plus 2 for '\0' and possible carry.
char* n1, * n2;

// Calculate a + b, and store the answer to a.
void add(char* const a, char* const b) {
size_t len_a = strlen(a);
size_t len_b = strlen(b);
reverse(a, a + len_a); // low digit, low index.
reverse(b, b + len_b);

unsigned char carry = 0; // Carry Flag
char* p = a, * q = b;
while (*p != '\0' && *q != '\0') {
*p = isdigit(*p) ? *p - '0' : *p - 'a' + 10;
*q = isdigit(*q) ? *q - '0' : *q - 'a' + 10;
*p = *p + *q + carry;
carry = *p / 30;
*p %= 30;
++p; ++q;
}
char* left = (*p == '\0' ? q : p);
while (*left != '\0') {
*left = isdigit(*left) ? *left - '0' : *left - 'a' + 10;
*p = *left + carry;
carry = *p / 30;
*p %= 30;
++p; ++left;
}

if (carry) { *p = carry; ++p; } // Possibly carry to a higher digit.
*p = '\0';
while (p > a && *(p - 1) == '\0') --p; // Remove high digit 0.
if (a == p) { *a = '0'; } // In case answer is zero.
reverse(a, p);
reverse(b, b + len_b);
for (char* t = a; t != p; ++t) { // Convert to show-up string.
if (*t < 10) *t += '0';
else *t += 'a' - 10;
}
}

int main() {
n1 = new char[MAX_DIGIT];
n2 = new char[MAX_DIGIT];
cin >> n1 >> n2;
add(n1, n2);
cout << n1 << endl;
delete[] n1, n2;
return 0;
}
32 changes: 32 additions & 0 deletions BasicLevel_C++/1114. 全素日 (20).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
char input[10];
bool is_prime(int n) {
if (n <= 1) return false;
else if (n == 2) return true;
int max_factor = sqrt(n) + 1;
for (int factor = 2; factor <= max_factor; ++factor)
if (n % factor == 0) return false;
return true;
}

int main() {
cin.getline(input, 10);
bool all_prime = true;
const char* cur_str = input;
while (*cur_str) {
cout << cur_str << " ";
if (is_prime(atoi(cur_str)))
cout << "Yes" << endl;
else {
cout << "No" << endl;
all_prime = false;
}
++cur_str;
}
if (all_prime)
cout << "All Prime!" << endl;
return 0;
}
Loading