本人计概上半个学期的作业做完了

This commit is contained in:
rogerwang2008 committed 2026-10-19 19:01:58 +08:00
1 parent e0c09e0c7b
commit 0cfdc8499c
28 files changed
+1544 -125

No files matched your search

@@ -1,5 +1,4 @@
#include <iostream>
#include <math.h>
constexpr int DAYS_OF_MONTHS[13] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
+98
View File
@@ -0,0 +1,98 @@
#include <iostream>
#include <sstream>
#include <unordered_map>
constexpr int DAYS_OF_MONTHS[13] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const std::unordered_map<std::string, int> MONTHS_DICT = {
{"Jan", 1},
{"Feb", 2},
{"Mar", 3},
{"Apr", 4},
{"May", 5},
{"Jun", 6},
{"Jul", 7},
{"Aug", 8},
{"Sep", 9},
{"Oct", 10},
{"Nov", 11},
{"Dec", 12},
};
const std::unordered_map<int, std::string> DAYS_ENG = {
{1, "Mon"},
{2, "Tue"},
{3, "Wed"},
{4, "Thu"},
{5, "Fri"},
{6, "Sat"},
{7, "Sun"},
};
constexpr bool is_leap(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
constexpr int get_days_of_month(int year, int month) {
if (month == 2) { return is_leap(year) ? 29 : 28; }
return DAYS_OF_MONTHS[month];
}
constexpr int get_days_of_year(int year) {
return is_leap(year) ? 366 : 365;
}
constexpr int calc_passed_days(int year, int month, int day) {
int passed_days = 0;
for (int m = 1; m < month; m++) {
passed_days += get_days_of_month(year, m);
}
return passed_days + day;
}
constexpr int calc_remaining_days(int year, int month, int day) {
return get_days_of_year(year) - calc_passed_days(year, month, day);
}
constexpr int calc_days_delta(int year1, int month1, int day1,
int year2, int month2, int day2) {
int days_delta = 0;
if (year1 == year2) {
return calc_passed_days(year2, month2, day2) - calc_passed_days(year1, month1, day1);
}
for (int y = year1 + 1; y < year2; y++) {
days_delta += get_days_of_year(y);
}
return days_delta
+ calc_remaining_days(year1, month1, day1)
+ calc_passed_days(year2, month2, day2);
}
std::tuple<int, int, int> parse_date(std::string s) {
std::istringstream ss(s);
std::string token;
int y, m, d;
while (std::getline(ss, token, '-')) {
if (token.size() == 4) {
y = std::stoi(token);
} else if (token.size() == 2) {
d = std::stoi(token);
} else {
m = MONTHS_DICT.at(token);
}
}
return std::make_tuple(y, m, d);
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::string date_string;
std::cin >> date_string;
while (std::cin >> date_string) {
auto [y, m, d] = parse_date(date_string);
int delta = calc_days_delta(1900, 1, 1, y, m, d);
std::cout << DAYS_ENG.at(delta % 7 + 1) << '.' << std::endl;
}
return 0;
}
@@ -0,0 +1,48 @@
#include <algorithm>
#include <array>
#include <iostream>
constexpr std::size_t GRID_SIZE = 5;
std::array<std::array<int, GRID_SIZE>, GRID_SIZE> grid;
std::array<int, GRID_SIZE> row_max;
std::array<int, GRID_SIZE> col_min;
void init_maxs() {
for (int i = 0; i < GRID_SIZE; i++) {
row_max[i] = *std::max_element(grid[i].begin(), grid[i].end());
}
for (int j = 0; j < GRID_SIZE; j++) {
col_min[j] = grid[0][j];
for (int i = 1; i < GRID_SIZE; i++) {
col_min[j] = std::min(col_min[j], grid[i][j]);
}
}
}
void print_ans() {
for (int i = 0; i < GRID_SIZE; i++) {
for (int j = 0; j < GRID_SIZE; j++) {
if (grid[i][j] == row_max[i] && grid[i][j] == col_min[j]) {
std::cout << i + 1 << " " << j + 1 << " " << grid[i][j] << std::endl;
return;
}
}
}
std::cout << "not found" << std::endl;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
for (int i = 0; i < GRID_SIZE; i++) {
for (int j = 0; j < GRID_SIZE; j++) {
std::cin >> grid[i][j];
}
}
init_maxs();
print_ans();
return 0;
}
@@ -0,0 +1,69 @@
#include <algorithm>
#include <array>
#include <iostream>
using RopeData = std::pair<int, int>;
constexpr std::size_t N = 5;
int max_height;
std::array<RopeData, N> rope_counts{};
void print_solution() {
std::array<RopeData, N> rope_lengths{};
for (int i = 0; i < N; i++) {
rope_lengths[i].first = rope_counts[i].first;
}
for (int height = 1; height <= max_height; height++) {
for (rope_lengths[0].second = 0;
rope_lengths[0].second < height / rope_counts[0].second;
rope_lengths[0].second++) {
if (height == 721 && rope_lengths[0].second == 265) {
std::clog << height << ' ' << rope_lengths[0].second << std::endl;
}
bool is_valid = true;
for (int i = 1; i < N; i++) {
rope_lengths[i].second = height - rope_lengths[i - 1].second * rope_counts[i - 1].second;
if (rope_lengths[i].second <= 0 || rope_lengths[i].second == rope_lengths[i - 1].second) {
is_valid = false;
break;
}
}
if (height - rope_lengths[N - 1].second * rope_counts[N - 1].second != rope_lengths[0].second) {
is_valid = false;
}
if (is_valid) {
// std::sort(rope_lengths.begin(), rope_lengths.end(), std::greater<>());
std::cout << height;
for (const auto& rope : rope_lengths) {
std::cout << ' ' << rope.second;
}
std::cout << std::endl;
return;
}
}
}
std::cout << "not found" << std::endl;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> max_height;
max_height *= 100;
for (int i = 0; i < N; i++) {
rope_counts[i].first = i;
std::cin >> rope_counts[i].second;
}
// std::sort(rope_counts.begin(), rope_counts.end(), std::greater<>());
print_solution();
return 0;
}
@@ -0,0 +1,63 @@
#include <array>
#include <iostream>
constexpr std::size_t ORDER = 5;
std::array<std::array<int, ORDER>, ORDER> matrix;
std::ostream& operator<<(std::ostream& os, const std::array<int, ORDER>& row) {
for (int i = 0; i < ORDER; i++) {
os << row[i] << " ";
}
os << std::endl;
return os;
}
std::ostream& operator<<(std::ostream& os, const std::array<std::array<int, ORDER>, ORDER>& matrix) {
for (const auto& row : matrix) {
os << row;
}
return os;
}
void simplify_matrix() {
for (int i = 1; i < ORDER; i++) {
for (int j = i; j < ORDER; j++) {
if (matrix[j][i - 1] != 0) {
int temp = matrix[j][i - 1];
for (int k = 0; k < ORDER; k++) {
matrix[j][k] = matrix[j][k] * matrix[i - 1][i - 1] - matrix[i - 1][k] * temp;
}
}
std::clog << matrix << std::endl;
}
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int max_height;
std::array<int, ORDER> values{};
std::cin >> max_height;
max_height *= 100;
for (int i = 0; i < ORDER; i++) {
std::cin >> values[i];
}
for (int i = 0; i < ORDER; i++) {
matrix[i][i] = values[i];
matrix[i][(i + 1) % ORDER] = 1 - values[(i + 1) % ORDER];
matrix[i][(i + 2) % ORDER] = -1;
}
std::clog << matrix << std::endl;
simplify_matrix();
std::clog << matrix << std::endl;
return 0;
}
+26
View File
@@ -0,0 +1,26 @@
#include <array>
#include <iostream>
using Person = char;
using Order = std::string;
bool judge(Order order, Person a, Person b) {
return order.find(a) < order.find(b);
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
for (auto order : {"ABC", "ACB", "BAC", "BCA", "CAB", "CBA"}) {
std::array<int, 3> judges{};
judges[0] = judge(order, 'A', 'B');
judges[1] = judge(order, 'B', 'A') + judge(order, 'C', 'A');
judges[2] = judge(order, 'B', 'C') + judge(order, 'A', 'B');
if (judges[order[0] - 'A'] > judges[order[1] - 'A'] &&
judges[order[1] - 'A'] > judges[order[2] - 'A']) {
std::cout << order << std::endl;
}
}
return 0;
}
+23
View File
@@ -0,0 +1,23 @@
#include <iostream>
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::string line;
std::getline(std::cin, line);
line += ' ';
for (int i = 0; i < line.size(); i++) {
if (line[i] == ' ') {
for (int j = i - 1; j >= 0 && line[j] != ' '; j--) {
std::cout << line[j];
}
if (i != line.size() - 1) {
std::cout << line[i];
}
}
}
return 0;
}
+60
View File
@@ -0,0 +1,60 @@
#include <algorithm>
#include <iostream>
using BasedNumber = std::string;
constexpr short char_to_digit(const char c) {
if (c >= '0' && c <= '9') {
return c - '0';
}
if (c >= 'a' && c <= 'f') {
return c - 'a' + 10;
}
return -1;
}
constexpr char digit_to_char(const short d) {
if (d >= 0 && d <= 9) {
return static_cast<char>('0' + d);
}
if (d >= 10 && d <= 15) {
return static_cast<char>('a' + d - 10);
}
return '?';
}
unsigned long long based_to_dec(BasedNumber based_number, unsigned short base) {
unsigned long long dec = 0;
for (int i = 0; i < based_number.size(); i++) {
dec *= base;
dec += char_to_digit(based_number[i]);
}
return dec;
}
BasedNumber dec_to_based(unsigned long long dec, unsigned short base) {
BasedNumber based_number;
while (dec > 0) {
based_number.push_back(digit_to_char(dec % base));
dec /= base;
}
std::reverse(based_number.begin(), based_number.end());
return based_number;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::string num1, num2;
unsigned short base1, base2, base3;
std::cin >> base1 >> num1 >> base2 >> num2 >> base3;
unsigned long long dec1 = based_to_dec(num1, base1);
unsigned long long dec2 = based_to_dec(num2, base2);
unsigned long long dec3 = dec1 + dec2;
std::clog << dec1 << " " << dec2 << " " << dec3 << std::endl;
std::cout << dec_to_based(dec3, base3) << std::endl;
return 0;
}
+32
View File
@@ -0,0 +1,32 @@
#include <iostream>
#include <sstream>
bool is_abbr(std::string target, std::string original) {
if (target.size() >= original.size()) { return false; }
int i = 0, j = 0;
for (; i < target.size() && j < original.size(); j++) {
if (target[i] == original[j]) { i++; }
}
if (i == target.size()) { return true; }
return false;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::string temp;
std::getline(std::cin, temp);
int n = std::stoi(temp);
for (int _ = 0; _ < n; _++) {
std::string target, original;
std::getline(std::cin, temp);
std::istringstream ss(temp);
std::getline(ss, target, ':');
std::getline(ss, original);
std::cout << (is_abbr(target, original) ? "YES" : "NO") << std::endl;
}
return 0;
}
+55
View File
@@ -0,0 +1,55 @@
#include <iostream>
#include <sstream>
#include <vector>
std::vector<int> prefix_function(const std::string& s) {
std::vector<int> pi(s.size());
for (int i = 1; i < s.size(); i++) {
int j = pi[i - 1];
while (j > 0 && s[i] != s[j]) {
j = pi[j - 1];
}
if (s[i] == s[j]) { j++; }
pi[i] = j;
}
return pi;
}
std::vector<int> find_substr_pos(const std::string& s, const std::string& subs) {
std::string concat_string = subs + '\0' + s;
auto pi = prefix_function(concat_string);
std::vector<int> positions;
for (int i = subs.size(); i < concat_string.size(); i++) {
if (pi[i] == subs.size()) { positions.push_back(i - 2 * subs.size()); }
}
return positions;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::string line;
std::string s, subs1, subs2;
while (std::getline(std::cin, line)) {
std::istringstream iss(line);
std::getline(iss, s, ',');
std::getline(iss, subs1, ',');
std::getline(iss, subs2, ',');
auto subs1_pos = find_substr_pos(s, subs1), subs2_pos = find_substr_pos(s, subs2);
if (subs1.empty() || subs2.empty()) {
std::cout << -1 << std::endl;
continue;
}
auto start_pos = subs1_pos[0] + subs1.size();
auto end_pos = subs2_pos[subs2_pos.size() - 1];
if (start_pos > end_pos) {
std::cout << -1 << std::endl;
} else {
std::cout << end_pos - start_pos << std::endl;
}
}
return 0;
}
@@ -0,0 +1,28 @@
#include <algorithm>
#include <iostream>
std::string calc_sequence(int n) {
std::string sequence;
while (n) {
if (n % 2) {
sequence += 'N';
n = (n - 1) / 2;
} else {
sequence += 'G';
n = (n - 2) / 2;
}
}
std::reverse(sequence.begin(), sequence.end());
return sequence;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
std::cout << calc_sequence(n) << std::endl;
return 0;
}
+49
View File
@@ -0,0 +1,49 @@
#include <iostream>
constexpr bool is_leap(int year) {
if (year % 40000 == 0) { return false; }
if (year % 10000 == 0) { return false; }
if (year % 400 == 0) { return true; }
if (year % 100 == 0) { return false; }
return year % 4 == 0;
}
constexpr int get_days_in_month(int year, int month) {
switch (month) {
case 1: return 31;
case 2: return is_leap(year) ? 29 : 28;
case 3: return 31;
case 4: return 30;
case 5: return 31;
case 6: return 30;
case 7: return 31;
case 8: return 31;
case 9: return 30;
case 10: return 31;
case 11: return 30;
case 12: return 31;
default: throw std::invalid_argument("Invalid month");
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
for (int _ = 0; _ < n; _++) {
int year, month1, month2;
std::cin >> year >> month1 >> month2;
if (month1 > month2) {
std::swap(month1, month2);
}
int days = 0;
for (int m = month1; m < month2; m++) {
days += get_days_in_month(year, m);
}
std::cout << (days % 7 == 0 ? "YES" : "NO") << std::endl;
}
return 0;
}
+48
View File
@@ -0,0 +1,48 @@
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <vector>
template <typename T>
struct ElemWithIndex {
T value;
std::size_t idx;
bool operator<(ElemWithIndex other) const { return value < other.value; }
auto operator+(ElemWithIndex other) -> decltype(value + value) { return value + other.value; }
};
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
std::vector<ElemWithIndex<int>> times(n);
for (int i = 0; i < n; ++i) {
times[i].idx = i + 1;
std::cin >> times[i].value;
}
std::stable_sort(times.begin(), times.end());
bool first = true;
for (auto& [_, idx] : times) {
if (!first) { std::cout << " "; }
std::cout << idx;
first = false;
}
std::vector<int> wait_times(times.size());
for (int i = 1; i < times.size(); i++) {
wait_times[i] = wait_times[i - 1] + times[i - 1].value;
}
int sum = std::accumulate(wait_times.begin(), wait_times.end(), 0);
std::cout << std::endl << std::fixed << std::setprecision(2) << 1.0 * sum / times.size() << std::endl;
return 0;
}
+20
View File
@@ -0,0 +1,20 @@
#include <iostream>
#include <numeric>
int calc_max_ans(int n) {
for (int i = n / 2; i >= 1; i--) {
if (std::gcd(i, n) == 1) { return i; }
}
__builtin_unreachable();
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
std::cout << calc_max_ans(n) << std::endl;
return 0;
}
+35
View File
@@ -0,0 +1,35 @@
#include <algorithm>
#include <iostream>
#include <map>
void print_char_map(std::map<char, int> map, std::ostream& os) {
int max_num = std::max_element(map.begin(), map.end(),
[](auto a, auto b) { return a.second < b.second; })->second;
for (; max_num > 0; max_num--) {
std::string line = "#";
for (char c : "ABCDEFGHIJKLMNOPQRSTUVWXYZ") {
if (map[c] >= max_num) {
line += " *";
} else {
line += " ";
}
}
while (!line.empty() && line.back() == ' ') { line.pop_back(); }
std::cout << line << std::endl;
}
std::cout << "# A B C D E F G H I J K L M N O P Q R S T U V W X Y Z" << std::endl;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::map<char, int> char_map;
std::string line;
while (std::cin >> line) {
for (auto c : line) { char_map[c]++; }
}
print_char_map(char_map, std::cout);
return 0;
}
@@ -0,0 +1,80 @@
#include <iostream>
#include <vector>
using CharMatrix = std::vector<std::vector<char>>;
bool operator==(const CharMatrix& a, const CharMatrix& b) {
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < a.size(); j++) {
if (a[i][j] != b[i][j]) { return false; }
}
}
return true;
}
CharMatrix get_init_matrix(std::size_t n, char value = 0) {
return std::vector(n, std::vector<char>(n, value));
}
CharMatrix rotated_clockwise(const CharMatrix& matrix) {
CharMatrix new_matrix = get_init_matrix(matrix.size());
for (int i = 0; i < matrix.size(); i++) {
for (int j = 0; j < matrix.size(); j++) {
new_matrix[j][matrix.size() - i - 1] = matrix[i][j];
}
}
return new_matrix;
}
CharMatrix rotated_counterclockwise(const CharMatrix& matrix) {
CharMatrix new_matrix = get_init_matrix(matrix.size());
for (int i = 0; i < matrix.size(); i++) {
for (int j = 0; j < matrix.size(); j++) {
new_matrix[i][j] = matrix[j][matrix.size() - i - 1];
}
}
return new_matrix;
}
CharMatrix central_symmetry_transformed(const CharMatrix& matrix) {
CharMatrix new_matrix = get_init_matrix(matrix.size());
for (int i = 0; i < matrix.size(); i++) {
for (int j = 0; j < matrix.size(); j++) {
new_matrix[matrix.size() - i - 1][matrix.size() - j - 1] = matrix[i][j];
}
}
return new_matrix;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
CharMatrix old_matrix = get_init_matrix(n), new_matrix = get_init_matrix(n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
std::cin >> old_matrix[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
std::cin >> new_matrix[i][j];
}
}
if (new_matrix == old_matrix) {
std::cout << 4 << std::endl;
} else if (rotated_clockwise(old_matrix) == new_matrix) {
std::cout << 1 << std::endl;
} else if (rotated_counterclockwise(old_matrix) == new_matrix) {
std::cout << 2 << std::endl;
} else if (central_symmetry_transformed(old_matrix) == new_matrix) {
std::cout << 3 << std::endl;
} else {
std::cout << 5 << std::endl;
}
return 0;
}
+54
View File
@@ -0,0 +1,54 @@
#include <iostream>
#include <vector>
std::vector<int> primes;
std::vector<bool> is_not_prime;
void prime_sieve(int max_n) {
is_not_prime.resize(max_n + 1);
for (int i = 2; i * i <= max_n; i++) {
if (!is_not_prime[i])
for (int j = i + i; j <= max_n; j += i) {
is_not_prime[j] = true;
}
}
for (int i = 2; i <= max_n; i++) {
if (!is_not_prime[i]) {
primes.push_back(i);
}
}
}
std::vector<int> get_prime_factors(int x) {
std::vector<int> prime_factors;
for (int prime : primes) {
if (x % prime == 0) {
prime_factors.push_back(prime);
while (x && x % prime == 0) { x /= prime; }
}
if (!x) { break; }
}
return prime_factors;
}
bool operator==(std::vector<int> a, std::vector<int> b) {
if (a.size() != b.size()) { return false; }
for (int i = 0; i < a.size(); i++) {
if (a[i] != b[i]) { return false; }
}
return true;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int x, y;
char temp_comma;
std::cin >> x >> temp_comma >> y;
prime_sieve(std::max(x, y));
std::cout << (get_prime_factors(x) == get_prime_factors(y) ? "YES" : "NO") << std::endl;
return 0;
}
+17
View File
@@ -0,0 +1,17 @@
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
std::vector<int> patients(n);
for (int i = 0; i < n; i++) { std::cin >> patients[i]; }
std::cout << (std::min_element(patients.begin(), patients.end()) - patients.begin()) << std::endl;
return 0;
}
@@ -0,0 +1,32 @@
#include <algorithm>
#include <iostream>
#include <vector>
using Range = std::pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
int calc_ans(std::vector<Range> time_ranges) {
std::sort(time_ranges.begin(), time_ranges.end(),
[](auto a, auto b) { return a.second < b.second; });
int i = 0, current_timestamp = -INF, ans = 0;
while (i < time_ranges.size()) {
current_timestamp = time_ranges[i].second;
ans++;
while (i < time_ranges.size() && time_ranges[i].first < current_timestamp) { i++; }
}
return ans;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
std::vector<Range> time_ranges(n);
for (int i = 0; i < n; i++) { std::cin >> time_ranges[i].first >> time_ranges[i].second; }
std::cout << calc_ans(time_ranges) << std::endl;
return 0;
}
+31
View File
@@ -0,0 +1,31 @@
#include <iostream>
#include <vector>
using ParagraphSeparated = std::vector<std::string>;
void print_paragraph(const ParagraphSeparated& paragraph, int wrap_pos = 80, std::ostream& os = std::cout) {
int line_length = 0;
for (const auto& word : paragraph) {
if (line_length + word.size() > wrap_pos) {
os << std::endl;
line_length = 0;
}
if (line_length != 0) { os << ' '; }
os << word;
line_length += word.size() + 1;
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int word_count;
std::cin >> word_count;
ParagraphSeparated paragraph(word_count);
for (int i = 0; i < word_count; i++) { std::cin >> paragraph[i]; }
print_paragraph(paragraph);
return 0;
}
+40
View File
@@ -0,0 +1,40 @@
#include <algorithm>
#include <iostream>
#include <vector>
bool run() {
int n, radius;
std::cin >> radius >> n;
if (radius == -1) { return false; }
std::vector<int> pos(n);
for (int i = 0; i < n; i++) { std::cin >> pos[i]; }
std::sort(pos.begin(), pos.end());
int uncovered_idx = 0, observer_idx = -1, observer_count = 0;
while (uncovered_idx < n) {
// 找下一个雷达
for (observer_idx = uncovered_idx;
observer_idx < n && pos[observer_idx] - pos[uncovered_idx] <= radius;
observer_idx++) {}
observer_idx--;
std::clog << '[' << observer_idx << ']' << pos[observer_idx] << " ";
observer_count++;
// 找下一个盖不住的兵
for (uncovered_idx = observer_idx;
uncovered_idx < n && pos[uncovered_idx] - pos[observer_idx] <= radius;
uncovered_idx++) {}
}
std::clog << std::endl;
std::cout << observer_count << std::endl;
return true;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
while (run()) {}
return 0;
}
+55
View File
@@ -0,0 +1,55 @@
#include <algorithm>
#include <iostream>
#include <vector>
int calc_wins(std::vector<int> king_times, std::vector<int> tianji_times) {
std::sort(king_times.begin(), king_times.end());
std::sort(tianji_times.begin(), tianji_times.end());
int wins = 0, k = -1;
std::vector<bool> king_used(king_times.size()), tianji_used(tianji_times.size());
// 先用最邻近的马战胜齐王,O(n^2)
for (int i = 0; i < king_times.size(); i++) {
do { k++; } while (k < tianji_times.size() && tianji_times[k] < king_times[i]);
do { k--; } while (k >= 0 && tianji_used[k]);
if (k >= 0) {
king_used[i] = true, tianji_used[k] = true;
wins++;
std::clog << '[' << i << '<' << k << "] ";
}
}
std::clog << std::endl;
// 没有赢的资本了,尽量平局
for (int i = 0; i < king_times.size(); i++) {
if (king_used[i]) { continue; }
for (int j = 0; j < tianji_times.size(); j++) {
if (tianji_used[j]) { continue; }
if (king_times[i] == tianji_times[j]) {
// ties++;
king_used[i] = true, tianji_used[j] = true;
std::clog << '(' << i << ',' << j << ") ";
break;
}
}
}
std::clog << std::endl;
// 只剩下输的资本了
int loses = static_cast<int>(std::count(king_used.begin(), king_used.end(), false));
std::clog << "LOSES " << loses << std::endl;
return wins - loses;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
while (std::cin >> n && n) {
std::vector<int> king_times(n), tianji_times(n);
for (int i = 0; i < n; i++) { std::cin >> king_times[i]; }
for (int i = 0; i < n; i++) { std::cin >> tianji_times[i]; }
std::cout << 200 * calc_wins(king_times, tianji_times) << std::endl;
}
return 0;
}
@@ -0,0 +1,35 @@
#include <algorithm>
#include <iostream>
#include <vector>
int calc_wins(std::vector<int> king_times, std::vector<int> tianji_times) {
std::sort(king_times.begin(), king_times.end());
std::sort(tianji_times.begin(), tianji_times.end());
int wins = 0, i = 0, j = 0, ties = 0;
while (i < king_times.size()) {
if (king_times[i] < tianji_times[j]) {
wins--, i++;
} else if (king_times[i] > tianji_times[j]) {
wins++, i++, j++;
} else {
ties++, i++, j++;
}
}
if (ties < 2) {ties = 2;}
return wins + ties - 2;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
while (std::cin >> n && n) {
std::vector<int> king_times(n), tianji_times(n);
for (int i = 0; i < n; i++) { std::cin >> king_times[i]; }
for (int i = 0; i < n; i++) { std::cin >> tianji_times[i]; }
std::cout << 200 * calc_wins(king_times, tianji_times) << std::endl;
}
return 0;
}