Compare commits
15 Commits
try-fast-r
...
04_03
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bf6e6b0c86
|
||
|
|
9fd8ea1918
|
||
|
|
f55fb12e50
|
||
|
|
b7b8af4811
|
||
|
|
cf1e94a5d1
|
||
|
|
744a773e95
|
||
|
|
1d3a916eb5
|
||
|
|
3aabda4b43
|
||
|
|
2642acd00a
|
||
|
|
32bd7e91fd
|
||
|
|
a370d5a723
|
||
|
|
a91c9637e8
|
||
|
|
4726deb30c
|
||
|
|
03e0e30a95
|
||
|
|
8a44704bdd
|
@@ -77,7 +77,7 @@ BreakConstructorInitializersBeforeComma: false
|
||||
BreakConstructorInitializers: BeforeColon
|
||||
BreakAfterJavaFieldAnnotations: false
|
||||
BreakStringLiterals: true
|
||||
ColumnLimit: 80
|
||||
ColumnLimit: 100
|
||||
CommentPragmas: '^ IWYU pragma:'
|
||||
QualifierAlignment: Leave
|
||||
CompactNamespaces: false
|
||||
|
||||
130
2023202/main.cpp
130
2023202/main.cpp
@@ -1,121 +1,47 @@
|
||||
#include <stdio.h>
|
||||
|
||||
template <class E> class Node {
|
||||
private:
|
||||
E content;
|
||||
Node<E> *next;
|
||||
|
||||
public:
|
||||
Node(const E &_content) : content(_content), next(this){};
|
||||
Node(const E &_content, Node<E> *nextPtr)
|
||||
: content(_content), next(nextPtr){};
|
||||
void setNext(Node<E> *_ptr) {
|
||||
this->next = _ptr;
|
||||
}
|
||||
E &getContent() {
|
||||
return this->content;
|
||||
}
|
||||
Node<E> *getNextPtr() {
|
||||
return this->next;
|
||||
}
|
||||
struct Node{
|
||||
int prev, next;
|
||||
};
|
||||
|
||||
template <class E> class Iterator {
|
||||
private:
|
||||
Node<E> *current;
|
||||
|
||||
public:
|
||||
Iterator(Node<E> *start) : current(start){};
|
||||
E &peekNext() {
|
||||
return this->current->getNextPtr()->getContent();
|
||||
}
|
||||
void next() {
|
||||
this->current = this->current->getNextPtr();
|
||||
}
|
||||
E &peek() {
|
||||
return this->current->getContent();
|
||||
}
|
||||
Node<E> *peekNode() {
|
||||
return this->current;
|
||||
}
|
||||
void insertHere(const E &newItem) {
|
||||
this->current->setNext(
|
||||
new Node<E>(newItem, this->current->getNextPtr()));
|
||||
}
|
||||
};
|
||||
|
||||
template <class E> class List {
|
||||
private:
|
||||
// int _length;
|
||||
Node<E> head;
|
||||
|
||||
public:
|
||||
List(const E &dummyValue)
|
||||
: head(Node<E>(dummyValue)){
|
||||
// this->_length = 0;
|
||||
};
|
||||
void insert(const E &target, const E &newItem) {
|
||||
Iterator<E> iter = Iterator<E>(&this->head);
|
||||
// Remember to add a loop to stop at length!!;
|
||||
while (iter.peek() != target) {
|
||||
iter.next();
|
||||
}
|
||||
iter.insertHere(newItem);
|
||||
// this->_length++;
|
||||
}
|
||||
void remove(const E &target) {
|
||||
Iterator<E> iter = Iterator<E>(&this->head);
|
||||
while (iter.peekNext() != target) {
|
||||
iter.next();
|
||||
}
|
||||
Node<E> *toBeDeleted = iter.peekNode()->getNextPtr();
|
||||
iter.peekNode()->setNext(toBeDeleted->getNextPtr());
|
||||
delete toBeDeleted;
|
||||
// this->_length--;
|
||||
}
|
||||
Iterator<E> iterate() {
|
||||
return Iterator<E>(&this->head);
|
||||
}
|
||||
};
|
||||
Node map[100000];
|
||||
|
||||
int main() {
|
||||
List<int> aList(0);
|
||||
aList.insert(0, 1);
|
||||
map[0].prev = 1;
|
||||
map[0].next = 1;
|
||||
map[1].prev = 0;
|
||||
map[1].next = 0;
|
||||
int n;
|
||||
int cmd, x, y;
|
||||
scanf("%d", &n);
|
||||
while (n) {
|
||||
scanf("%d %d", &cmd, &x);
|
||||
switch (cmd) {
|
||||
case 1: {
|
||||
scanf("%d", &y);
|
||||
aList.insert(x, y);
|
||||
break;
|
||||
}
|
||||
case 2: {
|
||||
Iterator<int> iter = aList.iterate();
|
||||
while (iter.peek() != x) {
|
||||
iter.next();
|
||||
switch(cmd) {
|
||||
case 1: {
|
||||
scanf("%d", &y);
|
||||
map[y].prev = x;
|
||||
map[y].next = map[x].next;
|
||||
map[x].next = y;
|
||||
map[map[y].next].prev = y;
|
||||
break;
|
||||
}
|
||||
case 2: {
|
||||
printf("%d\n", map[x].next);
|
||||
break;
|
||||
}
|
||||
case 3: {
|
||||
map[map[x].prev].next = map[x].next;
|
||||
map[map[x].next].prev = map[x].prev;
|
||||
break;
|
||||
}
|
||||
printf("%d\n", iter.peekNext());
|
||||
break;
|
||||
}
|
||||
case 3: {
|
||||
aList.remove(x);
|
||||
}
|
||||
}
|
||||
n--;
|
||||
}
|
||||
|
||||
Iterator<int> iter = aList.iterate();
|
||||
iter.next();
|
||||
while (true) {
|
||||
int content = iter.peek();
|
||||
if (content == 0) {
|
||||
break;
|
||||
}
|
||||
printf("%d\n", content);
|
||||
iter.next();
|
||||
int cur = map[0].next;
|
||||
while (cur != 0) {
|
||||
printf("%d\n", cur);
|
||||
cur = map[cur].next;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
48
2023203/main.cpp
Normal file
48
2023203/main.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#include <stdio.h>
|
||||
// 创建一个栈,拿到一个数,与栈顶比较,如果比栈顶小,压栈(正在波峰向波谷下降)
|
||||
// 比栈顶大,弹栈并计数;和栈顶相同,计数并且压入。
|
||||
|
||||
struct NumberNode {
|
||||
int data;
|
||||
int count;
|
||||
};
|
||||
|
||||
|
||||
long long totalPairs;
|
||||
NumberNode powers[500010];
|
||||
|
||||
int main() {
|
||||
int len = 1;
|
||||
int N;
|
||||
int input;
|
||||
totalPairs = 0;
|
||||
powers[0].data = 0;
|
||||
powers[0].count = 0;
|
||||
scanf("%d", &N);
|
||||
scanf("%d", &powers[1].data);
|
||||
powers[1].count = 1;
|
||||
N--;
|
||||
for (int i = 0; i < N; i++) {
|
||||
scanf("%d", &input);
|
||||
while (len != 0 && powers[len].data < input) {
|
||||
totalPairs += powers[len].count;
|
||||
len--;
|
||||
}
|
||||
if (len == 0 ) {
|
||||
powers[1].data = input;
|
||||
powers[1].count = 1;
|
||||
len = 1;
|
||||
} else {
|
||||
if (powers[len].data == input) {
|
||||
totalPairs += powers[len].count + (len > 1);
|
||||
powers[len].count++;
|
||||
}
|
||||
else {
|
||||
totalPairs += 1;
|
||||
powers[++len] = NumberNode {input, 1};
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("%lld", totalPairs);
|
||||
return 0;
|
||||
}
|
||||
164
2023204/main.cpp
Normal file
164
2023204/main.cpp
Normal file
@@ -0,0 +1,164 @@
|
||||
#include <stdio.h>
|
||||
#define DEAD false
|
||||
#define ALIVE true
|
||||
// 'Natural count' is the name of the node when all the nodes in the tree is
|
||||
// alive. xxx_num denotes the count as the question described.
|
||||
|
||||
// SEE ALSO BRANCH 04_02 WITH A CORRECT IMPLEMENTATION
|
||||
|
||||
int ans[200] = {0};
|
||||
long long dead_nodes_num[105] = {0};
|
||||
int total_dead_nodes = 0;
|
||||
long long target_nodes_num[105] = {0};
|
||||
int total_target_nodes = 0;
|
||||
|
||||
struct SeveralContinuedDeadNode {
|
||||
bool status;
|
||||
long long length; // Caution that when status == dead and length == 1, the node itself is alive.
|
||||
};
|
||||
|
||||
struct SearchResult {
|
||||
int next_ans_pos; // pos in the ans[] array.
|
||||
long long count_in_father_line; // natural count.
|
||||
};
|
||||
|
||||
SearchResult find_node(long long target, long long start_num, int next_dead_node_num_ptr,
|
||||
SeveralContinuedDeadNode *upper_line, long long upper_line_segment_count,
|
||||
int layer) {
|
||||
long long spawned_node_max_num = start_num - 1;
|
||||
long long segment_count = upper_line_segment_count;
|
||||
SeveralContinuedDeadNode current_line_node[200] = {0};
|
||||
long long current_line_len = 0;
|
||||
bool all_dead = true;
|
||||
|
||||
// Spawing all the nodes in current line
|
||||
for (int upper_line_iter = 0; upper_line_iter < upper_line_segment_count; upper_line_iter++) {
|
||||
if (upper_line[upper_line_iter].status == DEAD) {
|
||||
current_line_node[current_line_len] = upper_line[upper_line_iter];
|
||||
current_line_node[current_line_len].length *= 2;
|
||||
current_line_len++;
|
||||
}
|
||||
else {
|
||||
all_dead = false;
|
||||
long long total_spawned_after =
|
||||
spawned_node_max_num + upper_line[upper_line_iter].length * 2;
|
||||
while (dead_nodes_num[next_dead_node_num_ptr] <= total_spawned_after &&
|
||||
next_dead_node_num_ptr < total_dead_nodes) {
|
||||
if (dead_nodes_num[next_dead_node_num_ptr] - spawned_node_max_num > 1) {
|
||||
current_line_node[current_line_len] = SeveralContinuedDeadNode{
|
||||
ALIVE, dead_nodes_num[next_dead_node_num_ptr] - spawned_node_max_num - 1};
|
||||
current_line_len++; // things before this new dead node
|
||||
}
|
||||
current_line_node[current_line_len] =
|
||||
SeveralContinuedDeadNode{DEAD, 1}; // this new dead node
|
||||
current_line_len++;
|
||||
|
||||
spawned_node_max_num = dead_nodes_num[next_dead_node_num_ptr]; // we now spawned to
|
||||
// this dead node.
|
||||
next_dead_node_num_ptr++;
|
||||
}
|
||||
if (spawned_node_max_num < total_spawned_after) {
|
||||
current_line_node[current_line_len] =
|
||||
SeveralContinuedDeadNode{ALIVE, total_spawned_after - spawned_node_max_num};
|
||||
current_line_len++;
|
||||
}
|
||||
|
||||
spawned_node_max_num = total_spawned_after;
|
||||
}
|
||||
}
|
||||
|
||||
if (all_dead) {
|
||||
return SearchResult{1, -1};
|
||||
}
|
||||
|
||||
if (target <= spawned_node_max_num) {
|
||||
ans[0] = target;
|
||||
long long natural_pos = -1, num = start_num - 1;
|
||||
for (int i = 0; i < current_line_len; i++) {
|
||||
if (current_line_node[i].status == DEAD) {
|
||||
natural_pos += current_line_node[i].length;
|
||||
num += (current_line_node[i].length == 1);
|
||||
if (num == target) {
|
||||
return SearchResult{1, natural_pos / 2};
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Now deal with alive nodes
|
||||
if (num + current_line_node[i].length < target) {
|
||||
natural_pos += current_line_node[i].length;
|
||||
num += current_line_node[i].length;
|
||||
}
|
||||
else {
|
||||
// if we made it here, then the target > num + current_line_node[i].length
|
||||
return SearchResult{1, (natural_pos + target - num) / 2};
|
||||
}
|
||||
}
|
||||
return SearchResult{1, -1};
|
||||
}
|
||||
else {
|
||||
SearchResult current_line_pos =
|
||||
find_node(target, spawned_node_max_num + 1, next_dead_node_num_ptr, current_line_node,
|
||||
current_line_len, layer + 1);
|
||||
if (current_line_pos.count_in_father_line < 0) {
|
||||
// The path does not exist.
|
||||
return SearchResult{1, -1};
|
||||
}
|
||||
|
||||
long long natural_count = -1, num = start_num - 1;
|
||||
for (int i = 0; i < current_line_len; i++) {
|
||||
natural_count += current_line_node[i].length;
|
||||
if (current_line_node[i].status == ALIVE || current_line_node[i].length == 1) {
|
||||
num += current_line_node[i].length;
|
||||
}
|
||||
|
||||
if (natural_count >= current_line_pos.count_in_father_line) {
|
||||
num -= natural_count - current_line_pos.count_in_father_line;
|
||||
ans[current_line_pos.next_ans_pos++] = num;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return SearchResult{current_line_pos.next_ans_pos,
|
||||
current_line_pos.count_in_father_line / 2};
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
scanf("%d %d", &total_dead_nodes, &total_target_nodes);
|
||||
for (int i = 0; i < total_dead_nodes; i++) {
|
||||
scanf("%lld", &dead_nodes_num[i]);
|
||||
}
|
||||
for (int i = 0; i < total_target_nodes; i++) {
|
||||
scanf("%lld", &target_nodes_num[i]);
|
||||
}
|
||||
if (dead_nodes_num[0] == 1) {
|
||||
for (int i = 0; i < total_target_nodes; i++) {
|
||||
if (target_nodes_num[i] == 1) {
|
||||
printf("1\n");
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
printf("0\n");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
SeveralContinuedDeadNode firstline = SeveralContinuedDeadNode{ALIVE, 1};
|
||||
for (int i = 0; i < total_target_nodes; i++) {
|
||||
SearchResult result = find_node(target_nodes_num[i], 2, 0, &firstline, 1, 2);
|
||||
if (result.count_in_father_line == -1) {
|
||||
printf("0\n");
|
||||
}
|
||||
else {
|
||||
printf("1 ");
|
||||
for (int j = result.next_ans_pos - 1; j >= 0; j--) {
|
||||
printf("%lld ", ans[j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
71
2023205/main.cpp
Normal file
71
2023205/main.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
#include <stdio.h>
|
||||
// 并查集。并查集只关注双向连通性。
|
||||
|
||||
struct Edge {
|
||||
unsigned short end;
|
||||
int next;
|
||||
};
|
||||
|
||||
Edge edges[1050000];
|
||||
char nodes_visited[65540];
|
||||
int head[65540], len;
|
||||
|
||||
int add_edge(int start, int terminal) {
|
||||
edges[++len].end = terminal;
|
||||
edges[len].next = head[start];
|
||||
head[start] = len;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dfs(int start) {
|
||||
if (nodes_visited[start] >= 1) {
|
||||
nodes_visited[start] = 2;
|
||||
return 0;
|
||||
}
|
||||
nodes_visited[start] = 1;
|
||||
for (int j = head[start]; j != 0; j = edges[j].next) {
|
||||
dfs(edges[j].end);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int N, M;
|
||||
scanf("%d %d", &N, &M);
|
||||
for (int start = 0; start < N; start++) {
|
||||
int edge_count;
|
||||
scanf("%d", &edge_count);
|
||||
for (int j = 0; j < edge_count; j++) {
|
||||
int terminal;
|
||||
scanf("%d", &terminal);
|
||||
add_edge(start, terminal);
|
||||
}
|
||||
}
|
||||
|
||||
dfs(0);
|
||||
|
||||
for (int i = 0; i < N; i++) {
|
||||
if (nodes_visited[i] < 1) {
|
||||
// Not connected!
|
||||
for (int j = 0; j <= M; j++) {
|
||||
printf("0\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
printf("1\n");
|
||||
|
||||
for (int i = 0; i < M; i++) {
|
||||
int start, end;
|
||||
scanf("%d %d", &start, &end);
|
||||
if (nodes_visited[end] == 2) {
|
||||
printf("1\n");
|
||||
}
|
||||
else {
|
||||
printf("0\n");
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user