This commit is contained in:
2023-12-24 22:34:36 +08:00
parent 43ced8bd2a
commit 7c1e463123
5 changed files with 647 additions and 0 deletions

144
ACMOJ-1071.cpp Normal file
View File

@ -0,0 +1,144 @@
#include <bits/stdc++.h>
class LinkList {
struct Node {
int val;
Node *next;
Node() : val(0), next(nullptr) {}
Node(int __val, Node *__next) : val(__val), next(__next) {}
};
int count = 0;
Node *head = nullptr;
public:
LinkList() { ; }
~LinkList() {
Node *p = head;
while (p != nullptr) {
Node *next_p = p->next;
delete p;
p = next_p;
}
}
void push(int new_val) {
if (count == 0) {
head = new Node(new_val, nullptr);
count = 1;
return;
}
Node *cur = head, *last_cur = nullptr;
while (cur != nullptr) {
if (cur->val >= new_val) {
if (last_cur != nullptr) {
last_cur->next = new Node(new_val, cur);
} else {
head = new Node(new_val, cur);
}
count++;
return;
}
last_cur = cur;
cur = cur->next;
}
// std::cerr << last_cur << std::endl;
last_cur->next = new Node(new_val, cur);
count++;
}
int getKth(int idx) {
if (idx >= count) return -1;
Node *cur = head;
for (int i = 0; i < idx && cur != nullptr; i++) cur = cur->next;
return cur->val;
}
void merge(LinkList *oth) {
count += oth->count;
Node *cur_this = head, *cur_oth = oth->head;
head = nullptr;
Node *head_store = nullptr;
while (cur_this != nullptr && cur_oth != nullptr) {
if (cur_this->val <= cur_oth->val) {
if (head == nullptr) {
head_store = head = cur_this;
} else {
head->next = cur_this;
head = head->next;
}
cur_this = cur_this->next;
} else {
if (head == nullptr) {
head_store = head = cur_oth;
} else {
head->next = cur_oth;
head = head->next;
}
cur_oth = cur_oth->next;
}
}
while (cur_this != nullptr) {
if (head == nullptr) {
head_store = head = cur_this;
} else {
head->next = cur_this;
head = head->next;
}
cur_this = cur_this->next;
}
while (cur_oth != nullptr) {
if (head == nullptr) {
head_store = head = cur_oth;
} else {
head->next = cur_oth;
head = head->next;
}
cur_oth = cur_oth->next;
}
if (head != nullptr) head->next = nullptr;
head = head_store;
oth->count = 0;
oth->head = nullptr;
}
};
class LinkListArray {
private:
int len;
LinkList **lists;
public:
LinkListArray(int n) : len(n) {
lists = new LinkList *[n];
for (int i = 0; i < n; ++i) lists[i] = new LinkList;
}
~LinkListArray() {
for (int i = 0; i < len; ++i) {
delete lists[i];
}
delete[] lists;
}
void insertNumber(int n, int x) { lists[n]->push(x); }
int queryNumber(int n, int k) { return lists[n]->getKth(k); }
void mergeLists(int p, int q) { lists[p]->merge(lists[q]); }
};
int main() {
#ifdef local
freopen("pro.in", "r", stdin);
#endif
int len, m;
scanf("%d%d", &len, &m);
LinkListArray a = LinkListArray(len);
for (int i = 0, op, s1, s2; i < m; ++i) {
scanf("%d%d%d", &op, &s1, &s2);
if (op == 0) {
a.insertNumber(s1, s2);
}
if (op == 1) {
printf("%d\n", a.queryNumber(s1, s2));
}
if (op == 2) {
a.mergeLists(s1, s2);
}
}
return 0;
}

76
ACMOJ-1072.cpp Normal file
View File

@ -0,0 +1,76 @@
#include <iomanip>
#include <iostream>
using namespace std;
class CircleSet {
friend istream &operator>>(istream &in, CircleSet &obj);
private:
struct Circle {
long long x, y, r;
Circle(long long _x = 0, long long _y = 0, long long _r = 0)
: x(_x), y(_y), r(_r) {}
};
int count; // 集合中圆的数目
Circle *circles; // 集合中所有圆
public:
CircleSet(int n) : count(n) {
circles = new Circle[n];
count = n;
}
~CircleSet() { delete[] circles; }
int operator[](int idx) const noexcept { return circles[idx].r; }
bool checkContaining(int p, int q) {
if (circles[p].r <= circles[q].r) return false;
return (circles[p].x - circles[q].x) * (circles[p].x - circles[q].x) +
(circles[p].y - circles[q].y) * (circles[p].y - circles[q].y) <
(circles[p].r - circles[q].r) * (circles[p].r - circles[q].r);
}
int getCircleContainingQ(int q) {
int res = -1;
for (int p = 0; p < count; p++) {
if (!checkContaining(p, q)) continue;
if (res == -1)
res = circles[p].r;
else
res = (circles[p].r < res) ? circles[p].r : res;
}
return res;
}
};
istream &operator>>(istream &in, CircleSet &obj) {
for (int i = 0; i < obj.count; ++i) {
in >> obj.circles[i].x >> obj.circles[i].y >> obj.circles[i].r;
}
return in;
}
int main() {
#ifdef local
freopen("pro.in", "r", stdin);
#endif
int n, m, type;
cin >> n >> m;
CircleSet set(n);
cin >> set; // 输入集合中的所有圆
while (m--) {
int type, p, q;
cin >> type;
if (type == 1) {
// do nothing
} else if (type == 2) {
for (int i = 0; i < n; ++i) {
cout << set[i] << ' ';
}
cout << endl;
} else if (type == 3) {
cin >> p >> q;
cout << set.checkContaining(p, q) << endl;
} else if (type == 4) {
cin >> q;
cout << set.getCircleContainingQ(q) << endl;
}
}
return 0;
}

185
ACMOJ-1073.cpp Normal file
View File

@ -0,0 +1,185 @@
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
const int month_days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
class Date {
private:
int _year;
int _month;
int _day;
static bool IsValid(int year, int month, int day) {
if (month < 1 || month > 12) return false;
if (month != 2) return 1 <= day && day <= month_days[month];
return 1 <= day && day <= month_days[month] + IsLeapYear(year);
}
static Date *bucket;
static int bucket_cnt, it_cnt;
static int GetId(const Date &dt) {
if (bucket == nullptr) {
bucket = new Date[49275];
for (int year = 1900; year <= 2030; year++)
for (int month = 1; month <= 12; month++) {
int ds = month_days[month];
if (month == 2 && IsLeapYear(year)) ds++;
for (int day = 1; day <= ds; day++) {
bucket[bucket_cnt++] = Date(year, month, day);
}
}
}
int L = 0, R = bucket_cnt, M;
while (L < R) {
M = (L + R) >> 1;
if (bucket[M] < dt)
L = M + 1;
else if (dt < bucket[M])
R = M;
else
return M;
}
return (L + R) >> 1;
}
public:
// 构造函数
Date() : _year(0), _month(0), _day(0) {
it_cnt++;
_year = 1900;
_month = 1;
_day = 1;
}
Date(int yy, int mm, int dd) {
it_cnt++;
if (!IsValid(yy, mm, dd)) {
_year = 1900;
_month = 1;
_day = 1;
return;
}
_year = yy;
_month = mm;
_day = dd;
}
~Date() {
it_cnt--;
if (it_cnt == 0) {
delete[] bucket;
bucket = nullptr;
bucket_cnt = 0;
}
}
// 判断是否为闰年
static bool IsLeapYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return true;
} else {
return false;
}
}
// 一个日期加上一个天数
Date operator+(int det) {
int id = GetId(*this);
return bucket[id + det];
}
// 一个日期减去一个天数
Date operator-(int det) {
int id = GetId(*this);
return bucket[id - det];
}
// 前置++
Date &operator++() {
*this = *this + 1;
return *this;
}
// 后置++
Date operator++(int) {
Date tmp = *this;
*this = *this + 1;
return tmp;
}
// 前置--
Date &operator--() {
*this = *this - 1;
return *this;
}
// 后置--
Date operator--(int) {
Date tmp = *this;
*this = *this - 1;
return tmp;
}
int operator-(const Date &B) {
int ida = GetId(*this);
int idb = GetId(B);
return ida > idb ? ida - idb : idb - ida;
}
//<重载
bool operator<(const Date &B) const {
if (_year != B._year) return _year < B._year;
if (_month != B._month) return _month < B._month;
return _day < B._day;
}
// 重载输出运算符
friend ostream &operator<<(ostream &stream, const Date &dt);
// 以上仅为提示,不代表你需要完成所有,你也可以添加其他需要的函数。
};
Date *Date::bucket = nullptr;
int Date::it_cnt = 0;
int Date::bucket_cnt = 0;
ostream &operator<<(ostream &stream, const Date &dt) {
stream << dt._year << '-' << dt._month << '-' << dt._day;
return stream;
}
void Test() {
int op;
cin >> op;
int yy, mm, dd;
if (op == 1 || op == 0) {
Date d0;
Date d1(2000, 2, 29);
Date d2(1900, 2, 29);
cout << d0 << endl;
cout << d1 << endl;
cout << d2 << endl;
// d0.out(); d1.out(); d2.out();
}
if (op == 2 || op == 0) {
cin >> yy >> mm >> dd;
Date d0(yy, mm, dd);
for (int i = 0; i < 5; ++i) cout << ++d0 << endl; //(++d0).out();
for (int i = 0; i < 5; ++i) cout << d0++ << endl; //(d0++).out();
for (int i = 0; i < 5; ++i) cout << d0-- << endl; //(d0--).out();
for (int i = 0; i < 2; ++i) cout << --d0 << endl; //(--d0).out();
cout << d0 << endl;
// d0.out();
}
if (op == 3 || op == 0) {
cin >> yy >> mm >> dd;
Date d0(yy, mm, dd);
cout << d0 + 100 << endl;
// (d0+100).out();
cout << d0 - 1000 << endl;
// (d0-1000).out();
}
if (op == 4 || op == 0) {
cin >> yy >> mm >> dd;
Date d0(yy, mm, dd);
Date d1(2020, 12, 21);
cout << (d0 < d1) << endl;
}
if (op == 5 || op == 0) {
cin >> yy >> mm >> dd;
Date d0(yy, mm, dd);
Date d1(1912, 6, 23);
cout << d0 - d1 << endl;
}
}
int main() {
#ifdef local
freopen("pro.in", "r", stdin);
#endif
Test();
return 0;
}

112
ACMOJ-1448.cpp Normal file
View File

@ -0,0 +1,112 @@
#include "ACMOJ-1448.hpp"
#include <iostream>
#include <vector>
#include "task.hpp"
using namespace std;
namespace cpu_testing {
enum operationType { AddTask, ChangePriority };
struct Operation {
operationType type;
uint trig_time;
sjtu::Task task;
Operation(operationType _type, uint _trig_time, const sjtu::Task &_task)
: task(_task) {
type = _type;
trig_time = _trig_time;
}
~Operation() = default;
};
vector<Operation> readInputs() {
vector<Operation> operations;
int m, op;
uint trig_time, task_id, priority, time;
cin >> m;
for (int i = 0; i < m; ++i) {
cin >> op >> trig_time;
if (op == 0) {
cin >> task_id >> priority >> time;
operations.emplace_back(
Operation(AddTask, trig_time, sjtu::Task(task_id, priority, time)));
} else {
cin >> task_id >> priority;
operations.emplace_back(
Operation(ChangePriority, trig_time, sjtu::Task(task_id, priority)));
}
}
return operations;
}
void processOutputs(const vector<pair<sjtu::CPUState, uint> > &ans) {
for (auto log : ans) {
if (log.first == sjtu::busy && log.second == 0) {
cerr << "[Error] Busy CPU is not processing any task.\n";
exit(1);
}
if (log.first == sjtu::idle && log.second != 0) {
cerr << "[Error] Idle CPU is still processing tasks.\n";
exit(1);
}
cout << log.first << ' ' << log.second << endl;
}
}
} // namespace cpu_testing
int main() {
#ifdef local
freopen("pro.in", "r", stdin);
freopen("pro.out", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
vector<cpu_testing::Operation> operations(cpu_testing::readInputs());
sjtu::CPU *processor;
int CPUType;
cin >> CPUType;
switch (CPUType) {
case 0:
processor = new sjtu::CPU_FCFS();
break;
case 1:
processor = new sjtu::CPU_SRTF();
break;
case 2:
processor = new sjtu::CPU_PRIORITY();
break;
default:
cerr << "[Error] Unexpected CPU type.";
return 1;
}
vector<pair<sjtu::CPUState, uint> > ans;
int time = 0, cur = 0;
while (time <= 1000) {
while (cur < operations.size() && operations[cur].trig_time == time) {
int success = 0;
if (operations[cur].type == cpu_testing::AddTask)
success = processor->addTask(operations[cur].task);
if (operations[cur].type == cpu_testing::ChangePriority)
success = processor->changePriority(operations[cur].task.task_id,
operations[cur].task.priority);
if (success == 0) {
cerr << "[Error] Fail to execute operation " << cur << ".\n";
}
++cur;
}
ans.push_back(processor->run());
++time;
}
cpu_testing::processOutputs(ans);
delete processor;
return 0;
}

130
ACMOJ-1448.hpp Normal file
View File

@ -0,0 +1,130 @@
#ifndef _SJTU_CPP_FINAL_CPU_HPP_
#define _SJTU_CPP_FINAL_CPU_HPP_
#include <cassert>
#include <vector>
#include "task.hpp"
using namespace std;
typedef unsigned int uint;
namespace sjtu {
// CPU base class, modifications is not allowed.
class CPU {
protected:
CPUState state;
vector<Task> tasks;
public:
CPU() : tasks() { state = idle; }
// Add a new task.
int addTask(const Task &t) {
tasks.push_back(t);
return 1;
}
// Change the priority of one process, return 1 if success and return 0 if
// fail.
int changePriority(uint task_id, uint priority) {
for (auto &task : tasks)
if (task.task_id == task_id) {
task.priority = priority;
return 1;
}
return 0;
}
virtual pair<CPUState, uint> run() = 0;
virtual ~CPU() = default;
};
// FCFS method based CPU.
class CPU_FCFS : public CPU {
// TODO: complete the FCFS method.
public:
pair<CPUState, uint> run() override {
bool has_opt = false;
for (int i = 0; i < tasks.size(); i++) {
if (tasks[i].time > 0) {
has_opt = true;
state = CPUState::busy;
tasks[i].time--;
return std::make_pair(CPUState::busy, tasks[i].task_id);
}
}
state = CPUState::idle;
return std::make_pair(CPUState::idle, 0);
}
~CPU_FCFS() override = default;
};
// SRTF method based CPU.
class CPU_SRTF : public CPU {
// TODO: complete the SRTF method.
public:
pair<CPUState, uint> run() override {
bool has_opt = false;
int least_time = -1;
for (int i = 0; i < tasks.size(); i++) {
if (tasks[i].time > 0) {
has_opt = true;
if (least_time == -1)
least_time = tasks[i].time;
else
least_time =
(tasks[i].time < least_time) ? tasks[i].time : least_time;
}
}
if (!has_opt) {
state = CPUState::idle;
return std::make_pair(CPUState::idle, 0);
}
for (int i = 0; i < tasks.size(); i++) {
if (tasks[i].time == least_time) {
state = CPUState::busy;
tasks[i].time--;
return std::make_pair(CPUState::busy, tasks[i].task_id);
}
}
}
~CPU_SRTF() override = default;
};
// priority method based CPU.
class CPU_PRIORITY : public CPU {
// TODO: complete the priority method.
public:
pair<CPUState, uint> run() override {
bool has_opt = false;
int highest_pri = -1;
for (int i = 0; i < tasks.size(); i++) {
if (tasks[i].time > 0) {
has_opt = true;
if (highest_pri == -1)
highest_pri = tasks[i].priority;
else
highest_pri = (tasks[i].priority < highest_pri) ? tasks[i].priority
: highest_pri;
}
}
if (!has_opt) {
state = CPUState::idle;
return std::make_pair(CPUState::idle, 0);
}
for (int i = 0; i < tasks.size(); i++) {
if (tasks[i].priority == highest_pri && tasks[i].time > 0) {
state = CPUState::busy;
tasks[i].time--;
return std::make_pair(CPUState::busy, tasks[i].task_id);
}
}
}
~CPU_PRIORITY() override = default;
};
} // namespace sjtu
#endif