some programs!
This commit is contained in:
1 parent
5d30e847c1
commit
ed054247d5
32 files changed
+1882
-149
No files matched your search
@@ -0,0 +1,71 @@
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class CycledLinkedList {
|
||||
public:
|
||||
struct Node {
|
||||
int prev_id, next_id;
|
||||
};
|
||||
|
||||
vector<Node> nodes;
|
||||
|
||||
explicit CycledLinkedList(int n = 1) { nodes.reserve(n); }
|
||||
|
||||
void init_cycle(int n) {
|
||||
assert(nodes.empty());
|
||||
nodes.reserve(n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
nodes.push_back({(i + n - 1) % n, (i + 1) % n});
|
||||
}
|
||||
}
|
||||
|
||||
void remove_node(int id) {
|
||||
assert(id >= 0 && id < nodes.size());
|
||||
if (nodes[id].prev_id != -1) { nodes[nodes[id].prev_id].next_id = nodes[id].next_id; }
|
||||
if (nodes[id].next_id != -1) { nodes[nodes[id].next_id].prev_id = nodes[id].prev_id; }
|
||||
nodes[id].prev_id = nodes[id].next_id = -1;
|
||||
}
|
||||
};
|
||||
|
||||
int cow_amount, good_cards_amount, cards_amount, pass_amount;
|
||||
CycledLinkedList cards;
|
||||
|
||||
vector<int> simulate() {
|
||||
vector<int> good_nodes;
|
||||
int current_node = 0, previous_node = -1, cow = 1;
|
||||
while (good_nodes.size() < good_cards_amount) {
|
||||
// clog << "current_node: " << current_node << endl;
|
||||
if (cow == 0) { good_nodes.push_back(current_node); }
|
||||
previous_node = current_node, current_node = cards.nodes[current_node].next_id;
|
||||
cards.remove_node(previous_node);
|
||||
if (previous_node == current_node) { break; }
|
||||
cow = (cow + 1) % cow_amount;
|
||||
for (int _ = 0; _ < pass_amount; _++) {
|
||||
current_node = cards.nodes[current_node].next_id;
|
||||
}
|
||||
}
|
||||
return good_nodes;
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
cin >> cow_amount >> cards_amount >> pass_amount;
|
||||
good_cards_amount = cards_amount / cow_amount;
|
||||
|
||||
cards.init_cycle(cards_amount);
|
||||
|
||||
vector<int> good_nodes = simulate();
|
||||
sort(good_nodes.begin(), good_nodes.end());
|
||||
for (const int node : good_nodes) {
|
||||
cout << node + 1 << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
string inserted_string(string s, const string& sub_s) {
|
||||
int max_char_pos = -1;
|
||||
char max_char = 0;
|
||||
for (int i = 0; i < s.size(); i++) {
|
||||
if (s[i] > max_char) {
|
||||
max_char = s[i];
|
||||
max_char_pos = i;
|
||||
}
|
||||
}
|
||||
s.insert(max_char_pos + 1, sub_s); // STL 偷懒
|
||||
return s;
|
||||
}
|
||||
|
||||
int main() {
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
string s, sub_s;
|
||||
|
||||
while (cin >> s >> sub_s) {
|
||||
cout << inserted_string(s, sub_s) << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
constexpr int MAX_N = 1e6 + 5;
|
||||
int max_period[MAX_N];
|
||||
|
||||
void log_array(const int arr[], const size_t n) {
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
clog << arr[i] << " ";
|
||||
}
|
||||
clog << endl;
|
||||
}
|
||||
|
||||
void solve(string s) {
|
||||
memset(max_period, 0, sizeof(max_period));
|
||||
max_period[0] = 1;
|
||||
for (int i = 1; i < s.size(); i++) {
|
||||
if (s[i - max_period[i - 1]] == s[i]) {
|
||||
max_period[i] = max_period[i - 1];
|
||||
} else {
|
||||
max_period[i] = i + 1;
|
||||
}
|
||||
}
|
||||
log_array(max_period, s.size());
|
||||
}
|
||||
|
||||
int main() {
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
string s;
|
||||
while (cin >> n && n) {
|
||||
cin >> s;
|
||||
solve(s);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Polynomial : public map<unsigned int, long long, greater<>> {
|
||||
public:
|
||||
Polynomial operator+=(const Polynomial& other) {
|
||||
for (const auto& [exp, coef] : other) {
|
||||
(*this)[exp] += coef;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
void print() const {
|
||||
for (const auto& [exp, coef] : *this) {
|
||||
if (coef == 0) { continue; }
|
||||
cout << "[ " << coef << " " << exp << " ] ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
};
|
||||
|
||||
Polynomial input_polynomial() {
|
||||
long long coefficient;
|
||||
int exponent;
|
||||
Polynomial polynomial;
|
||||
while (cin >> coefficient >> exponent && exponent >= 0) {
|
||||
polynomial[exponent] += coefficient;
|
||||
}
|
||||
return polynomial;
|
||||
}
|
||||
|
||||
int main() {
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
for (int _ = 0; _ < n; _++) {
|
||||
Polynomial polynomial1 = input_polynomial();
|
||||
Polynomial polynomial2 = input_polynomial();
|
||||
polynomial1 += polynomial2;
|
||||
polynomial1.print();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
constexpr size_t MAX_N = 100005, MAX_DIGITS = 16;
|
||||
|
||||
int n, m;
|
||||
int bits[MAX_DIGITS];
|
||||
|
||||
void print_bits(ostream& os = clog) {
|
||||
for (const int bit : bits) { os << bit; }
|
||||
os << endl;
|
||||
}
|
||||
|
||||
void update_bits(const int num) {
|
||||
for (int i = 0; i < MAX_DIGITS; i++) {
|
||||
bits[i] += (num >> i) & 1;
|
||||
}
|
||||
}
|
||||
|
||||
void add(int num) {
|
||||
for (int i = 0; i < MAX_DIGITS; i++) {
|
||||
const bool current_bit = num & 1;
|
||||
num >>= 1;
|
||||
if (current_bit == 0) { continue; }
|
||||
num += bits[i];
|
||||
bits[i] = n - bits[i];
|
||||
print_bits();
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
cin >> n >> m;
|
||||
for (int _ = 0; _ < n; _++) {
|
||||
int num;
|
||||
cin >> num;
|
||||
update_bits(num);
|
||||
}
|
||||
for (int _ = 0; _ < m; _++) {
|
||||
print_bits();
|
||||
char op;
|
||||
int x;
|
||||
cin >> op >> x;
|
||||
if (op == 'Q') {
|
||||
cout << bits[x] << endl;
|
||||
} else {
|
||||
add(x);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
|
||||
using namespace std;
|
||||
|
||||
using Coord = pair<int, int>;
|
||||
|
||||
constexpr int MAX_N = 45;
|
||||
int n;
|
||||
int magic_square[MAX_N][MAX_N];
|
||||
|
||||
Coord next_coord(Coord current_coord) {
|
||||
Coord next_coord = current_coord;
|
||||
next_coord.first = (next_coord.first + n - 1) % n;
|
||||
next_coord.second = (next_coord.second + 1) % n;
|
||||
if ((current_coord.first == 0 && current_coord.second == n - 1)
|
||||
|| magic_square[next_coord.first][next_coord.second] != 0) {
|
||||
return {(current_coord.first + 1) % n, current_coord.second};
|
||||
}
|
||||
return next_coord;
|
||||
}
|
||||
|
||||
|
||||
void print_magic_square(ostream& os = cout) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
os << magic_square[i][j] << " ";
|
||||
}
|
||||
os << endl;
|
||||
}
|
||||
os << endl;
|
||||
}
|
||||
|
||||
void fill_magic_square() {
|
||||
Coord current_coord = {0, (n - 1) / 2};
|
||||
for (int num = 1; num <= n * n; num++) {
|
||||
assert(magic_square[current_coord.first][current_coord.second] == 0);
|
||||
magic_square[current_coord.first][current_coord.second] = num;
|
||||
current_coord = next_coord(current_coord);
|
||||
// print_magic_square(clog);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
cin >> n;
|
||||
n = 2 * n - 1;
|
||||
fill_magic_square();
|
||||
print_magic_square();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user