开始计概A下半学期。

This commit is contained in:
rogerwang2008 committed 2026-11-09 21:13:09 +08:00
1 parent 0cfdc8499c
commit 38e97654e1
31 files changed
+2144 -110

No files matched your search

+23
View File
@@ -0,0 +1,23 @@
#include <iostream>
std::size_t calc_digits(std::string num) {
if (num == "0") { return 1; }
std::size_t ans = num.size(), i = 0;
while (num[i] == '0') {
i++;
ans--;
}
return ans;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::string num;
std::cin >> num;
std::cout << calc_digits(num) << std::endl;
return 0;
}
@@ -0,0 +1,34 @@
#include <algorithm>
#include <array>
#include <iostream>
#include <vector>
constexpr std::size_t MAX_N = 1001;
int students_amount, task_per_student, total_days;
std::array<std::array<bool, MAX_N>, MAX_N> arrangements;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> students_amount >> task_per_student >> total_days;
for (int _ = 0; _ < students_amount; _++) {
for (int i = 0; i < task_per_student; i++) {
int day;
std::cin >> day;
arrangements[day][i] = true;
}
}
bool first = true;
for (int i = 1; i <= total_days; i++) {
if (!first) { std::cout << ' '; }
std::cout << std::count(arrangements[i].begin(), arrangements[i].end(), true);
first = false;
}
std::cout << std::endl;
return 0;
}
@@ -0,0 +1,38 @@
#include <algorithm>
#include <iostream>
#include <vector>
struct Patient {
int reservation_order = -1;
std::string id;
int age = -1;
bool operator<(const Patient& other) const {
if (age < 60 && other.age >= 60) { return !(other < *this); }
if (age >= 60) {
if (other.age < 60) { return true; }
if (age == other.age) { return reservation_order < other.reservation_order; }
return age > other.age;
}
return reservation_order < other.reservation_order;
}
};
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
std::vector<Patient> patients(n);
for (int i = 0; i < n; i++) {
std::cin >> patients[i].id >> patients[i].age;
patients[i].reservation_order = i;
}
std::sort(patients.begin(), patients.end());
for (const auto& [reservation_order, id, age] : patients) {
std::cout << id << std::endl;
}
return 0;
}
@@ -0,0 +1,29 @@
/*
* 特别处理有一个杆子超级长?
*/
#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> half_lengths;
half_lengths.reserve(2 * n);
for (int i = 0; i < n; i++) {
int length, flag_pos;
std::cin >> length >> flag_pos;
half_lengths.push_back(length - flag_pos);
half_lengths.push_back(flag_pos);
}
std::sort(half_lengths.begin(), half_lengths.end(), std::greater<>());
std::cout << half_lengths[0] + half_lengths[1] << std::endl;
return 0;
}
@@ -0,0 +1,57 @@
#include <iostream>
#include <unordered_map>
const std::unordered_map<std::string, int> TYPE_NAME_PRIORITIES = {
{"bool", 0},
{"char", 1},
{"int", 2},
{"longlong", 4},
{"float", 99},
{"double", 100},
};
class Type : public std::string {
public:
using std::string::string;
bool operator<(const Type& other) const {
if (empty()) { return true; }
return TYPE_NAME_PRIORITIES.at(*this) < TYPE_NAME_PRIORITIES.at(other);
}
};
void run() {
Type max_type, current_type;
std::string line;
std::cin >> line;
auto comma_pos = line.rfind(',');
if (comma_pos != std::string::npos) {
line.erase(0, comma_pos + 1);
}
for (char c : line) {
if (!std::isalpha(c)) {
std::clog << current_type << c << std::endl;
if (c == ',') {
max_type.clear();
} else {
if (max_type < Type("int")) { max_type = "int"; }
max_type = std::max(max_type, current_type);
}
current_type.clear();
} else {
current_type += c;
}
}
max_type = std::max(max_type, current_type);
std::cout << max_type << std::endl;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
while (n--) { run(); }
return 0;
}
+54
View File
@@ -0,0 +1,54 @@
#include <iostream>
#include <vector>
bool validate_arithmetic(const std::vector<bool>& bucket) {
if (bucket.size() <= 1) { return false; }
int previous_idx = -1, distance = -1;
for (int i = 0; i < bucket.size(); i++) {
if (!bucket[i]) { continue; }
if (previous_idx == -1) {
previous_idx = i;
continue;
}
if (distance == -1) {
distance = i - previous_idx;
}
if (i - previous_idx != distance) {
return false;
}
previous_idx = i;
}
return true;
}
int clac_ans(const std::vector<int>& nums) {
if (nums.size() == 1) { return 0; }
std::vector<bool> bucket_begin(nums.size(), false);
std::vector<bool> bucket_end(nums.size(), true);
int ans = 0;
for (int i = 0; i < nums.size(); i++) {
bucket_begin[nums[i]] = true;
bucket_end[nums[i]] = false;
if (validate_arithmetic(bucket_begin) && validate_arithmetic(bucket_end)) {
ans++;
std::clog << i << std::endl;
}
}
return ans - 1;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
while (std::cin >> n) {
std::vector<int> nums(n);
for (int i = 0; i < n; i++) { std::cin >> nums[i], nums[i]--; }
std::cout << clac_ans(nums) << std::endl;
}
return 0;
}
+20
View File
@@ -0,0 +1,20 @@
#include <cmath>
#include <iostream>
long long calc_min_ops(long long a, long long b) {
if (a > b) { return a - b; }
long long div = b / a;
auto multiply_times = static_cast<long long>(std::log2(div));
a = a * std::pow(2, multiply_times);
return std::min(multiply_times + b - a, multiply_times + 1 + (2 * a - b));
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
long long a, b;
std::cin >> a >> b;
std::cout << calc_min_ops(a, b) << std::endl;
return 0;
}
+43
View File
@@ -0,0 +1,43 @@
#include <iostream>
constexpr int MAX_N = 1000001, MOD = 10000;
/// dp[0][i]: 长方形;dp[1][i]: 阶梯型,i 为短边长
long long dp[2][MAX_N];
void init_dp() {
dp[0][0] = 1;
dp[0][1] = 1;
dp[0][2] = 2;
dp[1][0] = 0;
dp[1][1] = 1;
dp[1][2] = 2;
for (int i = 3; i < MAX_N; i++) {
dp[0][i] = (dp[0][i - 1] + dp[0][i - 2] + dp[1][i - 2] * 2) % MOD;
dp[1][i] = (dp[0][i - 1] + dp[1][i - 1]) % MOD;
}
}
void log_dp(int n) {
for (int i = 1; i <= n; i++) {
std::clog << dp[0][i] << ' ';
}
std::clog << std::endl;
for (int i = 1; i <= n; i++) {
std::clog << dp[1][i] << ' ';
}
std::clog << std::endl;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
init_dp();
int n;
std::cin >> n;
std::cout << dp[0][n] % MOD << std::endl;;
log_dp(n);
return 0;
}
+160
View File
@@ -0,0 +1,160 @@
#include <algorithm>
#include <array>
#include <iostream>
#include <vector>
constexpr int char_to_digit(char c) {
if (c >= '0' && c <= '9') { return static_cast<short>(c - '0'); }
if (c >= 'A' && c <= 'Z') { return static_cast<short>(c - 'A' + 10); }
throw std::invalid_argument("Invalid char as digit");
}
constexpr char digit_to_char(int digit) {
if (digit >= 0 && digit <= 9) { return static_cast<char>('0' + digit); }
return static_cast<char>('A' + digit - 10);
}
class BigFloat {
public:
std::array<std::vector<int>, 2> digit_vectors;
bool negative = false;
unsigned short base = 10;
explicit BigFloat(std::string s, unsigned short base = 10) : base(base) {
int i = 0;
if (s[0] == '-') { negative = true, i++; }
for (; i < s.size() && s[i] != '.'; i++) { digit_vectors[0].push_back(char_to_digit(s[i])); }
std::reverse(digit_vectors[0].begin(), digit_vectors[0].end());
i++;
for (; i < s.size(); i++) { digit_vectors[1].push_back(char_to_digit(s[i])); }
}
void normalize() {
digit_vectors[0].push_back(0);
digit_vectors[1].push_back(0);
for (int i = static_cast<int>(digit_vectors[1].size() - 2); i >= 0; i--) {
if (digit_vectors[1][i + 1] >= base) {
digit_vectors[1][i] += digit_vectors[1][i + 1] / base;
digit_vectors[1][i + 1] %= base;
}
if (digit_vectors[1][i + 1] < 0) {
int borrowed = -(digit_vectors[1][i + 1] + 1) / base + 1;
digit_vectors[1][i] -= borrowed;
digit_vectors[1][i + 1] += base * borrowed;
}
}
if (digit_vectors[1][0] >= base) {
digit_vectors[0][0] += digit_vectors[1][0] / base;
digit_vectors[1][0] %= base;
}
if (digit_vectors[1][0] < 0) {
int borrowed = -(digit_vectors[1][0] + 1) / base + 1;
digit_vectors[0][0] -= borrowed;
digit_vectors[1][0] += base * borrowed;
}
for (int i = 1; i < digit_vectors[0].size(); i++) {
if (digit_vectors[0][i - 1] >= base) {
digit_vectors[0][i] += digit_vectors[0][i - 1] / base;
digit_vectors[0][i - 1] %= base;
}
if (digit_vectors[0][i - 1] < 0) {
int borrowed = -(digit_vectors[0][i - 1] + 1) / base + 1;
digit_vectors[0][i] -= borrowed;
digit_vectors[0][i - 1] += base * borrowed;
}
}
if (digit_vectors[0][digit_vectors[0].size() - 1] < 0) {
// digit_vectors[0][digit_vectors[0].size() - 1] = -digit_vectors[0][digit_vectors[0].size() - 1];
negative = !negative;
for (auto& digits : digit_vectors) {
for (auto& digit : digits) { digit = -digit; }
}
normalize();
}
for (auto& digits : digit_vectors) {
while (!digits.empty() && digits.back() == 0) { digits.pop_back(); }
}
if (digit_vectors[0].size() + digit_vectors[1].size() == 0) {
digit_vectors[0].push_back(0);
negative = false;
}
}
std::string to_string() {
normalize();
std::string s;
for (auto digit : digit_vectors[0]) { s += digit_to_char(digit); }
if (negative) { s += '-'; }
std::reverse(s.begin(), s.end());
if (digit_vectors[1].empty()) { return s; }
s += '.';
for (auto digit : digit_vectors[1]) { s += digit_to_char(digit); }
return s;
}
friend std::ostream& operator<<(std::ostream& os, BigFloat big_float) {
return os << big_float.to_string();
}
static void align(BigFloat& a, BigFloat& b) {
for (auto part : {0, 1}) {
if (a.digit_vectors[part].size() < b.digit_vectors[part].size()) {
a.digit_vectors[part].resize(b.digit_vectors[part].size());
} else if (b.digit_vectors[part].size() < a.digit_vectors[part].size()) {
b.digit_vectors[part].resize(a.digit_vectors[part].size());
}
}
}
BigFloat operator-() const {
BigFloat result = *this;
result.negative = !result.negative;
return result;
}
BigFloat& operator+=(BigFloat other) {
if ((negative && !other.negative) || (!negative && other.negative)) { return *this -= -other; }
align(*this, other);
for (auto part : {0, 1}) {
for (int i = 0; i < digit_vectors[part].size(); i++) {
digit_vectors[part][i] += other.digit_vectors[part][i];
}
}
normalize();
return *this;
}
BigFloat& operator-=(BigFloat other) {
if ((negative && !other.negative) || (!negative && other.negative)) { return *this += -other; }
align(*this, other);
for (auto part : {0, 1}) {
for (int i = 0; i < digit_vectors[part].size(); i++) {
digit_vectors[part][i] -= other.digit_vectors[part][i];
}
}
normalize();
return *this;
}
};
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int cases;
std::cin >> cases;
while (cases--) {
unsigned short base;
std::string s1, s2;
std::cin >> base >> s1 >> s2;
BigFloat number1(s1, base), number2(s2, base);
std::clog << number1 << ' ' << number2 << std::endl;
number1 -= number2;
std::cout << number1 << std::endl;
}
return 0;
}