Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 612c974427 | |||
| 84fd6d7a92 | |||
| 1398ae9e17 | |||
| 56ae0d7124 | |||
| 0c012f7938 | |||
| e325fd405e | |||
| f90a7d1d0b | |||
| bd04896dae | |||
| f2b8d1fe4c | |||
| be03307655 |
@@ -23,3 +23,6 @@ Thumbs.db
|
|||||||
|
|
||||||
# 排除竞赛平台配置文件
|
# 排除竞赛平台配置文件
|
||||||
.cph/
|
.cph/
|
||||||
|
|
||||||
|
# 排除练习题目相关文件
|
||||||
|
.exercise/
|
||||||
Vendored
+1
-1
@@ -10,7 +10,7 @@
|
|||||||
"UNICODE",
|
"UNICODE",
|
||||||
"_UNICODE"
|
"_UNICODE"
|
||||||
],
|
],
|
||||||
"compilerPath": "D:/Program Files/mingw64/bin/gcc.exe",
|
"compilerPath": "D:/settings/mingw64/bin/gcc.exe",
|
||||||
"cStandard": "c17",
|
"cStandard": "c17",
|
||||||
"cppStandard": "c++17",
|
"cppStandard": "c++17",
|
||||||
"intelliSenseMode": "windows-gcc-x64",
|
"intelliSenseMode": "windows-gcc-x64",
|
||||||
|
|||||||
Vendored
+2
-2
@@ -8,8 +8,8 @@
|
|||||||
"args": [],
|
"args": [],
|
||||||
"stopAtEntry": false,
|
"stopAtEntry": false,
|
||||||
"externalConsole": true,
|
"externalConsole": true,
|
||||||
"cwd": "d:/Code/C_code/数据结构/陈越数据结构",
|
"cwd": "d:/Code/C_code/exercise",
|
||||||
"program": "d:/Code/C_code/数据结构/陈越数据结构/build/Debug/outDebug",
|
"program": "d:/Code/C_code/exercise/build/Debug/outDebug",
|
||||||
"MIMode": "gdb",
|
"MIMode": "gdb",
|
||||||
"miDebuggerPath": "gdb",
|
"miDebuggerPath": "gdb",
|
||||||
"setupCommands": [
|
"setupCommands": [
|
||||||
|
|||||||
Vendored
+1
-1
@@ -4,7 +4,7 @@
|
|||||||
{
|
{
|
||||||
"type": "shell",
|
"type": "shell",
|
||||||
"label": "C/C++: gcc.exe 生成活动文件",
|
"label": "C/C++: gcc.exe 生成活动文件",
|
||||||
"command": "D:\\Program Files\\mingw64\\bin\\gcc.exe",
|
"command": "D:\\settings\\mingw64\\bin\\gcc.exe",
|
||||||
"args":
|
"args":
|
||||||
[
|
[
|
||||||
"-fdiagnostics-color=always",
|
"-fdiagnostics-color=always",
|
||||||
|
|||||||
@@ -3,6 +3,9 @@
|
|||||||
## 项目简介
|
## 项目简介
|
||||||
这是一个完整的C语言学习代码库,包含了从基础语法到高级项目的全面学习内容。代码库涵盖了翁凯老师C语言课程的所有章节练习、课堂代码实践以及完整的项目实战。
|
这是一个完整的C语言学习代码库,包含了从基础语法到高级项目的全面学习内容。代码库涵盖了翁凯老师C语言课程的所有章节练习、课堂代码实践以及完整的项目实战。
|
||||||
|
|
||||||
|
### 仓库地址
|
||||||
|
- GitHub仓库为:https://github.com/LHY0125/Learn_C.git
|
||||||
|
|
||||||
## 项目结构
|
## 项目结构
|
||||||
|
|
||||||
### 📚 翁凯C语言/ - 系统化学习路径
|
### 📚 翁凯C语言/ - 系统化学习路径
|
||||||
|
|||||||
@@ -0,0 +1,177 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#include <direct.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define MAX_SIZE 1000
|
||||||
|
|
||||||
|
// 栈结构用于存储数字
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
int data[MAX_SIZE];
|
||||||
|
int top;
|
||||||
|
} Stack;
|
||||||
|
|
||||||
|
// 初始化栈
|
||||||
|
void initStack(Stack *s)
|
||||||
|
{
|
||||||
|
s->top = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 判断栈是否为空
|
||||||
|
int isEmpty(Stack *s)
|
||||||
|
{
|
||||||
|
return s->top == -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 入栈
|
||||||
|
void push(Stack *s, int value)
|
||||||
|
{
|
||||||
|
if (s->top < MAX_SIZE - 1)
|
||||||
|
{
|
||||||
|
s->data[++s->top] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 出栈
|
||||||
|
int pop(Stack *s)
|
||||||
|
{
|
||||||
|
if (!isEmpty(s))
|
||||||
|
{
|
||||||
|
return s->data[s->top--];
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取栈顶元素
|
||||||
|
int peek(Stack *s)
|
||||||
|
{
|
||||||
|
if (!isEmpty(s))
|
||||||
|
{
|
||||||
|
return s->data[s->top];
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 判断运算符优先级
|
||||||
|
int precedence(char op)
|
||||||
|
{
|
||||||
|
if (op == '+' || op == '-')
|
||||||
|
return 1;
|
||||||
|
if (op == '*' || op == '/')
|
||||||
|
return 2;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 执行运算
|
||||||
|
int calculate(int a, int b, char op)
|
||||||
|
{
|
||||||
|
switch (op)
|
||||||
|
{
|
||||||
|
case '+':
|
||||||
|
return a + b;
|
||||||
|
case '-':
|
||||||
|
return a - b;
|
||||||
|
case '*':
|
||||||
|
return a * b;
|
||||||
|
case '/':
|
||||||
|
return a / b;
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 计算表达式
|
||||||
|
int evaluateExpression(char *expression)
|
||||||
|
{
|
||||||
|
Stack numbers;
|
||||||
|
Stack operators;
|
||||||
|
initStack(&numbers);
|
||||||
|
initStack(&operators);
|
||||||
|
|
||||||
|
int len = strlen(expression);
|
||||||
|
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
// 跳过空格
|
||||||
|
if (expression[i] == ' ')
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果是数字
|
||||||
|
if (isdigit(expression[i]))
|
||||||
|
{
|
||||||
|
int num = 0;
|
||||||
|
// 读取完整的数字
|
||||||
|
while (i < len && isdigit(expression[i]))
|
||||||
|
{
|
||||||
|
num = num * 10 + (expression[i] - '0');
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
i--; // 回退一位,因为for循环会自动增加
|
||||||
|
push(&numbers, num);
|
||||||
|
}
|
||||||
|
// 如果是运算符
|
||||||
|
else if (expression[i] == '+' || expression[i] == '-' ||
|
||||||
|
expression[i] == '*' || expression[i] == '/')
|
||||||
|
{
|
||||||
|
|
||||||
|
// 处理运算符优先级
|
||||||
|
while (!isEmpty(&operators) &&
|
||||||
|
precedence((char)peek(&operators)) >= precedence(expression[i]))
|
||||||
|
{
|
||||||
|
int b = pop(&numbers);
|
||||||
|
int a = pop(&numbers);
|
||||||
|
char op = (char)pop(&operators);
|
||||||
|
push(&numbers, calculate(a, b, op));
|
||||||
|
}
|
||||||
|
push(&operators, expression[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理剩余的运算符
|
||||||
|
while (!isEmpty(&operators))
|
||||||
|
{
|
||||||
|
int b = pop(&numbers);
|
||||||
|
int a = pop(&numbers);
|
||||||
|
char op = (char)pop(&operators);
|
||||||
|
push(&numbers, calculate(a, b, op));
|
||||||
|
}
|
||||||
|
|
||||||
|
return pop(&numbers);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// 设置控制台编码为UTF-8,防止中文乱码
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul"); // 设置控制台编码为UTF-8
|
||||||
|
SetConsoleOutputCP(65001); // 设置控制台输出编码
|
||||||
|
SetConsoleCP(65001); // 设置控制台输入编码
|
||||||
|
_mkdir("records");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
char expression[MAX_SIZE];
|
||||||
|
|
||||||
|
// 读取表达式
|
||||||
|
if (fgets(expression, sizeof(expression), stdin) != NULL)
|
||||||
|
{
|
||||||
|
// 去除换行符
|
||||||
|
int len = strlen(expression);
|
||||||
|
if (len > 0 && expression[len - 1] == '\n')
|
||||||
|
{
|
||||||
|
expression[len - 1] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 计算并输出结果
|
||||||
|
int result = evaluateExpression(expression);
|
||||||
|
printf("%d\n", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,158 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#define MAX_SIZE 10002
|
||||||
|
|
||||||
|
// 栈结构
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
char data[MAX_SIZE];
|
||||||
|
int top;
|
||||||
|
} Stack;
|
||||||
|
|
||||||
|
// 初始化栈
|
||||||
|
void initStack(Stack *s)
|
||||||
|
{
|
||||||
|
s->top = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 判断栈是否为空
|
||||||
|
bool isEmpty(Stack *s)
|
||||||
|
{
|
||||||
|
return s->top == -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 判断栈是否已满
|
||||||
|
bool isFull(Stack *s)
|
||||||
|
{
|
||||||
|
return s->top == MAX_SIZE - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 入栈
|
||||||
|
bool push(Stack *s, char item)
|
||||||
|
{
|
||||||
|
if (isFull(s))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
s->data[++s->top] = item;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 出栈
|
||||||
|
bool pop(Stack *s, char *item)
|
||||||
|
{
|
||||||
|
if (isEmpty(s))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
*item = s->data[s->top--];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 判断字符是否为左括号
|
||||||
|
bool isLeftBracket(char c)
|
||||||
|
{
|
||||||
|
return c == '(' || c == '[' || c == '{';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 判断字符是否为右括号
|
||||||
|
bool isRightBracket(char c)
|
||||||
|
{
|
||||||
|
return c == ')' || c == ']' || c == '}';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 判断左右括号是否匹配
|
||||||
|
bool isMatching(char left, char right)
|
||||||
|
{
|
||||||
|
return (left == '(' && right == ')') ||
|
||||||
|
(left == '[' && right == ']') ||
|
||||||
|
(left == '{' && right == '}');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 括号匹配验证函数
|
||||||
|
bool isValidBrackets(char *s)
|
||||||
|
{
|
||||||
|
Stack stack;
|
||||||
|
initStack(&stack);
|
||||||
|
|
||||||
|
int len = strlen(s);
|
||||||
|
|
||||||
|
// 空字符串返回true
|
||||||
|
if (len == 0)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
char current = s[i];
|
||||||
|
|
||||||
|
// 如果是左括号,入栈
|
||||||
|
if (isLeftBracket(current))
|
||||||
|
{
|
||||||
|
if (!push(&stack, current))
|
||||||
|
{
|
||||||
|
// 栈满,返回false
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 如果是右括号,检查匹配
|
||||||
|
else if (isRightBracket(current))
|
||||||
|
{
|
||||||
|
// 栈为空,说明没有对应的左括号
|
||||||
|
if (isEmpty(&stack))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
char top;
|
||||||
|
pop(&stack, &top);
|
||||||
|
|
||||||
|
// 检查是否匹配
|
||||||
|
if (!isMatching(top, current))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 根据题目描述,输入只包含括号字符,不应该有其他字符
|
||||||
|
}
|
||||||
|
|
||||||
|
// 最后栈应该为空,表示所有括号都已匹配
|
||||||
|
return isEmpty(&stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
char s[MAX_SIZE];
|
||||||
|
|
||||||
|
// 读取输入字符串
|
||||||
|
if (fgets(s, sizeof(s), stdin) != NULL)
|
||||||
|
{
|
||||||
|
// 去除换行符
|
||||||
|
int len = strlen(s);
|
||||||
|
if (len > 0 && s[len - 1] == '\n')
|
||||||
|
{
|
||||||
|
s[len - 1] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证括号匹配并输出结果
|
||||||
|
if (isValidBrackets(s))
|
||||||
|
{
|
||||||
|
printf("true\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("false\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// 如果读取失败,输出false
|
||||||
|
printf("false\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef long long ll;
|
||||||
|
|
||||||
|
int main(void){
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul");
|
||||||
|
SetConsoleOutputCP(65001);
|
||||||
|
SetConsoleCP(65001);
|
||||||
|
#endif
|
||||||
|
int n; if (scanf("%d", &n) != 1) return 0;
|
||||||
|
int a, b; if (scanf("%d %d", &a, &b) != 2) return 0;
|
||||||
|
int *val = (int*)malloc((size_t)(n + 1) * sizeof(int));
|
||||||
|
for (int i = 1; i <= n; ++i) scanf("%d", &val[i]);
|
||||||
|
ll *dp = (ll*)malloc((size_t)(n + 1) * sizeof(ll));
|
||||||
|
for (int i = 0; i <= n; ++i) dp[i] = (ll)4e18;
|
||||||
|
dp[0] = 0;
|
||||||
|
for (int i = 1; i <= n; ++i){
|
||||||
|
int mn = val[i], mx = val[i];
|
||||||
|
for (int j = i; j >= 1; --j){
|
||||||
|
if (val[j] < mn) mn = val[j];
|
||||||
|
if (val[j] > mx) mx = val[j];
|
||||||
|
ll cost = dp[j-1] + (ll)a + (ll)b * (ll)(mx - mn) * (ll)(mx - mn);
|
||||||
|
if (cost < dp[i]) dp[i] = cost;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("%lld\n", dp[n]);
|
||||||
|
free(dp); free(val);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef unsigned long long ull;
|
||||||
|
|
||||||
|
typedef struct { ull l, r; } Interval;
|
||||||
|
|
||||||
|
static int cmpL(const void *a, const void *b){
|
||||||
|
const Interval *x = (const Interval*)a;
|
||||||
|
const Interval *y = (const Interval*)b;
|
||||||
|
if (x->l < y->l) return -1;
|
||||||
|
if (x->l > y->l) return 1;
|
||||||
|
if (x->r < y->r) return -1;
|
||||||
|
if (x->r > y->r) return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void){
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul");
|
||||||
|
SetConsoleOutputCP(65001);
|
||||||
|
SetConsoleCP(65001);
|
||||||
|
#endif
|
||||||
|
size_t cap = 1 << 16;
|
||||||
|
size_t n = 0;
|
||||||
|
Interval *arr = (Interval*)malloc(cap * sizeof(Interval));
|
||||||
|
ull A, B;
|
||||||
|
while (scanf("%llu %llu", &A, &B) == 2){
|
||||||
|
if (n == cap){
|
||||||
|
cap <<= 1;
|
||||||
|
Interval *tmp = (Interval*)realloc(arr, cap * sizeof(Interval));
|
||||||
|
if (!tmp){ free(arr); return 0; }
|
||||||
|
arr = tmp;
|
||||||
|
}
|
||||||
|
ull l = A < B ? A : B;
|
||||||
|
ull r = A > B ? A : B;
|
||||||
|
arr[n].l = l;
|
||||||
|
arr[n].r = r;
|
||||||
|
++n;
|
||||||
|
}
|
||||||
|
if (n < 2){
|
||||||
|
printf("0\n");
|
||||||
|
free(arr);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
qsort(arr, n, sizeof(Interval), cmpL);
|
||||||
|
ull ans = 0ULL;
|
||||||
|
ull max_r_prev = arr[0].r;
|
||||||
|
for (size_t i = 1; i < n; ++i){
|
||||||
|
ull l = arr[i].l;
|
||||||
|
ull r = arr[i].r;
|
||||||
|
ull M = r < max_r_prev ? r : max_r_prev;
|
||||||
|
if (M >= l){
|
||||||
|
ull len = M - l + 1ULL;
|
||||||
|
if (len > ans) ans = len;
|
||||||
|
}
|
||||||
|
if (r > max_r_prev) max_r_prev = r;
|
||||||
|
}
|
||||||
|
printf("%llu\n", ans);
|
||||||
|
free(arr);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef unsigned long long ull;
|
||||||
|
|
||||||
|
static char* dupstr(const char* s){ size_t len = strlen(s); char* p = (char*)malloc(len + 1); memcpy(p, s, len + 1); return p; }
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char **keys;
|
||||||
|
char **vals;
|
||||||
|
unsigned char *used;
|
||||||
|
size_t cap;
|
||||||
|
} Map;
|
||||||
|
|
||||||
|
static ull fnv1a(const char *s){
|
||||||
|
ull h = 1469598103934665603ULL;
|
||||||
|
while (*s){ h ^= (unsigned char)(*s++); h *= 1099511628211ULL; }
|
||||||
|
return h;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void map_init(Map *m, size_t n){ size_t c = 1; while (c < (n << 1)) c <<= 1; m->cap = c; m->keys = (char**)calloc(c, sizeof(char*)); m->vals = (char**)calloc(c, sizeof(char*)); m->used = (unsigned char*)calloc(c, 1); }
|
||||||
|
|
||||||
|
static void map_put(Map *m, const char *k, const char *v){ size_t mask = m->cap - 1; size_t i = (size_t)(fnv1a(k) & mask); for (;;){ if (!m->used[i]){ m->used[i] = 1; m->keys[i] = dupstr(k); m->vals[i] = dupstr(v); return; } if (strcmp(m->keys[i], k) == 0){ free(m->vals[i]); m->vals[i] = dupstr(v); return; } i = (i + 1) & mask; } }
|
||||||
|
|
||||||
|
static const char* map_get(Map *m, const char *k){ size_t mask = m->cap - 1; size_t i = (size_t)(fnv1a(k) & mask); for (;;){ if (!m->used[i]) return NULL; if (strcmp(m->keys[i], k) == 0) return m->vals[i]; i = (i + 1) & mask; } }
|
||||||
|
|
||||||
|
static void map_free(Map *m){ for (size_t i = 0; i < m->cap; ++i){ if (m->used[i]){ free(m->keys[i]); free(m->vals[i]); } } free(m->used); free(m->keys); free(m->vals); }
|
||||||
|
|
||||||
|
int main(void){
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul");
|
||||||
|
SetConsoleOutputCP(65001);
|
||||||
|
SetConsoleCP(65001);
|
||||||
|
#endif
|
||||||
|
int M; if (scanf("%d", &M) != 1) return 0;
|
||||||
|
Map mp; map_init(&mp, (size_t)(M > 0 ? M : 1));
|
||||||
|
char s1[4096], s2[4096];
|
||||||
|
for (int i = 0; i < M; ++i){ if (scanf("%4095s %4095s", s1, s2) != 2) return 0; map_put(&mp, s1, s2); }
|
||||||
|
int L; if (scanf("%d", &L) != 1){ map_free(&mp); return 0; }
|
||||||
|
int ch; do { ch = getchar(); } while (ch != '\n' && ch != EOF);
|
||||||
|
char *line = NULL; size_t bufcap = 0; ssize_t linelen;
|
||||||
|
for (int li = 0; li < L; ++li){
|
||||||
|
size_t len = 0; bufcap = 4096; line = (char*)malloc(bufcap);
|
||||||
|
int c;
|
||||||
|
while ((c = getchar()) != EOF && c != '\n'){
|
||||||
|
if (len + 1 >= bufcap){ bufcap <<= 1; line = (char*)realloc(line, bufcap); }
|
||||||
|
line[len++] = (char)c;
|
||||||
|
}
|
||||||
|
line[len] = '\0';
|
||||||
|
char *out = (char*)malloc(len * 2 + 16);
|
||||||
|
size_t outlen = 0;
|
||||||
|
const char *p = line;
|
||||||
|
while (*p){
|
||||||
|
while (*p && (*p == ' ' || *p == '\t' || *p == '\r')) ++p;
|
||||||
|
if (!*p) break;
|
||||||
|
const char *q = p;
|
||||||
|
while (*q && *q != ' ' && *q != '\t' && *q != '\r') ++q;
|
||||||
|
size_t toklen = (size_t)(q - p);
|
||||||
|
char *tok = (char*)malloc(toklen + 1);
|
||||||
|
memcpy(tok, p, toklen); tok[toklen] = '\0';
|
||||||
|
const char *rep = map_get(&mp, tok);
|
||||||
|
const char *emit = rep ? rep : tok;
|
||||||
|
size_t emitlen = strlen(emit);
|
||||||
|
if (outlen) out[outlen++] = ' ';
|
||||||
|
if (outlen + emitlen + 1 >= bufcap){ size_t need = outlen + emitlen + 1; size_t nc = bufcap; while (nc < need) nc <<= 1; out = (char*)realloc(out, nc); bufcap = nc; }
|
||||||
|
memcpy(out + outlen, emit, emitlen); outlen += emitlen; out[outlen] = '\0';
|
||||||
|
free(tok);
|
||||||
|
p = q;
|
||||||
|
}
|
||||||
|
printf("%s\n", out);
|
||||||
|
free(out);
|
||||||
|
free(line);
|
||||||
|
if (c == EOF) break;
|
||||||
|
}
|
||||||
|
map_free(&mp);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef unsigned long long ull;
|
||||||
|
|
||||||
|
static char* dupstr(const char* s){ size_t len = strlen(s); char* p = (char*)malloc(len + 1); memcpy(p, s, len + 1); return p; }
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char **keys;
|
||||||
|
int *cnt;
|
||||||
|
unsigned char *used;
|
||||||
|
size_t cap;
|
||||||
|
} MapC;
|
||||||
|
|
||||||
|
static ull fnv1a(const char *s){ ull h = 1469598103934665603ULL; while (*s){ h ^= (unsigned char)(*s++); h *= 1099511628211ULL; } return h; }
|
||||||
|
|
||||||
|
static void map_init(MapC *m, size_t n){ size_t c = 1; while (c < (n << 1)) c <<= 1; m->cap = c; m->keys = (char**)calloc(c, sizeof(char*)); m->cnt = (int*)calloc(c, sizeof(int)); m->used = (unsigned char*)calloc(c, 1); }
|
||||||
|
|
||||||
|
static void map_inc(MapC *m, const char *k){ size_t mask = m->cap - 1; size_t i = (size_t)(fnv1a(k) & mask); for (;;){ if (!m->used[i]){ m->used[i] = 1; m->keys[i] = dupstr(k); m->cnt[i] = 1; return; } if (strcmp(m->keys[i], k) == 0){ ++m->cnt[i]; return; } i = (i + 1) & mask; } }
|
||||||
|
|
||||||
|
static int map_get_cnt(MapC *m, const char *k){ size_t mask = m->cap - 1; size_t i = (size_t)(fnv1a(k) & mask); for (;;){ if (!m->used[i]) return 0; if (strcmp(m->keys[i], k) == 0) return m->cnt[i]; i = (i + 1) & mask; } }
|
||||||
|
|
||||||
|
static void map_free(MapC *m){ for (size_t i = 0; i < m->cap; ++i){ if (m->used[i]) free(m->keys[i]); } free(m->used); free(m->cnt); free(m->keys); }
|
||||||
|
|
||||||
|
int main(void){
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul");
|
||||||
|
SetConsoleOutputCP(65001);
|
||||||
|
SetConsoleCP(65001);
|
||||||
|
#endif
|
||||||
|
int N; if (scanf("%d", &N) != 1) return 0;
|
||||||
|
MapC mp; map_init(&mp, (size_t)(N > 0 ? N : 1));
|
||||||
|
char **tks = (char**)malloc((size_t)N * sizeof(char*));
|
||||||
|
char buf[4096];
|
||||||
|
for (int i = 0; i < N; ++i){ if (scanf("%4095s", buf) != 1){ N = i; break; } tks[i] = dupstr(buf); map_inc(&mp, tks[i]); }
|
||||||
|
int minc = 0x7fffffff; for (size_t i = 0; i < mp.cap; ++i){ if (mp.used[i]){ if (mp.cnt[i] < minc) minc = mp.cnt[i]; } }
|
||||||
|
int first = 1;
|
||||||
|
for (int i = 0; i < N; ++i){ int c = map_get_cnt(&mp, tks[i]); if (c != minc){ if (!first) putchar(' '); first = 0; fputs(tks[i], stdout); } }
|
||||||
|
putchar('\n');
|
||||||
|
for (int i = 0; i < N; ++i) free(tks[i]);
|
||||||
|
free(tks);
|
||||||
|
map_free(&mp);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static int M;
|
||||||
|
|
||||||
|
static int imin(int a, int b){ return a < b ? a : b; }
|
||||||
|
static int imax(int a, int b){ return a > b ? a : b; }
|
||||||
|
|
||||||
|
static int S;
|
||||||
|
static int *col;
|
||||||
|
static int *cnt;
|
||||||
|
static int *memo;
|
||||||
|
|
||||||
|
static int idx(int l, int r, int k){ return ((l - 1) * S + (r - 1)) * M + k; }
|
||||||
|
|
||||||
|
static int F(int l, int r, int k){
|
||||||
|
if (k >= M) k = M - 1;
|
||||||
|
int id = idx(l, r, k);
|
||||||
|
int v = memo[id];
|
||||||
|
if (v != -1) return v;
|
||||||
|
if (l == r){
|
||||||
|
int L = cnt[r] + k;
|
||||||
|
v = (L >= M) ? 0 : (M - L);
|
||||||
|
memo[id] = v;
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
int need = cnt[r] + k;
|
||||||
|
int best = F(l, r - 1, 0) + ((need >= M) ? 0 : (M - need));
|
||||||
|
for (int i = l; i <= r - 1; ++i){
|
||||||
|
if (col[i] == col[r]){
|
||||||
|
int nk = cnt[r] + k;
|
||||||
|
if (nk >= M) nk = M - 1;
|
||||||
|
int cand = F(l, i, nk) + F(i + 1, r - 1, 0);
|
||||||
|
if (cand < best) best = cand;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
memo[id] = best;
|
||||||
|
return best;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void){
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul");
|
||||||
|
SetConsoleOutputCP(65001);
|
||||||
|
SetConsoleCP(65001);
|
||||||
|
#endif
|
||||||
|
char s[256];
|
||||||
|
while (scanf("%d", &M) == 1){
|
||||||
|
if (scanf("%255s", s) != 1) break;
|
||||||
|
int n = (int)strlen(s);
|
||||||
|
col = (int*)malloc((size_t)(n + 1) * sizeof(int));
|
||||||
|
cnt = (int*)malloc((size_t)(n + 1) * sizeof(int));
|
||||||
|
S = 0;
|
||||||
|
for (int i = 0; i < n;){
|
||||||
|
int j = i;
|
||||||
|
while (j < n && s[j] == s[i]) ++j;
|
||||||
|
col[++S] = (int)s[i];
|
||||||
|
cnt[S] = j - i;
|
||||||
|
i = j;
|
||||||
|
}
|
||||||
|
memo = (int*)malloc((size_t)S * (size_t)S * (size_t)M * sizeof(int));
|
||||||
|
for (int i = 0; i < S * S * M; ++i) memo[i] = -1;
|
||||||
|
int ans = F(1, S, 0);
|
||||||
|
printf("%d\n", ans);
|
||||||
|
free(memo); free(cnt); free(col);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef long long ll;
|
||||||
|
|
||||||
|
typedef struct { ll x; ll a; } Item;
|
||||||
|
|
||||||
|
static int cmp_item(const void *p, const void *q){
|
||||||
|
const Item *A = (const Item*)p;
|
||||||
|
const Item *B = (const Item*)q;
|
||||||
|
if (A->x < B->x) return -1;
|
||||||
|
if (A->x > B->x) return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int upper_less_ll(const ll *arr, int n, ll key){
|
||||||
|
int l = 1, r = n, ans = 0;
|
||||||
|
while (l <= r){
|
||||||
|
int m = (l + r) >> 1;
|
||||||
|
if (arr[m] < key){ ans = m; l = m + 1; }
|
||||||
|
else r = m - 1;
|
||||||
|
}
|
||||||
|
return ans;
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct { int val; int idx; } Node;
|
||||||
|
|
||||||
|
static Node *seg;
|
||||||
|
static int N;
|
||||||
|
|
||||||
|
static Node merge(Node a, Node b){
|
||||||
|
if (a.val >= b.val) return a; else return b;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void build(int p, int l, int r, const int *R){
|
||||||
|
if (l == r){ seg[p].val = R[l]; seg[p].idx = l; return; }
|
||||||
|
int m = (l + r) >> 1;
|
||||||
|
build(p<<1, l, m, R);
|
||||||
|
build(p<<1|1, m+1, r, R);
|
||||||
|
seg[p] = merge(seg[p<<1], seg[p<<1|1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Node query(int p, int l, int r, int ql, int qr){
|
||||||
|
if (ql <= l && r <= qr) return seg[p];
|
||||||
|
int m = (l + r) >> 1;
|
||||||
|
if (qr <= m) return query(p<<1, l, m, ql, qr);
|
||||||
|
if (ql > m) return query(p<<1|1, m+1, r, ql, qr);
|
||||||
|
Node a = query(p<<1, l, m, ql, qr);
|
||||||
|
Node b = query(p<<1|1, m+1, r, ql, qr);
|
||||||
|
return merge(a, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void){
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul");
|
||||||
|
SetConsoleOutputCP(65001);
|
||||||
|
SetConsoleCP(65001);
|
||||||
|
#endif
|
||||||
|
int n; if (scanf("%d", &n) != 1) return 0;
|
||||||
|
Item *arr = (Item*)malloc((size_t)(n + 1) * sizeof(Item));
|
||||||
|
for (int i = 1; i <= n; ++i){ ll xi, ai; scanf("%lld %lld", &xi, &ai); arr[i].x = xi; arr[i].a = ai; }
|
||||||
|
qsort(arr + 1, n, sizeof(Item), cmp_item);
|
||||||
|
ll *xs = (ll*)malloc((size_t)(n + 1) * sizeof(ll));
|
||||||
|
for (int i = 1; i <= n; ++i) xs[i] = arr[i].x;
|
||||||
|
int *R = (int*)malloc((size_t)(n + 1) * sizeof(int));
|
||||||
|
for (int i = 1; i <= n; ++i){ ll key = arr[i].x + arr[i].a; R[i] = upper_less_ll(xs, n, key); }
|
||||||
|
N = n; seg = (Node*)malloc((size_t)(4 * N + 8) * sizeof(Node));
|
||||||
|
build(1, 1, N, R);
|
||||||
|
int *E = (int*)calloc((size_t)(n + 2), sizeof(int));
|
||||||
|
for (int i = 1; i <= n; ++i){
|
||||||
|
int t = i;
|
||||||
|
int stsz = 0;
|
||||||
|
int *stack = (int*)malloc((size_t)(n - i + 2) * sizeof(int));
|
||||||
|
while (E[t] == 0){
|
||||||
|
int rt = R[t];
|
||||||
|
Node nd = query(1, 1, N, t, rt);
|
||||||
|
if (nd.val == rt){ E[t] = rt + 1; break; }
|
||||||
|
stack[stsz++] = t;
|
||||||
|
t = nd.idx;
|
||||||
|
}
|
||||||
|
int res = E[t];
|
||||||
|
while (stsz){ E[stack[--stsz]] = res; }
|
||||||
|
free(stack);
|
||||||
|
}
|
||||||
|
const int MOD = 998244353;
|
||||||
|
int *dp = (int*)calloc((size_t)(n + 2), sizeof(int));
|
||||||
|
dp[n+1] = 1;
|
||||||
|
for (int i = n; i >= 1; --i){
|
||||||
|
int a1 = dp[i+1];
|
||||||
|
int a2 = dp[E[i]];
|
||||||
|
int s = a1 + a2; if (s >= MOD) s -= MOD;
|
||||||
|
dp[i] = s;
|
||||||
|
}
|
||||||
|
printf("%d\n", dp[1]);
|
||||||
|
free(dp); free(E); free(seg); free(R); free(xs); free(arr);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct { int to, next; } Edge;
|
||||||
|
|
||||||
|
static int *head;
|
||||||
|
static Edge *edges;
|
||||||
|
static int ecnt;
|
||||||
|
|
||||||
|
static void add_edge(int u, int v){ edges[ecnt].to = v; edges[ecnt].next = head[u]; head[u] = ecnt++; }
|
||||||
|
|
||||||
|
static int cmp_desc(const void *a, const void *b){ int x = *(const int*)a, y = *(const int*)b; return y - x; }
|
||||||
|
|
||||||
|
static int dfs(int u, int p){
|
||||||
|
int cnt = 0;
|
||||||
|
for (int e = head[u]; e != -1; e = edges[e].next){ int v = edges[e].to; if (v == p) continue; ++cnt; }
|
||||||
|
if (cnt == 0) return 0;
|
||||||
|
int *ts = (int*)malloc((size_t)cnt * sizeof(int));
|
||||||
|
int k = 0;
|
||||||
|
for (int e = head[u]; e != -1; e = edges[e].next){ int v = edges[e].to; if (v == p) continue; ts[k++] = dfs(v, u); }
|
||||||
|
qsort(ts, (size_t)cnt, sizeof(int), cmp_desc);
|
||||||
|
int best = 0;
|
||||||
|
for (int i = 0; i < cnt; ++i){ int t = 1 + i + ts[i]; if (t > best) best = t; }
|
||||||
|
free(ts);
|
||||||
|
return best;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void){
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul");
|
||||||
|
SetConsoleOutputCP(65001);
|
||||||
|
SetConsoleCP(65001);
|
||||||
|
#endif
|
||||||
|
int N; if (scanf("%d", &N) != 1) return 0;
|
||||||
|
head = (int*)malloc(((size_t)N + 1) * sizeof(int));
|
||||||
|
for (int i = 1; i <= N; ++i) head[i] = -1;
|
||||||
|
edges = (Edge*)malloc((size_t)(2 * (N - 1)) * sizeof(Edge));
|
||||||
|
ecnt = 0;
|
||||||
|
for (int i = 2; i <= N; ++i){ int p; scanf("%d", &p); add_edge(i, p); add_edge(p, i); }
|
||||||
|
int *times = (int*)malloc(((size_t)N + 1) * sizeof(int));
|
||||||
|
int best = 1 << 30;
|
||||||
|
for (int s = 1; s <= N; ++s){ int t = dfs(s, 0) + 1; times[s] = t; if (t < best) best = t; }
|
||||||
|
printf("%d\n", best);
|
||||||
|
int first = 1;
|
||||||
|
for (int s = 1; s <= N; ++s){ if (times[s] == best){ if (!first) putchar(' '); first = 0; printf("%d", s); } }
|
||||||
|
putchar('\n');
|
||||||
|
free(times); free(edges); free(head);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct { int to, next; } Edge;
|
||||||
|
|
||||||
|
int main(void){
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul");
|
||||||
|
SetConsoleOutputCP(65001);
|
||||||
|
SetConsoleCP(65001);
|
||||||
|
#endif
|
||||||
|
int n; if (scanf("%d", &n) != 1) return 0;
|
||||||
|
int m = n - 1;
|
||||||
|
int *head = (int*)malloc(((size_t)n + 1) * sizeof(int));
|
||||||
|
for (int i = 1; i <= n; ++i) head[i] = -1;
|
||||||
|
Edge *edges = (Edge*)malloc((size_t)(2 * m) * sizeof(Edge));
|
||||||
|
int ecnt = 0;
|
||||||
|
for (int i = 0; i < m; ++i){ int x, y; scanf("%d %d", &x, &y); edges[ecnt] = (Edge){ y, head[x] }; head[x] = ecnt++; edges[ecnt] = (Edge){ x, head[y] }; head[y] = ecnt++; }
|
||||||
|
int *parent = (int*)malloc(((size_t)n + 1) * sizeof(int));
|
||||||
|
int *iter = (int*)malloc(((size_t)n + 1) * sizeof(int));
|
||||||
|
char *vis = (char*)calloc((size_t)n + 1, 1);
|
||||||
|
int *order = (int*)malloc((size_t)n * sizeof(int));
|
||||||
|
int *stk = (int*)malloc((size_t)n * sizeof(int));
|
||||||
|
int top = -1, ord = 0;
|
||||||
|
parent[1] = 0; iter[1] = head[1]; vis[1] = 1; stk[++top] = 1;
|
||||||
|
while (top >= 0){
|
||||||
|
int u = stk[top];
|
||||||
|
int e = iter[u];
|
||||||
|
if (e != -1){
|
||||||
|
iter[u] = edges[e].next;
|
||||||
|
int v = edges[e].to;
|
||||||
|
if (v == parent[u]) continue;
|
||||||
|
if (!vis[v]){ vis[v] = 1; parent[v] = u; iter[v] = head[v]; stk[++top] = v; }
|
||||||
|
} else {
|
||||||
|
order[ord++] = u; --top;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int *dp0 = (int*)calloc((size_t)n + 1, sizeof(int));
|
||||||
|
int *dp1 = (int*)malloc(((size_t)n + 1) * sizeof(int));
|
||||||
|
const int NEG = -1000000000;
|
||||||
|
for (int i = 1; i <= n; ++i) dp1[i] = NEG;
|
||||||
|
for (int t = 0; t < ord; ++t){
|
||||||
|
int u = order[t];
|
||||||
|
int base = 0;
|
||||||
|
for (int e = head[u]; e != -1; e = edges[e].next){ int v = edges[e].to; if (v == parent[u]) continue; int mv = dp0[v] > dp1[v] ? dp0[v] : dp1[v]; base += mv; }
|
||||||
|
dp0[u] = base;
|
||||||
|
int best = NEG;
|
||||||
|
for (int e = head[u]; e != -1; e = edges[e].next){ int v = edges[e].to; if (v == parent[u]) continue; int mv = dp0[v] > dp1[v] ? dp0[v] : dp1[v]; int cand = base - mv + dp0[v] + 1; if (cand > best) best = cand; }
|
||||||
|
dp1[u] = best;
|
||||||
|
}
|
||||||
|
int ans = dp0[1] > dp1[1] ? dp0[1] : dp1[1];
|
||||||
|
printf("%d\n", ans);
|
||||||
|
free(dp1); free(dp0); free(stk); free(order); free(vis); free(iter); free(parent); free(edges); free(head);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int main(void){
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul");
|
||||||
|
SetConsoleOutputCP(65001);
|
||||||
|
SetConsoleCP(65001);
|
||||||
|
#endif
|
||||||
|
int n; if (scanf("%d", &n) != 1) return 0;
|
||||||
|
for (int i = 0; i < n - 1; ++i){ int x, y; scanf("%d %d", &x, &y); }
|
||||||
|
if ((n & 1) == 1) puts("YES"); else puts("NO");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct { int to, next; } Edge;
|
||||||
|
|
||||||
|
static int *head;
|
||||||
|
static Edge *edges;
|
||||||
|
static int ecnt;
|
||||||
|
|
||||||
|
static void add_edge(int u, int v){ edges[ecnt].to = v; edges[ecnt].next = head[u]; head[u] = ecnt++; }
|
||||||
|
|
||||||
|
static int cmp_desc(const void *a, const void *b){ int x = *(const int*)a, y = *(const int*)b; return y - x; }
|
||||||
|
|
||||||
|
static int dfs(int u, int p){
|
||||||
|
int cnt = 0;
|
||||||
|
for (int e = head[u]; e != -1; e = edges[e].next){ int v = edges[e].to; if (v == p) continue; ++cnt; }
|
||||||
|
if (cnt == 0) return 0;
|
||||||
|
int *ts = (int*)malloc((size_t)cnt * sizeof(int));
|
||||||
|
int k = 0;
|
||||||
|
for (int e = head[u]; e != -1; e = edges[e].next){ int v = edges[e].to; if (v == p) continue; ts[k++] = dfs(v, u); }
|
||||||
|
qsort(ts, (size_t)cnt, sizeof(int), cmp_desc);
|
||||||
|
int best = 0;
|
||||||
|
for (int i = 0; i < cnt; ++i){ int t = 1 + i + ts[i]; if (t > best) best = t; }
|
||||||
|
free(ts);
|
||||||
|
return best;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void){
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul");
|
||||||
|
SetConsoleOutputCP(65001);
|
||||||
|
SetConsoleCP(65001);
|
||||||
|
#endif
|
||||||
|
int N; if (scanf("%d", &N) != 1) return 0;
|
||||||
|
head = (int*)malloc(((size_t)N + 1) * sizeof(int));
|
||||||
|
for (int i = 1; i <= N; ++i) head[i] = -1;
|
||||||
|
edges = (Edge*)malloc((size_t)(2 * (N - 1)) * sizeof(Edge));
|
||||||
|
ecnt = 0;
|
||||||
|
for (int i = 2; i <= N; ++i){ int p; scanf("%d", &p); add_edge(i, p); add_edge(p, i); }
|
||||||
|
int *times = (int*)malloc(((size_t)N + 1) * sizeof(int));
|
||||||
|
int best = 1 << 30;
|
||||||
|
for (int s = 1; s <= N; ++s){ int t = dfs(s, 0) + 1; times[s] = t; if (t < best) best = t; }
|
||||||
|
printf("%d\n", best);
|
||||||
|
int first = 1;
|
||||||
|
for (int s = 1; s <= N; ++s){ if (times[s] == best){ if (!first) putchar(' '); first = 0; printf("%d", s); } }
|
||||||
|
putchar('\n');
|
||||||
|
free(times); free(edges); free(head);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,163 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#include <direct.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define NUM_COUNT 10000
|
||||||
|
#define INPUT_FILE "random_numbers.txt"
|
||||||
|
#define OUTPUT_FILE "sorted_numbers.txt"
|
||||||
|
|
||||||
|
/* 选择排序函数 */
|
||||||
|
void SelectSort(int arr[], int n)
|
||||||
|
{
|
||||||
|
int i, j, minIndex, temp;
|
||||||
|
for (i = 0; i < n - 1; i++)
|
||||||
|
{
|
||||||
|
// 假设当前索引i的元素为最小值
|
||||||
|
minIndex = i;
|
||||||
|
for (j = i + 1; j < n; j++)
|
||||||
|
{
|
||||||
|
// 在未排序部分查找更小元素
|
||||||
|
if (arr[j] < arr[minIndex])
|
||||||
|
{
|
||||||
|
// 更新最小值的索引
|
||||||
|
minIndex = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将找到的最小值与当前位置交换
|
||||||
|
if (minIndex != i)
|
||||||
|
{
|
||||||
|
temp = arr[i];
|
||||||
|
arr[i] = arr[minIndex];
|
||||||
|
arr[minIndex] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 生成随机数并写入文件 */
|
||||||
|
void MakeRandomNumber(const char *filename)
|
||||||
|
{
|
||||||
|
FILE *file = fopen(filename, "w");
|
||||||
|
if (file == NULL)
|
||||||
|
{
|
||||||
|
printf("错误:无法创建文件 %s\n", filename);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化随机数种子
|
||||||
|
srand((unsigned int)time(NULL));
|
||||||
|
for (int i = 0; i < NUM_COUNT; i++)
|
||||||
|
{
|
||||||
|
// 生成0-99999范围内的随机数
|
||||||
|
fprintf(file, "%d\n", rand() % 100000);
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
|
printf("已生成 %d 个随机数到文件 %s\n", NUM_COUNT, filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 从文件读取数字到数组 */
|
||||||
|
int ReadNumbers(const char *filename, int arr[])
|
||||||
|
{
|
||||||
|
FILE *file = fopen(filename, "r");
|
||||||
|
if (file == NULL)
|
||||||
|
{
|
||||||
|
printf("错误:无法打开文件 %s\n", filename);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
while (fscanf(file, "%d", &arr[count]) != EOF && count < NUM_COUNT)
|
||||||
|
{
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
|
printf("已从文件 %s 读取 %d 个数字\n", filename, count);
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 将数组中的数字写入文件 */
|
||||||
|
void writeNumbersToFile(const char *filename, int arr[], int n)
|
||||||
|
{
|
||||||
|
FILE *file = fopen(filename, "w");
|
||||||
|
if (file == NULL)
|
||||||
|
{
|
||||||
|
printf("错误:无法创建文件 %s\n", filename);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
fprintf(file, "%d\n", arr[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
|
printf("已将 %d 个排序后的数字写入文件 %s\n", n, filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// 设置控制台编码为UTF-8,防止中文乱码
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul"); // 设置控制台编码为UTF-8
|
||||||
|
SetConsoleOutputCP(65001); // 设置控制台输出编码
|
||||||
|
SetConsoleCP(65001); // 设置控制台输入编码
|
||||||
|
_mkdir("records");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int numbers[NUM_COUNT];
|
||||||
|
int actual_count;
|
||||||
|
clock_t start, end;
|
||||||
|
double time_used;
|
||||||
|
|
||||||
|
printf("=== 选择排序执行时间测量程序 ===\n");
|
||||||
|
|
||||||
|
// 生成随机数并写入文件
|
||||||
|
printf("\n1. 生成随机数...\n");
|
||||||
|
MakeRandomNumber(INPUT_FILE);
|
||||||
|
|
||||||
|
// 从文件读取数字到数组
|
||||||
|
printf("\n2. 读取文件...\n");
|
||||||
|
actual_count = ReadNumbers(INPUT_FILE, numbers);
|
||||||
|
|
||||||
|
// 显示部分未排序数据
|
||||||
|
printf("\n3. 未排序数据(前10个): ");
|
||||||
|
for (int i = 0; i < 10 && i < actual_count; i++)
|
||||||
|
{
|
||||||
|
printf("%d ", numbers[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// 测量选择排序执行时间
|
||||||
|
printf("\n4. 开始选择排序...\n");
|
||||||
|
// 记录开始时间
|
||||||
|
start = clock();
|
||||||
|
SelectSort(numbers, actual_count);
|
||||||
|
// 记录结束时间
|
||||||
|
end = clock();
|
||||||
|
|
||||||
|
// 计算并显示执行时间
|
||||||
|
time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
|
||||||
|
printf("选择排序完成!\n");
|
||||||
|
printf("执行时间: %.6f 秒\n", time_used);
|
||||||
|
|
||||||
|
// 显示部分已排序数据
|
||||||
|
printf("\n6. 已排序数据(前10个): ");
|
||||||
|
for (int i = 0; i < 10 && i < actual_count; i++)
|
||||||
|
{
|
||||||
|
printf("%d ", numbers[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// 将排序后的数据写入新文件
|
||||||
|
printf("\n7. 写入排序结果...\n");
|
||||||
|
writeNumbersToFile(OUTPUT_FILE, numbers, actual_count);
|
||||||
|
|
||||||
|
printf("\n=== 程序执行完成 ===\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef unsigned int u32;
|
||||||
|
|
||||||
|
static u32 press[25];
|
||||||
|
|
||||||
|
static void init_masks(void){
|
||||||
|
for (int r = 0; r < 5; ++r){
|
||||||
|
for (int c = 0; c < 5; ++c){
|
||||||
|
int p = r * 5 + c;
|
||||||
|
u32 m = 0U;
|
||||||
|
m |= (1U << p);
|
||||||
|
if (r > 0) m |= (1U << (p - 5));
|
||||||
|
if (r < 4) m |= (1U << (p + 5));
|
||||||
|
if (c > 0) m |= (1U << (p - 1));
|
||||||
|
if (c < 4) m |= (1U << (p + 1));
|
||||||
|
press[p] = m;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct { u32 key; unsigned char used; unsigned char val; } Entry;
|
||||||
|
|
||||||
|
static Entry *map;
|
||||||
|
static size_t cap;
|
||||||
|
|
||||||
|
static void map_init(size_t need){ size_t c = 1; while (c < need) c <<= 1; cap = c; map = (Entry*)calloc(cap, sizeof(Entry)); }
|
||||||
|
|
||||||
|
static void map_put(u32 key, unsigned char v){ size_t mask = cap - 1; size_t i = (size_t)(key & mask); for (;;){ if (!map[i].used){ map[i].used = 1; map[i].key = key; map[i].val = v; return; } if (map[i].key == key){ if (v < map[i].val) map[i].val = v; return; } i = (i + 1) & mask; } }
|
||||||
|
|
||||||
|
static int map_get(u32 key){ size_t mask = cap - 1; size_t i = (size_t)(key & mask); for (;;){ if (!map[i].used) return -1; if (map[i].key == key) return (int)map[i].val; i = (i + 1) & mask; } }
|
||||||
|
|
||||||
|
static int popcnt(u32 x){ return __builtin_popcount(x); }
|
||||||
|
|
||||||
|
int main(void){
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul");
|
||||||
|
SetConsoleOutputCP(65001);
|
||||||
|
SetConsoleCP(65001);
|
||||||
|
#endif
|
||||||
|
init_masks();
|
||||||
|
int T; if (scanf("%d", &T) != 1) return 0;
|
||||||
|
u32 ones = 0x1FFFFFFU;
|
||||||
|
int leftN = 12, rightN = 13;
|
||||||
|
int leftIdx[12]; for (int i = 0; i < 12; ++i) leftIdx[i] = i;
|
||||||
|
int rightIdx[13]; for (int i = 0; i < 13; ++i) rightIdx[i] = 12 + i;
|
||||||
|
while (T--){
|
||||||
|
u32 B = 0U;
|
||||||
|
for (int r = 0; r < 5; ++r){
|
||||||
|
for (int c = 0; c < 5; ++c){
|
||||||
|
int v; scanf("%d", &v);
|
||||||
|
if (v) B |= (1U << (r * 5 + c));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
u32 D = ones ^ B;
|
||||||
|
size_t need = 16384;
|
||||||
|
map_init(need);
|
||||||
|
for (u32 s = 0; s < (1U << leftN); ++s){ int w = popcnt(s); if (w > 6) continue; u32 m = 0U; for (int i = 0; i < leftN; ++i){ if ((s >> i) & 1U) m ^= press[leftIdx[i]]; } map_put(m, (unsigned char)w); }
|
||||||
|
int best = 7;
|
||||||
|
for (u32 t = 0; t < (1U << rightN); ++t){ int wr = popcnt(t); if (wr > 6) continue; u32 mr = 0U; for (int i = 0; i < rightN; ++i){ if ((t >> i) & 1U) mr ^= press[rightIdx[i]]; } u32 needL = D ^ mr; int wl = map_get(needL); if (wl >= 0){ int s = wl + wr; if (s < best) best = s; } }
|
||||||
|
if (best <= 6) printf("%d\n", best); else printf("-1\n");
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef unsigned int u32;
|
||||||
|
|
||||||
|
static int MOD = 99989;
|
||||||
|
|
||||||
|
static int powmod(int a, unsigned long long e){
|
||||||
|
long long r = 1 % MOD;
|
||||||
|
long long x = a % MOD;
|
||||||
|
while (e){
|
||||||
|
if (e & 1ULL) r = (r * x) % MOD;
|
||||||
|
x = (x * x) % MOD;
|
||||||
|
e >>= 1ULL;
|
||||||
|
}
|
||||||
|
return (int)r;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int no_adj(u32 m){ return (m & (m << 1)) == 0U; }
|
||||||
|
|
||||||
|
int main(void){
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul");
|
||||||
|
SetConsoleOutputCP(65001);
|
||||||
|
SetConsoleCP(65001);
|
||||||
|
#endif
|
||||||
|
int n; if (scanf("%d", &n) != 1) return 0;
|
||||||
|
int maxH = 0; { int t = n; while (t >= 3){ ++maxH; t /= 3; } ++maxH; }
|
||||||
|
int maxA = 0; { int t = n; while (t >= 2){ ++maxA; t /= 2; } }
|
||||||
|
if (maxH < 1) maxH = 1;
|
||||||
|
if (maxA < 0) maxA = 0;
|
||||||
|
u32 **states = (u32**)malloc((size_t)(maxH + 1) * sizeof(u32*));
|
||||||
|
int *state_count = (int*)malloc((size_t)(maxH + 1) * sizeof(int));
|
||||||
|
for (int h = 1; h <= maxH; ++h){
|
||||||
|
int cap = 1 << h;
|
||||||
|
u32 *tmp = (u32*)malloc((size_t)cap * sizeof(u32));
|
||||||
|
int cnt = 0;
|
||||||
|
for (int m = 0; m < cap; ++m){ if (no_adj((u32)m)) tmp[cnt++] = (u32)m; }
|
||||||
|
states[h] = (u32*)malloc((size_t)cnt * sizeof(u32));
|
||||||
|
for (int i = 0; i < cnt; ++i) states[h][i] = tmp[i];
|
||||||
|
state_count[h] = cnt;
|
||||||
|
free(tmp);
|
||||||
|
}
|
||||||
|
unsigned long long ans = 1ULL;
|
||||||
|
int r = 1;
|
||||||
|
while (r <= n){
|
||||||
|
int m = n / r;
|
||||||
|
int R = n / m;
|
||||||
|
int L = r;
|
||||||
|
long long cR = (long long)R - (R / 2) - (R / 3) + (R / 6);
|
||||||
|
long long Lm1 = (long long)(L - 1);
|
||||||
|
long long cL = Lm1 - (Lm1 / 2) - (Lm1 / 3) + (Lm1 / 6);
|
||||||
|
unsigned long long cnt_r = (unsigned long long)(cR - cL);
|
||||||
|
int Amax = 0; { int t = m; while (t >= 2){ ++Amax; t /= 2; } }
|
||||||
|
int *H = (int*)malloc(((size_t)Amax + 1) * sizeof(int));
|
||||||
|
int tm = m;
|
||||||
|
for (int a = 0; a <= Amax; ++a){
|
||||||
|
int s = tm;
|
||||||
|
int hb = 0; while (s >= 3){ ++hb; s /= 3; }
|
||||||
|
H[a] = hb + 1;
|
||||||
|
tm >>= 1;
|
||||||
|
}
|
||||||
|
int maxStates = 0;
|
||||||
|
for (int a = 0; a <= Amax; ++a){ if (state_count[H[a]] > maxStates) maxStates = state_count[H[a]]; }
|
||||||
|
int *dp_cur = (int*)calloc((size_t)maxStates, sizeof(int));
|
||||||
|
int *dp_nxt = (int*)calloc((size_t)maxStates, sizeof(int));
|
||||||
|
int h0 = H[0];
|
||||||
|
for (int i = 0; i < state_count[h0]; ++i){ dp_cur[i] = 1; }
|
||||||
|
for (int a = 0; a + 1 <= Amax; ++a){
|
||||||
|
int h1 = H[a];
|
||||||
|
int h2 = H[a+1];
|
||||||
|
int c1 = state_count[h1];
|
||||||
|
int c2 = state_count[h2];
|
||||||
|
for (int j = 0; j < c2; ++j) dp_nxt[j] = 0;
|
||||||
|
u32 *S1 = states[h1];
|
||||||
|
u32 *S2 = states[h2];
|
||||||
|
for (int i = 0; i < c1; ++i){
|
||||||
|
int v = dp_cur[i]; if (!v) continue;
|
||||||
|
u32 s1 = S1[i];
|
||||||
|
for (int j = 0; j < c2; ++j){
|
||||||
|
u32 s2 = S2[j];
|
||||||
|
if ((s1 & s2) == 0U){
|
||||||
|
int t = dp_nxt[j] + v;
|
||||||
|
if (t >= MOD) t -= MOD;
|
||||||
|
dp_nxt[j] = t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int j = 0; j < c2; ++j) dp_cur[j] = dp_nxt[j];
|
||||||
|
}
|
||||||
|
int hlast = H[Amax];
|
||||||
|
int cnt_last = state_count[hlast];
|
||||||
|
int f_m = 0; for (int i = 0; i < cnt_last; ++i){ f_m += dp_cur[i]; if (f_m >= MOD) f_m -= MOD; }
|
||||||
|
int contrib = powmod(f_m, cnt_r);
|
||||||
|
ans = (ans * (unsigned long long)contrib) % MOD;
|
||||||
|
free(dp_nxt); free(dp_cur); free(H);
|
||||||
|
r = R + 1;
|
||||||
|
}
|
||||||
|
printf("%u\n", (unsigned int)ans);
|
||||||
|
for (int h = 1; h <= maxH; ++h) free(states[h]);
|
||||||
|
free(state_count); free(states);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static int MOD = 99989;
|
||||||
|
|
||||||
|
static int popcnt(int x){
|
||||||
|
int c = 0;
|
||||||
|
while (x){ x &= (x - 1); ++c; }
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void){
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul");
|
||||||
|
SetConsoleOutputCP(65001);
|
||||||
|
SetConsoleCP(65001);
|
||||||
|
#endif
|
||||||
|
int N, M;
|
||||||
|
if (scanf("%d %d", &N, &M) != 2) return 0;
|
||||||
|
int Smax = 1 << N;
|
||||||
|
int *okrow = (int*)malloc((size_t)Smax * sizeof(int));
|
||||||
|
int *bits = (int*)malloc((size_t)Smax * sizeof(int));
|
||||||
|
int *list = (int*)malloc((size_t)Smax * sizeof(int));
|
||||||
|
int cntS = 0;
|
||||||
|
for (int m = 0; m < Smax; ++m){
|
||||||
|
if ((m & (m << 1)) == 0){ okrow[m] = 1; bits[m] = popcnt(m); list[cntS++] = m; } else { okrow[m] = 0; bits[m] = popcnt(m); }
|
||||||
|
}
|
||||||
|
char **ok = (char**)malloc((size_t)cntS * sizeof(char*));
|
||||||
|
for (int i = 0; i < cntS; ++i) ok[i] = (char*)malloc((size_t)cntS * sizeof(char));
|
||||||
|
for (int i = 0; i < cntS; ++i){
|
||||||
|
int a = list[i];
|
||||||
|
for (int j = 0; j < cntS; ++j){
|
||||||
|
int b = list[j];
|
||||||
|
int bad = (a & b) | (a & (b << 1)) | (a & (b >> 1));
|
||||||
|
ok[i][j] = (bad == 0) ? 1 : 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int **dp_prev = (int**)malloc((size_t)cntS * sizeof(int*));
|
||||||
|
int **dp_cur = (int**)malloc((size_t)cntS * sizeof(int*));
|
||||||
|
for (int i = 0; i < cntS; ++i){ dp_prev[i] = (int*)calloc((size_t)M + 1, sizeof(int)); dp_cur[i] = (int*)calloc((size_t)M + 1, sizeof(int)); }
|
||||||
|
for (int i = 0; i < cntS; ++i){ int b = bits[list[i]]; if (b <= M) dp_prev[i][b] = 1; }
|
||||||
|
for (int r = 1; r < N; ++r){
|
||||||
|
for (int j = 0; j < cntS; ++j){ for (int k = 0; k <= M; ++k) dp_cur[j][k] = 0; }
|
||||||
|
for (int i = 0; i < cntS; ++i){
|
||||||
|
for (int j = 0; j < cntS; ++j){ if (!ok[i][j]) continue; int add = bits[list[j]]; for (int k = 0; k + add <= M; ++k){ int v = dp_prev[i][k]; if (!v) continue; int t = dp_cur[j][k + add] + v; if (t >= MOD) t -= MOD; dp_cur[j][k + add] = t; } }
|
||||||
|
}
|
||||||
|
for (int j = 0; j < cntS; ++j){ for (int k = 0; k <= M; ++k) dp_prev[j][k] = dp_cur[j][k]; }
|
||||||
|
}
|
||||||
|
int ans = 0;
|
||||||
|
for (int i = 0; i < cntS; ++i){ int v = dp_prev[i][M]; ans += v; if (ans >= MOD) ans -= MOD; }
|
||||||
|
printf("%d\n", ans);
|
||||||
|
for (int i = 0; i < cntS; ++i){ free(dp_prev[i]); free(dp_cur[i]); free(ok[i]); }
|
||||||
|
free(dp_prev); free(dp_cur); free(ok); free(list); free(bits); free(okrow);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef unsigned long long ull;
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul");
|
||||||
|
SetConsoleOutputCP(65001);
|
||||||
|
SetConsoleCP(65001);
|
||||||
|
#endif
|
||||||
|
unsigned long long n;
|
||||||
|
if (scanf("%llu", &n) != 1)
|
||||||
|
return 0;
|
||||||
|
unsigned long long ans = 0ULL;
|
||||||
|
for (unsigned long long factor = 1ULL; factor <= n; factor *= 10ULL)
|
||||||
|
{
|
||||||
|
unsigned long long high = n / (factor * 10ULL);
|
||||||
|
unsigned long long low = n % factor;
|
||||||
|
unsigned long long cur = (n / factor) % 10ULL;
|
||||||
|
unsigned long long term = factor * (high * 45ULL + (cur * (cur - 1ULL)) / 2ULL) + cur * (low + 1ULL);
|
||||||
|
ans += term;
|
||||||
|
}
|
||||||
|
printf("%llu\n", ans);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef unsigned long long ull;
|
||||||
|
|
||||||
|
static const int MOD = 1000000007;
|
||||||
|
|
||||||
|
typedef struct { int c; int s; int q; } Node;
|
||||||
|
|
||||||
|
static ull digits[32];
|
||||||
|
|
||||||
|
static ull solve(ull n){
|
||||||
|
if (n == (ull)-1) return 0ULL;
|
||||||
|
int L = 0;
|
||||||
|
if (n == 0ULL){ digits[L++] = 0ULL; }
|
||||||
|
else { while (n){ digits[L++] = n % 10ULL; n /= 10ULL; } for (int i = 0, j = L - 1; i < j; ++i, --j){ ull t = digits[i]; digits[i] = digits[j]; digits[j] = t; } }
|
||||||
|
static Node dp[2][2][2][6][6];
|
||||||
|
static Node nx[2][2][2][6][6];
|
||||||
|
for (int t = 0; t < 2; ++t) for (int a = 0; a < 2; ++a) for (int b = 0; b < 2; ++b) for (int m = 0; m < 6; ++m) for (int r = 0; r < 6; ++r){ dp[t][a][b][m][r].c = 0; dp[t][a][b][m][r].s = 0; dp[t][a][b][m][r].q = 0; }
|
||||||
|
dp[1][1][0][0][0].c = 1;
|
||||||
|
dp[1][1][0][0][0].s = 0;
|
||||||
|
dp[1][1][0][0][0].q = 0;
|
||||||
|
for (int pos = 0; pos < L; ++pos){
|
||||||
|
int lim = (int)digits[pos];
|
||||||
|
for (int t = 0; t < 2; ++t) for (int a = 0; a < 2; ++a) for (int b = 0; b < 2; ++b) for (int m = 0; m < 6; ++m) for (int r = 0; r < 6; ++r){ nx[t][a][b][m][r].c = 0; nx[t][a][b][m][r].s = 0; nx[t][a][b][m][r].q = 0; }
|
||||||
|
for (int t = 0; t < 2; ++t){
|
||||||
|
int up = t ? lim : 9;
|
||||||
|
for (int a = 0; a < 2; ++a){
|
||||||
|
for (int b = 0; b < 2; ++b){
|
||||||
|
for (int m = 0; m < 6; ++m){
|
||||||
|
for (int r = 0; r < 6; ++r){
|
||||||
|
Node cur = dp[t][a][b][m][r];
|
||||||
|
if (!cur.c && !cur.s && !cur.q) continue;
|
||||||
|
for (int d = 0; d <= up; ++d){
|
||||||
|
int nt = (t && (d == up)) ? 1 : 0;
|
||||||
|
int na = (a && d == 0) ? 1 : 0;
|
||||||
|
int nb = b || (d == 6);
|
||||||
|
int nm = (m * 10 + d) % 6;
|
||||||
|
int nr = (r + d) % 6;
|
||||||
|
Node *dst = &nx[nt][na][nb][nm][nr];
|
||||||
|
int cadd = cur.c;
|
||||||
|
int sadd = (int)((10LL * cur.s + (long long)cadd * d) % MOD);
|
||||||
|
int qadd = (int)((100LL * cur.q + (20LL * d % MOD) * cur.s + (long long)cadd * (long long)d * (long long)d) % MOD);
|
||||||
|
dst->c += cadd; if (dst->c >= MOD) dst->c -= MOD;
|
||||||
|
dst->s += sadd; if (dst->s >= MOD) dst->s -= MOD;
|
||||||
|
dst->q += qadd; if (dst->q >= MOD) dst->q -= MOD;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int t = 0; t < 2; ++t) for (int a = 0; a < 2; ++a) for (int b = 0; b < 2; ++b) for (int m = 0; m < 6; ++m) for (int r = 0; r < 6; ++r){ dp[t][a][b][m][r] = nx[t][a][b][m][r]; }
|
||||||
|
}
|
||||||
|
long long ans = 0;
|
||||||
|
for (int t = 0; t < 2; ++t){
|
||||||
|
for (int a = 0; a < 2; ++a){
|
||||||
|
for (int b = 0; b < 2; ++b){
|
||||||
|
if (b) continue;
|
||||||
|
for (int m = 0; m < 6; ++m){
|
||||||
|
if (m == 0) continue;
|
||||||
|
for (int r = 0; r < 6; ++r){
|
||||||
|
if (r == 0) continue;
|
||||||
|
ans += dp[t][a][b][m][r].q;
|
||||||
|
if (ans >= MOD) ans -= MOD;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (ull)ans;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void){
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul");
|
||||||
|
SetConsoleOutputCP(65001);
|
||||||
|
SetConsoleCP(65001);
|
||||||
|
#endif
|
||||||
|
int T; if (scanf("%d", &T) != 1) return 0;
|
||||||
|
while (T--){
|
||||||
|
unsigned long long a, b; scanf("%llu %llu", &a, &b);
|
||||||
|
ull x = solve(b);
|
||||||
|
ull y = solve(a - 1ULL);
|
||||||
|
ull ans = (x >= y) ? (x - y) : (x + MOD - y);
|
||||||
|
printf("%llu\n", ans);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
if (scanf("%d", &n) != 1)
|
||||||
|
return 0;
|
||||||
|
unsigned int N = (1u << (n + 1)) - 1u;
|
||||||
|
long long *w = (long long *)malloc(((size_t)N + 2) * sizeof(long long));
|
||||||
|
long long *mx = (long long *)malloc(((size_t)N + 2) * sizeof(long long));
|
||||||
|
for (unsigned int i = 1; i <= N; ++i)
|
||||||
|
scanf("%lld", &w[i]);
|
||||||
|
unsigned int first_leaf = (N >> 1) + 1u;
|
||||||
|
for (unsigned int i = first_leaf; i <= N; ++i)
|
||||||
|
mx[i] = w[i];
|
||||||
|
long long add = 0;
|
||||||
|
for (int i = (int)(first_leaf - 1u); i >= 1; --i)
|
||||||
|
{
|
||||||
|
long long L = mx[i << 1];
|
||||||
|
long long R = mx[(i << 1) | 1];
|
||||||
|
add += (L >= R) ? (L - R) : (R - L);
|
||||||
|
mx[i] = w[i] + ((L >= R) ? L : R);
|
||||||
|
}
|
||||||
|
printf("%lld\n", add);
|
||||||
|
free(w);
|
||||||
|
free(mx);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static const int MOD = 1000000007;
|
||||||
|
|
||||||
|
int main(void){
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul");
|
||||||
|
SetConsoleOutputCP(65001);
|
||||||
|
SetConsoleCP(65001);
|
||||||
|
#endif
|
||||||
|
int f; if (scanf("%d", &f) != 1) return 0;
|
||||||
|
int *ns = (int*)malloc((size_t)f * sizeof(int));
|
||||||
|
int maxN = 0;
|
||||||
|
for (int i = 0; i < f; ++i){ scanf("%d", &ns[i]); if (ns[i] > maxN) maxN = ns[i]; }
|
||||||
|
if (maxN < 0) maxN = 0;
|
||||||
|
int *dp1 = (int*)calloc((size_t)maxN + 2, sizeof(int));
|
||||||
|
int *dp2 = (int*)calloc((size_t)maxN + 2, sizeof(int));
|
||||||
|
if (maxN >= 1){ dp1[1] = 3; dp2[1] = 0; }
|
||||||
|
for (int n = 2; n <= maxN; ++n){
|
||||||
|
long long a = (2LL * dp1[n-1] + 2LL * dp2[n-1]) % MOD;
|
||||||
|
long long b = (1LL * dp1[n-1]) % MOD;
|
||||||
|
dp1[n] = (int)a;
|
||||||
|
dp2[n] = (int)b;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < f; ++i){
|
||||||
|
int n = ns[i];
|
||||||
|
if (n == 0){ printf("1\n"); continue; }
|
||||||
|
int ans = dp1[n]; ans += dp2[n]; if (ans >= MOD) ans -= MOD;
|
||||||
|
printf("%d\n", ans);
|
||||||
|
}
|
||||||
|
free(dp2); free(dp1); free(ns);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,223 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static const int P = 61;
|
||||||
|
|
||||||
|
static int C[61][61];
|
||||||
|
static int pow2_small[400];
|
||||||
|
|
||||||
|
static int pow2_mod(unsigned long long e)
|
||||||
|
{
|
||||||
|
unsigned long long b = 2ULL;
|
||||||
|
unsigned long long r = 1ULL;
|
||||||
|
while (e)
|
||||||
|
{
|
||||||
|
if (e & 1ULL)
|
||||||
|
r = (r * b) % P;
|
||||||
|
b = (b * b) % P;
|
||||||
|
e >>= 1ULL;
|
||||||
|
}
|
||||||
|
return (int)r;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int sum_prefix_mod61(unsigned long long k, long long t)
|
||||||
|
{
|
||||||
|
if (t < 0)
|
||||||
|
return 0;
|
||||||
|
if ((unsigned long long)t >= k)
|
||||||
|
return pow2_mod(k);
|
||||||
|
int Kdig[16], Tdig[16];
|
||||||
|
int L = 0;
|
||||||
|
unsigned long long kk = k;
|
||||||
|
while (kk)
|
||||||
|
{
|
||||||
|
Kdig[L++] = (int)(kk % P);
|
||||||
|
kk /= P;
|
||||||
|
}
|
||||||
|
if (L == 0)
|
||||||
|
{
|
||||||
|
Kdig[L++] = 0;
|
||||||
|
}
|
||||||
|
unsigned long long tt = (unsigned long long)t;
|
||||||
|
int Lt = 0;
|
||||||
|
while (tt)
|
||||||
|
{
|
||||||
|
Tdig[Lt++] = (int)(tt % P);
|
||||||
|
tt /= P;
|
||||||
|
}
|
||||||
|
while (Lt < L)
|
||||||
|
{
|
||||||
|
Tdig[Lt++] = 0;
|
||||||
|
}
|
||||||
|
int Krev[16], Trev[16];
|
||||||
|
for (int i = 0; i < L; ++i)
|
||||||
|
{
|
||||||
|
Krev[i] = Kdig[L - 1 - i];
|
||||||
|
Trev[i] = Tdig[L - 1 - i];
|
||||||
|
}
|
||||||
|
int suffix_sum_digits[17];
|
||||||
|
suffix_sum_digits[L] = 0;
|
||||||
|
for (int i = L - 1; i >= 0; --i)
|
||||||
|
suffix_sum_digits[i] = suffix_sum_digits[i + 1] + Krev[i];
|
||||||
|
int res = 0;
|
||||||
|
for (int i = 0; i < L; ++i)
|
||||||
|
{
|
||||||
|
int limit = Trev[i];
|
||||||
|
int nd = Krev[i];
|
||||||
|
int up = (limit < nd) ? limit : nd;
|
||||||
|
for (int w = 0; w < up; ++w)
|
||||||
|
{
|
||||||
|
int cv = C[nd][w];
|
||||||
|
if (!cv)
|
||||||
|
continue;
|
||||||
|
int tail = suffix_sum_digits[i + 1];
|
||||||
|
int add = (int)((cv * (long long)pow2_small[tail]) % P);
|
||||||
|
res += add;
|
||||||
|
if (res >= P)
|
||||||
|
res -= P;
|
||||||
|
}
|
||||||
|
if (up >= 0)
|
||||||
|
{
|
||||||
|
int w = up;
|
||||||
|
int cv = C[nd][w];
|
||||||
|
if (!cv)
|
||||||
|
{
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
if (w < limit)
|
||||||
|
{
|
||||||
|
int tail = suffix_sum_digits[i + 1];
|
||||||
|
int add = (int)((cv * (long long)pow2_small[tail]) % P);
|
||||||
|
res += add;
|
||||||
|
if (res >= P)
|
||||||
|
res -= P;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res += 1;
|
||||||
|
if (res >= P)
|
||||||
|
res -= P;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline char map_char(int x)
|
||||||
|
{
|
||||||
|
if (x <= 9)
|
||||||
|
return (char)('0' + x);
|
||||||
|
if (x <= 35)
|
||||||
|
return (char)('A' + (x - 10));
|
||||||
|
return (char)('a' + (x - 36));
|
||||||
|
}
|
||||||
|
|
||||||
|
static int read_int_fast()
|
||||||
|
{
|
||||||
|
int c = getchar();
|
||||||
|
while (c != EOF && (c <= ' '))
|
||||||
|
c = getchar();
|
||||||
|
if (c == EOF)
|
||||||
|
return -1;
|
||||||
|
int x = 0;
|
||||||
|
while (c > ' ')
|
||||||
|
{
|
||||||
|
x = x * 10 + (c - '0');
|
||||||
|
c = getchar();
|
||||||
|
}
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul");
|
||||||
|
SetConsoleOutputCP(65001);
|
||||||
|
SetConsoleCP(65001);
|
||||||
|
#endif
|
||||||
|
for (int n = 0; n <= 60; ++n)
|
||||||
|
{
|
||||||
|
C[n][0] = 1 % P;
|
||||||
|
for (int r = 1; r <= n; ++r)
|
||||||
|
{
|
||||||
|
int v = (C[n - 1][r - 1] + C[n - 1][r]) % P;
|
||||||
|
C[n][r] = v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pow2_small[0] = 1 % P;
|
||||||
|
for (int i = 1; i < 400; ++i)
|
||||||
|
{
|
||||||
|
pow2_small[i] = (pow2_small[i - 1] * 2) % P;
|
||||||
|
}
|
||||||
|
int Q = read_int_fast();
|
||||||
|
unsigned char *A = (unsigned char *)malloc((size_t)Q + 5);
|
||||||
|
int head = 0, tail = 0;
|
||||||
|
long long cnt0 = 0, cnt1 = 0, cntq = 0;
|
||||||
|
int last = 0;
|
||||||
|
int outputs = 0;
|
||||||
|
for (int qi = 0; qi < Q; ++qi)
|
||||||
|
{
|
||||||
|
int x = read_int_fast();
|
||||||
|
x ^= (last & 3);
|
||||||
|
if (x == 0)
|
||||||
|
{
|
||||||
|
A[tail++] = 0;
|
||||||
|
++cnt0;
|
||||||
|
}
|
||||||
|
else if (x == 1)
|
||||||
|
{
|
||||||
|
A[tail++] = 1;
|
||||||
|
++cnt1;
|
||||||
|
}
|
||||||
|
else if (x == 2)
|
||||||
|
{
|
||||||
|
A[tail++] = 2;
|
||||||
|
++cntq;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
unsigned char v = A[head++];
|
||||||
|
if (v == 0)
|
||||||
|
--cnt0;
|
||||||
|
else if (v == 1)
|
||||||
|
--cnt1;
|
||||||
|
else
|
||||||
|
--cntq;
|
||||||
|
}
|
||||||
|
int L = tail - head;
|
||||||
|
if ((L & 1) == 1)
|
||||||
|
{
|
||||||
|
long long ones = cnt1;
|
||||||
|
long long k = cntq;
|
||||||
|
long long T = (L + 1) / 2;
|
||||||
|
long long need = T - ones;
|
||||||
|
int ans;
|
||||||
|
if (need <= 0)
|
||||||
|
{
|
||||||
|
ans = pow2_mod((unsigned long long)k);
|
||||||
|
}
|
||||||
|
else if (need > k)
|
||||||
|
{
|
||||||
|
ans = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
unsigned long long K = (unsigned long long)k;
|
||||||
|
unsigned long long sum0 = sum_prefix_mod61(K, (long long)(need - 1));
|
||||||
|
int twoK = pow2_mod(K);
|
||||||
|
ans = twoK - (int)sum0;
|
||||||
|
if (ans < 0)
|
||||||
|
ans += P;
|
||||||
|
}
|
||||||
|
char ch = map_char(ans);
|
||||||
|
putchar(ch);
|
||||||
|
putchar('\n');
|
||||||
|
last = ans;
|
||||||
|
++outputs;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(A);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,240 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
int x, y;
|
||||||
|
} Pt;
|
||||||
|
|
||||||
|
static int manh(Pt a, Pt b)
|
||||||
|
{
|
||||||
|
int dx = a.x - b.x;
|
||||||
|
if (dx < 0)
|
||||||
|
dx = -dx;
|
||||||
|
int dy = a.y - b.y;
|
||||||
|
if (dy < 0)
|
||||||
|
dy = -dy;
|
||||||
|
return dx + dy;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gen_segment(Pt a, Pt b)
|
||||||
|
{
|
||||||
|
int sx = (b.x > a.x) ? 1 : (b.x < a.x ? -1 : 0);
|
||||||
|
int sy = (b.y > a.y) ? 1 : (b.y < a.y ? -1 : 0);
|
||||||
|
while (a.x != b.x)
|
||||||
|
{
|
||||||
|
a.x += sx;
|
||||||
|
printf(" (%d,%d)", a.x, a.y);
|
||||||
|
}
|
||||||
|
while (a.y != b.y)
|
||||||
|
{
|
||||||
|
a.y += sy;
|
||||||
|
printf(" (%d,%d)", a.x, a.y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int perm_next(int *p, int n)
|
||||||
|
{
|
||||||
|
int i = n - 2;
|
||||||
|
while (i >= 0 && p[i] > p[i + 1])
|
||||||
|
--i;
|
||||||
|
if (i < 0)
|
||||||
|
return 0;
|
||||||
|
int j = n - 1;
|
||||||
|
while (p[j] < p[i])
|
||||||
|
--j;
|
||||||
|
int t = p[i];
|
||||||
|
p[i] = p[j];
|
||||||
|
p[j] = t;
|
||||||
|
int l = i + 1, r = n - 1;
|
||||||
|
while (l < r)
|
||||||
|
{
|
||||||
|
t = p[l];
|
||||||
|
p[l] = p[r];
|
||||||
|
p[r] = t;
|
||||||
|
++l;
|
||||||
|
--r;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void best_route(Pt S, Pt *grp, int k, int *best_len, int *best_order)
|
||||||
|
{
|
||||||
|
int idx[4];
|
||||||
|
for (int i = 0; i < k; ++i)
|
||||||
|
idx[i] = i;
|
||||||
|
int first = 1;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
int sum = 0;
|
||||||
|
Pt cur = S;
|
||||||
|
for (int i = 0; i < k; ++i)
|
||||||
|
{
|
||||||
|
sum += manh(cur, grp[idx[i]]);
|
||||||
|
cur = grp[idx[i]];
|
||||||
|
}
|
||||||
|
if (first || sum < *best_len)
|
||||||
|
{
|
||||||
|
*best_len = sum;
|
||||||
|
for (int i = 0; i < k; ++i)
|
||||||
|
best_order[i] = idx[i];
|
||||||
|
first = 0;
|
||||||
|
}
|
||||||
|
} while (perm_next(idx, k));
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
Pt *pts;
|
||||||
|
int cnt;
|
||||||
|
} Bucket;
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul");
|
||||||
|
SetConsoleOutputCP(65001);
|
||||||
|
SetConsoleCP(65001);
|
||||||
|
#endif
|
||||||
|
int N;
|
||||||
|
if (scanf("%d", &N) != 1)
|
||||||
|
return 0;
|
||||||
|
Pt *emps = (Pt *)malloc((size_t)N * sizeof(Pt));
|
||||||
|
for (int i = 0; i < N; ++i)
|
||||||
|
scanf("%d %d", &emps[i].x, &emps[i].y);
|
||||||
|
Pt S;
|
||||||
|
scanf("%d %d", &S.x, &S.y);
|
||||||
|
Pt *eff = (Pt *)malloc((size_t)N * sizeof(Pt));
|
||||||
|
int M = 0;
|
||||||
|
for (int i = 0; i < N; ++i)
|
||||||
|
{
|
||||||
|
if (emps[i].x == S.x && emps[i].y == S.y)
|
||||||
|
continue;
|
||||||
|
eff[M++] = emps[i];
|
||||||
|
}
|
||||||
|
if (M == 0)
|
||||||
|
{
|
||||||
|
printf("0\n0\n");
|
||||||
|
free(eff);
|
||||||
|
free(emps);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
Bucket bkt[8];
|
||||||
|
for (int i = 0; i < 8; ++i)
|
||||||
|
{
|
||||||
|
bkt[i].pts = (Pt *)malloc((size_t)M * sizeof(Pt));
|
||||||
|
bkt[i].cnt = 0;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < M; ++i)
|
||||||
|
{
|
||||||
|
int dx = eff[i].x - S.x, dy = eff[i].y - S.y;
|
||||||
|
int id = 0;
|
||||||
|
if (dx > 0 && dy > 0)
|
||||||
|
id = 0;
|
||||||
|
else if (dx < 0 && dy > 0)
|
||||||
|
id = 1;
|
||||||
|
else if (dx > 0 && dy < 0)
|
||||||
|
id = 2;
|
||||||
|
else if (dx < 0 && dy < 0)
|
||||||
|
id = 3;
|
||||||
|
else if (dx == 0 && dy > 0)
|
||||||
|
id = 4;
|
||||||
|
else if (dx == 0 && dy < 0)
|
||||||
|
id = 5;
|
||||||
|
else if (dy == 0 && dx > 0)
|
||||||
|
id = 6;
|
||||||
|
else
|
||||||
|
id = 7;
|
||||||
|
bkt[id].pts[bkt[id].cnt++] = eff[i];
|
||||||
|
}
|
||||||
|
int groups = 0;
|
||||||
|
int cap = (M + 3) / 4;
|
||||||
|
Pt **grp_pts = (Pt **)malloc((size_t)cap * sizeof(Pt *));
|
||||||
|
int *grp_sizes = (int *)malloc((size_t)cap * sizeof(int));
|
||||||
|
for (int id = 0; id < 8; ++id)
|
||||||
|
{
|
||||||
|
int c = bkt[id].cnt;
|
||||||
|
if (c == 0)
|
||||||
|
continue;
|
||||||
|
int *ord = (int *)malloc((size_t)c * sizeof(int));
|
||||||
|
for (int i = 0; i < c; ++i)
|
||||||
|
ord[i] = i;
|
||||||
|
for (int i = 0; i < c; ++i)
|
||||||
|
{
|
||||||
|
for (int j = i + 1; j < c; ++j)
|
||||||
|
{
|
||||||
|
if (manh(S, bkt[id].pts[ord[i]]) > manh(S, bkt[id].pts[ord[j]]))
|
||||||
|
{
|
||||||
|
int t = ord[i];
|
||||||
|
ord[i] = ord[j];
|
||||||
|
ord[j] = t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int i = 0;
|
||||||
|
while (i < c)
|
||||||
|
{
|
||||||
|
int k = c - i;
|
||||||
|
if (k > 4)
|
||||||
|
k = 4;
|
||||||
|
Pt *arr = (Pt *)malloc((size_t)k * sizeof(Pt));
|
||||||
|
for (int t = 0; t < k; ++t)
|
||||||
|
arr[t] = bkt[id].pts[ord[i + t]];
|
||||||
|
grp_pts[groups] = arr;
|
||||||
|
grp_sizes[groups] = k;
|
||||||
|
++groups;
|
||||||
|
i += k;
|
||||||
|
}
|
||||||
|
free(ord);
|
||||||
|
}
|
||||||
|
int *lens = (int *)malloc((size_t)groups * sizeof(int));
|
||||||
|
int **order = (int **)malloc((size_t)groups * sizeof(int *));
|
||||||
|
for (int g = 0; g < groups; ++g)
|
||||||
|
{
|
||||||
|
order[g] = (int *)malloc(4 * sizeof(int));
|
||||||
|
int bl = 0;
|
||||||
|
best_route(S, grp_pts[g], grp_sizes[g], &bl, order[g]);
|
||||||
|
lens[g] = bl;
|
||||||
|
}
|
||||||
|
int total = 0;
|
||||||
|
for (int g = 0; g < groups; ++g)
|
||||||
|
total += lens[g];
|
||||||
|
printf("%d\n", total);
|
||||||
|
printf("%d\n", groups);
|
||||||
|
for (int g = 0; g < groups; ++g)
|
||||||
|
{
|
||||||
|
printf("%d", lens[g]);
|
||||||
|
for (int i = 0; i < grp_sizes[g]; ++i)
|
||||||
|
{
|
||||||
|
Pt p = grp_pts[g][i];
|
||||||
|
printf("(%d,%d)%s", p.x, p.y, (i + 1 == grp_sizes[g]) ? " " : " ");
|
||||||
|
}
|
||||||
|
printf(": ");
|
||||||
|
Pt cur = S;
|
||||||
|
printf("(%d,%d)", cur.x, cur.y);
|
||||||
|
for (int i = 0; i < grp_sizes[g]; ++i)
|
||||||
|
{
|
||||||
|
Pt nxt = grp_pts[g][order[g][i]];
|
||||||
|
gen_segment(cur, nxt);
|
||||||
|
cur = nxt;
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
for (int g = 0; g < groups; ++g)
|
||||||
|
{
|
||||||
|
free(order[g]);
|
||||||
|
free(grp_pts[g]);
|
||||||
|
}
|
||||||
|
free(order);
|
||||||
|
free(grp_pts);
|
||||||
|
free(grp_sizes);
|
||||||
|
free(lens);
|
||||||
|
for (int i = 0; i < 8; ++i)
|
||||||
|
free(bkt[i].pts);
|
||||||
|
free(eff);
|
||||||
|
free(emps);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct { char *name; char *path; } Node;
|
||||||
|
|
||||||
|
static char* dupstr(const char* s){ size_t n = strlen(s); char* p = (char*)malloc(n + 1); if (!p) exit(1); memcpy(p, s, n + 1); return p; }
|
||||||
|
|
||||||
|
int main(void){
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul");
|
||||||
|
SetConsoleOutputCP(65001);
|
||||||
|
SetConsoleCP(65001);
|
||||||
|
#endif
|
||||||
|
Node *nodes = (Node*)malloc(2000 * sizeof(Node));
|
||||||
|
int cap = 2000, cnt = 0;
|
||||||
|
int last_idx[2048];
|
||||||
|
for (int i = 0; i < 2048; ++i) last_idx[i] = -1;
|
||||||
|
char buf[4096];
|
||||||
|
while (fgets(buf, sizeof(buf), stdin)){
|
||||||
|
size_t len = strlen(buf);
|
||||||
|
while (len && (buf[len-1] == '\n' || buf[len-1] == '\r')) buf[--len] = '\0';
|
||||||
|
while (len && buf[len-1] == ' ') buf[--len] = '\0';
|
||||||
|
if (len == 0) continue;
|
||||||
|
if (strcmp(buf, "#") == 0) break;
|
||||||
|
size_t i = 0; while (buf[i] == ' ') ++i;
|
||||||
|
int level = (int)i;
|
||||||
|
const char *name = buf + i;
|
||||||
|
if (*name == '\0') continue;
|
||||||
|
size_t nlen = strlen(name);
|
||||||
|
char *path;
|
||||||
|
if (level == 0){
|
||||||
|
path = (char*)malloc(1 + nlen + 1);
|
||||||
|
path[0] = '/';
|
||||||
|
memcpy(path + 1, name, nlen);
|
||||||
|
path[1 + nlen] = '\0';
|
||||||
|
} else {
|
||||||
|
const char *parent_path = nodes[last_idx[level - 1]].path;
|
||||||
|
size_t plen = strlen(parent_path);
|
||||||
|
path = (char*)malloc(plen + 1 + nlen + 1);
|
||||||
|
memcpy(path, parent_path, plen);
|
||||||
|
path[plen] = '/';
|
||||||
|
memcpy(path + plen + 1, name, nlen);
|
||||||
|
path[plen + 1 + nlen] = '\0';
|
||||||
|
}
|
||||||
|
if (cnt == cap){ cap <<= 1; nodes = (Node*)realloc(nodes, (size_t)cap * sizeof(Node)); }
|
||||||
|
nodes[cnt].name = dupstr(name);
|
||||||
|
nodes[cnt].path = path;
|
||||||
|
last_idx[level] = cnt;
|
||||||
|
++cnt;
|
||||||
|
}
|
||||||
|
if (!fgets(buf, sizeof(buf), stdin)) return 0;
|
||||||
|
size_t len = strlen(buf);
|
||||||
|
while (len && (buf[len-1] == '\n' || buf[len-1] == '\r')) buf[--len] = '\0';
|
||||||
|
while (len && buf[len-1] == ' ') buf[--len] = '\0';
|
||||||
|
size_t j = 0; while (buf[j] == ' ') ++j; const char *q = buf + j;
|
||||||
|
int found = 0;
|
||||||
|
for (int i = 0; i < cnt; ++i){ if (strcmp(nodes[i].name, q) == 0){ puts(nodes[i].path); found = 1; } }
|
||||||
|
if (!found) puts("NULL");
|
||||||
|
for (int i = 0; i < cnt; ++i){ free(nodes[i].name); free(nodes[i].path); }
|
||||||
|
free(nodes);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,388 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#include <direct.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// 定义双向链表节点结构体
|
||||||
|
struct Node
|
||||||
|
{
|
||||||
|
int data;
|
||||||
|
struct Node *prev;
|
||||||
|
struct Node *next;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 定义双向链表结构体
|
||||||
|
struct DoubleList
|
||||||
|
{
|
||||||
|
struct Node *head;
|
||||||
|
struct Node *tail;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 初始化双向链表
|
||||||
|
void initDoubleList(struct DoubleList *list)
|
||||||
|
{
|
||||||
|
list->head = NULL;
|
||||||
|
list->tail = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取双向链表长度
|
||||||
|
int getListLength(struct DoubleList *list)
|
||||||
|
{
|
||||||
|
int length = 0;
|
||||||
|
struct Node *current = list->head;
|
||||||
|
while (current != NULL)
|
||||||
|
{
|
||||||
|
length++;
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 向双向链表头部添加节点
|
||||||
|
void addNodeToHead(struct DoubleList *list, int data)
|
||||||
|
{
|
||||||
|
// 分配新节点内存
|
||||||
|
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
|
||||||
|
newNode->data = data;
|
||||||
|
newNode->prev = NULL;
|
||||||
|
newNode->next = list->head;
|
||||||
|
|
||||||
|
// 如果链表为空,则新节点同时是头节点和尾节点
|
||||||
|
if (list->head == NULL)
|
||||||
|
{
|
||||||
|
list->tail = newNode;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
list->head->prev = newNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新头节点为新节点
|
||||||
|
list->head = newNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 向双向链表尾部添加节点
|
||||||
|
void addNodeToTail(struct DoubleList *list, int data)
|
||||||
|
{
|
||||||
|
// 分配新节点内存
|
||||||
|
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
|
||||||
|
newNode->data = data;
|
||||||
|
newNode->prev = list->tail;
|
||||||
|
newNode->next = NULL;
|
||||||
|
|
||||||
|
// 如果链表为空,则新节点同时是头节点和尾节点
|
||||||
|
if (list->tail == NULL)
|
||||||
|
{
|
||||||
|
list->head = newNode;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
list->tail->next = newNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新尾节点为新节点
|
||||||
|
list->tail = newNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 在指定位置插入节点
|
||||||
|
void insertNode(struct DoubleList *list, int data, int position)
|
||||||
|
{
|
||||||
|
// 分配新节点内存
|
||||||
|
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
|
||||||
|
newNode->data = data;
|
||||||
|
|
||||||
|
// 如果插入位置为0,则在头部插入
|
||||||
|
if (position == 0)
|
||||||
|
{
|
||||||
|
addNodeToHead(list, data);
|
||||||
|
}
|
||||||
|
// 如果插入位置大于等于链表长度,则在尾部插入
|
||||||
|
else if (position >= getListLength(list))
|
||||||
|
{
|
||||||
|
addNodeToTail(list, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 否则,在指定位置插入
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// 首先遍历到指定位置
|
||||||
|
struct Node *current = list->head;
|
||||||
|
for (int i = 0; i < position; i++)
|
||||||
|
{
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 然后插入到指定位置
|
||||||
|
newNode->prev = current->prev;
|
||||||
|
newNode->next = current;
|
||||||
|
|
||||||
|
// 更新当前节点的前一个节点的next指针
|
||||||
|
current->prev->next = newNode;
|
||||||
|
|
||||||
|
// 更新当前节点的前指针为新节点
|
||||||
|
current->prev = newNode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 删除指定位置的节点
|
||||||
|
int delectNode(struct DoubleList *list, int position)
|
||||||
|
{
|
||||||
|
// 如果删除位置为0,则删除头节点
|
||||||
|
if (position == 0)
|
||||||
|
{
|
||||||
|
// 如果链表只有一个节点,则删除后链表为空
|
||||||
|
if (list->head == list->tail)
|
||||||
|
{
|
||||||
|
free(list->head);
|
||||||
|
list->head = NULL;
|
||||||
|
list->tail = NULL;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// 更新头节点为下一个节点
|
||||||
|
list->head = list->head->next;
|
||||||
|
// 释放旧头节点内存,更新新头节点的prev指针为NULL
|
||||||
|
free(list->head->prev);
|
||||||
|
list->head->prev = NULL;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
// 如果删除位置大于等于链表长度,则删除尾节点
|
||||||
|
else if (position >= getListLength(list))
|
||||||
|
{
|
||||||
|
// 更新尾节点为前一个节点
|
||||||
|
list->tail = list->tail->prev;
|
||||||
|
// 释放旧尾节点内存,更新新尾节点的next指针为NULL
|
||||||
|
free(list->tail->next);
|
||||||
|
list->tail->next = NULL;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
// 如果删除位置在中间,则删除指定位置节点
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// 首先遍历到指定位置
|
||||||
|
struct Node *current = list->head;
|
||||||
|
for (int i = 0; i < position; i++)
|
||||||
|
{
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
// 更新前一个节点的next指针为后一个节点
|
||||||
|
current->prev->next = current->next;
|
||||||
|
|
||||||
|
// 更新后一个节点的prev指针为前一个节点,更新当前节点的next指针为NULL
|
||||||
|
current->next->prev = current->prev;
|
||||||
|
|
||||||
|
free(current);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 修改指定位置节点的数据
|
||||||
|
int modifyNode(struct DoubleList *list, int data, int position)
|
||||||
|
{
|
||||||
|
// 首先遍历到指定位置
|
||||||
|
struct Node *current = list->head;
|
||||||
|
for (int i = 0; i < position; i++)
|
||||||
|
{
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
// 修改节点数据
|
||||||
|
current->data = data;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查找指定数据的节点位置
|
||||||
|
int findNodePosition(struct DoubleList *list, int data)
|
||||||
|
{
|
||||||
|
// 从头节点开始遍历
|
||||||
|
struct Node *current = list->head;
|
||||||
|
int position = 0;
|
||||||
|
while (current != NULL)
|
||||||
|
{
|
||||||
|
// 如果找到匹配数据,则返回当前位置
|
||||||
|
if (current->data == data)
|
||||||
|
{
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
// 否则,继续遍历下一个节点
|
||||||
|
current = current->next;
|
||||||
|
position++;
|
||||||
|
}
|
||||||
|
// 如果遍历完链表仍未找到匹配数据,则返回-1
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查找指定位置的节点数据
|
||||||
|
int findNodeData(struct DoubleList *list, int position)
|
||||||
|
{
|
||||||
|
// 首先遍历到指定位置
|
||||||
|
struct Node *current = list->head;
|
||||||
|
for (int i = 0; i < position; i++)
|
||||||
|
{
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
// 返回当前节点数据
|
||||||
|
return current->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 打印链表所有节点数据
|
||||||
|
void printList(struct DoubleList *list)
|
||||||
|
{
|
||||||
|
// 从头节点开始遍历
|
||||||
|
struct Node *current = list->head;
|
||||||
|
while (current != NULL)
|
||||||
|
{
|
||||||
|
// 打印当前节点数据
|
||||||
|
printf("%d ", current->data);
|
||||||
|
// 继续遍历下一个节点
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
// 打印换行符
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// 设置控制台编码为UTF-8,防止中文乱码
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul"); // 设置控制台编码为UTF-8
|
||||||
|
SetConsoleOutputCP(65001); // 设置控制台输出编码
|
||||||
|
SetConsoleCP(65001); // 设置控制台输入编码
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// 初始化双端链表
|
||||||
|
struct DoubleList list;
|
||||||
|
initDoubleList(&list);
|
||||||
|
|
||||||
|
// 输入节点数据
|
||||||
|
printf("请输入初始节点数据的数量(默认在链表尾端插入):");
|
||||||
|
int count;
|
||||||
|
scanf("%d", &count);
|
||||||
|
for (int i = 0; i < count; i++)
|
||||||
|
{
|
||||||
|
printf("请输入第 %d 个节点数据:", i + 1);
|
||||||
|
int data;
|
||||||
|
scanf("%d", &data);
|
||||||
|
addNodeToTail(&list, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
// 打印菜单
|
||||||
|
printf("1. 插入节点\n");
|
||||||
|
printf("2. 删除节点\n");
|
||||||
|
printf("3. 修改节点\n");
|
||||||
|
printf("4. 查找节点\n");
|
||||||
|
printf("5. 打印链表\n");
|
||||||
|
printf("0. 退出程序\n");
|
||||||
|
printf("请输入操作选择:");
|
||||||
|
int choice;
|
||||||
|
scanf("%d", &choice);
|
||||||
|
|
||||||
|
// 根据选择执行相应操作
|
||||||
|
switch (choice)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
printf("1. 在链表头插入节点\n");
|
||||||
|
printf("2. 在链表尾插入节点\n");
|
||||||
|
printf("3. 在指定位置插入节点\n");
|
||||||
|
printf("请输入插入方式选择:");
|
||||||
|
int addchoice;
|
||||||
|
scanf("%d", &addchoice);
|
||||||
|
|
||||||
|
printf("请输入要插入的数据:");
|
||||||
|
int insertData;
|
||||||
|
scanf("%d", &insertData);
|
||||||
|
switch (addchoice)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
addNodeToHead(&list, insertData);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
addNodeToTail(&list, insertData);
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
printf("请输入要插入的位置:");
|
||||||
|
int insertPosition;
|
||||||
|
scanf("%d", &insertPosition);
|
||||||
|
insertNode(&list, insertData, insertPosition);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
printf("无效选择,请重新输入\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
printf("请输入要删除的位置:");
|
||||||
|
int deletePosition;
|
||||||
|
scanf("%d", &deletePosition);
|
||||||
|
delectNode(&list, deletePosition);
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
printf("请输入要修改的数据:");
|
||||||
|
int modifyData;
|
||||||
|
scanf("%d", &modifyData);
|
||||||
|
printf("请输入要修改的位置:");
|
||||||
|
int modifyPosition;
|
||||||
|
scanf("%d", &modifyPosition);
|
||||||
|
modifyNode(&list, modifyData, modifyPosition);
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
printf("1. 查找指定数据的节点位置\n");
|
||||||
|
printf("2. 查找指定位置的节点数据\n");
|
||||||
|
printf("请输入查找方式选择:");
|
||||||
|
int findchoice;
|
||||||
|
scanf("%d", &findchoice);
|
||||||
|
switch (findchoice)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
printf("请输入要查找的数据:");
|
||||||
|
int findData;
|
||||||
|
scanf("%d", &findData);
|
||||||
|
int position = findNodePosition(&list, findData);
|
||||||
|
if (position != -1)
|
||||||
|
{
|
||||||
|
printf("数据 %d 位于链表第 %d 个节点\n", findData, position);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("链表中不存在数据 %d\n", findData);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
printf("请输入要查找的位置:");
|
||||||
|
int findPosition;
|
||||||
|
scanf("%d", &findPosition);
|
||||||
|
int data = findNodeData(&list, findPosition);
|
||||||
|
if (data != -1)
|
||||||
|
{
|
||||||
|
printf("链表第 %d 个节点的数据为 %d\n", findPosition, data);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("链表中不存在第 %d 个节点\n", findPosition);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
printf("无效选择,请重新输入\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
printList(&list);
|
||||||
|
break;
|
||||||
|
case 0:
|
||||||
|
printf("程序退出\n");
|
||||||
|
return 0;
|
||||||
|
default:
|
||||||
|
printf("无效选择,请重新输入\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
typedef struct { int x, y; } P;
|
||||||
|
|
||||||
|
static int n;
|
||||||
|
static P a[105];
|
||||||
|
static int vis[105];
|
||||||
|
|
||||||
|
static void dfs(int u)
|
||||||
|
{
|
||||||
|
vis[u] = 1;
|
||||||
|
for (int v = 0; v < n; ++v)
|
||||||
|
{
|
||||||
|
if (!vis[v] && (a[v].x == a[u].x || a[v].y == a[u].y))
|
||||||
|
dfs(v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
if (scanf("%d", &n) != 1) return 0;
|
||||||
|
for (int i = 0; i < n; ++i)
|
||||||
|
scanf("%d %d", &a[i].x, &a[i].y);
|
||||||
|
for (int i = 0; i < n; ++i) vis[i] = 0;
|
||||||
|
int comp = 0;
|
||||||
|
for (int i = 0; i < n; ++i)
|
||||||
|
{
|
||||||
|
if (!vis[i])
|
||||||
|
{
|
||||||
|
++comp;
|
||||||
|
dfs(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("%d\n", comp - 1);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n, m;
|
||||||
|
if (scanf("%d %d", &n, &m) != 2) return 0;
|
||||||
|
int *head = (int *)malloc(((size_t)n + 2) * sizeof(int));
|
||||||
|
for (int i = 1; i <= n; ++i) head[i] = -1;
|
||||||
|
int E = m * 2;
|
||||||
|
int *to = (int *)malloc(((size_t)E + 5) * sizeof(int));
|
||||||
|
int *next = (int *)malloc(((size_t)E + 5) * sizeof(int));
|
||||||
|
int idx = 0;
|
||||||
|
for (int i = 0; i < m; ++i)
|
||||||
|
{
|
||||||
|
int a, b; scanf("%d %d", &a, &b);
|
||||||
|
to[idx] = b; next[idx] = head[a]; head[a] = idx++;
|
||||||
|
to[idx] = a; next[idx] = head[b]; head[b] = idx++;
|
||||||
|
}
|
||||||
|
int x; scanf("%d", &x);
|
||||||
|
int *dist = (int *)malloc(((size_t)n + 2) * sizeof(int));
|
||||||
|
for (int i = 1; i <= n; ++i) dist[i] = -1;
|
||||||
|
int *q = (int *)malloc(((size_t)n + 5) * sizeof(int));
|
||||||
|
int hh = 0, tt = 0;
|
||||||
|
dist[x] = 0; q[tt++] = x;
|
||||||
|
while (hh < tt)
|
||||||
|
{
|
||||||
|
int u = q[hh++];
|
||||||
|
for (int i = head[u]; i != -1; i = next[i])
|
||||||
|
{
|
||||||
|
int v = to[i];
|
||||||
|
if (dist[v] == -1)
|
||||||
|
{
|
||||||
|
dist[v] = dist[u] + 1;
|
||||||
|
q[tt++] = v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int ans = 0;
|
||||||
|
for (int i = 1; i <= n; ++i)
|
||||||
|
if (dist[i] > ans) ans = dist[i];
|
||||||
|
printf("%d\n", ans);
|
||||||
|
free(head); free(to); free(next); free(dist); free(q);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
static int parent[105][105];
|
||||||
|
static unsigned char used[105];
|
||||||
|
|
||||||
|
static int findc(int c, int x)
|
||||||
|
{
|
||||||
|
int p = parent[c][x];
|
||||||
|
while (p != x)
|
||||||
|
{
|
||||||
|
int gp = parent[c][p];
|
||||||
|
parent[c][x] = gp;
|
||||||
|
x = p;
|
||||||
|
p = gp;
|
||||||
|
}
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n, m;
|
||||||
|
if (scanf("%d %d", &n, &m) != 2) return 0;
|
||||||
|
for (int c = 1; c <= m; ++c)
|
||||||
|
{
|
||||||
|
for (int i = 1; i <= n; ++i)
|
||||||
|
parent[c][i] = i;
|
||||||
|
used[c] = 0;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < m; ++i)
|
||||||
|
{
|
||||||
|
int x, y, c;
|
||||||
|
scanf("%d %d %d", &x, &y, &c);
|
||||||
|
used[c] = 1;
|
||||||
|
int rx = findc(c, x);
|
||||||
|
int ry = findc(c, y);
|
||||||
|
if (rx != ry) parent[c][rx] = ry;
|
||||||
|
}
|
||||||
|
int q;
|
||||||
|
scanf("%d", &q);
|
||||||
|
while (q--)
|
||||||
|
{
|
||||||
|
int u, v;
|
||||||
|
scanf("%d %d", &u, &v);
|
||||||
|
int ans = 0;
|
||||||
|
for (int c = 1; c <= m; ++c)
|
||||||
|
{
|
||||||
|
if (!used[c]) continue;
|
||||||
|
int ru = findc(c, u);
|
||||||
|
int rv = findc(c, v);
|
||||||
|
if (ru == rv) ++ans;
|
||||||
|
}
|
||||||
|
printf("%d\n", ans);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n; if (scanf("%d", &n) != 1) return 0;
|
||||||
|
int E = (n - 1) * 2;
|
||||||
|
int *head = (int *)malloc(((size_t)n + 2) * sizeof(int));
|
||||||
|
int *to = (int *)malloc(((size_t)E + 5) * sizeof(int));
|
||||||
|
int *next = (int *)malloc(((size_t)E + 5) * sizeof(int));
|
||||||
|
for (int i = 1; i <= n; ++i) head[i] = -1;
|
||||||
|
int idx = 0;
|
||||||
|
for (int i = 0; i < n - 1; ++i)
|
||||||
|
{
|
||||||
|
int u, v; scanf("%d %d", &u, &v);
|
||||||
|
to[idx] = v; next[idx] = head[u]; head[u] = idx++;
|
||||||
|
to[idx] = u; next[idx] = head[v]; head[v] = idx++;
|
||||||
|
}
|
||||||
|
int *color = (int *)malloc(((size_t)n + 2) * sizeof(int));
|
||||||
|
for (int i = 1; i <= n; ++i) color[i] = -1;
|
||||||
|
int *q = (int *)malloc(((size_t)n + 5) * sizeof(int));
|
||||||
|
int hh = 0, tt = 0;
|
||||||
|
color[1] = 0; q[tt++] = 1;
|
||||||
|
while (hh < tt)
|
||||||
|
{
|
||||||
|
int u = q[hh++];
|
||||||
|
for (int i = head[u]; i != -1; i = next[i])
|
||||||
|
{
|
||||||
|
int v = to[i];
|
||||||
|
if (color[v] == -1)
|
||||||
|
{
|
||||||
|
color[v] = color[u] ^ 1;
|
||||||
|
q[tt++] = v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
long long a = 0, b = 0;
|
||||||
|
for (int i = 1; i <= n; ++i) if (color[i] == 0) ++a; else ++b;
|
||||||
|
long long ans = a * b - (long long)(n - 1);
|
||||||
|
printf("%lld\n", ans);
|
||||||
|
free(head); free(to); free(next); free(color); free(q);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,118 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
|
typedef struct { int v, w, next; } Edge;
|
||||||
|
typedef struct { long long d; int u, m; } HN;
|
||||||
|
|
||||||
|
static int n, m, p;
|
||||||
|
static int *head;
|
||||||
|
static Edge *E;
|
||||||
|
static int ec;
|
||||||
|
static int *pc, *pa, *pb, *pt;
|
||||||
|
|
||||||
|
static HN *heap;
|
||||||
|
static int hsz;
|
||||||
|
|
||||||
|
static void hpush(HN x)
|
||||||
|
{
|
||||||
|
int i = hsz++;
|
||||||
|
heap[i] = x;
|
||||||
|
while (i)
|
||||||
|
{
|
||||||
|
int p = (i - 1) >> 1;
|
||||||
|
if (heap[p].d <= heap[i].d) break;
|
||||||
|
HN t = heap[p]; heap[p] = heap[i]; heap[i] = t; i = p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static HN hpop()
|
||||||
|
{
|
||||||
|
HN r = heap[0];
|
||||||
|
heap[0] = heap[--hsz];
|
||||||
|
int i = 0;
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
int l = i * 2 + 1, rgt = l + 1, s = i;
|
||||||
|
if (l < hsz && heap[l].d < heap[s].d) s = l;
|
||||||
|
if (rgt < hsz && heap[rgt].d < heap[s].d) s = rgt;
|
||||||
|
if (s == i) break;
|
||||||
|
HN t = heap[i]; heap[i] = heap[s]; heap[s] = t; i = s;
|
||||||
|
}
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
if (scanf("%d %d %d", &n, &m, &p) != 3) return 0;
|
||||||
|
head = (int *)malloc(((size_t)n + 2) * sizeof(int));
|
||||||
|
E = (Edge *)malloc(((size_t)m * 2 + 5) * sizeof(Edge));
|
||||||
|
for (int i = 1; i <= n; ++i) head[i] = -1;
|
||||||
|
ec = 0;
|
||||||
|
for (int i = 0; i < m; ++i)
|
||||||
|
{
|
||||||
|
int u, v, w; scanf("%d %d %d", &u, &v, &w);
|
||||||
|
E[ec].v = v; E[ec].w = w; E[ec].next = head[u]; head[u] = ec++;
|
||||||
|
E[ec].v = u; E[ec].w = w; E[ec].next = head[v]; head[v] = ec++;
|
||||||
|
}
|
||||||
|
pc = (int *)malloc(((size_t)p + 2) * sizeof(int));
|
||||||
|
pa = (int *)malloc(((size_t)p + 2) * sizeof(int));
|
||||||
|
pb = (int *)malloc(((size_t)p + 2) * sizeof(int));
|
||||||
|
pt = (int *)malloc(((size_t)p + 2) * sizeof(int));
|
||||||
|
for (int i = 0; i < p; ++i)
|
||||||
|
{
|
||||||
|
int c, a, b, t; scanf("%d %d %d %d", &c, &a, &b, &t);
|
||||||
|
pc[i] = c; pa[i] = a; pb[i] = b; pt[i] = t;
|
||||||
|
}
|
||||||
|
int M = 1 << p;
|
||||||
|
long long *dist = (long long *)malloc(((size_t)n * (size_t)M) * sizeof(long long));
|
||||||
|
for (size_t i = 0; i < (size_t)n * (size_t)M; ++i) dist[i] = LLONG_MAX;
|
||||||
|
heap = (HN *)malloc(((size_t)n * (size_t)M + 5) * sizeof(HN));
|
||||||
|
hsz = 0;
|
||||||
|
dist[(0) * (size_t)n + (size_t)(1 - 1)] = 0;
|
||||||
|
hpush((HN){0LL, 1, 0});
|
||||||
|
while (hsz)
|
||||||
|
{
|
||||||
|
HN cur = hpop();
|
||||||
|
long long cd = cur.d; int u = cur.u; int mask = cur.m;
|
||||||
|
if (cd != dist[(size_t)mask * (size_t)n + (size_t)(u - 1)]) continue;
|
||||||
|
if (u == n)
|
||||||
|
{
|
||||||
|
printf("%lld\n", cd);
|
||||||
|
free(head); free(E); free(pc); free(pa); free(pb); free(pt); free(dist); free(heap);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
for (int i = head[u]; i != -1; i = E[i].next)
|
||||||
|
{
|
||||||
|
int v = E[i].v; long long nd = cd + (long long)E[i].w;
|
||||||
|
size_t idx = (size_t)mask * (size_t)n + (size_t)(v - 1);
|
||||||
|
if (nd < dist[idx]) { dist[idx] = nd; hpush((HN){nd, v, mask}); }
|
||||||
|
}
|
||||||
|
for (int i = 0; i < p; ++i)
|
||||||
|
{
|
||||||
|
int bit = 1 << i;
|
||||||
|
if (!(mask & bit) && u == pc[i])
|
||||||
|
{
|
||||||
|
int nmask = mask | bit;
|
||||||
|
size_t idx = (size_t)nmask * (size_t)n + (size_t)(u - 1);
|
||||||
|
if (cd < dist[idx]) { dist[idx] = cd; hpush((HN){cd, u, nmask}); }
|
||||||
|
}
|
||||||
|
if (mask & bit)
|
||||||
|
{
|
||||||
|
if (u == pa[i])
|
||||||
|
{
|
||||||
|
int v = pb[i]; long long nd = cd + (long long)pt[i];
|
||||||
|
size_t idx = (size_t)mask * (size_t)n + (size_t)(v - 1);
|
||||||
|
if (nd < dist[idx]) { dist[idx] = nd; hpush((HN){nd, v, mask}); }
|
||||||
|
}
|
||||||
|
if (u == pb[i])
|
||||||
|
{
|
||||||
|
int v = pa[i]; long long nd = cd + (long long)pt[i];
|
||||||
|
size_t idx = (size_t)mask * (size_t)n + (size_t)(v - 1);
|
||||||
|
if (nd < dist[idx]) { dist[idx] = nd; hpush((HN){nd, v, mask}); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
|
typedef struct { int v, w, next; } Edge;
|
||||||
|
typedef struct { long long d; int u; } HN;
|
||||||
|
|
||||||
|
static long long *dij(int n, int *head, Edge *E, int src)
|
||||||
|
{
|
||||||
|
long long *dist = (long long *)malloc(((size_t)n + 1) * sizeof(long long));
|
||||||
|
for (int i = 1; i <= n; ++i) dist[i] = LLONG_MAX;
|
||||||
|
HN *heap = (HN *)malloc(((size_t)n * 4 + 5) * sizeof(HN));
|
||||||
|
int hsz = 0;
|
||||||
|
dist[src] = 0; heap[hsz++] = (HN){0, src};
|
||||||
|
for (int i = hsz - 1; i > 0; --i)
|
||||||
|
{
|
||||||
|
int p = (i - 1) >> 1;
|
||||||
|
if (heap[p].d > heap[i].d) { HN t = heap[p]; heap[p] = heap[i]; heap[i] = t; }
|
||||||
|
}
|
||||||
|
while (hsz)
|
||||||
|
{
|
||||||
|
HN cur = heap[0];
|
||||||
|
heap[0] = heap[--hsz];
|
||||||
|
int i = 0;
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
int l = i * 2 + 1, r = l + 1, s = i;
|
||||||
|
if (l < hsz && heap[l].d < heap[s].d) s = l;
|
||||||
|
if (r < hsz && heap[r].d < heap[s].d) s = r;
|
||||||
|
if (s == i) break;
|
||||||
|
HN t = heap[i]; heap[i] = heap[s]; heap[s] = t; i = s;
|
||||||
|
}
|
||||||
|
long long cd = cur.d; int u = cur.u;
|
||||||
|
if (cd != dist[u]) continue;
|
||||||
|
for (int e = head[u]; e != -1; e = E[e].next)
|
||||||
|
{
|
||||||
|
int v = E[e].v; long long nd = cd + (long long)E[e].w;
|
||||||
|
if (nd < dist[v])
|
||||||
|
{
|
||||||
|
dist[v] = nd;
|
||||||
|
int k = hsz++;
|
||||||
|
heap[k] = (HN){nd, v};
|
||||||
|
while (k)
|
||||||
|
{
|
||||||
|
int p = (k - 1) >> 1;
|
||||||
|
if (heap[p].d <= heap[k].d) break;
|
||||||
|
HN t = heap[p]; heap[p] = heap[k]; heap[k] = t; k = p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(heap);
|
||||||
|
return dist;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int N, M, X;
|
||||||
|
if (scanf("%d %d %d", &N, &M, &X) != 3) return 0;
|
||||||
|
int *h1 = (int *)malloc(((size_t)N + 2) * sizeof(int));
|
||||||
|
int *h2 = (int *)malloc(((size_t)N + 2) * sizeof(int));
|
||||||
|
Edge *E1 = (Edge *)malloc(((size_t)M + 5) * sizeof(Edge));
|
||||||
|
Edge *E2 = (Edge *)malloc(((size_t)M + 5) * sizeof(Edge));
|
||||||
|
for (int i = 1; i <= N; ++i) h1[i] = h2[i] = -1;
|
||||||
|
int c1 = 0, c2 = 0;
|
||||||
|
for (int i = 0; i < M; ++i)
|
||||||
|
{
|
||||||
|
int a, b, t; scanf("%d %d %d", &a, &b, &t);
|
||||||
|
E1[c1].v = b; E1[c1].w = t; E1[c1].next = h1[a]; h1[a] = c1++;
|
||||||
|
E2[c2].v = a; E2[c2].w = t; E2[c2].next = h2[b]; h2[b] = c2++;
|
||||||
|
}
|
||||||
|
long long *dx = dij(N, h1, E1, X);
|
||||||
|
long long *rx = dij(N, h2, E2, X);
|
||||||
|
long long ans = 0;
|
||||||
|
for (int i = 1; i <= N; ++i)
|
||||||
|
{
|
||||||
|
long long a = rx[i]; long long b = dx[i];
|
||||||
|
if (a == LLONG_MAX || b == LLONG_MAX) continue;
|
||||||
|
long long s = a + b;
|
||||||
|
if (s > ans) ans = s;
|
||||||
|
}
|
||||||
|
printf("%lld\n", ans);
|
||||||
|
free(h1); free(h2); free(E1); free(E2); free(dx); free(rx);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef struct { int v, next; } E;
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n; if (scanf("%d", &n) != 1) return 0;
|
||||||
|
int *a = (int *)malloc(((size_t)n + 2) * sizeof(int));
|
||||||
|
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
|
||||||
|
int m = 2 * (n - 1) + n;
|
||||||
|
int *head = (int *)malloc(((size_t)n + 2) * sizeof(int));
|
||||||
|
E *e = (E *)malloc(((size_t)m + 5) * sizeof(E));
|
||||||
|
for (int i = 1; i <= n; ++i) head[i] = -1;
|
||||||
|
int idx = 0;
|
||||||
|
for (int i = 1; i < n; ++i)
|
||||||
|
{
|
||||||
|
e[idx].v = i + 1; e[idx].next = head[i]; head[i] = idx++;
|
||||||
|
e[idx].v = i; e[idx].next = head[i + 1]; head[i + 1] = idx++;
|
||||||
|
}
|
||||||
|
for (int i = 1; i <= n; ++i)
|
||||||
|
{
|
||||||
|
int v = a[i];
|
||||||
|
e[idx].v = v; e[idx].next = head[i]; head[i] = idx++;
|
||||||
|
}
|
||||||
|
int *dist = (int *)malloc(((size_t)n + 2) * sizeof(int));
|
||||||
|
for (int i = 1; i <= n; ++i) dist[i] = -1;
|
||||||
|
int *q = (int *)malloc(((size_t)n + 5) * sizeof(int));
|
||||||
|
int hh = 0, tt = 0; q[tt++] = 1; dist[1] = 0;
|
||||||
|
while (hh < tt)
|
||||||
|
{
|
||||||
|
int u = q[hh++];
|
||||||
|
for (int i = head[u]; i != -1; i = e[i].next)
|
||||||
|
{
|
||||||
|
int v = e[i].v;
|
||||||
|
if (dist[v] == -1)
|
||||||
|
{
|
||||||
|
dist[v] = dist[u] + 1;
|
||||||
|
q[tt++] = v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = 1; i <= n; ++i)
|
||||||
|
{
|
||||||
|
if (i > 1) putchar(' ');
|
||||||
|
printf("%d", dist[i]);
|
||||||
|
}
|
||||||
|
putchar('\n');
|
||||||
|
free(a); free(head); free(e); free(dist); free(q);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n, m; long long L;
|
||||||
|
if (scanf("%d %d %lld", &n, &m, &L) != 3) return 0;
|
||||||
|
long long *dist = (long long *)malloc((size_t)(n + 1) * (size_t)(n + 1) * sizeof(long long));
|
||||||
|
for (int i = 1; i <= n; ++i)
|
||||||
|
for (int j = 1; j <= n; ++j)
|
||||||
|
dist[(size_t)i * (n + 1) + j] = (i == j) ? 0LL : LLONG_MAX / 4;
|
||||||
|
for (int i = 0; i < m; ++i)
|
||||||
|
{
|
||||||
|
int u, v; long long c; scanf("%d %d %lld", &u, &v, &c);
|
||||||
|
long long *d1 = &dist[(size_t)u * (n + 1) + v];
|
||||||
|
long long *d2 = &dist[(size_t)v * (n + 1) + u];
|
||||||
|
if (c < *d1) *d1 = c;
|
||||||
|
if (c < *d2) *d2 = c;
|
||||||
|
}
|
||||||
|
for (int k = 1; k <= n; ++k)
|
||||||
|
{
|
||||||
|
for (int i = 1; i <= n; ++i)
|
||||||
|
{
|
||||||
|
long long dik = dist[(size_t)i * (n + 1) + k];
|
||||||
|
if (dik == LLONG_MAX / 4) continue;
|
||||||
|
for (int j = 1; j <= n; ++j)
|
||||||
|
{
|
||||||
|
long long skj = dist[(size_t)k * (n + 1) + j];
|
||||||
|
if (skj == LLONG_MAX / 4) continue;
|
||||||
|
long long alt = dik + skj;
|
||||||
|
long long *dij = &dist[(size_t)i * (n + 1) + j];
|
||||||
|
if (alt < *dij) *dij = alt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unsigned char *reach = (unsigned char *)malloc((size_t)(n + 1) * (size_t)(n + 1));
|
||||||
|
for (int i = 1; i <= n; ++i)
|
||||||
|
for (int j = 1; j <= n; ++j)
|
||||||
|
reach[(size_t)i * (n + 1) + j] = (dist[(size_t)i * (n + 1) + j] <= L) ? 1 : 0;
|
||||||
|
int *steps = (int *)malloc((size_t)(n + 1) * (size_t)(n + 1) * sizeof(int));
|
||||||
|
int *q = (int *)malloc(((size_t)n + 5) * sizeof(int));
|
||||||
|
for (int s = 1; s <= n; ++s)
|
||||||
|
{
|
||||||
|
for (int i = 1; i <= n; ++i) steps[(size_t)s * (n + 1) + i] = -1;
|
||||||
|
int hh = 0, tt = 0; q[tt++] = s; steps[(size_t)s * (n + 1) + s] = 0;
|
||||||
|
while (hh < tt)
|
||||||
|
{
|
||||||
|
int u = q[hh++];
|
||||||
|
int du = steps[(size_t)s * (n + 1) + u];
|
||||||
|
unsigned char *row = &reach[(size_t)u * (n + 1)];
|
||||||
|
for (int v = 1; v <= n; ++v)
|
||||||
|
{
|
||||||
|
if (u == v) continue;
|
||||||
|
if (row[v] && steps[(size_t)s * (n + 1) + v] == -1)
|
||||||
|
{
|
||||||
|
steps[(size_t)s * (n + 1) + v] = du + 1;
|
||||||
|
q[tt++] = v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int qn; scanf("%d", &qn);
|
||||||
|
while (qn--)
|
||||||
|
{
|
||||||
|
int s, t; scanf("%d %d", &s, &t);
|
||||||
|
int d = steps[(size_t)s * (n + 1) + t];
|
||||||
|
if (d == -1) puts("-1"); else printf("%d\n", d - 1);
|
||||||
|
}
|
||||||
|
free(dist); free(reach); free(steps); free(q);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef struct { int u, v, w; } Edge;
|
||||||
|
|
||||||
|
static int cmp_edge(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
const Edge *x = (const Edge *)a;
|
||||||
|
const Edge *y = (const Edge *)b;
|
||||||
|
return x->w - y->w;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int find(int *fa, int x)
|
||||||
|
{
|
||||||
|
while (fa[x] != x) { fa[x] = fa[fa[x]]; x = fa[x]; }
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n, k;
|
||||||
|
if (scanf("%d %d", &n, &k) != 2) return 0;
|
||||||
|
Edge *E = (Edge *)malloc(((size_t)k + 5) * sizeof(Edge));
|
||||||
|
for (int i = 0; i < k; ++i) scanf("%d %d %d", &E[i].u, &E[i].v, &E[i].w);
|
||||||
|
qsort(E, (size_t)k, sizeof(Edge), cmp_edge);
|
||||||
|
int *fa = (int *)malloc(((size_t)n + 2) * sizeof(int));
|
||||||
|
for (int i = 1; i <= n; ++i) fa[i] = i;
|
||||||
|
long long sum = 0;
|
||||||
|
int used = 0;
|
||||||
|
for (int i = 0; i < k; ++i)
|
||||||
|
{
|
||||||
|
int fu = find(fa, E[i].u);
|
||||||
|
int fv = find(fa, E[i].v);
|
||||||
|
if (fu != fv)
|
||||||
|
{
|
||||||
|
fa[fu] = fv;
|
||||||
|
sum += E[i].w;
|
||||||
|
++used;
|
||||||
|
if (used == n - 1) break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (used != n - 1) printf("-1\n"); else printf("%lld\n", sum);
|
||||||
|
free(E); free(fa);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,124 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
|
typedef struct { int u, v, w; } Edge;
|
||||||
|
|
||||||
|
static int cmp_desc(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
const Edge *x = (const Edge *)a;
|
||||||
|
const Edge *y = (const Edge *)b;
|
||||||
|
return y->w - x->w;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int find(int *fa, int x)
|
||||||
|
{
|
||||||
|
while (fa[x] != x) { fa[x] = fa[fa[x]]; x = fa[x]; }
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n, m;
|
||||||
|
if (scanf("%d %d", &n, &m) != 2) return 0;
|
||||||
|
Edge *E = (Edge *)malloc(((size_t)m + 5) * sizeof(Edge));
|
||||||
|
for (int i = 0; i < m; ++i) scanf("%d %d %d", &E[i].u, &E[i].v, &E[i].w);
|
||||||
|
qsort(E, (size_t)m, sizeof(Edge), cmp_desc);
|
||||||
|
int *fa = (int *)malloc(((size_t)n + 2) * sizeof(int));
|
||||||
|
for (int i = 1; i <= n; ++i) fa[i] = i;
|
||||||
|
int *head = (int *)malloc(((size_t)n + 2) * sizeof(int));
|
||||||
|
for (int i = 1; i <= n; ++i) head[i] = -1;
|
||||||
|
int *to = (int *)malloc(((size_t)(n - 1) * 2 + 5) * sizeof(int));
|
||||||
|
int *wt = (int *)malloc(((size_t)(n - 1) * 2 + 5) * sizeof(int));
|
||||||
|
int *next = (int *)malloc(((size_t)(n - 1) * 2 + 5) * sizeof(int));
|
||||||
|
int idx = 0;
|
||||||
|
for (int i = 0; i < m; ++i)
|
||||||
|
{
|
||||||
|
int fu = find(fa, E[i].u);
|
||||||
|
int fv = find(fa, E[i].v);
|
||||||
|
if (fu != fv)
|
||||||
|
{
|
||||||
|
fa[fu] = fv;
|
||||||
|
to[idx] = E[i].v; wt[idx] = E[i].w; next[idx] = head[E[i].u]; head[E[i].u] = idx++;
|
||||||
|
to[idx] = E[i].u; wt[idx] = E[i].w; next[idx] = head[E[i].v]; head[E[i].v] = idx++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int LOG = 15;
|
||||||
|
int *depth = (int *)malloc(((size_t)n + 2) * sizeof(int));
|
||||||
|
for (int i = 1; i <= n; ++i) depth[i] = -1;
|
||||||
|
int *up = (int *)malloc((size_t)LOG * (size_t)(n + 1) * sizeof(int));
|
||||||
|
int *mn = (int *)malloc((size_t)LOG * (size_t)(n + 1) * sizeof(int));
|
||||||
|
int *q = (int *)malloc(((size_t)n + 5) * sizeof(int));
|
||||||
|
for (int i = 1; i <= n; ++i)
|
||||||
|
{
|
||||||
|
if (depth[i] != -1) continue;
|
||||||
|
int hh = 0, tt = 0; q[tt++] = i; depth[i] = 0; up[i] = i; mn[i] = INT_MAX;
|
||||||
|
while (hh < tt)
|
||||||
|
{
|
||||||
|
int u = q[hh++];
|
||||||
|
for (int e = head[u]; e != -1; e = next[e])
|
||||||
|
{
|
||||||
|
int v = to[e];
|
||||||
|
if (depth[v] == -1)
|
||||||
|
{
|
||||||
|
depth[v] = depth[u] + 1;
|
||||||
|
up[v] = u;
|
||||||
|
mn[v] = wt[e];
|
||||||
|
q[tt++] = v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int k = 1; k < LOG; ++k)
|
||||||
|
{
|
||||||
|
for (int v = 1; v <= n; ++v)
|
||||||
|
{
|
||||||
|
int p = up[(k - 1) * (n + 1) + v];
|
||||||
|
up[k * (n + 1) + v] = up[(k - 1) * (n + 1) + p];
|
||||||
|
int a = mn[(k - 1) * (n + 1) + v];
|
||||||
|
int b = mn[(k - 1) * (n + 1) + p];
|
||||||
|
mn[k * (n + 1) + v] = (a < b) ? a : b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int qn; scanf("%d", &qn);
|
||||||
|
while (qn--)
|
||||||
|
{
|
||||||
|
int x, y; scanf("%d %d", &x, &y);
|
||||||
|
if (find(fa, x) != find(fa, y)) { puts("-1"); continue; }
|
||||||
|
int res = INT_MAX;
|
||||||
|
if (depth[x] < depth[y]) { int t = x; x = y; y = t; }
|
||||||
|
int diff = depth[x] - depth[y];
|
||||||
|
for (int k = LOG - 1; k >= 1; --k)
|
||||||
|
{
|
||||||
|
int step = 1 << k;
|
||||||
|
if (diff >= step)
|
||||||
|
{
|
||||||
|
int val = mn[k * (n + 1) + x];
|
||||||
|
if (val < res) res = val;
|
||||||
|
x = up[k * (n + 1) + x];
|
||||||
|
diff -= step;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (diff)
|
||||||
|
{
|
||||||
|
int val = mn[x]; if (val < res) res = val; x = up[x];
|
||||||
|
}
|
||||||
|
if (x == y) { printf("%d\n", res); continue; }
|
||||||
|
for (int k = LOG - 1; k >= 1; --k)
|
||||||
|
{
|
||||||
|
int ux = up[k * (n + 1) + x];
|
||||||
|
int uy = up[k * (n + 1) + y];
|
||||||
|
if (ux != uy)
|
||||||
|
{
|
||||||
|
int vx = mn[k * (n + 1) + x]; if (vx < res) res = vx;
|
||||||
|
int vy = mn[k * (n + 1) + y]; if (vy < res) res = vy;
|
||||||
|
x = ux; y = uy;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int vx = mn[x]; if (vx < res) res = vx; x = up[x];
|
||||||
|
int vy = mn[y]; if (vy < res) res = vy; y = up[y];
|
||||||
|
printf("%d\n", res);
|
||||||
|
}
|
||||||
|
free(E); free(fa); free(head); free(to); free(wt); free(next); free(depth); free(up); free(mn); free(q);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,136 @@
|
|||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#include <direct.h>
|
||||||
|
#endif
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
// 二叉树节点定义
|
||||||
|
typedef struct BiTNode {
|
||||||
|
char data;
|
||||||
|
struct BiTNode *lchild;
|
||||||
|
struct BiTNode *rchild;
|
||||||
|
} BiTNode, *BiTree;
|
||||||
|
|
||||||
|
static void CreateBiTree(BiTree *T) {
|
||||||
|
char ch;
|
||||||
|
if (scanf(" %c", &ch) != 1) {
|
||||||
|
*T = NULL;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (ch == '#') {
|
||||||
|
*T = NULL;
|
||||||
|
} else {
|
||||||
|
*T = (BiTNode *)malloc(sizeof(BiTNode));
|
||||||
|
if (!*T) {
|
||||||
|
fprintf(stderr, "内存分配失败\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
(*T)->data = ch;
|
||||||
|
CreateBiTree(&(*T)->lchild);
|
||||||
|
CreateBiTree(&(*T)->rchild);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void PreOrderTraverse(BiTree T) {
|
||||||
|
if (T) {
|
||||||
|
putchar(T->data);
|
||||||
|
putchar(' ');
|
||||||
|
PreOrderTraverse(T->lchild);
|
||||||
|
PreOrderTraverse(T->rchild);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void InOrderTraverse(BiTree T) {
|
||||||
|
if (T) {
|
||||||
|
InOrderTraverse(T->lchild);
|
||||||
|
putchar(T->data);
|
||||||
|
putchar(' ');
|
||||||
|
InOrderTraverse(T->rchild);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void PostOrderTraverse(BiTree T) {
|
||||||
|
if (T) {
|
||||||
|
PostOrderTraverse(T->lchild);
|
||||||
|
PostOrderTraverse(T->rchild);
|
||||||
|
putchar(T->data);
|
||||||
|
putchar(' ');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void DestroyTree(BiTree T) {
|
||||||
|
if (!T) return;
|
||||||
|
DestroyTree(T->lchild);
|
||||||
|
DestroyTree(T->rchild);
|
||||||
|
free(T);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void PrintMenu(void) {
|
||||||
|
printf("\n--- 选择遍历方式 ---\n");
|
||||||
|
printf("1. 先序遍历\n");
|
||||||
|
printf("2. 中序遍历\n");
|
||||||
|
printf("3. 后序遍历\n");
|
||||||
|
printf("4. 退出\n");
|
||||||
|
printf("请输入选择 (1-4): ");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
// 设置控制台编码为UTF-8,防止中文乱码
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul"); // 设置控制台编码为UTF-8
|
||||||
|
SetConsoleOutputCP(65001); // 设置控制台输出编码
|
||||||
|
SetConsoleCP(65001); // 设置控制台输入编码
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// 初始化二叉树
|
||||||
|
BiTree T = NULL;
|
||||||
|
int choice = 0;
|
||||||
|
|
||||||
|
printf("=== 链式二叉树遍历系统 ===\n");
|
||||||
|
printf("请输入先序扩展序列构建二叉树(# 表示空节点,例如 AB#C##D##):\n");
|
||||||
|
// 构建二叉树
|
||||||
|
CreateBiTree(&T);
|
||||||
|
|
||||||
|
// 主循环处理用户选择
|
||||||
|
while (choice != 4)
|
||||||
|
{
|
||||||
|
PrintMenu();
|
||||||
|
if (scanf("%d", &choice) != 1)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "输入错误,程序结束。\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
switch (choice)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
// 先序遍历
|
||||||
|
printf("先序遍历结果: ");
|
||||||
|
PreOrderTraverse(T);
|
||||||
|
putchar('\n');
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
// 中序遍历
|
||||||
|
printf("中序遍历结果: ");
|
||||||
|
InOrderTraverse(T);
|
||||||
|
putchar('\n');
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
// 后序遍历
|
||||||
|
printf("后序遍历结果: ");
|
||||||
|
PostOrderTraverse(T);
|
||||||
|
putchar('\n');
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
printf("程序已退出。\n");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
printf("无效输入,请输入 1~4 之间的数字!\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 销毁二叉树
|
||||||
|
DestroyTree(T);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef struct { int u, v; int w; } E;
|
||||||
|
|
||||||
|
static int cmp(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
const E *x = (const E *)a; const E *y = (const E *)b;
|
||||||
|
return (x->w < y->w) ? -1 : (x->w > y->w);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int find(int *fa, int x)
|
||||||
|
{
|
||||||
|
while (fa[x] != x) { fa[x] = fa[fa[x]]; x = fa[x]; }
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n, k;
|
||||||
|
if (scanf("%d %d", &n, &k) != 2) return 0;
|
||||||
|
int *X = (int *)malloc(((size_t)n + 2) * sizeof(int));
|
||||||
|
int *Y = (int *)malloc(((size_t)n + 2) * sizeof(int));
|
||||||
|
for (int i = 1; i <= n; ++i) scanf("%d %d", &X[i], &Y[i]);
|
||||||
|
size_t m = (size_t)n * (size_t)(n - 1) / 2;
|
||||||
|
E *edges = (E *)malloc((m + 5) * sizeof(E));
|
||||||
|
size_t idx = 0;
|
||||||
|
for (int i = 1; i <= n; ++i)
|
||||||
|
{
|
||||||
|
for (int j = i + 1; j <= n; ++j)
|
||||||
|
{
|
||||||
|
long long dx = (long long)X[i] - (long long)X[j];
|
||||||
|
long long dy = (long long)Y[i] - (long long)Y[j];
|
||||||
|
long long w = dx * dx + dy * dy;
|
||||||
|
edges[idx].u = i; edges[idx].v = j; edges[idx].w = (int)w; ++idx;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
qsort(edges, idx, sizeof(E), cmp);
|
||||||
|
int *fa = (int *)malloc(((size_t)n + 2) * sizeof(int));
|
||||||
|
for (int i = 1; i <= n; ++i) fa[i] = i;
|
||||||
|
int comps = n;
|
||||||
|
double ans = 0.0;
|
||||||
|
for (size_t i = 0; i < idx; ++i)
|
||||||
|
{
|
||||||
|
int u = edges[i].u, v = edges[i].v;
|
||||||
|
int fu = find(fa, u), fv = find(fa, v);
|
||||||
|
if (fu != fv)
|
||||||
|
{
|
||||||
|
if (comps > k)
|
||||||
|
{
|
||||||
|
fa[fu] = fv;
|
||||||
|
--comps;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ans = (double)edges[i].w;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("%.2f\n", ans);
|
||||||
|
free(X); free(Y); free(edges); free(fa);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
static int M, N;
|
||||||
|
static int CAP;
|
||||||
|
static int *ks, *kn, *kf;
|
||||||
|
static long long *kv;
|
||||||
|
|
||||||
|
static long long dfs(int s, int f, int cur)
|
||||||
|
{
|
||||||
|
if (cur == 0)
|
||||||
|
return (s == 0 && f == 0) ? 1LL : 0LL;
|
||||||
|
if (s == 0 && f == 0)
|
||||||
|
return (cur == 0) ? 1LL : 0LL;
|
||||||
|
int h = (((s * 37 + f) * 99991) ^ cur) & (CAP - 1);
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
if (ks[h] == -1)
|
||||||
|
break;
|
||||||
|
if (ks[h] == s && kn[h] == f && kf[h] == cur)
|
||||||
|
return kv[h];
|
||||||
|
h = (h + 1) & (CAP - 1);
|
||||||
|
}
|
||||||
|
long long ans = 0;
|
||||||
|
if (s > 0)
|
||||||
|
ans += dfs(s - 1, f, cur * 2);
|
||||||
|
if (f > 0 && cur >= 2)
|
||||||
|
ans += dfs(s, f - 1, cur - 2);
|
||||||
|
ks[h] = s; kn[h] = f; kf[h] = cur; kv[h] = ans;
|
||||||
|
return ans;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
if (scanf("%d %d", &M, &N) != 2) return 0;
|
||||||
|
CAP = 1 << 20;
|
||||||
|
ks = (int *)malloc((size_t)CAP * sizeof(int));
|
||||||
|
kn = (int *)malloc((size_t)CAP * sizeof(int));
|
||||||
|
kf = (int *)malloc((size_t)CAP * sizeof(int));
|
||||||
|
kv = (long long *)malloc((size_t)CAP * sizeof(long long));
|
||||||
|
for (int i = 0; i < CAP; ++i) ks[i] = -1;
|
||||||
|
long long ans = dfs(M, N, 5);
|
||||||
|
printf("%lld\n", ans);
|
||||||
|
free(ks); free(kn); free(kf); free(kv);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
static int n, m;
|
||||||
|
static int cur[25];
|
||||||
|
|
||||||
|
static void dfs(int start, int k)
|
||||||
|
{
|
||||||
|
if (k == m)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < m; ++i)
|
||||||
|
{
|
||||||
|
if (i) putchar(' ');
|
||||||
|
printf("%d", cur[i]);
|
||||||
|
}
|
||||||
|
putchar('\n');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (int i = start; i <= n - (m - k) + 1; ++i)
|
||||||
|
{
|
||||||
|
cur[k] = i;
|
||||||
|
dfs(i + 1, k + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
if (scanf("%d %d", &n, &m) != 2) return 0;
|
||||||
|
dfs(1, 0);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
static long long target;
|
||||||
|
static int L;
|
||||||
|
static int K;
|
||||||
|
static int found;
|
||||||
|
static long long ans;
|
||||||
|
|
||||||
|
static void gen(int pos, int c4, int c7, long long cur)
|
||||||
|
{
|
||||||
|
if (found) return;
|
||||||
|
if (pos == L)
|
||||||
|
{
|
||||||
|
if (c4 == K && c7 == K && cur >= target)
|
||||||
|
{
|
||||||
|
ans = cur;
|
||||||
|
found = 1;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (c4 < K) gen(pos + 1, c4 + 1, c7, cur * 10 + 4);
|
||||||
|
if (!found && c7 < K) gen(pos + 1, c4, c7 + 1, cur * 10 + 7);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
long long n;
|
||||||
|
if (scanf("%lld", &n) != 1) return 0;
|
||||||
|
target = n;
|
||||||
|
int len = 0; long long t = n;
|
||||||
|
while (t) { ++len; t /= 10; }
|
||||||
|
if (len == 0) len = 1;
|
||||||
|
if ((len & 1) == 1)
|
||||||
|
{
|
||||||
|
L = len + 1; K = L / 2;
|
||||||
|
long long cur = 0;
|
||||||
|
for (int i = 0; i < K; ++i) cur = cur * 10 + 4;
|
||||||
|
for (int i = 0; i < K; ++i) cur = cur * 10 + 7;
|
||||||
|
printf("%lld\n", cur);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
L = len; K = L / 2; found = 0; ans = 0;
|
||||||
|
gen(0, 0, 0, 0);
|
||||||
|
if (found)
|
||||||
|
{
|
||||||
|
printf("%lld\n", ans);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
L = len + 2; K = L / 2;
|
||||||
|
long long cur = 0;
|
||||||
|
for (int i = 0; i < K; ++i) cur = cur * 10 + 4;
|
||||||
|
for (int i = 0; i < K; ++i) cur = cur * 10 + 7;
|
||||||
|
printf("%lld\n", cur);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
static int CAP;
|
||||||
|
static int *K;
|
||||||
|
|
||||||
|
static int add(int x)
|
||||||
|
{
|
||||||
|
int h = (x * 1315423911u) & (CAP - 1);
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
if (K[h] == -1) { K[h] = x; return 1; }
|
||||||
|
if (K[h] == x) return 0;
|
||||||
|
h = (h + 1) & (CAP - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n; if (scanf("%d", &n) != 1) return 0;
|
||||||
|
CAP = 1 << 18;
|
||||||
|
K = (int *)malloc((size_t)CAP * sizeof(int));
|
||||||
|
for (int i = 0; i < CAP; ++i) K[i] = -1;
|
||||||
|
int *q = (int *)malloc(1024 * sizeof(int));
|
||||||
|
int hh = 0, tt = 0;
|
||||||
|
add(n); q[tt++] = n;
|
||||||
|
int cnt = 1;
|
||||||
|
while (hh < tt)
|
||||||
|
{
|
||||||
|
int x = q[hh++];
|
||||||
|
int t = x;
|
||||||
|
while (t >= 10) t /= 10;
|
||||||
|
int h = t;
|
||||||
|
int m = h / 2;
|
||||||
|
int d = 1, y = x;
|
||||||
|
while (y >= 10) { d *= 10; y /= 10; }
|
||||||
|
for (int k = 1; k <= m; ++k)
|
||||||
|
{
|
||||||
|
int v = k * d * 10 + x;
|
||||||
|
if (add(v)) { if (tt < 1024) q[tt++] = v; ++cnt; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("%d\n", cnt);
|
||||||
|
free(K); free(q);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int K, N, M;
|
||||||
|
if (scanf("%d %d %d", &K, &N, &M) != 3) return 0;
|
||||||
|
int total = K * N * M;
|
||||||
|
char *A = (char *)malloc((size_t)total);
|
||||||
|
for (int z = 0; z < K; ++z)
|
||||||
|
for (int i = 0; i < N; ++i)
|
||||||
|
for (int j = 0; j < M; ++j)
|
||||||
|
{
|
||||||
|
char c;
|
||||||
|
do { if (scanf(" %c", &c) != 1) return 0; } while (c != '.' && c != '#');
|
||||||
|
A[z * N * M + i * M + j] = c;
|
||||||
|
}
|
||||||
|
int sx, sy; if (scanf("%d %d", &sx, &sy) != 2) return 0;
|
||||||
|
int *q = (int *)malloc((size_t)(total + 5) * sizeof(int));
|
||||||
|
unsigned char *vis = (unsigned char *)malloc((size_t)total);
|
||||||
|
for (int i = 0; i < total; ++i) vis[i] = 0;
|
||||||
|
int hh = 0, tt = 0;
|
||||||
|
int sidx = 0 * N * M + (sx - 1) * M + (sy - 1);
|
||||||
|
q[tt++] = sidx; vis[sidx] = 1;
|
||||||
|
int infected = 1;
|
||||||
|
const int dz[6] = {1,-1,0,0,0,0};
|
||||||
|
const int dx[6] = {0,0,1,-1,0,0};
|
||||||
|
const int dy[6] = {0,0,0,0,1,-1};
|
||||||
|
while (hh < tt)
|
||||||
|
{
|
||||||
|
int idx = q[hh++];
|
||||||
|
int z = idx / (N * M);
|
||||||
|
int rem = idx % (N * M);
|
||||||
|
int x = rem / M;
|
||||||
|
int y = rem % M;
|
||||||
|
for (int d = 0; d < 6; ++d)
|
||||||
|
{
|
||||||
|
int nz = z + dz[d];
|
||||||
|
int nx = x + dx[d];
|
||||||
|
int ny = y + dy[d];
|
||||||
|
if (nz < 0 || nz >= K || nx < 0 || nx >= N || ny < 0 || ny >= M) continue;
|
||||||
|
int nidx = nz * N * M + nx * M + ny;
|
||||||
|
if (vis[nidx]) continue;
|
||||||
|
if (A[nidx] == '.')
|
||||||
|
{
|
||||||
|
vis[nidx] = 1;
|
||||||
|
q[tt++] = nidx;
|
||||||
|
++infected;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("%d\n", infected);
|
||||||
|
free(A); free(q); free(vis);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n; if (scanf("%d", &n) != 1) return 0;
|
||||||
|
int *a = (int *)malloc((size_t)n * (size_t)n * sizeof(int));
|
||||||
|
int sx = -1, sy = -1;
|
||||||
|
for (int i = 0; i < n; ++i)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < n; ++j)
|
||||||
|
{
|
||||||
|
int v; scanf("%d", &v);
|
||||||
|
a[i * n + j] = v;
|
||||||
|
if (v == 2) { sx = i; sy = j; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int *dist = (int *)malloc((size_t)n * (size_t)n * sizeof(int));
|
||||||
|
unsigned char *vis = (unsigned char *)malloc((size_t)n * (size_t)n);
|
||||||
|
for (int i = 0; i < n * n; ++i) { dist[i] = -1; vis[i] = 0; }
|
||||||
|
int *q = (int *)malloc(((size_t)n * (size_t)n + 5) * sizeof(int));
|
||||||
|
int hh = 0, tt = 0;
|
||||||
|
int sidx = sx * n + sy;
|
||||||
|
dist[sidx] = 0; vis[sidx] = 1; q[tt++] = sidx;
|
||||||
|
const int dx[4] = {1,-1,0,0};
|
||||||
|
const int dy[4] = {0,0,1,-1};
|
||||||
|
while (hh < tt)
|
||||||
|
{
|
||||||
|
int idx = q[hh++];
|
||||||
|
int x = idx / n; int y = idx % n;
|
||||||
|
for (int d = 0; d < 4; ++d)
|
||||||
|
{
|
||||||
|
int nx = x + dx[d]; int ny = y + dy[d];
|
||||||
|
if (nx < 0) nx = n - 1; else if (nx >= n) nx = 0;
|
||||||
|
if (ny < 0) ny = n - 1; else if (ny >= n) ny = 0;
|
||||||
|
int nidx = nx * n + ny;
|
||||||
|
if (vis[nidx]) continue;
|
||||||
|
if (a[nidx] == 0) continue;
|
||||||
|
vis[nidx] = 1;
|
||||||
|
dist[nidx] = dist[idx] + 1;
|
||||||
|
q[tt++] = nidx;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = 0; i < n; ++i)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < n; ++j)
|
||||||
|
{
|
||||||
|
int idx = i * n + j;
|
||||||
|
int out = (dist[idx] <= 0) ? 0 : dist[idx];
|
||||||
|
if (j) putchar(' ');
|
||||||
|
printf("%d", out);
|
||||||
|
}
|
||||||
|
putchar('\n');
|
||||||
|
}
|
||||||
|
free(a); free(dist); free(vis); free(q);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
static int len0;
|
||||||
|
|
||||||
|
static int get_len(int x){ int c = 0; do { ++c; x /= 10; } while (x); return c; }
|
||||||
|
|
||||||
|
static int has_zero(int x){ while (x){ if (x % 10 == 0) return 1; x /= 10; } return 0; }
|
||||||
|
|
||||||
|
static void to_digits(int x, int *d, int *L){ int t = x; int l = get_len(x); *L = l; for (int i = l - 1; i >= 0; --i){ d[i] = t % 10; t /= 10; } }
|
||||||
|
|
||||||
|
static int from_digits(int *d, int L){ int v = 0; for (int i = 0; i < L; ++i) v = v * 10 + d[i]; return v; }
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int s; if (scanf("%d", &s) != 1) return 0;
|
||||||
|
len0 = get_len(s);
|
||||||
|
int m; scanf("%d", &m);
|
||||||
|
int *dist = (int *)malloc(100000 * sizeof(int));
|
||||||
|
for (int i = 0; i < 100000; ++i) dist[i] = -1;
|
||||||
|
int *q = (int *)malloc(70000 * sizeof(int));
|
||||||
|
int hh = 0, tt = 0;
|
||||||
|
dist[s] = 0; q[tt++] = s;
|
||||||
|
int d[6], tmp[6];
|
||||||
|
while (hh < tt)
|
||||||
|
{
|
||||||
|
int x = q[hh++];
|
||||||
|
int L; to_digits(x, d, &L);
|
||||||
|
for (int i = 0; i < L; ++i)
|
||||||
|
{
|
||||||
|
for (int j = i + 1; j < L; ++j)
|
||||||
|
{
|
||||||
|
for (int k = 0; k < L; ++k) tmp[k] = d[k];
|
||||||
|
int t = tmp[i]; tmp[i] = tmp[j]; tmp[j] = t;
|
||||||
|
int y = from_digits(tmp, L);
|
||||||
|
if (dist[y] == -1){ dist[y] = dist[x] + 1; q[tt++] = y; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (L >= 2)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < L; ++i)
|
||||||
|
{
|
||||||
|
int p = 0; for (int k = 0; k < L; ++k){ if (k == i) continue; tmp[p++] = d[k]; }
|
||||||
|
int y = from_digits(tmp, L - 1);
|
||||||
|
if (dist[y] == -1){ dist[y] = dist[x] + 1; q[tt++] = y; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (L + 1 <= len0)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < L - 1; ++i)
|
||||||
|
{
|
||||||
|
if (d[i] < d[i + 1])
|
||||||
|
{
|
||||||
|
for (int xdig = d[i] + 1; xdig <= d[i + 1] - 1; ++xdig)
|
||||||
|
{
|
||||||
|
int p = 0; for (int k = 0; k <= i; ++k) tmp[p++] = d[k]; tmp[p++] = xdig; for (int k = i + 1; k < L; ++k) tmp[p++] = d[k];
|
||||||
|
int y = from_digits(tmp, L + 1);
|
||||||
|
if (dist[y] == -1){ dist[y] = dist[x] + 1; q[tt++] = y; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = 0; i < m; ++i)
|
||||||
|
{
|
||||||
|
int t; scanf("%d", &t);
|
||||||
|
if (get_len(t) > len0 || has_zero(t)) { printf("-1\n"); continue; }
|
||||||
|
printf("%d\n", dist[t]);
|
||||||
|
}
|
||||||
|
free(dist); free(q);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n, k;
|
||||||
|
if (scanf("%d %d", &n, &k) != 2)
|
||||||
|
return 0;
|
||||||
|
char *t = (char *)malloc((size_t)n + 5);
|
||||||
|
scanf("%s", t);
|
||||||
|
int *pi = (int *)malloc(((size_t)n) * sizeof(int));
|
||||||
|
pi[0] = 0;
|
||||||
|
for (int i = 1; i < n; ++i)
|
||||||
|
{
|
||||||
|
int j = pi[i - 1];
|
||||||
|
while (j > 0 && t[i] != t[j])
|
||||||
|
j = pi[j - 1];
|
||||||
|
if (t[i] == t[j])
|
||||||
|
++j;
|
||||||
|
pi[i] = j;
|
||||||
|
}
|
||||||
|
int L = pi[n - 1];
|
||||||
|
int add = n - L;
|
||||||
|
size_t total = (size_t)n + (size_t)(k - 1) * (size_t)add;
|
||||||
|
char *s = (char *)malloc(total + 1);
|
||||||
|
memcpy(s, t, (size_t)n);
|
||||||
|
size_t pos = (size_t)n;
|
||||||
|
for (int i = 1; i < k; ++i)
|
||||||
|
{
|
||||||
|
memcpy(s + pos, t + L, (size_t)add);
|
||||||
|
pos += (size_t)add;
|
||||||
|
}
|
||||||
|
s[total] = '\0';
|
||||||
|
printf("%s\n", s);
|
||||||
|
free(t);
|
||||||
|
free(pi);
|
||||||
|
free(s);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
char *s = (char *)malloc(1000005);
|
||||||
|
if (scanf("%1000000s", s) != 1) return 0;
|
||||||
|
int n = (int)strlen(s);
|
||||||
|
int *pi = (int *)malloc((size_t)n * sizeof(int));
|
||||||
|
pi[0] = 0;
|
||||||
|
for (int i = 1; i < n; ++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;
|
||||||
|
}
|
||||||
|
int L = pi[n - 1];
|
||||||
|
int mx = 0;
|
||||||
|
for (int i = 0; i < n - 1; ++i) if (pi[i] > mx) mx = pi[i];
|
||||||
|
while (L > 0 && mx < L) L = pi[L - 1];
|
||||||
|
if (L == 0)
|
||||||
|
{
|
||||||
|
printf("Just a legend\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
s[L] = '\0';
|
||||||
|
printf("%s\n", s);
|
||||||
|
}
|
||||||
|
free(pi); free(s);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
// 二叉树先序遍历(给出每个结点的左右孩子编号)
|
||||||
|
// 输入:n,随后 n 行,每行两个整数 l、r 表示结点 i 的左右孩子编号(0 表示空)
|
||||||
|
// 根节点固定为 1。输出:先序遍历序列(每个数字后均有一个空格)。
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
int n;
|
||||||
|
if (scanf("%d", &n) != 1) return 0;
|
||||||
|
|
||||||
|
int *L = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
int *R = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
if (!L || !R) return 0;
|
||||||
|
|
||||||
|
for (int i = 1; i <= n; ++i) {
|
||||||
|
int l, r;
|
||||||
|
if (scanf("%d %d", &l, &r) != 2) {
|
||||||
|
free(L); free(R);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
L[i] = l; R[i] = r;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 迭代版先序遍历,避免最坏情况下递归栈溢出(n 可达 1e5)
|
||||||
|
int *stack = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
int top = 0;
|
||||||
|
stack[top++] = 1; // 根节点编号为 1
|
||||||
|
|
||||||
|
while (top) {
|
||||||
|
int u = stack[--top];
|
||||||
|
if (u == 0) continue;
|
||||||
|
printf("%d ", u);
|
||||||
|
// 先压右再压左,保证左子树先被访问
|
||||||
|
if (R[u]) stack[top++] = R[u];
|
||||||
|
if (L[u]) stack[top++] = L[u];
|
||||||
|
}
|
||||||
|
|
||||||
|
free(stack);
|
||||||
|
free(L); free(R);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n, m;
|
||||||
|
if (scanf("%d %d", &n, &m) != 2) return 0;
|
||||||
|
int *nxt = (int *)malloc(((size_t)n + 1) * sizeof(int));
|
||||||
|
int *vis = (int *)malloc(((size_t)n + 1) * sizeof(int));
|
||||||
|
for (int i = 1; i <= n; ++i) { nxt[i] = 0; vis[i] = 0; }
|
||||||
|
int stamp = 1;
|
||||||
|
for (int i = 0; i < m; ++i)
|
||||||
|
{
|
||||||
|
char op;
|
||||||
|
int x, y;
|
||||||
|
if (scanf(" %c %d %d", &op, &x, &y) != 3) return 0;
|
||||||
|
if (op == 'A')
|
||||||
|
{
|
||||||
|
nxt[x] = y;
|
||||||
|
if (nxt[y] == x) nxt[y] = 0;
|
||||||
|
}
|
||||||
|
else if (op == 'Q')
|
||||||
|
{
|
||||||
|
++stamp;
|
||||||
|
int v = x;
|
||||||
|
while (v && vis[v] != stamp)
|
||||||
|
{
|
||||||
|
vis[v] = stamp;
|
||||||
|
if (v == y) break;
|
||||||
|
v = nxt[v];
|
||||||
|
}
|
||||||
|
if (v == y) puts("Yes"); else puts("No");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(nxt); free(vis);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n, q;
|
||||||
|
if (scanf("%d %d", &n, &q) != 2) return 0;
|
||||||
|
int *next = (int *)malloc(((size_t)n + 1) * sizeof(int));
|
||||||
|
for (int i = 1; i <= n; ++i) next[i] = (i == n) ? 1 : (i + 1);
|
||||||
|
int cur = 1, prev = n;
|
||||||
|
while (next[cur] != cur)
|
||||||
|
{
|
||||||
|
for (int c = 1; c < q; ++c)
|
||||||
|
{
|
||||||
|
prev = cur;
|
||||||
|
cur = next[cur];
|
||||||
|
}
|
||||||
|
next[prev] = next[cur];
|
||||||
|
cur = next[cur];
|
||||||
|
}
|
||||||
|
printf("%d\n", cur);
|
||||||
|
free(next);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
typedef struct Node {
|
||||||
|
int id;
|
||||||
|
char name[32];
|
||||||
|
char gender[4];
|
||||||
|
char birth[16];
|
||||||
|
int zip;
|
||||||
|
char addr[64];
|
||||||
|
long long qq;
|
||||||
|
long long phone;
|
||||||
|
struct Node *next;
|
||||||
|
} Node;
|
||||||
|
|
||||||
|
static void read_rec(Node *p)
|
||||||
|
{
|
||||||
|
char g[8];
|
||||||
|
scanf("%d %31s %7s %15s %d %63s %lld %lld", &p->id, p->name, g, p->birth, &p->zip, p->addr, &p->qq, &p->phone);
|
||||||
|
strcpy(p->gender, g);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void print_rec(Node *p)
|
||||||
|
{
|
||||||
|
printf("%d %s %s %s %d %s %lld %lld\n", p->id, p->name, p->gender, p->birth, p->zip, p->addr, p->qq, p->phone);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int cnt; if (scanf("%d", &cnt) != 1) return 0;
|
||||||
|
Node *head = NULL, *tail = NULL;
|
||||||
|
for (int i = 0; i < cnt; ++i)
|
||||||
|
{
|
||||||
|
Node *p = (Node *)malloc(sizeof(Node));
|
||||||
|
read_rec(p); p->next = NULL;
|
||||||
|
if (!head) head = tail = p; else { tail->next = p; tail = p; }
|
||||||
|
}
|
||||||
|
int op; scanf("%d", &op);
|
||||||
|
if (op == 1)
|
||||||
|
{
|
||||||
|
Node *p = (Node *)malloc(sizeof(Node));
|
||||||
|
read_rec(p); p->next = NULL;
|
||||||
|
if (!head) head = tail = p; else { tail->next = p; tail = p; }
|
||||||
|
printf("The records is:\n");
|
||||||
|
int sz = 0; for (Node *q = head; q; q = q->next) ++sz;
|
||||||
|
Node **arr = (Node **)malloc((size_t)sz * sizeof(Node *));
|
||||||
|
int idx = 0; for (Node *q = head; q; q = q->next) arr[idx++] = q;
|
||||||
|
for (int i = sz - 1; i >= 0; --i) print_rec(arr[i]);
|
||||||
|
free(arr);
|
||||||
|
}
|
||||||
|
else if (op == 2)
|
||||||
|
{
|
||||||
|
int did; scanf("%d", &did);
|
||||||
|
Node *prev = NULL, *cur = head;
|
||||||
|
while (cur && cur->id != did) { prev = cur; cur = cur->next; }
|
||||||
|
if (cur)
|
||||||
|
{
|
||||||
|
if (prev) prev->next = cur->next; else head = cur->next;
|
||||||
|
if (tail == cur) tail = prev;
|
||||||
|
free(cur);
|
||||||
|
}
|
||||||
|
printf("The records is:\n");
|
||||||
|
int sz = 0; for (Node *q = head; q; q = q->next) ++sz;
|
||||||
|
Node **arr = (Node **)malloc((size_t)sz * sizeof(Node *));
|
||||||
|
int idx = 0; for (Node *q = head; q; q = q->next) arr[idx++] = q;
|
||||||
|
for (int i = sz - 1; i >= 0; --i) print_rec(arr[i]);
|
||||||
|
free(arr);
|
||||||
|
}
|
||||||
|
else if (op == 3)
|
||||||
|
{
|
||||||
|
int fid; scanf("%d", &fid);
|
||||||
|
Node *cur = head;
|
||||||
|
while (cur && cur->id != fid) cur = cur->next;
|
||||||
|
if (cur) print_rec(cur); else printf("not found\n");
|
||||||
|
}
|
||||||
|
else if (op == 4)
|
||||||
|
{
|
||||||
|
printf("The records is:\n");
|
||||||
|
int sz = 0; for (Node *q = head; q; q = q->next) ++sz;
|
||||||
|
Node **arr = (Node **)malloc((size_t)sz * sizeof(Node *));
|
||||||
|
int idx = 0; for (Node *q = head; q; q = q->next) arr[idx++] = q;
|
||||||
|
for (int i = sz - 1; i >= 0; --i) print_rec(arr[i]);
|
||||||
|
free(arr);
|
||||||
|
}
|
||||||
|
for (Node *q = head; q; ) { Node *nq = q->next; free(q); q = nq; }
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef struct { int c; int e; } T;
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n, m;
|
||||||
|
if (scanf("%d %d", &n, &m) != 2) return 0;
|
||||||
|
T *A = (T *)malloc((size_t)n * sizeof(T));
|
||||||
|
T *B = (T *)malloc((size_t)m * sizeof(T));
|
||||||
|
for (int i = 0; i < n; ++i) scanf("%d %d", &A[i].c, &A[i].e);
|
||||||
|
for (int i = 0; i < m; ++i) scanf("%d %d", &B[i].c, &B[i].e);
|
||||||
|
int i = 0, j = 0;
|
||||||
|
while (i < n && j < m)
|
||||||
|
{
|
||||||
|
if (A[i].e == B[j].e)
|
||||||
|
{
|
||||||
|
int c = A[i].c + B[j].c;
|
||||||
|
if (c != 0) printf("%d %d\n", c, A[i].e);
|
||||||
|
++i; ++j;
|
||||||
|
}
|
||||||
|
else if (A[i].e < B[j].e)
|
||||||
|
{
|
||||||
|
if (A[i].c != 0) printf("%d %d\n", A[i].c, A[i].e);
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (B[j].c != 0) printf("%d %d\n", B[j].c, B[j].e);
|
||||||
|
++j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (i < n) { if (A[i].c != 0) printf("%d %d\n", A[i].c, A[i].e); ++i; }
|
||||||
|
while (j < m) { if (B[j].c != 0) printf("%d %d\n", B[j].c, B[j].e); ++j; }
|
||||||
|
free(A); free(B);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
static uint64_t getv(FILE *f, int idx)
|
||||||
|
{
|
||||||
|
uint64_t v;
|
||||||
|
long off = (long)idx * (long)sizeof(uint64_t);
|
||||||
|
fseek(f, off, SEEK_SET);
|
||||||
|
fread(&v, sizeof(uint64_t), 1, f);
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n; if (scanf("%d", &n) != 1) return 0;
|
||||||
|
FILE *f = tmpfile();
|
||||||
|
if (!f) return 0;
|
||||||
|
for (int i = 0; i < n; ++i)
|
||||||
|
{
|
||||||
|
unsigned long long x; scanf("%llu", &x);
|
||||||
|
fwrite(&x, sizeof(uint64_t), 1, f);
|
||||||
|
}
|
||||||
|
int *st = (int *)malloc((size_t)n * sizeof(int));
|
||||||
|
int top = 0;
|
||||||
|
long long ans = 0;
|
||||||
|
size_t B = 1 << 20;
|
||||||
|
uint64_t *buf = (uint64_t *)malloc(B * sizeof(uint64_t));
|
||||||
|
long long pos = (long long)n - 1;
|
||||||
|
while (pos >= 0)
|
||||||
|
{
|
||||||
|
size_t take = (pos + 1 < (long long)B) ? (size_t)(pos + 1) : B;
|
||||||
|
long long start = pos - (long long)take + 1;
|
||||||
|
long off = (long)(start * (long long)sizeof(uint64_t));
|
||||||
|
fseek(f, off, SEEK_SET);
|
||||||
|
fread(buf, sizeof(uint64_t), take, f);
|
||||||
|
for (long long p = (long long)take - 1; p >= 0; --p)
|
||||||
|
{
|
||||||
|
int i = (int)(start + p);
|
||||||
|
uint64_t h = buf[p];
|
||||||
|
while (top > 0)
|
||||||
|
{
|
||||||
|
uint64_t t = getv(f, st[top - 1]);
|
||||||
|
if (t < h) --top; else break;
|
||||||
|
}
|
||||||
|
if (top > 0) ans += (long long)(st[top - 1] - i - 1);
|
||||||
|
else ans += (long long)(n - 1 - i);
|
||||||
|
st[top++] = i;
|
||||||
|
}
|
||||||
|
pos = start - 1;
|
||||||
|
}
|
||||||
|
printf("%lld\n", ans);
|
||||||
|
free(st); free(buf); fclose(f);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n; if (scanf("%d", &n) != 1) return 0;
|
||||||
|
long long *st = (long long *)malloc((size_t)n * sizeof(long long));
|
||||||
|
int top = 0;
|
||||||
|
long long ans = 0;
|
||||||
|
for (int i = 0; i < n; ++i)
|
||||||
|
{
|
||||||
|
long long d, h; scanf("%lld %lld", &d, &h);
|
||||||
|
while (top > 0 && st[top - 1] > h) --top;
|
||||||
|
if (h > 0 && (top == 0 || st[top - 1] < h)) { st[top++] = h; ++ans; }
|
||||||
|
}
|
||||||
|
printf("%lld\n", ans);
|
||||||
|
free(st);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
static const long long MOD = 99887765LL;
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n; long long x;
|
||||||
|
if (scanf("%d %lld", &n, &x) != 2) return 0;
|
||||||
|
int *a = (int *)malloc(((size_t)n + 1) * sizeof(int));
|
||||||
|
for (int i = 0; i <= n; ++i) scanf("%d", &a[i]);
|
||||||
|
int *st = (int *)malloc(((size_t)n + 1) * sizeof(int));
|
||||||
|
int top = 0;
|
||||||
|
long long ans = 0;
|
||||||
|
long long xmod = x % MOD; if (xmod < 0) xmod += MOD;
|
||||||
|
long long pw = 1;
|
||||||
|
for (int i = n; i >= 0; --i)
|
||||||
|
{
|
||||||
|
while (top > 0 && a[st[top - 1]] >= a[i]) --top;
|
||||||
|
int bi = (top > 0) ? a[st[top - 1]] : 0;
|
||||||
|
long long bmi = bi % MOD; if (bmi < 0) bmi += MOD;
|
||||||
|
ans += (bmi * pw) % MOD;
|
||||||
|
ans %= MOD;
|
||||||
|
if (i > 0) { pw = (pw * xmod) % MOD; }
|
||||||
|
st[top++] = i;
|
||||||
|
}
|
||||||
|
if (ans < 0) ans += MOD;
|
||||||
|
printf("%lld\n", ans);
|
||||||
|
free(a); free(st);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n, m;
|
||||||
|
if (scanf("%d %d", &n, &m) != 2) return 0;
|
||||||
|
if (m == 0) { printf("0\n"); return 0; }
|
||||||
|
long long *a = (long long *)malloc((size_t)n * sizeof(long long));
|
||||||
|
for (int i = 0; i < n; ++i) scanf("%lld", &a[i]);
|
||||||
|
long long *p = (long long *)malloc(((size_t)n + 1) * sizeof(long long));
|
||||||
|
p[0] = 0;
|
||||||
|
for (int i = 1; i <= n; ++i) p[i] = p[i - 1] + a[i - 1];
|
||||||
|
int *dq = (int *)malloc(((size_t)n + 1) * sizeof(int));
|
||||||
|
int head = 0, tail = 0;
|
||||||
|
dq[tail++] = 0;
|
||||||
|
long long ans = p[1] - p[0];
|
||||||
|
for (int i = 1; i <= n; ++i)
|
||||||
|
{
|
||||||
|
while (head < tail && dq[head] < i - m) ++head;
|
||||||
|
if (head < tail)
|
||||||
|
{
|
||||||
|
long long val = p[i] - p[dq[head]];
|
||||||
|
if (val > ans) ans = val;
|
||||||
|
}
|
||||||
|
while (head < tail && p[dq[tail - 1]] >= p[i]) --tail;
|
||||||
|
dq[tail++] = i;
|
||||||
|
}
|
||||||
|
printf("%lld\n", ans);
|
||||||
|
free(a); free(p); free(dq);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef struct { long long x, y; } P;
|
||||||
|
|
||||||
|
static int cmpx(const void *a, const void *b){ const P *pa=a, *pb=b; if (pa->x<pb->x) return -1; if (pa->x>pb->x) return 1; return 0; }
|
||||||
|
|
||||||
|
static int ok(P *p, int n, long long W, long long D)
|
||||||
|
{
|
||||||
|
int *qmax = (int *)malloc((size_t)n * sizeof(int));
|
||||||
|
int *qmin = (int *)malloc((size_t)n * sizeof(int));
|
||||||
|
int hmax=0,tmax=0,hmin=0,tmin=0;
|
||||||
|
int l=0;
|
||||||
|
for (int r=0;r<n;++r)
|
||||||
|
{
|
||||||
|
while (tmax>hmax && p[qmax[tmax-1]].y<=p[r].y) --tmax;
|
||||||
|
qmax[tmax++]=r;
|
||||||
|
while (tmin>hmin && p[qmin[tmin-1]].y>=p[r].y) --tmin;
|
||||||
|
qmin[tmin++]=r;
|
||||||
|
while (p[r].x - p[l].x > W)
|
||||||
|
{
|
||||||
|
if (hmax<tmax && qmax[hmax]==l) ++hmax;
|
||||||
|
if (hmin<tmin && qmin[hmin]==l) ++hmin;
|
||||||
|
++l;
|
||||||
|
}
|
||||||
|
if (tmax>hmax && tmin>hmin)
|
||||||
|
{
|
||||||
|
long long mx = p[qmax[hmax]].y;
|
||||||
|
long long mn = p[qmin[hmin]].y;
|
||||||
|
if (mx - mn >= D) { free(qmax); free(qmin); return 1; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(qmax); free(qmin); return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int N; long long D;
|
||||||
|
if (scanf("%d %lld", &N, &D) != 2) return 0;
|
||||||
|
P *p = (P *)malloc((size_t)N * sizeof(P));
|
||||||
|
for (int i=0;i<N;++i) scanf("%lld %lld", &p[i].x, &p[i].y);
|
||||||
|
qsort(p, N, sizeof(P), cmpx);
|
||||||
|
long long L=0, R=0;
|
||||||
|
if (N>0) R = p[N-1].x - p[0].x;
|
||||||
|
long long ans=-1;
|
||||||
|
long long lo=L, hi=R;
|
||||||
|
while (lo<=hi)
|
||||||
|
{
|
||||||
|
long long mid = lo + ((hi - lo) >> 1);
|
||||||
|
if (ok(p, N, mid, D)) { ans = mid; hi = mid - 1; }
|
||||||
|
else lo = mid + 1;
|
||||||
|
}
|
||||||
|
printf("%lld\n", ans);
|
||||||
|
free(p);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,93 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef long long LL;
|
||||||
|
|
||||||
|
static LL *low, *up; // max-heap(low), min-heap(up)
|
||||||
|
static int szLow, szUp;
|
||||||
|
static LL *qarr; static int qh, qt;
|
||||||
|
|
||||||
|
static LL *delKeys; static unsigned char *delUsed; static int DEL_CAP;
|
||||||
|
|
||||||
|
static unsigned int h64(LL x){ unsigned long long u = (unsigned long long)x; u ^= u >> 33; u *= 0xff51afd7ed558ccdULL; u ^= u >> 33; u *= 0xc4ceb9fe1a85aa39ULL; u ^= u >> 33; return (unsigned int)u; }
|
||||||
|
|
||||||
|
static void delInit(void){ DEL_CAP = 1<<20; delKeys = (LL*)malloc((size_t)DEL_CAP * sizeof(LL)); delUsed = (unsigned char*)malloc((size_t)DEL_CAP); for (int i=0;i<DEL_CAP;++i) delUsed[i]=0; }
|
||||||
|
static void delClear(void){ for (int i=0;i<DEL_CAP;++i) delUsed[i]=0; }
|
||||||
|
static void delMark(LL x){ unsigned int m = DEL_CAP-1; unsigned int h = h64(x) & m; while (delUsed[h] && delKeys[h]!=x){ h=(h+1)&m; } delKeys[h]=x; delUsed[h]=1; }
|
||||||
|
static int delHas(LL x){ unsigned int m = DEL_CAP-1; unsigned int h = h64(x) & m; while (delUsed[h]){ if (delKeys[h]==x) return 1; h=(h+1)&m; } return 0; }
|
||||||
|
|
||||||
|
static void maxPush(LL *a, int *n, LL v){ int i=*n; a[i]=v; (*n)++; while (i>0){ int p=(i-1)/2; if (a[p] >= a[i]) break; LL t=a[p]; a[p]=a[i]; a[i]=t; i=p; } }
|
||||||
|
static LL maxTop(LL *a, int n){ return n? a[0] : 0; }
|
||||||
|
static void maxPop(LL *a, int *n){ int s=*n; if (s==0) return; a[0]=a[s-1]; (*n)--; s=*n; int i=0; while (1){ int l=2*i+1, r=2*i+2, b=i; if (l<s && a[l]>a[b]) b=l; if (r<s && a[r]>a[b]) b=r; if (b==i) break; LL t=a[i]; a[i]=a[b]; a[b]=t; i=b; } }
|
||||||
|
|
||||||
|
static void minPush(LL *a, int *n, LL v){ int i=*n; a[i]=v; (*n)++; while (i>0){ int p=(i-1)/2; if (a[p] <= a[i]) break; LL t=a[p]; a[p]=a[i]; a[i]=t; i=p; } }
|
||||||
|
static LL minTop(LL *a, int n){ return n? a[0] : 0; }
|
||||||
|
static void minPop(LL *a, int *n){ int s=*n; if (s==0) return; a[0]=a[s-1]; (*n)--; s=*n; int i=0; while (1){ int l=2*i+1, r=2*i+2, b=i; if (l<s && a[l]<a[b]) b=l; if (r<s && a[r]<a[b]) b=r; if (b==i) break; LL t=a[i]; a[i]=a[b]; a[b]=t; i=b; } }
|
||||||
|
|
||||||
|
static void pruneLow(void){ while (szLow>0){ LL v = maxTop(low, szLow); if (!delHas(v)) break; maxPop(low, &szLow); }
|
||||||
|
}
|
||||||
|
static void pruneUp(void){ while (szUp>0){ LL v = minTop(up, szUp); if (!delHas(v)) break; minPop(up, &szUp); }
|
||||||
|
}
|
||||||
|
|
||||||
|
static int mcount(void){ return szLow + szUp; }
|
||||||
|
static int targetK(void){ int m = mcount(); return m/2 + 1; }
|
||||||
|
|
||||||
|
static void rebalance(void)
|
||||||
|
{
|
||||||
|
pruneLow(); pruneUp();
|
||||||
|
int k = targetK();
|
||||||
|
while (szLow > 0 && szLow > k){ LL v = maxTop(low, szLow); maxPop(low, &szLow); minPush(up, &szUp, v); pruneLow(); }
|
||||||
|
while (szUp > 0 && szLow < k){ LL v = minTop(up, szUp); minPop(up, &szUp); maxPush(low, &szLow, v); pruneUp(); }
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
delInit();
|
||||||
|
int n; int cas=1; while (scanf("%d", &n)==1)
|
||||||
|
{
|
||||||
|
int cap = n + 5;
|
||||||
|
low = (LL*)malloc((size_t)cap * sizeof(LL));
|
||||||
|
up = (LL*)malloc((size_t)cap * sizeof(LL));
|
||||||
|
qarr= (LL*)malloc((size_t)cap * sizeof(LL));
|
||||||
|
szLow=szUp=0; qh=0; qt=0;
|
||||||
|
delClear();
|
||||||
|
printf("Case #%d:\n", cas++);
|
||||||
|
for (int i=0;i<n;++i)
|
||||||
|
{
|
||||||
|
char op[16];
|
||||||
|
if (scanf("%15s", op)!=1) return 0;
|
||||||
|
if (op[0]=='i')
|
||||||
|
{
|
||||||
|
LL x; scanf("%lld", &x);
|
||||||
|
qarr[qt++]=x;
|
||||||
|
pruneLow();
|
||||||
|
if (szLow==0){ maxPush(low, &szLow, x); }
|
||||||
|
else{
|
||||||
|
LL med = maxTop(low, szLow);
|
||||||
|
if (x <= med) maxPush(low, &szLow, x);
|
||||||
|
else minPush(up, &szUp, x);
|
||||||
|
}
|
||||||
|
rebalance();
|
||||||
|
}
|
||||||
|
else if (op[0]=='o')
|
||||||
|
{
|
||||||
|
LL v = qarr[qh++];
|
||||||
|
pruneLow();
|
||||||
|
LL med = szLow? maxTop(low, szLow) : (LL)0;
|
||||||
|
delMark(v);
|
||||||
|
if (szLow>0 && v <= med) { szLow--; }
|
||||||
|
else { szUp--; }
|
||||||
|
rebalance();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
pruneLow();
|
||||||
|
LL med = maxTop(low, szLow);
|
||||||
|
printf("%lld\n", med);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(low); free(up); free(qarr);
|
||||||
|
}
|
||||||
|
free(delKeys); free(delUsed);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
// 二叉树中序遍历(给出每个结点的左右孩子编号,根为1)
|
||||||
|
// 输入:n,随后 n 行,每行两个整数 l、r(0 表示空)。
|
||||||
|
// 输出:中序遍历序列,每个数字后均输出一个空格。
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
int n;
|
||||||
|
if (scanf("%d", &n) != 1) return 0;
|
||||||
|
|
||||||
|
int *L = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
int *R = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
if (!L || !R) return 0;
|
||||||
|
|
||||||
|
for (int i = 1; i <= n; ++i) {
|
||||||
|
int l, r;
|
||||||
|
if (scanf("%d %d", &l, &r) != 2) {
|
||||||
|
free(L); free(R);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
L[i] = l; R[i] = r;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 迭代式中序遍历:一路向左压栈,弹栈访问,再转向右子树
|
||||||
|
int *stack = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
int top = 0;
|
||||||
|
int cur = 1; // 根节点编号为 1
|
||||||
|
|
||||||
|
while (cur || top) {
|
||||||
|
while (cur) { // 沿左链入栈
|
||||||
|
stack[top++] = cur;
|
||||||
|
cur = L[cur];
|
||||||
|
}
|
||||||
|
int u = stack[--top];
|
||||||
|
printf("%d ", u);
|
||||||
|
cur = R[u];
|
||||||
|
}
|
||||||
|
|
||||||
|
free(stack);
|
||||||
|
free(L); free(R);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n; if (scanf("%d", &n) != 1) return 0;
|
||||||
|
long long *a = (long long *)malloc(((size_t)n + 1) * sizeof(long long));
|
||||||
|
for (int i = 1; i <= n; ++i) scanf("%lld", &a[i]);
|
||||||
|
int *L = (int *)malloc(((size_t)n + 1) * sizeof(int));
|
||||||
|
int *R = (int *)malloc(((size_t)n + 1) * sizeof(int));
|
||||||
|
for (int i = 1; i <= n; ++i) { int l, r; scanf("%d %d", &l, &r); L[i] = l; R[i] = r; }
|
||||||
|
int *stk = (int *)malloc(((size_t)n + 5) * sizeof(int));
|
||||||
|
int top = 0;
|
||||||
|
int cur = 1;
|
||||||
|
int cnt = 0;
|
||||||
|
int hasPrev = 0; long long prev = 0;
|
||||||
|
while (cur != 0 || top > 0)
|
||||||
|
{
|
||||||
|
while (cur != 0)
|
||||||
|
{
|
||||||
|
stk[top++] = cur;
|
||||||
|
cur = L[cur];
|
||||||
|
}
|
||||||
|
cur = stk[--top];
|
||||||
|
if (!hasPrev) { prev = a[cur]; hasPrev = 1; }
|
||||||
|
else { if (a[cur] <= prev) { printf("No\n"); free(a); free(L); free(R); free(stk); return 0; } prev = a[cur]; }
|
||||||
|
++cnt;
|
||||||
|
cur = R[cur];
|
||||||
|
}
|
||||||
|
if (cnt != n) { printf("No\n"); }
|
||||||
|
else { printf("Yes\n"); }
|
||||||
|
free(a); free(L); free(R); free(stk);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
static void minPush(long long *h, int *sz, long long v)
|
||||||
|
{
|
||||||
|
int i = (*sz);
|
||||||
|
h[i] = v; (*sz)++;
|
||||||
|
while (i > 0)
|
||||||
|
{
|
||||||
|
int p = (i - 1) >> 1;
|
||||||
|
if (h[p] <= h[i]) break;
|
||||||
|
long long t = h[p]; h[p] = h[i]; h[i] = t; i = p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static long long minPop(long long *h, int *sz)
|
||||||
|
{
|
||||||
|
long long r = h[0];
|
||||||
|
int s = --(*sz);
|
||||||
|
h[0] = h[s];
|
||||||
|
int i = 0;
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
int l = (i << 1) | 1, rgt = l + 1, b = i;
|
||||||
|
if (l < s && h[l] < h[b]) b = l;
|
||||||
|
if (rgt < s && h[rgt] < h[b]) b = rgt;
|
||||||
|
if (b == i) break;
|
||||||
|
long long t = h[i]; h[i] = h[b]; h[b] = t; i = b;
|
||||||
|
}
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n; if (scanf("%d", &n) != 1) return 0;
|
||||||
|
long long *h = (long long *)malloc((size_t)n * sizeof(long long));
|
||||||
|
int sz = 0;
|
||||||
|
for (int i = 0; i < n; ++i)
|
||||||
|
{
|
||||||
|
long long x; scanf("%lld", &x);
|
||||||
|
minPush(h, &sz, x);
|
||||||
|
}
|
||||||
|
long long ans = 0;
|
||||||
|
if (sz == 0) { printf("0\n"); free(h); return 0; }
|
||||||
|
while (sz > 1)
|
||||||
|
{
|
||||||
|
long long a = minPop(h, &sz);
|
||||||
|
long long b = minPop(h, &sz);
|
||||||
|
long long c = a + b;
|
||||||
|
ans += c;
|
||||||
|
minPush(h, &sz, c);
|
||||||
|
}
|
||||||
|
printf("%lld\n", ans);
|
||||||
|
free(h);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
static void minPush(long long *h, int *sz, long long v)
|
||||||
|
{
|
||||||
|
int i = (*sz);
|
||||||
|
h[i] = v; (*sz)++;
|
||||||
|
while (i > 0)
|
||||||
|
{
|
||||||
|
int p = (i - 1) >> 1;
|
||||||
|
if (h[p] <= h[i]) break;
|
||||||
|
long long t = h[p]; h[p] = h[i]; h[i] = t; i = p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static long long minPop(long long *h, int *sz)
|
||||||
|
{
|
||||||
|
long long r = h[0];
|
||||||
|
int s = --(*sz);
|
||||||
|
h[0] = h[s];
|
||||||
|
int i = 0;
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
int l = (i << 1) | 1, rgt = l + 1, b = i;
|
||||||
|
if (l < s && h[l] < h[b]) b = l;
|
||||||
|
if (rgt < s && h[rgt] < h[b]) b = rgt;
|
||||||
|
if (b == i) break;
|
||||||
|
long long t = h[i]; h[i] = h[b]; h[b] = t; i = b;
|
||||||
|
}
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
char *s = (char *)malloc(200005);
|
||||||
|
if (scanf("%200000s", s) != 1) return 0;
|
||||||
|
long long cnt[26] = {0};
|
||||||
|
for (char *p = s; *p; ++p) cnt[*p - 'A']++;
|
||||||
|
int kinds = 0;
|
||||||
|
for (int i = 0; i < 26; ++i) if (cnt[i] > 0) ++kinds;
|
||||||
|
if (kinds == 0) { printf("0\n"); free(s); return 0; }
|
||||||
|
long long *h = (long long *)malloc((size_t)kinds * sizeof(long long));
|
||||||
|
int sz = 0;
|
||||||
|
for (int i = 0; i < 26; ++i) if (cnt[i] > 0) minPush(h, &sz, cnt[i]);
|
||||||
|
if (sz == 1)
|
||||||
|
{
|
||||||
|
long long ans = h[0];
|
||||||
|
printf("%lld\n", ans);
|
||||||
|
free(h); free(s); return 0;
|
||||||
|
}
|
||||||
|
long long ans = 0;
|
||||||
|
while (sz > 1)
|
||||||
|
{
|
||||||
|
long long a = minPop(h, &sz);
|
||||||
|
long long b = minPop(h, &sz);
|
||||||
|
long long c = a + b;
|
||||||
|
ans += c;
|
||||||
|
minPush(h, &sz, c);
|
||||||
|
}
|
||||||
|
printf("%lld\n", ans);
|
||||||
|
free(h); free(s);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
// 二叉树后序遍历(给出每个结点的左右孩子编号,根为1)
|
||||||
|
// 输入:n,随后 n 行,每行两个整数 l、r(0 表示空)。
|
||||||
|
// 输出:后序遍历序列,每个数字后均输出一个空格。
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
int n;
|
||||||
|
if (scanf("%d", &n) != 1) return 0;
|
||||||
|
|
||||||
|
int *L = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
int *R = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
if (!L || !R) return 0;
|
||||||
|
|
||||||
|
for (int i = 1; i <= n; ++i) {
|
||||||
|
int l, r;
|
||||||
|
if (scanf("%d %d", &l, &r) != 2) {
|
||||||
|
free(L); free(R);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
L[i] = l; R[i] = r;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 迭代式后序遍历:使用两个栈
|
||||||
|
// 栈1:根右左;栈2:左右根(输出时弹出栈2)
|
||||||
|
int *s1 = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
int *s2 = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
int t1 = 0, t2 = 0;
|
||||||
|
|
||||||
|
s1[t1++] = 1; // 根节点为 1
|
||||||
|
while (t1) {
|
||||||
|
int u = s1[--t1];
|
||||||
|
if (u == 0) continue;
|
||||||
|
s2[t2++] = u;
|
||||||
|
// 先压左,再压右,使栈2形成 左右根 的逆序
|
||||||
|
if (L[u]) s1[t1++] = L[u];
|
||||||
|
if (R[u]) s1[t1++] = R[u];
|
||||||
|
}
|
||||||
|
|
||||||
|
while (t2) {
|
||||||
|
int u = s2[--t2];
|
||||||
|
printf("%d ", u);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(s1); free(s2);
|
||||||
|
free(L); free(R);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
// 非递归先序遍历(给出每个结点的左右孩子编号,根为1)
|
||||||
|
// 输入:n,随后 n 行,每行两个整数 l、r(0 表示无孩子)。
|
||||||
|
// 输出:先序遍历序列,每个数字后均输出一个空格。
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
int n;
|
||||||
|
if (scanf("%d", &n) != 1) return 0;
|
||||||
|
|
||||||
|
int *L = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
int *R = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
if (!L || !R) return 0;
|
||||||
|
|
||||||
|
for (int i = 1; i <= n; ++i) {
|
||||||
|
int l, r;
|
||||||
|
if (scanf("%d %d", &l, &r) != 2) {
|
||||||
|
free(L); free(R);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
L[i] = l; R[i] = r;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 迭代版先序遍历:使用一个栈,根开始;弹出访问当前结点,先压右再压左
|
||||||
|
int *stack = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
int top = 0;
|
||||||
|
stack[top++] = 1; // 根节点为 1
|
||||||
|
|
||||||
|
while (top) {
|
||||||
|
int u = stack[--top];
|
||||||
|
if (u == 0) continue;
|
||||||
|
printf("%d ", u);
|
||||||
|
if (R[u]) stack[top++] = R[u];
|
||||||
|
if (L[u]) stack[top++] = L[u];
|
||||||
|
}
|
||||||
|
|
||||||
|
free(stack);
|
||||||
|
free(L); free(R);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
// 非递归中序遍历。给出每个结点的左右孩子编号,根为 1。
|
||||||
|
// 输入:n,随后 n 行,每行两个整数 l、r(0 表示无孩子)。
|
||||||
|
// 输出:中序遍历序列,每个数字后均输出一个空格。
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
int n;
|
||||||
|
if (scanf("%d", &n) != 1) return 0;
|
||||||
|
|
||||||
|
int *L = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
int *R = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
if (!L || !R) return 0;
|
||||||
|
|
||||||
|
for (int i = 1; i <= n; ++i) {
|
||||||
|
int l, r;
|
||||||
|
if (scanf("%d %d", &l, &r) != 2) {
|
||||||
|
free(L); free(R);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
L[i] = l; R[i] = r;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 非递归中序遍历(栈)
|
||||||
|
int *stack = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
int top = 0;
|
||||||
|
int cur = 1; // 根结点为 1
|
||||||
|
|
||||||
|
while (cur || top) {
|
||||||
|
while (cur) { // 沿左链压栈
|
||||||
|
stack[top++] = cur;
|
||||||
|
cur = L[cur];
|
||||||
|
}
|
||||||
|
int u = stack[--top]; // 访问栈顶
|
||||||
|
printf("%d ", u);
|
||||||
|
cur = R[u]; // 转向右子树
|
||||||
|
}
|
||||||
|
|
||||||
|
free(stack);
|
||||||
|
free(L); free(R);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
// 非递归后序遍历(给出每个结点的左右孩子编号,根为1)
|
||||||
|
// 输入:n,随后 n 行,每行两个整数 l、r(0 表示无孩子)。
|
||||||
|
// 输出:后序遍历序列,每个数字后均输出一个空格。
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
int n;
|
||||||
|
if (scanf("%d", &n) != 1) return 0;
|
||||||
|
|
||||||
|
int *L = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
int *R = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
if (!L || !R) return 0;
|
||||||
|
|
||||||
|
for (int i = 1; i <= n; ++i) {
|
||||||
|
int l, r;
|
||||||
|
if (scanf("%d %d", &l, &r) != 2) {
|
||||||
|
free(L); free(R);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
L[i] = l; R[i] = r;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 单栈 + 最近访问标记 实现后序遍历
|
||||||
|
int *stack = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
int top = 0;
|
||||||
|
int cur = 1; // 根结点编号为 1
|
||||||
|
int prev = 0; // 最近一次输出的结点编号
|
||||||
|
|
||||||
|
while (cur || top) {
|
||||||
|
if (cur) {
|
||||||
|
stack[top++] = cur;
|
||||||
|
cur = L[cur]; // 沿左链压栈
|
||||||
|
} else {
|
||||||
|
int u = stack[top - 1];
|
||||||
|
// 若右子树存在且未被处理,则转向右子树
|
||||||
|
if (R[u] && prev != R[u]) {
|
||||||
|
cur = R[u];
|
||||||
|
} else {
|
||||||
|
// 右子树为空或已处理,弹栈并输出该结点
|
||||||
|
--top;
|
||||||
|
printf("%d ", u);
|
||||||
|
prev = u;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
free(stack);
|
||||||
|
free(L); free(R);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
// 题目描述
|
||||||
|
// 给定一个只包括(,)
|
||||||
|
// ,{,} ,[,] 的字符串
|
||||||
|
// s
|
||||||
|
// s ,判断字符串是否有效。
|
||||||
|
// 有效字符串需满足:
|
||||||
|
|
||||||
|
// 左括号必须用相同类型的右括号闭合。
|
||||||
|
// 左括号必须以正确的顺序闭合。
|
||||||
|
// 每个右括号都有一个对应的相同类型的左括号。
|
||||||
|
// 输入
|
||||||
|
// 输入一个字符串
|
||||||
|
// s
|
||||||
|
|
||||||
|
// (
|
||||||
|
// 1
|
||||||
|
// ≤ s
|
||||||
|
// ≤ 10 4)
|
||||||
|
// s(1≤s≤10 4)。
|
||||||
|
|
||||||
|
// 输出
|
||||||
|
// 有效字符串输出 true,否则输出 false。
|
||||||
|
|
||||||
|
// 样例
|
||||||
|
// 输入 #1 复制代码()
|
||||||
|
// 输出 #1 复制代码
|
||||||
|
// true 输入 #2 复制代码()[]
|
||||||
|
// {
|
||||||
|
// }
|
||||||
|
// 输出 #2
|
||||||
|
// 复制代码
|
||||||
|
// true
|
||||||
|
// 输入 #3
|
||||||
|
// 复制代码
|
||||||
|
// (]
|
||||||
|
// 输出 #3
|
||||||
|
// 复制代码
|
||||||
|
// false
|
||||||
|
// 输入 #4
|
||||||
|
// 复制代码
|
||||||
|
// ([])
|
||||||
|
// 输出 #4
|
||||||
|
// 复制代码
|
||||||
|
// true
|
||||||
|
// 输入 #5
|
||||||
|
// 复制代码
|
||||||
|
// ()[{]}
|
||||||
|
// 输出 #5
|
||||||
|
// 复制代码
|
||||||
|
//
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
// 非递归层序遍历(BFS)(给出每个结点的左右孩子编号,根为1)
|
||||||
|
// 输入:n,随后 n 行,每行两个整数 l、r(0 表示无孩子)。
|
||||||
|
// 输出:层序遍历序列,每个数字后均输出一个空格。
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
int n;
|
||||||
|
if (scanf("%d", &n) != 1) return 0;
|
||||||
|
|
||||||
|
if (n <= 0) return 0;
|
||||||
|
|
||||||
|
int *L = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
int *R = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
if (!L || !R) return 0;
|
||||||
|
|
||||||
|
for (int i = 1; i <= n; ++i) {
|
||||||
|
int l, r;
|
||||||
|
if (scanf("%d %d", &l, &r) != 2) {
|
||||||
|
free(L); free(R);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
L[i] = l; R[i] = r;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 队列 BFS,从根 1 开始,左到右按层输出
|
||||||
|
int *q = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
if (!q) { free(L); free(R); return 0; }
|
||||||
|
int head = 0, tail = 0;
|
||||||
|
|
||||||
|
q[tail++] = 1; // 根节点为 1
|
||||||
|
while (head < tail) {
|
||||||
|
int u = q[head++];
|
||||||
|
if (u == 0) continue;
|
||||||
|
printf("%d ", u);
|
||||||
|
if (L[u]) q[tail++] = L[u];
|
||||||
|
if (R[u]) q[tail++] = R[u];
|
||||||
|
}
|
||||||
|
|
||||||
|
free(q);
|
||||||
|
free(L); free(R);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
// 前序线索二叉树构建(根为1)
|
||||||
|
// 输入:n,随后 n 行,每行两个整数 l、r(0 表示无孩子)。
|
||||||
|
// 输出:n 行,每行四个整数:lchild(i) ltag(i) rchild(i) rtag(i)
|
||||||
|
// 规则:
|
||||||
|
// - 若有左儿子:ltag(i)=0,lchild(i) 为左儿子编号;否则 ltag(i)=1,lchild(i) 为前序前驱,无前驱为 -2。
|
||||||
|
// - 若有右儿子:rtag(i)=0,rchild(i) 为右儿子编号;否则 rtag(i)=1,rchild(i) 为前序后继,无后继为 -1。
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
int n;
|
||||||
|
if (scanf("%d", &n) != 1) return 0;
|
||||||
|
if (n <= 0) return 0;
|
||||||
|
|
||||||
|
int *L = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
int *R = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
if (!L || !R) return 0;
|
||||||
|
|
||||||
|
for (int i = 1; i <= n; ++i) {
|
||||||
|
int l, r;
|
||||||
|
if (scanf("%d %d", &l, &r) != 2) {
|
||||||
|
free(L); free(R);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
L[i] = l; R[i] = r;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 迭代前序遍历,生成访问序列 order
|
||||||
|
int *stack = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
int *order = (int *)malloc(n * sizeof(int));
|
||||||
|
if (!stack || !order) { free(L); free(R); return 0; }
|
||||||
|
int top = 0, k = 0;
|
||||||
|
|
||||||
|
stack[top++] = 1; // 根为 1
|
||||||
|
while (top) {
|
||||||
|
int u = stack[--top];
|
||||||
|
if (u == 0) continue;
|
||||||
|
order[k++] = u;
|
||||||
|
if (R[u]) stack[top++] = R[u];
|
||||||
|
if (L[u]) stack[top++] = L[u];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 位置映射:pos[node] = 在前序序列中的索引
|
||||||
|
int *pos = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
if (!pos) { free(stack); free(order); free(L); free(R); return 0; }
|
||||||
|
for (int i = 1; i <= n; ++i) pos[i] = -1;
|
||||||
|
for (int i = 0; i < k; ++i) pos[order[i]] = i;
|
||||||
|
|
||||||
|
// 输出域与标记域
|
||||||
|
int *outL = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
int *ltag = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
int *outR = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
int *rtag = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
if (!outL || !ltag || !outR || !rtag) {
|
||||||
|
free(pos); free(stack); free(order); free(L); free(R);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 1; i <= n; ++i) {
|
||||||
|
if (L[i]) { // 有左儿子
|
||||||
|
ltag[i] = 0;
|
||||||
|
outL[i] = L[i];
|
||||||
|
} else { // 无左儿子,线索到前序前驱
|
||||||
|
ltag[i] = 1;
|
||||||
|
if (pos[i] > 0) outL[i] = order[pos[i] - 1];
|
||||||
|
else outL[i] = -2; // 无前驱
|
||||||
|
}
|
||||||
|
|
||||||
|
if (R[i]) { // 有右儿子
|
||||||
|
rtag[i] = 0;
|
||||||
|
outR[i] = R[i];
|
||||||
|
} else { // 无右儿子,线索到前序后继
|
||||||
|
rtag[i] = 1;
|
||||||
|
if (pos[i] >= 0 && pos[i] < k - 1) outR[i] = order[pos[i] + 1];
|
||||||
|
else outR[i] = -1; // 无后继
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 1; i <= n; ++i) {
|
||||||
|
printf("%d %d %d %d\n", outL[i], ltag[i], outR[i], rtag[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(outL); free(ltag); free(outR); free(rtag);
|
||||||
|
free(pos); free(stack); free(order);
|
||||||
|
free(L); free(R);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
// 中序线索二叉树构建(根为1)
|
||||||
|
// 输入:n,随后 n 行,每行两个整数 l、r(0 表示无孩子)。
|
||||||
|
// 输出:n 行,每行四个整数:lchild(i) ltag(i) rchild(i) rtag(i)
|
||||||
|
// 规则:
|
||||||
|
// - 若有左儿子:ltag(i)=0,lchild(i) 为左儿子编号;否则 ltag(i)=1,lchild(i) 为中序前驱,无前驱为 -2。
|
||||||
|
// - 若有右儿子:rtag(i)=0,rchild(i) 为右儿子编号;否则 rtag(i)=1,rchild(i) 为中序后继,无后继为 -1。
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
int n;
|
||||||
|
if (scanf("%d", &n) != 1) return 0;
|
||||||
|
if (n <= 0) return 0;
|
||||||
|
|
||||||
|
int *L = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
int *R = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
if (!L || !R) return 0;
|
||||||
|
|
||||||
|
for (int i = 1; i <= n; ++i) {
|
||||||
|
int l, r;
|
||||||
|
if (scanf("%d %d", &l, &r) != 2) {
|
||||||
|
free(L); free(R);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
L[i] = l; R[i] = r;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 迭代中序遍历,生成访问序列 order
|
||||||
|
int *stack = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
int *order = (int *)malloc(n * sizeof(int));
|
||||||
|
if (!stack || !order) { free(L); free(R); return 0; }
|
||||||
|
int top = 0, k = 0;
|
||||||
|
|
||||||
|
int cur = 1; // 根为 1
|
||||||
|
while (cur || top) {
|
||||||
|
if (cur) {
|
||||||
|
stack[top++] = cur;
|
||||||
|
cur = L[cur];
|
||||||
|
} else {
|
||||||
|
int u = stack[--top];
|
||||||
|
order[k++] = u;
|
||||||
|
cur = R[u];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 位置映射:pos[node] = 在中序序列中的索引
|
||||||
|
int *pos = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
if (!pos) { free(stack); free(order); free(L); free(R); return 0; }
|
||||||
|
for (int i = 1; i <= n; ++i) pos[i] = -1;
|
||||||
|
for (int i = 0; i < k; ++i) pos[order[i]] = i;
|
||||||
|
|
||||||
|
// 输出域与标记域
|
||||||
|
int *outL = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
int *ltag = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
int *outR = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
int *rtag = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
if (!outL || !ltag || !outR || !rtag) {
|
||||||
|
free(pos); free(stack); free(order); free(L); free(R);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 1; i <= n; ++i) {
|
||||||
|
if (L[i]) { // 有左儿子
|
||||||
|
ltag[i] = 0;
|
||||||
|
outL[i] = L[i];
|
||||||
|
} else { // 无左儿子,线索到中序前驱
|
||||||
|
ltag[i] = 1;
|
||||||
|
if (pos[i] > 0) outL[i] = order[pos[i] - 1];
|
||||||
|
else outL[i] = -2; // 无前驱
|
||||||
|
}
|
||||||
|
|
||||||
|
if (R[i]) { // 有右儿子
|
||||||
|
rtag[i] = 0;
|
||||||
|
outR[i] = R[i];
|
||||||
|
} else { // 无右儿子,线索到中序后继
|
||||||
|
rtag[i] = 1;
|
||||||
|
if (pos[i] >= 0 && pos[i] < k - 1) outR[i] = order[pos[i] + 1];
|
||||||
|
else outR[i] = -1; // 无后继
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 1; i <= n; ++i) {
|
||||||
|
printf("%d %d %d %d\n", outL[i], ltag[i], outR[i], rtag[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(outL); free(ltag); free(outR); free(rtag);
|
||||||
|
free(pos); free(stack); free(order);
|
||||||
|
free(L); free(R);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,98 @@
|
|||||||
|
// 后序线索二叉树构建(根为1)
|
||||||
|
// 输入:n,随后 n 行,每行两个整数 l、r(0 表示无孩子)。
|
||||||
|
// 输出:n 行,每行四个整数:lchild(i) ltag(i) rchild(i) rtag(i)
|
||||||
|
// 规则:
|
||||||
|
// - 若有左儿子:ltag(i)=0,lchild(i) 为左儿子编号;否则 ltag(i)=1,lchild(i) 为后序前驱,无前驱为 -2。
|
||||||
|
// - 若有右儿子:rtag(i)=0,rchild(i) 为右儿子编号;否则 rtag(i)=1,rchild(i) 为后序后继,无后继为 -1。
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
int n;
|
||||||
|
if (scanf("%d", &n) != 1) return 0;
|
||||||
|
if (n <= 0) return 0;
|
||||||
|
|
||||||
|
int *L = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
int *R = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
if (!L || !R) return 0;
|
||||||
|
|
||||||
|
for (int i = 1; i <= n; ++i) {
|
||||||
|
int l, r;
|
||||||
|
if (scanf("%d %d", &l, &r) != 2) {
|
||||||
|
free(L); free(R);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
L[i] = l; R[i] = r;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 迭代后序遍历,生成访问序列 order
|
||||||
|
int *stack = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
int *order = (int *)malloc(n * sizeof(int));
|
||||||
|
if (!stack || !order) { free(L); free(R); return 0; }
|
||||||
|
int top = 0, k = 0;
|
||||||
|
|
||||||
|
int cur = 1; // 根为 1
|
||||||
|
int prev = 0; // 最近已输出的结点
|
||||||
|
while (cur || top) {
|
||||||
|
if (cur) {
|
||||||
|
stack[top++] = cur;
|
||||||
|
cur = L[cur]; // 沿左链压栈
|
||||||
|
} else {
|
||||||
|
int u = stack[top - 1];
|
||||||
|
if (R[u] && prev != R[u]) {
|
||||||
|
cur = R[u]; // 右子树存在且未处理,转向右子树
|
||||||
|
} else {
|
||||||
|
// 右子树为空或已处理,弹栈并记录该结点
|
||||||
|
--top;
|
||||||
|
order[k++] = u;
|
||||||
|
prev = u;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 位置映射:pos[node] = 在后序序列中的索引
|
||||||
|
int *pos = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
if (!pos) { free(stack); free(order); free(L); free(R); return 0; }
|
||||||
|
for (int i = 1; i <= n; ++i) pos[i] = -1;
|
||||||
|
for (int i = 0; i < k; ++i) pos[order[i]] = i;
|
||||||
|
|
||||||
|
// 输出域与标记域
|
||||||
|
int *outL = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
int *ltag = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
int *outR = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
int *rtag = (int *)malloc((n + 1) * sizeof(int));
|
||||||
|
if (!outL || !ltag || !outR || !rtag) {
|
||||||
|
free(pos); free(stack); free(order); free(L); free(R);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 1; i <= n; ++i) {
|
||||||
|
if (L[i]) { // 有左儿子
|
||||||
|
ltag[i] = 0;
|
||||||
|
outL[i] = L[i];
|
||||||
|
} else { // 无左儿子,线索到后序前驱
|
||||||
|
ltag[i] = 1;
|
||||||
|
if (pos[i] > 0) outL[i] = order[pos[i] - 1];
|
||||||
|
else outL[i] = -2; // 无前驱
|
||||||
|
}
|
||||||
|
|
||||||
|
if (R[i]) { // 有右儿子
|
||||||
|
rtag[i] = 0;
|
||||||
|
outR[i] = R[i];
|
||||||
|
} else { // 无右儿子,线索到后序后继
|
||||||
|
rtag[i] = 1;
|
||||||
|
if (pos[i] >= 0 && pos[i] < k - 1) outR[i] = order[pos[i] + 1];
|
||||||
|
else outR[i] = -1; // 无后继
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 1; i <= n; ++i) {
|
||||||
|
printf("%d %d %d %d\n", outL[i], ltag[i], outR[i], rtag[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(outL); free(ltag); free(outR); free(rtag);
|
||||||
|
free(pos); free(stack); free(order);
|
||||||
|
free(L); free(R);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,209 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#include <direct.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// 栈结构定义
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
long long *data;
|
||||||
|
int top;
|
||||||
|
int capacity;
|
||||||
|
} Stack;
|
||||||
|
|
||||||
|
// 栈初始化
|
||||||
|
void stack_init(Stack *st, int initial_capacity)
|
||||||
|
{
|
||||||
|
int cap = initial_capacity > 0 ? initial_capacity : 2;
|
||||||
|
|
||||||
|
st->data = (long long *)malloc(sizeof(long long) * cap);
|
||||||
|
st->top = 0;
|
||||||
|
st->capacity = cap;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 栈释放
|
||||||
|
void stack_free(Stack *st)
|
||||||
|
{
|
||||||
|
free(st->data);
|
||||||
|
st->data = NULL;
|
||||||
|
st->top = 0;
|
||||||
|
st->capacity = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 栈是否为空
|
||||||
|
int stack_empty(const Stack *st)
|
||||||
|
{
|
||||||
|
return st->top == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 栈压栈
|
||||||
|
void stack_push(Stack *st, long long v)
|
||||||
|
{
|
||||||
|
if (st->top >= st->capacity)
|
||||||
|
{
|
||||||
|
int new_cap = st->capacity ? st->capacity * 2 : 2;
|
||||||
|
long long *nd = (long long *)realloc(st->data, sizeof(long long) * new_cap);
|
||||||
|
if (!nd)
|
||||||
|
{
|
||||||
|
free(st->data);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
st->data = nd;
|
||||||
|
st->capacity = new_cap;
|
||||||
|
}
|
||||||
|
st->data[st->top++] = v;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 栈弹栈
|
||||||
|
long long stack_pop(Stack *st)
|
||||||
|
{
|
||||||
|
if (st->top == 0)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return st->data[--st->top];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 栈顶元素
|
||||||
|
long long stack_top(const Stack *st)
|
||||||
|
{
|
||||||
|
if (st->top == 0)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return st->data[st->top - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
// 设置控制台编码为UTF-8,防止中文乱码
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul"); // 设置控制台编码为UTF-8
|
||||||
|
SetConsoleOutputCP(65001); // 设置控制台输出编码
|
||||||
|
SetConsoleCP(65001); // 设置控制台输入编码
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// 动态读取一行表达式
|
||||||
|
size_t cap = 1024, len = 0;
|
||||||
|
char *s = (char *)malloc(cap);
|
||||||
|
if (!s)
|
||||||
|
return 0;
|
||||||
|
int ch;
|
||||||
|
// 读取表达式
|
||||||
|
while ((ch = getchar()) != EOF && ch != '\n')
|
||||||
|
{
|
||||||
|
if (len + 1 >= cap)
|
||||||
|
{
|
||||||
|
// 容量不足时,倍增扩容
|
||||||
|
cap <<= 1;
|
||||||
|
char *ns = (char *)realloc(s, cap);
|
||||||
|
if (!ns)
|
||||||
|
{
|
||||||
|
free(s);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
s = ns;
|
||||||
|
}
|
||||||
|
// 逐字符写入
|
||||||
|
s[len++] = (char)ch;
|
||||||
|
}
|
||||||
|
s[len] = '\0';
|
||||||
|
if (len == 0)
|
||||||
|
{
|
||||||
|
free(s);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
Stack st;
|
||||||
|
stack_init(&st, (int)len + 1);
|
||||||
|
|
||||||
|
long long num = 0;
|
||||||
|
// 记录最近的运算符,初始为加号
|
||||||
|
char op = '+';
|
||||||
|
|
||||||
|
for (size_t i = 0; i < len; ++i)
|
||||||
|
{
|
||||||
|
char c = s[i];
|
||||||
|
// 跳过空格字符
|
||||||
|
if (isspace((unsigned char)c))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (isdigit((unsigned char)c))
|
||||||
|
{
|
||||||
|
// 累积多位数字
|
||||||
|
num = num * 10 + (c - '0');
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// 根据“前一个”运算符 op 处理当前数字 num;
|
||||||
|
// 乘除即时计算以确保高于加减的优先级
|
||||||
|
switch (op)
|
||||||
|
{
|
||||||
|
case '+':
|
||||||
|
stack_push(&st, num);
|
||||||
|
break;
|
||||||
|
case '-':
|
||||||
|
stack_push(&st, -num);
|
||||||
|
break;
|
||||||
|
case '*':
|
||||||
|
{
|
||||||
|
long long a = stack_pop(&st);
|
||||||
|
stack_push(&st, a * num);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case '/':
|
||||||
|
{
|
||||||
|
long long a = stack_pop(&st);
|
||||||
|
stack_push(&st, a / num);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// 更新待应用的运算符为当前位置字符
|
||||||
|
op = c;
|
||||||
|
// 重置当前数字,继续解析
|
||||||
|
num = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理末尾累积数字(循环结束后最后一个数尚未被处理)
|
||||||
|
switch (op)
|
||||||
|
{
|
||||||
|
case '+':
|
||||||
|
stack_push(&st, num);
|
||||||
|
break;
|
||||||
|
case '-':
|
||||||
|
stack_push(&st, -num);
|
||||||
|
break;
|
||||||
|
case '*':
|
||||||
|
{
|
||||||
|
long long a = stack_pop(&st);
|
||||||
|
stack_push(&st, a * num);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case '/':
|
||||||
|
{
|
||||||
|
long long a = stack_pop(&st);
|
||||||
|
// C 的整数除法向零截断
|
||||||
|
stack_push(&st, a / num);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
long long ans = 0;
|
||||||
|
// 栈中为已应用乘除的加减项,累加得到结果
|
||||||
|
while (!stack_empty(&st))
|
||||||
|
ans += stack_pop(&st);
|
||||||
|
|
||||||
|
printf("%lld\n", ans);
|
||||||
|
|
||||||
|
stack_free(&st);
|
||||||
|
free(s);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#include <direct.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// 简单环形队列
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
int *buf;
|
||||||
|
int cap;
|
||||||
|
int head;
|
||||||
|
int tail;
|
||||||
|
int len;
|
||||||
|
} Queue;
|
||||||
|
|
||||||
|
static void q_init(Queue *q, int cap)
|
||||||
|
{
|
||||||
|
q->buf = (int *)malloc(sizeof(int) * cap);
|
||||||
|
q->cap = cap;
|
||||||
|
q->head = 0;
|
||||||
|
q->tail = 0;
|
||||||
|
q->len = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int q_empty(const Queue *q) { return q->len == 0; }
|
||||||
|
|
||||||
|
static void q_push(Queue *q, int x)
|
||||||
|
{
|
||||||
|
q->buf[q->tail] = x;
|
||||||
|
q->tail = (q->tail + 1) % q->cap;
|
||||||
|
q->len++;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int q_pop(Queue *q)
|
||||||
|
{
|
||||||
|
int x = q->buf[q->head];
|
||||||
|
q->head = (q->head + 1) % q->cap;
|
||||||
|
q->len--;
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void q_free(Queue *q)
|
||||||
|
{
|
||||||
|
free(q->buf);
|
||||||
|
q->buf = NULL;
|
||||||
|
q->cap = q->head = q->tail = q->len = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
// 设置控制台编码为UTF-8,防止中文乱码
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul"); // 设置控制台编码为UTF-8
|
||||||
|
SetConsoleOutputCP(65001); // 设置控制台输出编码
|
||||||
|
SetConsoleCP(65001); // 设置控制台输入编码
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int n, m;
|
||||||
|
if (scanf("%d %d", &n, &m) != 2)
|
||||||
|
return 0;
|
||||||
|
if (n <= 0 || m <= 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
Queue q;
|
||||||
|
q_init(&q, n);
|
||||||
|
for (int i = 1; i <= n; ++i)
|
||||||
|
q_push(&q, i);
|
||||||
|
|
||||||
|
while (!q_empty(&q))
|
||||||
|
{
|
||||||
|
// 旋转 m-1 次:把队头移动到队尾(取模优化减少旋转次数)
|
||||||
|
int k = (m - 1) % q.len;
|
||||||
|
for (int t = 0; t < k; ++t)
|
||||||
|
{
|
||||||
|
q_push(&q, q_pop(&q));
|
||||||
|
}
|
||||||
|
int out = q_pop(&q);
|
||||||
|
printf("%d\n", out);
|
||||||
|
}
|
||||||
|
|
||||||
|
q_free(&q);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#include <direct.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// 简单环形队列
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
int *buf; // 队列缓冲区
|
||||||
|
int cap; // 队列容量
|
||||||
|
int head; // 队头指针
|
||||||
|
int tail; // 队尾指针
|
||||||
|
int len; // 当前队列长度
|
||||||
|
} Queue;
|
||||||
|
|
||||||
|
// 初始化队列
|
||||||
|
static void Queue_init(Queue *q, int cap)
|
||||||
|
{
|
||||||
|
q->buf = (int *)malloc(sizeof(int) * cap);
|
||||||
|
q->cap = cap;
|
||||||
|
q->head = 0;
|
||||||
|
q->tail = 0;
|
||||||
|
q->len = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查队列是否为空
|
||||||
|
static int Queue_empty(const Queue *q)
|
||||||
|
{
|
||||||
|
return q->len == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 入队操作
|
||||||
|
static void Queue_push(Queue *q, int x)
|
||||||
|
{
|
||||||
|
q->buf[q->tail] = x;
|
||||||
|
q->tail = (q->tail + 1) % q->cap;
|
||||||
|
q->len++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 弹出队列元素
|
||||||
|
static int Queue_pop(Queue *q)
|
||||||
|
{
|
||||||
|
int x = q->buf[q->head];
|
||||||
|
q->head = (q->head + 1) % q->cap;
|
||||||
|
q->len--;
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 释放队列内存(避免与标准库 free 冲突)
|
||||||
|
static void Queue_free(Queue *q)
|
||||||
|
{
|
||||||
|
free(q->buf);
|
||||||
|
q->buf = NULL;
|
||||||
|
q->cap = 0;
|
||||||
|
q->head = 0;
|
||||||
|
q->tail = 0;
|
||||||
|
q->len = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
// 设置控制台编码为UTF-8,防止中文乱码
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul"); // 设置控制台编码为UTF-8
|
||||||
|
SetConsoleOutputCP(65001); // 设置控制台输出编码
|
||||||
|
SetConsoleCP(65001); // 设置控制台输入编码
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int n, m;
|
||||||
|
printf("请输入约瑟夫环的人数n和报数m: ");
|
||||||
|
scanf("%d %d", &n, &m);
|
||||||
|
if (n < 8 || n > 15 || m < 5 || m > 32767)
|
||||||
|
{
|
||||||
|
printf("输入错误,请重新输入!\n");
|
||||||
|
printf("请重输入约瑟夫环的人数n和报数m: ");
|
||||||
|
scanf("%d %d", &n, &m);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化队列
|
||||||
|
Queue q;
|
||||||
|
Queue_init(&q, n);
|
||||||
|
|
||||||
|
// 入队操作
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
Queue_push(&q, i + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 出队操作:模拟报数过程
|
||||||
|
while (!Queue_empty(&q))
|
||||||
|
{
|
||||||
|
// 旋转 m-1 次:把队头移动到队尾(取模优化减少旋转次数)
|
||||||
|
int k = (m - 1) % q.len;
|
||||||
|
for (int t = 0; t < k; ++t)
|
||||||
|
{
|
||||||
|
Queue_push(&q, Queue_pop(&q));
|
||||||
|
}
|
||||||
|
int out = Queue_pop(&q);
|
||||||
|
printf("%d ", out);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 释放队列内存
|
||||||
|
Queue_free(&q);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,116 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
// 目标:在允许使用容量为 c 的栈缓冲的前提下,
|
||||||
|
// 将输入序列(商店从上到下)重排为字典序最小的最终堆叠(从下到上)。
|
||||||
|
// 操作等价于:依次读取输入;每次要么将下一个元素压入栈(若未满),要么从栈顶弹出到输出;
|
||||||
|
// 栈容量不超过 c;输出顺序即为最终堆叠的自下而上的编号。
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n, c;
|
||||||
|
if (scanf("%d %d", &n, &c) != 2)
|
||||||
|
return 0;
|
||||||
|
int *a = (int *)malloc(sizeof(int) * n);
|
||||||
|
for (int i = 0; i < n; ++i)
|
||||||
|
scanf("%d", &a[i]);
|
||||||
|
|
||||||
|
// 栈缓冲(手上带着的箱子),存储值
|
||||||
|
int *stk = (int *)malloc(sizeof(int) * n);
|
||||||
|
int len = 0; // 当前栈大小
|
||||||
|
|
||||||
|
// 单调队列(维护接下来可压入窗口中的最小值及其位置)
|
||||||
|
int *dq_val = (int *)malloc(sizeof(int) * n);
|
||||||
|
int *dq_idx = (int *)malloc(sizeof(int) * n);
|
||||||
|
int dq_head = 0, dq_tail = 0; // [head, tail) 有效
|
||||||
|
|
||||||
|
int i = 0; // 下一个待读取的输入位置
|
||||||
|
int j = 0; // 已加入单调队列的输入位置(下一个待加入的位置)
|
||||||
|
int out_cnt = 0;
|
||||||
|
|
||||||
|
while (out_cnt < n)
|
||||||
|
{
|
||||||
|
// 计算当前还能压入的最大数量 R(窗口大小)
|
||||||
|
int R = c - len;
|
||||||
|
if (R < 0)
|
||||||
|
R = 0;
|
||||||
|
|
||||||
|
// 扩充单调队列,使其覆盖 [i, min(i+R-1, n-1)]
|
||||||
|
while (j < n && j < i + R)
|
||||||
|
{
|
||||||
|
int v = a[j];
|
||||||
|
while (dq_tail > dq_head && dq_val[dq_tail - 1] > v)
|
||||||
|
--dq_tail;
|
||||||
|
dq_val[dq_tail] = v;
|
||||||
|
dq_idx[dq_tail] = j;
|
||||||
|
++dq_tail;
|
||||||
|
++j;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 若栈非空,比较栈顶与窗口内最小值,决定弹出还是继续压入到最小值位置
|
||||||
|
if (len > 0)
|
||||||
|
{
|
||||||
|
int top = stk[len - 1];
|
||||||
|
int hasUpcoming = (dq_tail > dq_head);
|
||||||
|
int minUpcoming = hasUpcoming ? dq_val[dq_head] : 2147483647; // INF
|
||||||
|
if (!hasUpcoming || top <= minUpcoming)
|
||||||
|
{
|
||||||
|
// 栈顶不大于可达窗口中的最小值(或无可压入项),弹出栈顶到输出
|
||||||
|
printf("%d", top);
|
||||||
|
++out_cnt;
|
||||||
|
if (out_cnt < n)
|
||||||
|
putchar(' ');
|
||||||
|
--len;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 否则(栈为空或栈顶较大且尚有可压入项),把队列窗口的最小值所在位置的元素压入到栈顶,然后立即弹出
|
||||||
|
if (dq_tail > dq_head)
|
||||||
|
{
|
||||||
|
int target_pos = dq_idx[dq_head];
|
||||||
|
// 逐个压入直到到达 target_pos
|
||||||
|
while (i <= target_pos)
|
||||||
|
{
|
||||||
|
stk[len++] = a[i++];
|
||||||
|
}
|
||||||
|
// 清理队列中过期项(索引 < i 的)
|
||||||
|
while (dq_tail > dq_head && dq_idx[dq_head] < i)
|
||||||
|
++dq_head;
|
||||||
|
|
||||||
|
// 弹出刚压入的最小值
|
||||||
|
int val = stk[len - 1];
|
||||||
|
printf("%d", val);
|
||||||
|
++out_cnt;
|
||||||
|
if (out_cnt < n)
|
||||||
|
putchar(' ');
|
||||||
|
--len;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// 没有可压入项,但仍需输出 -> 只能弹出栈顶
|
||||||
|
if (len > 0)
|
||||||
|
{
|
||||||
|
int val = stk[len - 1];
|
||||||
|
printf("%d", val);
|
||||||
|
++out_cnt;
|
||||||
|
if (out_cnt < n)
|
||||||
|
putchar(' ');
|
||||||
|
--len;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// 理论上不会到这里(既无可压入项又无栈元素),为安全直接中断
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
putchar('\n');
|
||||||
|
|
||||||
|
free(a);
|
||||||
|
free(stk);
|
||||||
|
free(dq_val);
|
||||||
|
free(dq_idx);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int *data;
|
||||||
|
int *mins;
|
||||||
|
int top; // index of next insertion in data
|
||||||
|
int mtop; // index of next insertion in mins
|
||||||
|
int cap; // capacity
|
||||||
|
} MinStack;
|
||||||
|
|
||||||
|
static void ms_init(MinStack *s, int cap) {
|
||||||
|
s->data = (int *)malloc(sizeof(int) * cap);
|
||||||
|
s->mins = (int *)malloc(sizeof(int) * cap);
|
||||||
|
s->top = 0;
|
||||||
|
s->mtop = 0;
|
||||||
|
s->cap = cap;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ms_empty(const MinStack *s) { return s->top == 0; }
|
||||||
|
|
||||||
|
static void ms_push(MinStack *s, int x) {
|
||||||
|
// Assume total pushes won't exceed cap per problem constraints
|
||||||
|
s->data[s->top++] = x;
|
||||||
|
if (s->mtop == 0 || x <= s->mins[s->mtop - 1]) {
|
||||||
|
s->mins[s->mtop++] = x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ms_pop(MinStack *s) {
|
||||||
|
if (s->top == 0) return; // ignore if empty
|
||||||
|
int v = s->data[s->top - 1];
|
||||||
|
s->top--;
|
||||||
|
if (s->mtop > 0 && v == s->mins[s->mtop - 1]) {
|
||||||
|
s->mtop--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ms_top(const MinStack *s) {
|
||||||
|
return s->data[s->top - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ms_get_min(const MinStack *s) {
|
||||||
|
return s->mins[s->mtop - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ms_free(MinStack *s) {
|
||||||
|
free(s->data);
|
||||||
|
free(s->mins);
|
||||||
|
s->data = s->mins = NULL;
|
||||||
|
s->top = s->mtop = s->cap = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
int n;
|
||||||
|
if (scanf("%d", &n) != 1) return 0;
|
||||||
|
MinStack st;
|
||||||
|
ms_init(&st, n > 0 ? n : 1);
|
||||||
|
|
||||||
|
for (int i = 0; i < n; ++i) {
|
||||||
|
int t;
|
||||||
|
if (scanf("%d", &t) != 1) t = 0;
|
||||||
|
if (t == 1) {
|
||||||
|
int x;
|
||||||
|
scanf("%d", &x);
|
||||||
|
ms_push(&st, x);
|
||||||
|
} else if (t == 2) {
|
||||||
|
ms_pop(&st);
|
||||||
|
} else if (t == 3) {
|
||||||
|
if (!ms_empty(&st)) {
|
||||||
|
printf("%d\n", ms_top(&st));
|
||||||
|
}
|
||||||
|
} else if (t == 4) {
|
||||||
|
if (!ms_empty(&st)) {
|
||||||
|
printf("%d\n", ms_get_min(&st));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// ignore unknown command
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ms_free(&st);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
// 计算最长合法括号子串长度及数量(数量指达到该最长长度的子串个数)
|
||||||
|
// 使用经典 DP:dp[i] 表示以 i 结尾的最长合法括号长度。
|
||||||
|
// 当 maxLen 为 0 时,按题意输出 "0 1"。
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
const int MAXN = 1000005; // 题面最大长度约 1e6
|
||||||
|
char *s = (char *)malloc(MAXN);
|
||||||
|
if (!s) return 0;
|
||||||
|
if (scanf("%1000000s", s) != 1) {
|
||||||
|
free(s);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int n = (int)strlen(s);
|
||||||
|
if (n == 0) {
|
||||||
|
printf("0 1\n");
|
||||||
|
free(s);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int *dp = (int *)calloc(n, sizeof(int));
|
||||||
|
if (!dp) {
|
||||||
|
free(s);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int maxLen = 0;
|
||||||
|
int count = 1; // 若最终 maxLen 为 0,答案应为 0 1
|
||||||
|
|
||||||
|
for (int i = 1; i < n; ++i) {
|
||||||
|
if (s[i] == ')') {
|
||||||
|
if (s[i - 1] == '(') {
|
||||||
|
dp[i] = 2 + (i >= 2 ? dp[i - 2] : 0);
|
||||||
|
} else {
|
||||||
|
int prev = i - dp[i - 1] - 1;
|
||||||
|
if (prev >= 0 && s[prev] == '(') {
|
||||||
|
dp[i] = dp[i - 1] + 2 + (prev >= 1 ? dp[prev - 1] : 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dp[i] > 0) {
|
||||||
|
if (dp[i] > maxLen) {
|
||||||
|
maxLen = dp[i];
|
||||||
|
count = 1;
|
||||||
|
} else if (dp[i] == maxLen) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (maxLen == 0) {
|
||||||
|
printf("0 1\n");
|
||||||
|
} else {
|
||||||
|
printf("%d %d\n", maxLen, count);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(dp);
|
||||||
|
free(s);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int T;
|
||||||
|
scanf("%d", &T);
|
||||||
|
|
||||||
|
while (T--)
|
||||||
|
{
|
||||||
|
int N;
|
||||||
|
scanf("%d", &N);
|
||||||
|
|
||||||
|
int a[10000];
|
||||||
|
|
||||||
|
// 读取序列
|
||||||
|
for (int i = 0; i < N; i++)
|
||||||
|
{
|
||||||
|
scanf("%d", &a[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 找最大值和最小值的位置
|
||||||
|
int max_pos = 0, min_pos = 0;
|
||||||
|
for (int i = 0; i < N; i++)
|
||||||
|
{
|
||||||
|
if (a[i] > a[max_pos])
|
||||||
|
{
|
||||||
|
max_pos = i;
|
||||||
|
}
|
||||||
|
if (a[i] < a[min_pos])
|
||||||
|
{
|
||||||
|
min_pos = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 交换最大值和最小值
|
||||||
|
int temp = a[max_pos];
|
||||||
|
a[max_pos] = a[min_pos];
|
||||||
|
a[min_pos] = temp;
|
||||||
|
|
||||||
|
// 输出结果
|
||||||
|
for (int i = 0; i < N; i++)
|
||||||
|
{
|
||||||
|
printf("%d", a[i]);
|
||||||
|
if (i < N - 1)
|
||||||
|
{
|
||||||
|
printf(" ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
// 语法:
|
||||||
|
// E := T ('|' T)* // 选择,取较长长度
|
||||||
|
// T := F+ // 连接,长度相加
|
||||||
|
// F := 'a' | '(' E ')' // 原子:一个 a 或括号组
|
||||||
|
// 输出化简后(全为 a)的长度
|
||||||
|
|
||||||
|
static const char *s;
|
||||||
|
static int n, pos;
|
||||||
|
|
||||||
|
static int parse_E(void);
|
||||||
|
static int parse_T(void);
|
||||||
|
static int parse_F(void);
|
||||||
|
|
||||||
|
static int parse_E(void)
|
||||||
|
{
|
||||||
|
int best = parse_T();
|
||||||
|
while (pos < n && s[pos] == '|') {
|
||||||
|
++pos; // consume '|'
|
||||||
|
int t = parse_T();
|
||||||
|
if (t > best) best = t;
|
||||||
|
}
|
||||||
|
return best;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int parse_T(void)
|
||||||
|
{
|
||||||
|
int sum = 0;
|
||||||
|
while (pos < n && s[pos] != ')' && s[pos] != '|') {
|
||||||
|
sum += parse_F();
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int parse_F(void)
|
||||||
|
{
|
||||||
|
if (pos < n && s[pos] == 'a') {
|
||||||
|
++pos;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (pos < n && s[pos] == '(') {
|
||||||
|
++pos; // consume '('
|
||||||
|
int v = parse_E();
|
||||||
|
if (pos < n && s[pos] == ')') ++pos; // consume ')'
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
// 理论上不会到达(保证输入合法且不含空串),安全返回 0
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
// 读取一行字符串(仅包含 a | ( )),长度不超过 1e5
|
||||||
|
static char buf[1000005];
|
||||||
|
if (scanf("%1000000s", buf) != 1) return 0;
|
||||||
|
s = buf;
|
||||||
|
n = (int)strlen(s);
|
||||||
|
pos = 0;
|
||||||
|
int ans = parse_E();
|
||||||
|
printf("%d\n", ans);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,119 @@
|
|||||||
|
// 可随机访问的“栈”,支持按下标 O(1) 访问与快 I/O
|
||||||
|
// 操作:
|
||||||
|
// 1 x:压入整数 x
|
||||||
|
// 2:输出栈顶元素
|
||||||
|
// 3 i:输出第 i 个元素(从 0 开始)
|
||||||
|
// 4:弹出栈顶元素
|
||||||
|
// 约束:操作数最多 5,000,000;执行 3 时保证 i 有效
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
// 快读整数
|
||||||
|
static inline int read_int(void) {
|
||||||
|
int c = getchar();
|
||||||
|
while (c != EOF && (c == ' ' || c == '\n' || c == '\t' || c == '\r')) c = getchar();
|
||||||
|
int sign = 1;
|
||||||
|
if (c == '-') { sign = -1; c = getchar(); }
|
||||||
|
int x = 0;
|
||||||
|
while (c >= '0' && c <= '9') { x = x * 10 + (c - '0'); c = getchar(); }
|
||||||
|
return sign * x;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 快速输出整数并换行
|
||||||
|
static inline void write_int_ln(int x) {
|
||||||
|
char buf[32];
|
||||||
|
char *p = buf + sizeof(buf);
|
||||||
|
*(--p) = '\n';
|
||||||
|
unsigned int ux;
|
||||||
|
if (x < 0) {
|
||||||
|
ux = (unsigned int)(-(long long)x);
|
||||||
|
} else {
|
||||||
|
ux = (unsigned int)x;
|
||||||
|
}
|
||||||
|
do {
|
||||||
|
*(--p) = (char)('0' + (ux % 10));
|
||||||
|
ux /= 10;
|
||||||
|
} while (ux);
|
||||||
|
if (x < 0) *(--p) = '-';
|
||||||
|
fwrite(p, 1, (size_t)(buf + sizeof(buf) - p), stdout);
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int *a;
|
||||||
|
size_t len;
|
||||||
|
size_t cap;
|
||||||
|
} RAStack;
|
||||||
|
|
||||||
|
static inline void ras_init(RAStack *s) {
|
||||||
|
s->len = 0;
|
||||||
|
s->cap = 1024; // 初始容量
|
||||||
|
s->a = (int*)malloc(s->cap * sizeof(int));
|
||||||
|
if (!s->a) {
|
||||||
|
// 若内存紧张,回退到较小容量以继续运行
|
||||||
|
s->cap = 16;
|
||||||
|
s->a = (int*)malloc(s->cap * sizeof(int));
|
||||||
|
if (!s->a) exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void ras_reserve(RAStack *s, size_t need) {
|
||||||
|
if (need <= s->cap) return;
|
||||||
|
size_t new_cap = s->cap;
|
||||||
|
while (new_cap < need) {
|
||||||
|
// 约 1.5 倍扩容,以便在超大输入时节省内存
|
||||||
|
new_cap = new_cap + (new_cap >> 1);
|
||||||
|
if (new_cap < s->cap + 1) new_cap = s->cap + 1; // 避免停滞
|
||||||
|
}
|
||||||
|
int *na = (int*)realloc(s->a, new_cap * sizeof(int));
|
||||||
|
if (!na) {
|
||||||
|
// 若 1.5 倍失败则尝试翻倍
|
||||||
|
new_cap = s->cap * 2;
|
||||||
|
na = (int*)realloc(s->a, new_cap * sizeof(int));
|
||||||
|
if (!na) exit(1);
|
||||||
|
}
|
||||||
|
s->a = na;
|
||||||
|
s->cap = new_cap;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void ras_push(RAStack *s, int x) {
|
||||||
|
if (s->len == s->cap) ras_reserve(s, s->len + 1);
|
||||||
|
s->a[s->len++] = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int ras_top(const RAStack *s) {
|
||||||
|
return s->a[s->len - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int ras_get(const RAStack *s, size_t i) {
|
||||||
|
return s->a[i]; // 从底部 0 开始计数
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void ras_pop(RAStack *s) {
|
||||||
|
--s->len;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
RAStack st;
|
||||||
|
ras_init(&st);
|
||||||
|
|
||||||
|
int n = read_int();
|
||||||
|
for (int k = 0; k < n; ++k) {
|
||||||
|
int op = read_int();
|
||||||
|
if (op == 1) {
|
||||||
|
int x = read_int();
|
||||||
|
ras_push(&st, x);
|
||||||
|
} else if (op == 2) {
|
||||||
|
write_int_ln(ras_top(&st));
|
||||||
|
} else if (op == 3) {
|
||||||
|
int i = read_int();
|
||||||
|
write_int_ln(ras_get(&st, (size_t)i));
|
||||||
|
} else if (op == 4) {
|
||||||
|
ras_pop(&st);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
free(st.a);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
// 计算含运算符 '&'(取两数较小值)与 '|'(取两数较大值)的表达式
|
||||||
|
// 两者同优先级,按从左到右结合;遇到括号优先计算括号内表达式。
|
||||||
|
// 输入:一行字符串,仅由数字、&、|、(、) 组成
|
||||||
|
// 输出:一个整数,表示表达式的值
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
|
static inline int min_i(int a, int b) { return a < b ? a : b; }
|
||||||
|
static inline int max_i(int a, int b) { return a > b ? a : b; }
|
||||||
|
|
||||||
|
static inline void apply_top(int *vals, int *vtop, char *ops, int *otop) {
|
||||||
|
char op = ops[--(*otop)];
|
||||||
|
int b = vals[--(*vtop)];
|
||||||
|
int a = vals[--(*vtop)];
|
||||||
|
int r = (op == '&') ? min_i(a, b) : max_i(a, b);
|
||||||
|
vals[(*vtop)++] = r;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
// 读取整行输入
|
||||||
|
char *line = NULL;
|
||||||
|
size_t cap = 0;
|
||||||
|
{
|
||||||
|
// getline 若不可用则用 fgets 简易读取
|
||||||
|
// Windows 平台无 getline,这里使用 fgets
|
||||||
|
size_t bufcap = 16384; // 大于题目约束的 10000
|
||||||
|
line = (char*)malloc(bufcap);
|
||||||
|
if (!line) return 1;
|
||||||
|
if (!fgets(line, (int)bufcap, stdin)) return 0;
|
||||||
|
size_t len = strlen(line);
|
||||||
|
// 去掉行尾 CR/LF
|
||||||
|
while (len && (line[len-1] == '\n' || line[len-1] == '\r')) line[--len] = '\0';
|
||||||
|
cap = len + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t L = strlen(line);
|
||||||
|
int *vals = (int*)malloc((L + 1) * sizeof(int));
|
||||||
|
char *ops = (char*)malloc(L + 1);
|
||||||
|
if (!vals || !ops) return 1;
|
||||||
|
int vtop = 0, otop = 0;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < L;) {
|
||||||
|
char c = line[i];
|
||||||
|
if (isspace((unsigned char)c)) { ++i; continue; } // 忽略空白
|
||||||
|
if (c >= '0' && c <= '9') {
|
||||||
|
int x = 0;
|
||||||
|
while (i < L && line[i] >= '0' && line[i] <= '9') {
|
||||||
|
x = x * 10 + (line[i] - '0');
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
vals[vtop++] = x;
|
||||||
|
} else if (c == '&' || c == '|') {
|
||||||
|
// 同优先级:在遇到 '(' 之前,应用所有已入栈的运算符
|
||||||
|
while (otop > 0 && ops[otop - 1] != '(') {
|
||||||
|
apply_top(vals, &vtop, ops, &otop);
|
||||||
|
}
|
||||||
|
ops[otop++] = c;
|
||||||
|
++i;
|
||||||
|
} else if (c == '(') {
|
||||||
|
ops[otop++] = c;
|
||||||
|
++i;
|
||||||
|
} else if (c == ')') {
|
||||||
|
while (otop > 0 && ops[otop - 1] != '(') {
|
||||||
|
apply_top(vals, &vtop, ops, &otop);
|
||||||
|
}
|
||||||
|
if (otop > 0 && ops[otop - 1] == '(') --otop; // 弹出左括号
|
||||||
|
++i;
|
||||||
|
} else {
|
||||||
|
// 非法字符:跳过以增强鲁棒性
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while (otop > 0) {
|
||||||
|
apply_top(vals, &vtop, ops, &otop);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (vtop > 0) printf("%d\n", vals[vtop - 1]);
|
||||||
|
|
||||||
|
free(vals);
|
||||||
|
free(ops);
|
||||||
|
free(line);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,170 @@
|
|||||||
|
// 算术表达式求值:支持 + - * / ^ 和小括号
|
||||||
|
// 规则:
|
||||||
|
// - 运算符优先级:括号 > 一元± > ^(右结合)> * /(左结合)> + -(左结合)
|
||||||
|
// - 输入为一行表达式,可能包含多余括号与空格
|
||||||
|
// - 输出为结果,按四舍五入取整
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
// 手写整数乘方(支持负指数):右结合的 ^ 使用该实现
|
||||||
|
static inline long double powi_ld(long double base, long long exp) {
|
||||||
|
if (exp == 0) return 1.0L;
|
||||||
|
int neg = 0;
|
||||||
|
if (exp < 0) { neg = 1; exp = -exp; }
|
||||||
|
long double res = 1.0L, a = base;
|
||||||
|
while (exp) {
|
||||||
|
if (exp & 1LL) res *= a;
|
||||||
|
a *= a;
|
||||||
|
exp >>= 1LL;
|
||||||
|
}
|
||||||
|
if (neg) {
|
||||||
|
// 避免 1/0 产生异常(表达式一般不会出现该情况)
|
||||||
|
if (res == 0.0L) return 0.0L; // 约定返回 0,防止 RE
|
||||||
|
return 1.0L / res;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 手写四舍五入为 long long,避免依赖 llround
|
||||||
|
static inline long long roundll(long double x) {
|
||||||
|
return (x >= 0.0L) ? (long long)(x + 0.5L) : (long long)(x - 0.5L);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 操作符优先级
|
||||||
|
static inline int prec(char op) {
|
||||||
|
switch (op) {
|
||||||
|
case '~': return 4; // 一元负号
|
||||||
|
case '^': return 3; // 乘方,右结合
|
||||||
|
case '*': case '/': return 2; // 乘除
|
||||||
|
case '+': case '-': return 1; // 加减
|
||||||
|
default: return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 应用一个操作符到栈顶操作数
|
||||||
|
static inline void apply_top(long double *vals, int *vtop, char *ops, int *otop) {
|
||||||
|
char op = ops[--(*otop)];
|
||||||
|
if (op == '~') {
|
||||||
|
long double a = vals[--(*vtop)];
|
||||||
|
vals[(*vtop)++] = -a;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
long double b = vals[--(*vtop)];
|
||||||
|
long double a = vals[--(*vtop)];
|
||||||
|
long double r;
|
||||||
|
switch (op) {
|
||||||
|
case '+': r = a + b; break;
|
||||||
|
case '-': r = a - b; break;
|
||||||
|
case '*': r = a * b; break;
|
||||||
|
case '/':
|
||||||
|
// 防御除零导致的 RE:约定返回 0(题目数据通常不会出现此情况)
|
||||||
|
if (b == 0.0L) r = 0.0L; else r = a / b;
|
||||||
|
break;
|
||||||
|
case '^': {
|
||||||
|
long long be = roundll(b); // 指数按近似整数处理
|
||||||
|
r = powi_ld(a, be);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: r = 0.0; break;
|
||||||
|
}
|
||||||
|
vals[(*vtop)++] = r;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
// 读取整行表达式
|
||||||
|
char *line = NULL;
|
||||||
|
size_t cap = 0;
|
||||||
|
{
|
||||||
|
size_t bufcap = 1024; // 表达式长度 ≤ 30,这里足够
|
||||||
|
line = (char*)malloc(bufcap);
|
||||||
|
if (!line) return 1;
|
||||||
|
if (!fgets(line, (int)bufcap, stdin)) return 0;
|
||||||
|
size_t len = strlen(line);
|
||||||
|
while (len && (line[len-1] == '\n' || line[len-1] == '\r')) line[--len] = '\0';
|
||||||
|
cap = len + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t L = strlen(line);
|
||||||
|
// 栈容量按输入长度分配
|
||||||
|
long double *vals = (long double*)malloc((L + 1) * sizeof(long double));
|
||||||
|
char *ops = (char*)malloc(L + 1);
|
||||||
|
if (!vals || !ops) return 1;
|
||||||
|
int vtop = 0, otop = 0;
|
||||||
|
|
||||||
|
int expect_unary = 1; // 是否期待一元运算符(开头或在'('或二元运算符之后)
|
||||||
|
|
||||||
|
for (size_t i = 0; i < L;) {
|
||||||
|
char c = line[i];
|
||||||
|
if (isspace((unsigned char)c)) { ++i; continue; }
|
||||||
|
if (c >= '0' && c <= '9') {
|
||||||
|
// 解析数字(整数)
|
||||||
|
long long x = 0;
|
||||||
|
while (i < L && line[i] >= '0' && line[i] <= '9') {
|
||||||
|
x = x * 10 + (line[i] - '0');
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
vals[vtop++] = (long double)x;
|
||||||
|
expect_unary = 0;
|
||||||
|
} else if (c == '(') {
|
||||||
|
ops[otop++] = c;
|
||||||
|
++i;
|
||||||
|
expect_unary = 1;
|
||||||
|
} else if (c == ')') {
|
||||||
|
while (otop > 0 && ops[otop - 1] != '(') {
|
||||||
|
apply_top(vals, &vtop, ops, &otop);
|
||||||
|
}
|
||||||
|
if (otop > 0 && ops[otop - 1] == '(') --otop; // 弹出左括号
|
||||||
|
++i;
|
||||||
|
expect_unary = 0;
|
||||||
|
} else if (c == '+' || c == '-' || c == '*' || c == '/' || c == '^') {
|
||||||
|
// 处理一元 ±
|
||||||
|
if (expect_unary && (c == '+' || c == '-')) {
|
||||||
|
if (c == '-') {
|
||||||
|
// 一元负号入栈(最高优先级)
|
||||||
|
ops[otop++] = '~';
|
||||||
|
}
|
||||||
|
// 一元 '+' 不产生效果,直接跳过
|
||||||
|
++i;
|
||||||
|
expect_unary = 1; // 仍可能继续接 '(' 或 数字
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理二元运算符的结合与优先级
|
||||||
|
int p = prec(c);
|
||||||
|
int right_assoc = (c == '^');
|
||||||
|
while (otop > 0 && ops[otop - 1] != '(') {
|
||||||
|
int pt = prec(ops[otop - 1]);
|
||||||
|
if ((right_assoc && pt > p) || (!right_assoc && pt >= p)) {
|
||||||
|
apply_top(vals, &vtop, ops, &otop);
|
||||||
|
} else break;
|
||||||
|
}
|
||||||
|
ops[otop++] = c;
|
||||||
|
++i;
|
||||||
|
expect_unary = 1;
|
||||||
|
} else {
|
||||||
|
// 其他字符(若存在):跳过
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 清空剩余操作符
|
||||||
|
while (otop > 0) {
|
||||||
|
apply_top(vals, &vtop, ops, &otop);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 四舍五入输出整数
|
||||||
|
if (vtop > 0) {
|
||||||
|
long double res = vals[vtop - 1];
|
||||||
|
long long rounded = roundll(res);
|
||||||
|
printf("%lld\n", rounded);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(vals);
|
||||||
|
free(ops);
|
||||||
|
free(line);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,133 @@
|
|||||||
|
// 逆波兰式逐步规约打印
|
||||||
|
// 输入:仅含 0-9、+ - * / 和小括号 () 的中缀表达式,数字均为一位
|
||||||
|
// 要求:
|
||||||
|
// - 先将中缀转为后缀(RPN)
|
||||||
|
// - 按从左到右每次规约一个“两个数字 + 一个运算符”的三元组,得到新的后缀表达式
|
||||||
|
// - 逐行输出:首行是完整后缀表达式;之后每行比上一行少 1 个运算符和 1 个数字;最后一行只有一个数字(最终结果)
|
||||||
|
// - 令 / 为整除(向零截断),允许中间结果为负数
|
||||||
|
// - 输出格式:各符号之间以单个空格分隔;只有首行和最后一行行末保留一个空格,其他行末无空格
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int is_num; // 1 表示数字,0 表示运算符
|
||||||
|
int val; // 数字值
|
||||||
|
char op; // 运算符字符
|
||||||
|
} Token;
|
||||||
|
|
||||||
|
// 运算符优先级:* / 高于 + -,全部左结合
|
||||||
|
static inline int prec(char c) {
|
||||||
|
if (c == '*' || c == '/') return 2;
|
||||||
|
if (c == '+' || c == '-') return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 打印一行后缀表达式:tokens[0..n-1]
|
||||||
|
// 参数 trailing_space 控制是否在行末额外打印一个空格
|
||||||
|
static void print_tokens(const Token *t, int n, int trailing_space) {
|
||||||
|
for (int i = 0; i < n; ++i) {
|
||||||
|
if (t[i].is_num) {
|
||||||
|
printf("%d", t[i].val);
|
||||||
|
} else {
|
||||||
|
printf("%c", t[i].op);
|
||||||
|
}
|
||||||
|
if (i + 1 < n) putchar(' ');
|
||||||
|
}
|
||||||
|
if (trailing_space) putchar(' ');
|
||||||
|
putchar('\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 计算 a (op) b,/ 为整除(向零),其余为常规整数算术
|
||||||
|
static inline int apply_int(int a, int b, char op) {
|
||||||
|
switch (op) {
|
||||||
|
case '+': return a + b;
|
||||||
|
case '-': return a - b;
|
||||||
|
case '*': return a * b;
|
||||||
|
case '/': return (b == 0 ? 0 : a / b); // 题面保证不会越界;防御除零
|
||||||
|
default: return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
// 读取整行
|
||||||
|
char line[256];
|
||||||
|
if (!fgets(line, sizeof(line), stdin)) return 0;
|
||||||
|
size_t len = strlen(line);
|
||||||
|
while (len && (line[len-1] == '\n' || line[len-1] == '\r')) line[--len] = '\0';
|
||||||
|
|
||||||
|
// 中缀转后缀(shunting-yard)
|
||||||
|
Token out[256];
|
||||||
|
int on = 0;
|
||||||
|
char ops[256];
|
||||||
|
int ot = 0;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < len; ++i) {
|
||||||
|
char c = line[i];
|
||||||
|
if (isspace((unsigned char)c)) continue;
|
||||||
|
if (c >= '0' && c <= '9') {
|
||||||
|
out[on].is_num = 1;
|
||||||
|
out[on].val = c - '0';
|
||||||
|
out[on].op = 0;
|
||||||
|
++on;
|
||||||
|
} else if (c == '+' || c == '-' || c == '*' || c == '/') {
|
||||||
|
int p = prec(c);
|
||||||
|
while (ot > 0 && ops[ot - 1] != '(' && prec(ops[ot - 1]) >= p) {
|
||||||
|
out[on].is_num = 0;
|
||||||
|
out[on].op = ops[--ot];
|
||||||
|
++on;
|
||||||
|
}
|
||||||
|
ops[ot++] = c;
|
||||||
|
} else if (c == '(') {
|
||||||
|
ops[ot++] = c;
|
||||||
|
} else if (c == ')') {
|
||||||
|
while (ot > 0 && ops[ot - 1] != '(') {
|
||||||
|
out[on].is_num = 0;
|
||||||
|
out[on].op = ops[--ot];
|
||||||
|
++on;
|
||||||
|
}
|
||||||
|
if (ot > 0 && ops[ot - 1] == '(') --ot; // 弹出左括号
|
||||||
|
} else {
|
||||||
|
// 题面保证无需判错
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (ot > 0) {
|
||||||
|
out[on].is_num = 0;
|
||||||
|
out[on].op = ops[--ot];
|
||||||
|
++on;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 打印首行(保留行末空格)
|
||||||
|
print_tokens(out, on, 1);
|
||||||
|
|
||||||
|
// 逐步规约:每次找到第一个运算符(其前必有两个数字),规约为一个数字
|
||||||
|
Token cur[256];
|
||||||
|
memcpy(cur, out, on * sizeof(Token));
|
||||||
|
int n = on;
|
||||||
|
|
||||||
|
while (n > 1) {
|
||||||
|
int idx = -1;
|
||||||
|
for (int i = 0; i < n; ++i) {
|
||||||
|
if (!cur[i].is_num) { idx = i; break; }
|
||||||
|
}
|
||||||
|
// 规约 cur[idx-2], cur[idx-1], cur[idx]
|
||||||
|
int a = cur[idx - 2].val;
|
||||||
|
int b = cur[idx - 1].val;
|
||||||
|
int r = apply_int(a, b, cur[idx].op);
|
||||||
|
// 将结果写回到 idx-2,并左移覆盖 idx-1 与 idx
|
||||||
|
cur[idx - 2].is_num = 1;
|
||||||
|
cur[idx - 2].val = r;
|
||||||
|
// 左移 n-(idx+1) 个元素
|
||||||
|
for (int j = idx - 1; j + 2 < n; ++j) {
|
||||||
|
cur[j] = cur[j + 2];
|
||||||
|
}
|
||||||
|
n -= 2;
|
||||||
|
|
||||||
|
// 是否最后一行:n==1 时保留行末空格,否则不保留
|
||||||
|
print_tokens(cur, n, (n == 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,111 @@
|
|||||||
|
// 简单逻辑表达式求值:支持 true/false 与 not/and/or,带错误检测
|
||||||
|
// 规则:
|
||||||
|
// - 仅包含单词 true, false, not, and, or 与空格;全部小写
|
||||||
|
// - 优先级:not > and > or;同级从左到右(左结合)
|
||||||
|
// - 输入为一行,输出为 true / false;如果语法或词法错误,输出 error
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
|
typedef enum { TK_TRUE, TK_FALSE, TK_AND, TK_OR, TK_NOT } Tok;
|
||||||
|
|
||||||
|
// 读取一行到缓冲区,并去除尾部换行符
|
||||||
|
static int read_line(char *buf, size_t sz) {
|
||||||
|
if (!fgets(buf, (int)sz, stdin)) return 0;
|
||||||
|
size_t n = strlen(buf);
|
||||||
|
while (n && (buf[n-1] == '\n' || buf[n-1] == '\r')) buf[--n] = '\0';
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 词法:按字母分组,提取单词,映射到令牌;出错返回 -1
|
||||||
|
static int tokenize(const char *s, Tok *out, int maxn) {
|
||||||
|
int nt = 0;
|
||||||
|
size_t i = 0, n = strlen(s);
|
||||||
|
while (i < n) {
|
||||||
|
while (i < n && isspace((unsigned char)s[i])) ++i;
|
||||||
|
if (i >= n) break;
|
||||||
|
if (!isalpha((unsigned char)s[i])) return -1; // 非法字符
|
||||||
|
size_t j = i;
|
||||||
|
while (j < n && isalpha((unsigned char)s[j])) ++j;
|
||||||
|
size_t wlen = j - i;
|
||||||
|
if (wlen == 4 && strncmp(s + i, "true", 4) == 0) {
|
||||||
|
if (nt >= maxn) return -1;
|
||||||
|
out[nt++] = TK_TRUE;
|
||||||
|
} else if (wlen == 5 && strncmp(s + i, "false", 5) == 0) {
|
||||||
|
if (nt >= maxn) return -1;
|
||||||
|
out[nt++] = TK_FALSE;
|
||||||
|
} else if (wlen == 3 && strncmp(s + i, "and", 3) == 0) {
|
||||||
|
if (nt >= maxn) return -1;
|
||||||
|
out[nt++] = TK_AND;
|
||||||
|
} else if (wlen == 2 && strncmp(s + i, "or", 2) == 0) {
|
||||||
|
if (nt >= maxn) return -1;
|
||||||
|
out[nt++] = TK_OR;
|
||||||
|
} else if (wlen == 3 && strncmp(s + i, "not", 3) == 0) {
|
||||||
|
if (nt >= maxn) return -1;
|
||||||
|
out[nt++] = TK_NOT;
|
||||||
|
} else {
|
||||||
|
return -1; // 未知单词
|
||||||
|
}
|
||||||
|
i = j;
|
||||||
|
}
|
||||||
|
return nt;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 递归下降(迭代实现)语法:
|
||||||
|
// unary := { not }* ( true | false )
|
||||||
|
// and := unary { and unary }*
|
||||||
|
// or := and { or and }*
|
||||||
|
|
||||||
|
static int parse_unary(const Tok *toks, int nt, int *idx, int *ok) {
|
||||||
|
int neg = 0;
|
||||||
|
while (*idx < nt && toks[*idx] == TK_NOT) { ++neg; ++*idx; }
|
||||||
|
if (*idx >= nt) { *ok = 0; return 0; }
|
||||||
|
int val;
|
||||||
|
if (toks[*idx] == TK_TRUE) { val = 1; }
|
||||||
|
else if (toks[*idx] == TK_FALSE) { val = 0; }
|
||||||
|
else { *ok = 0; return 0; }
|
||||||
|
++*idx;
|
||||||
|
if (neg & 1) val = !val;
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int parse_and(const Tok *toks, int nt, int *idx, int *ok) {
|
||||||
|
int lhs = parse_unary(toks, nt, idx, ok);
|
||||||
|
if (!*ok) return 0;
|
||||||
|
while (*idx < nt && toks[*idx] == TK_AND) {
|
||||||
|
++*idx;
|
||||||
|
int rhs = parse_unary(toks, nt, idx, ok);
|
||||||
|
if (!*ok) return 0;
|
||||||
|
lhs = (lhs && rhs);
|
||||||
|
}
|
||||||
|
return lhs;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int parse_or(const Tok *toks, int nt, int *idx, int *ok) {
|
||||||
|
int lhs = parse_and(toks, nt, idx, ok);
|
||||||
|
if (!*ok) return 0;
|
||||||
|
while (*idx < nt && toks[*idx] == TK_OR) {
|
||||||
|
++*idx;
|
||||||
|
int rhs = parse_and(toks, nt, idx, ok);
|
||||||
|
if (!*ok) return 0;
|
||||||
|
lhs = (lhs || rhs);
|
||||||
|
}
|
||||||
|
return lhs;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
char line[512];
|
||||||
|
if (!read_line(line, sizeof(line))) return 0;
|
||||||
|
|
||||||
|
Tok toks[300];
|
||||||
|
int nt = tokenize(line, toks, 300);
|
||||||
|
if (nt <= 0) { printf("error\n"); return 0; }
|
||||||
|
|
||||||
|
int idx = 0, ok = 1;
|
||||||
|
int result = parse_or(toks, nt, &idx, &ok);
|
||||||
|
if (!ok || idx != nt) { printf("error\n"); return 0; }
|
||||||
|
|
||||||
|
printf(result ? "true\n" : "false\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
// 地道连通查询(线性房屋 1..n):支持操作
|
||||||
|
// D x: 摧毁房屋 x;R: 修复上一次摧毁的房屋;Q x: 询问士兵在 x 能到达的房屋数
|
||||||
|
// 思路:维护“被摧毁房屋”的有序集合,查询时找离 x 最近的左右两个摧毁位置。
|
||||||
|
// 为了在 C 中高效实现前驱/后继,使用树状数组(Fenwick)存放摧毁标记(1 表示摧毁),
|
||||||
|
// 并用“按前缀和查找第 k 个 1”的技巧 O(log n) 求出前驱/后继位置。
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
static int N; // 房屋总数
|
||||||
|
static int *bit; // 树状数组,1..N
|
||||||
|
static int *dead; // 摧毁标记
|
||||||
|
static int *stackD; // 摧毁栈,支持 R
|
||||||
|
static int topD = 0;
|
||||||
|
|
||||||
|
static inline int lowbit(int x) { return x & -x; }
|
||||||
|
|
||||||
|
static void bit_add(int i, int delta) {
|
||||||
|
for (; i <= N; i += lowbit(i)) bit[i] += delta;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int bit_sum(int i) {
|
||||||
|
int s = 0;
|
||||||
|
for (; i > 0; i -= lowbit(i)) s += bit[i];
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 返回使得前缀和等于 k 的最小下标(1..N)。要求 k>=1 且 k<=sum(N)
|
||||||
|
static int bit_find_kth(int k) {
|
||||||
|
int idx = 0;
|
||||||
|
// 最大 2 的幂覆盖到 N
|
||||||
|
int p = 1;
|
||||||
|
while ((p << 1) <= N) p <<= 1;
|
||||||
|
for (; p; p >>= 1) {
|
||||||
|
int next = idx + p;
|
||||||
|
if (next <= N && bit[next] < k) {
|
||||||
|
idx = next;
|
||||||
|
k -= bit[next];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return idx + 1; // 1..N
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
int n, m;
|
||||||
|
if (scanf("%d %d", &n, &m) != 2) return 0;
|
||||||
|
N = n;
|
||||||
|
bit = (int *)calloc((size_t)N + 1u, sizeof(int));
|
||||||
|
dead = (int *)calloc((size_t)N + 2u, sizeof(int));
|
||||||
|
stackD = (int *)malloc(((size_t)m + 5u) * sizeof(int));
|
||||||
|
if (!bit || !dead || !stackD) return 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < m; ++i) {
|
||||||
|
char cmd[8];
|
||||||
|
if (scanf("%7s", cmd) != 1) break;
|
||||||
|
if (cmd[0] == 'D') {
|
||||||
|
int x; scanf("%d", &x);
|
||||||
|
if (x >= 1 && x <= n && !dead[x]) {
|
||||||
|
dead[x] = 1;
|
||||||
|
bit_add(x, +1);
|
||||||
|
stackD[topD++] = x;
|
||||||
|
}
|
||||||
|
} else if (cmd[0] == 'R') {
|
||||||
|
if (topD > 0) {
|
||||||
|
int x = stackD[--topD];
|
||||||
|
if (dead[x]) { dead[x] = 0; bit_add(x, -1); }
|
||||||
|
}
|
||||||
|
} else if (cmd[0] == 'Q') {
|
||||||
|
int x; scanf("%d", &x);
|
||||||
|
if (x < 1 || x > n) { printf("0\n"); continue; }
|
||||||
|
if (dead[x]) {
|
||||||
|
printf("0\n");
|
||||||
|
} else {
|
||||||
|
int left_cnt = bit_sum(x - 1);
|
||||||
|
int total_cnt = bit_sum(n);
|
||||||
|
int upto_x_cnt = bit_sum(x);
|
||||||
|
int left_break = (left_cnt == 0) ? 0 : bit_find_kth(left_cnt);
|
||||||
|
int right_break = (upto_x_cnt == total_cnt) ? (n + 1) : bit_find_kth(upto_x_cnt + 1);
|
||||||
|
int reachable = right_break - left_break - 1;
|
||||||
|
printf("%d\n", reachable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
free(bit); free(dead); free(stackD);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,152 @@
|
|||||||
|
// 随机连续子序列的最小值期望满意度:
|
||||||
|
// 选取所有子数组(连续子序列)均匀随机;score 为该子数组的最小值;满意度为 max(0, score - expect)
|
||||||
|
// 目标:对每个 expect 求 E[max(0, min - expect)],用最简分数输出。
|
||||||
|
// 做法:
|
||||||
|
// - 用单调栈统计每个位置作为“子数组最小值的代表”的子数组个数 c_i,采用去重规则:
|
||||||
|
// prev 为前一个 < 当前值的位置,next 为后一个 <= 当前值的位置;贡献个数为 (i-prev)*(next-i)
|
||||||
|
// - 将相同值的贡献累加,得到“最小值为 v 的子数组个数” count[v]
|
||||||
|
// - 总子数组数 T = n*(n+1)/2;对 expect=e,有
|
||||||
|
// E = (1/T) * sum_{v>e} (v-e) * count[v]
|
||||||
|
// = (1/T) * ( sum_{v>e} v*count[v] - e * sum_{v>e} count[v] )
|
||||||
|
// - 预先按 v 排序并做前缀和,查询时二分找 v>e 的起点,O(log n) 得到分子;化简为最简分数输出。
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef long long ll;
|
||||||
|
typedef unsigned long long ull;
|
||||||
|
typedef __int128 i128;
|
||||||
|
typedef unsigned __int128 u128;
|
||||||
|
|
||||||
|
typedef struct { ll v; ull c; } Pair;
|
||||||
|
|
||||||
|
static int cmp_pair(const void *a, const void *b) {
|
||||||
|
const Pair *x = (const Pair *)a, *y = (const Pair *)b;
|
||||||
|
if (x->v < y->v) return -1;
|
||||||
|
if (x->v > y->v) return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void print_u128(u128 x) {
|
||||||
|
if (x == 0) { putchar('0'); return; }
|
||||||
|
char buf[64];
|
||||||
|
int p = 0;
|
||||||
|
while (x) {
|
||||||
|
unsigned int d = (unsigned int)(x % 10);
|
||||||
|
buf[p++] = (char)('0' + d);
|
||||||
|
x /= 10;
|
||||||
|
}
|
||||||
|
while (p--) putchar(buf[p]);
|
||||||
|
}
|
||||||
|
|
||||||
|
static ull gcd_ull(ull a, ull b) {
|
||||||
|
while (b) { ull t = a % b; a = b; b = t; }
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
int T;
|
||||||
|
if (scanf("%d", &T) != 1) return 0;
|
||||||
|
for (int tc = 1; tc <= T; ++tc) {
|
||||||
|
int n; if (scanf("%d", &n) != 1) return 0;
|
||||||
|
ll *a = (ll *)malloc(((size_t)n + 5u) * sizeof(ll));
|
||||||
|
for (int i = 1; i <= n; ++i) scanf("%lld", &a[i]);
|
||||||
|
|
||||||
|
// 单调栈:prev(<) 与 next(<=)
|
||||||
|
int *prev = (int *)malloc(((size_t)n + 5u) * sizeof(int));
|
||||||
|
int *next = (int *)malloc(((size_t)n + 5u) * sizeof(int));
|
||||||
|
int *st = (int *)malloc(((size_t)n + 5u) * sizeof(int));
|
||||||
|
int top = 0;
|
||||||
|
for (int i = 1; i <= n; ++i) {
|
||||||
|
while (top > 0 && a[st[top - 1]] >= a[i]) --top;
|
||||||
|
prev[i] = (top == 0 ? 0 : st[top - 1]);
|
||||||
|
st[top++] = i;
|
||||||
|
}
|
||||||
|
top = 0;
|
||||||
|
for (int i = n; i >= 1; --i) {
|
||||||
|
while (top > 0 && a[st[top - 1]] > a[i]) --top;
|
||||||
|
next[i] = (top == 0 ? (n + 1) : st[top - 1]);
|
||||||
|
st[top++] = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 生成 (value, count) 对
|
||||||
|
Pair *pairs = (Pair *)malloc(((size_t)n + 5u) * sizeof(Pair));
|
||||||
|
for (int i = 1; i <= n; ++i) {
|
||||||
|
ull L = (ull)(i - prev[i]);
|
||||||
|
ull R = (ull)(next[i] - i);
|
||||||
|
pairs[i - 1].v = a[i];
|
||||||
|
pairs[i - 1].c = L * R;
|
||||||
|
}
|
||||||
|
|
||||||
|
qsort(pairs, (size_t)n, sizeof(Pair), cmp_pair);
|
||||||
|
|
||||||
|
// 压缩相同值
|
||||||
|
int k = 0;
|
||||||
|
Pair *uniq = (Pair *)malloc(((size_t)n + 5u) * sizeof(Pair));
|
||||||
|
for (int i = 0; i < n; ) {
|
||||||
|
ll v = pairs[i].v; u128 csum = 0;
|
||||||
|
while (i < n && pairs[i].v == v) { csum += (u128)pairs[i].c; ++i; }
|
||||||
|
uniq[k].v = v; // csum 可能 >2^64,但总子数组数 <= 5e10,仍可放进 64bit
|
||||||
|
// 但为了稳妥,仍按 64 位存储(安全范围内)
|
||||||
|
unsigned long long cc = (unsigned long long)csum; // csum <= n*(n+1)/2 <= 5e10
|
||||||
|
uniq[k].c = cc;
|
||||||
|
++k;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 前缀和(用于后缀查询)
|
||||||
|
unsigned long long *prefC = (unsigned long long *)malloc(((size_t)k + 5u) * sizeof(unsigned long long));
|
||||||
|
u128 *prefVC = (u128 *)malloc(((size_t)k + 5u) * sizeof(u128));
|
||||||
|
u128 sVC = 0; unsigned long long sC = 0;
|
||||||
|
for (int i = 0; i < k; ++i) {
|
||||||
|
sC += uniq[i].c;
|
||||||
|
sVC += (u128)((i128)uniq[i].v) * (u128)uniq[i].c;
|
||||||
|
prefC[i] = sC;
|
||||||
|
prefVC[i] = sVC;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 总子数组数
|
||||||
|
unsigned long long Total = (unsigned long long)n * (unsigned long long)(n + 1) / 2ull;
|
||||||
|
|
||||||
|
int m; scanf("%d", &m);
|
||||||
|
printf("Case %d:\n", tc);
|
||||||
|
for (int qi = 0; qi < m; ++qi) {
|
||||||
|
long long e; scanf("%lld", &e);
|
||||||
|
// 二分找第一个 v>e 的位置
|
||||||
|
int lo = 0, hi = k;
|
||||||
|
while (lo < hi) {
|
||||||
|
int mid = (lo + hi) >> 1;
|
||||||
|
if (uniq[mid].v <= e) lo = mid + 1; else hi = mid;
|
||||||
|
}
|
||||||
|
if (lo == k) { puts("0"); continue; }
|
||||||
|
// 后缀和
|
||||||
|
unsigned long long S2 = prefC[k - 1] - (lo ? prefC[lo - 1] : 0ull);
|
||||||
|
u128 S1 = prefVC[k - 1] - (lo ? prefVC[lo - 1] : (u128)0);
|
||||||
|
// 分子:S1 - e*S2
|
||||||
|
u128 Numer = S1 - (u128)((i128)e) * (u128)S2;
|
||||||
|
if (Numer == 0) { puts("0"); continue; }
|
||||||
|
// 若可整除,输出整数
|
||||||
|
u128 mod = Numer % (u128)Total;
|
||||||
|
if (mod == 0) {
|
||||||
|
u128 A = Numer / (u128)Total;
|
||||||
|
print_u128(A); putchar('\n');
|
||||||
|
} else {
|
||||||
|
unsigned long long r = (unsigned long long)mod; // mod < Total
|
||||||
|
unsigned long long g = gcd_ull(Total, r);
|
||||||
|
u128 A = Numer / (u128)g;
|
||||||
|
unsigned long long B = Total / g;
|
||||||
|
print_u128(A); putchar('/');
|
||||||
|
// 打印 B(64 位)
|
||||||
|
char buf[32]; int p = 0; unsigned long long Bb = B;
|
||||||
|
if (Bb == 0) { putchar('0'); }
|
||||||
|
else {
|
||||||
|
while (Bb) { buf[p++] = (char)('0' + (Bb % 10)); Bb /= 10; }
|
||||||
|
while (p--) putchar(buf[p]);
|
||||||
|
}
|
||||||
|
putchar('\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
free(a); free(prev); free(next); free(st); free(pairs); free(uniq); free(prefC); free(prefVC);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
// 优化版:每个区间长度 k(1..n) 的最大区间价值 max(subarray_max * subarray_min)
|
||||||
|
// 算法:最大值笛卡尔树 + 跨越合并(单调两指针),整体显著优于 O(n^2)
|
||||||
|
// 说明:对每个树节点 i 及其覆盖段 [l,r],所有包含 i 的子数组最大值均为 a[i];
|
||||||
|
// 设从 i 向左取 x 个、向右取 y 个,长度 k=x+y+1,区间最小值为 min(Lmin[x], Rmin[y]);
|
||||||
|
// 其中 Lmin/Rmin 为以 i 为中心的左/右最小值前后缀。对固定 s=x+y,
|
||||||
|
// 利用两指针在可行范围内最大化 min(Lmin[s-y], Rmin[y]),O(lenL+lenR) 完成一次合并。
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef long long ll;
|
||||||
|
|
||||||
|
static inline ll minll(ll a, ll b) { return a < b ? a : b; }
|
||||||
|
|
||||||
|
// 构建“以最大值为堆”的笛卡尔树。相等值用 <= 弹出以保证唯一性(倾向右侧作为父)。
|
||||||
|
// 输出:left child lc,right child rc,parent;返回根下标。
|
||||||
|
static int build_cartesian_tree(const ll *a, int n, int *lc, int *rc, int *parent) {
|
||||||
|
int *st = (int*)malloc((n + 1) * sizeof(int));
|
||||||
|
int top = 0;
|
||||||
|
int root = 0;
|
||||||
|
for (int i = 1; i <= n; ++i) {
|
||||||
|
int last = 0;
|
||||||
|
while (top > 0 && a[st[top - 1]] <= a[i]) {
|
||||||
|
last = st[--top];
|
||||||
|
}
|
||||||
|
if (top > 0) {
|
||||||
|
rc[st[top - 1]] = i;
|
||||||
|
parent[i] = st[top - 1];
|
||||||
|
} else {
|
||||||
|
root = i;
|
||||||
|
parent[i] = 0;
|
||||||
|
}
|
||||||
|
lc[i] = last;
|
||||||
|
if (last) parent[last] = i;
|
||||||
|
st[top++] = i;
|
||||||
|
}
|
||||||
|
free(st);
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
int n;
|
||||||
|
if (scanf("%d", &n) != 1) return 0;
|
||||||
|
int cap = n + 5;
|
||||||
|
int *dq = (int*)malloc((size_t)cap * sizeof(int));
|
||||||
|
int head = 0, tail = 0, size = 0;
|
||||||
|
char *out = (char*)malloc((size_t)(n * 14 + 1));
|
||||||
|
int pos = 0;
|
||||||
|
for (int i = 0; i < n; ++i) {
|
||||||
|
int op;
|
||||||
|
scanf("%d", &op);
|
||||||
|
if (op == 1) {
|
||||||
|
int x; scanf("%d", &x);
|
||||||
|
head = (head - 1 + cap) % cap;
|
||||||
|
dq[head] = x;
|
||||||
|
++size;
|
||||||
|
} else if (op == 2) {
|
||||||
|
int x; scanf("%d", &x);
|
||||||
|
dq[tail] = x;
|
||||||
|
tail = (tail + 1) % cap;
|
||||||
|
++size;
|
||||||
|
} else if (op == 3) {
|
||||||
|
pos += sprintf(out + pos, "%d\n", dq[head]);
|
||||||
|
} else if (op == 4) {
|
||||||
|
int idx = (tail - 1 + cap) % cap;
|
||||||
|
pos += sprintf(out + pos, "%d\n", dq[idx]);
|
||||||
|
} else if (op == 5) {
|
||||||
|
head = (head + 1) % cap;
|
||||||
|
--size;
|
||||||
|
} else if (op == 6) {
|
||||||
|
tail = (tail - 1 + cap) % cap;
|
||||||
|
--size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fwrite(out, 1, (size_t)pos, stdout);
|
||||||
|
free(out);
|
||||||
|
free(dq);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
int n, m;
|
||||||
|
if (scanf("%d %d", &n, &m) != 2) return 0;
|
||||||
|
int *a = (int*)malloc((size_t)(n + 1) * sizeof(int));
|
||||||
|
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
|
||||||
|
int *cnt = (int*)calloc((size_t)(m + 1), sizeof(int));
|
||||||
|
int covered = 0;
|
||||||
|
int l = 1;
|
||||||
|
int best_l = 1, best_r = n;
|
||||||
|
int best_len = n;
|
||||||
|
for (int r = 1; r <= n; ++r) {
|
||||||
|
int c = a[r];
|
||||||
|
if (cnt[c] == 0) ++covered;
|
||||||
|
++cnt[c];
|
||||||
|
if (covered == m) {
|
||||||
|
while (cnt[a[l]] > 1) { --cnt[a[l]]; ++l; }
|
||||||
|
int len = r - l + 1;
|
||||||
|
if (len < best_len || (len == best_len && l < best_l)) {
|
||||||
|
best_len = len; best_l = l; best_r = r;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("%d %d\n", best_l, best_r);
|
||||||
|
free(cnt);
|
||||||
|
free(a);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// 递归实现
|
||||||
|
long long f_recursive(int n)
|
||||||
|
{
|
||||||
|
if (n == 1)
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return n + f_recursive(n - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 数学公式实现(更高效)
|
||||||
|
long long f_formula(long long n)
|
||||||
|
{
|
||||||
|
return n * (n + 1) / 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int T;
|
||||||
|
scanf("%d", &T);
|
||||||
|
|
||||||
|
while (T--)
|
||||||
|
{
|
||||||
|
long long n;
|
||||||
|
scanf("%lld", &n);
|
||||||
|
|
||||||
|
// 由于n可能很大(<10^9),使用数学公式更安全
|
||||||
|
printf("%lld\n", f_formula(n));
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,156 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#include <direct.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// 二叉树节点定义
|
||||||
|
struct BiTNode
|
||||||
|
{
|
||||||
|
char data; // 节点数据
|
||||||
|
struct BiTNode *lchild; // 左子节点
|
||||||
|
struct BiTNode *rchild; // 右子节点
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct BiTNode *BiTree;
|
||||||
|
|
||||||
|
// 函数声明
|
||||||
|
void CreateBiTree(BiTree *T); // 创建二叉树
|
||||||
|
void PreOrderTraverse(BiTree T); // 先序遍历
|
||||||
|
void InOrderTraverse(BiTree T); // 中序遍历
|
||||||
|
void PostOrderTraverse(BiTree T); // 后序遍历
|
||||||
|
void PrintMenu(); // 打印菜单
|
||||||
|
void FreeTree(BiTree T);
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// 设置控制台编码为UTF-8,防止中文乱码
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul"); // 设置控制台编码为UTF-8
|
||||||
|
SetConsoleOutputCP(65001); // 设置控制台输出编码
|
||||||
|
SetConsoleCP(65001); // 设置控制台输入编码
|
||||||
|
#endif
|
||||||
|
|
||||||
|
BiTree T = NULL;
|
||||||
|
int choice;
|
||||||
|
|
||||||
|
printf("=== 链式二叉树遍历系统 (C语言版本) ===\n");
|
||||||
|
printf("请输入先序扩展序列构建二叉树(# 表示空节点,例如 AB#C##D##):\n");
|
||||||
|
CreateBiTree(&T);
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
PrintMenu();
|
||||||
|
if (scanf("%d", &choice) != 1)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (choice)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
printf("先序遍历结果: ");
|
||||||
|
PreOrderTraverse(T);
|
||||||
|
printf("\n");
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
printf("中序遍历结果: ");
|
||||||
|
InOrderTraverse(T);
|
||||||
|
printf("\n");
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
printf("后序遍历结果: ");
|
||||||
|
PostOrderTraverse(T);
|
||||||
|
printf("\n");
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
printf("程序已退出。\n");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
printf("无效输入,请输入 1~4 之间的数字!\n");
|
||||||
|
}
|
||||||
|
} while (choice != 4);
|
||||||
|
|
||||||
|
FreeTree(T);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建二叉树(先序扩展输入)
|
||||||
|
void CreateBiTree(BiTree *T)
|
||||||
|
{
|
||||||
|
char ch;
|
||||||
|
if (scanf(" %c", &ch) != 1)
|
||||||
|
{
|
||||||
|
*T = NULL;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ch == '#')
|
||||||
|
{
|
||||||
|
*T = NULL;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
*T = (struct BiTNode *)malloc(sizeof(struct BiTNode));
|
||||||
|
(*T)->data = ch;
|
||||||
|
(*T)->lchild = NULL;
|
||||||
|
(*T)->rchild = NULL;
|
||||||
|
CreateBiTree(&(*T)->lchild);
|
||||||
|
CreateBiTree(&(*T)->rchild);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 先序遍历:根 -> 左 -> 右
|
||||||
|
void PreOrderTraverse(BiTree T)
|
||||||
|
{
|
||||||
|
if (T != NULL)
|
||||||
|
{
|
||||||
|
printf("%c ", T->data);
|
||||||
|
PreOrderTraverse(T->lchild);
|
||||||
|
PreOrderTraverse(T->rchild);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 中序遍历:左 -> 根 -> 右
|
||||||
|
void InOrderTraverse(BiTree T)
|
||||||
|
{
|
||||||
|
if (T != NULL)
|
||||||
|
{
|
||||||
|
InOrderTraverse(T->lchild);
|
||||||
|
printf("%c ", T->data);
|
||||||
|
InOrderTraverse(T->rchild);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 后序遍历:左 -> 右 -> 根
|
||||||
|
void PostOrderTraverse(BiTree T)
|
||||||
|
{
|
||||||
|
if (T != NULL)
|
||||||
|
{
|
||||||
|
PostOrderTraverse(T->lchild);
|
||||||
|
PostOrderTraverse(T->rchild);
|
||||||
|
printf("%c ", T->data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 打印菜单
|
||||||
|
void PrintMenu()
|
||||||
|
{
|
||||||
|
printf("\n--- 选择遍历方式 ---\n");
|
||||||
|
printf("1. 先序遍历\n");
|
||||||
|
printf("2. 中序遍历\n");
|
||||||
|
printf("3. 后序遍历\n");
|
||||||
|
printf("4. 退出\n");
|
||||||
|
printf("请输入选择 (1-4): ");
|
||||||
|
}
|
||||||
|
|
||||||
|
void FreeTree(BiTree T)
|
||||||
|
{
|
||||||
|
if (T)
|
||||||
|
{
|
||||||
|
FreeTree(T->lchild);
|
||||||
|
FreeTree(T->rchild);
|
||||||
|
free(T);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,312 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#include <direct.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// 链式二叉树节点结构
|
||||||
|
struct BiTNode
|
||||||
|
{
|
||||||
|
char data;
|
||||||
|
struct BiTNode *lchild;
|
||||||
|
struct BiTNode *rchild;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 二叉树指针类型定义
|
||||||
|
typedef struct BiTNode *BiTree;
|
||||||
|
|
||||||
|
// 函数声明
|
||||||
|
void CreateBiTree(BiTree *T); // 创建二叉树
|
||||||
|
void PreOrderTraverse(BiTree T); // 先序遍历
|
||||||
|
void InOrderTraverse(BiTree T); // 中序遍历
|
||||||
|
void PostOrderTraverse(BiTree T); // 后序遍历
|
||||||
|
void LevelOrderTraverse(BiTree T); // 层次遍历
|
||||||
|
int MaxWidth(BiTree T); // 计算最大宽度
|
||||||
|
void PrintMenu(); // 打印菜单
|
||||||
|
void FreeTree(BiTree T); // 释放二叉树内存
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
// 设置控制台编码为UTF-8,防止中文乱码
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul"); // 设置控制台编码为UTF-8
|
||||||
|
SetConsoleOutputCP(65001); // 设置控制台输出编码
|
||||||
|
SetConsoleCP(65001); // 设置控制台输入编码
|
||||||
|
#endif
|
||||||
|
|
||||||
|
BiTree T = NULL;
|
||||||
|
int choice;
|
||||||
|
|
||||||
|
printf("=== 链式二叉树遍历系统 (C语言非递归) ===\n");
|
||||||
|
printf("请输入先序扩展序列构建二叉树(# 表示空节点,例如 AB#C##D##):\n");
|
||||||
|
CreateBiTree(&T);
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
PrintMenu();
|
||||||
|
if (scanf("%d", &choice) != 1)
|
||||||
|
{
|
||||||
|
printf("无效输入,请输入1~6之间的数字!\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (choice)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
printf("先序遍历结果: ");
|
||||||
|
PreOrderTraverse(T);
|
||||||
|
printf("\n");
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
printf("中序遍历结果: ");
|
||||||
|
InOrderTraverse(T);
|
||||||
|
printf("\n");
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
printf("后序遍历结果: ");
|
||||||
|
PostOrderTraverse(T);
|
||||||
|
printf("\n");
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
printf("层次遍历结果: ");
|
||||||
|
LevelOrderTraverse(T);
|
||||||
|
printf("\n");
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
printf("程序已退出。\n");
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
printf("最大宽度: %d\n", MaxWidth(T));
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
printf("无效输入,请输入 1~6 之间的数字!\n");
|
||||||
|
}
|
||||||
|
} while (choice != 5);
|
||||||
|
|
||||||
|
FreeTree(T);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建二叉树(先序扩展输入)
|
||||||
|
void CreateBiTree(BiTree *T)
|
||||||
|
{
|
||||||
|
char ch;
|
||||||
|
if (scanf(" %c", &ch) != 1)
|
||||||
|
{
|
||||||
|
*T = NULL;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ch == '#')
|
||||||
|
{
|
||||||
|
*T = NULL;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
*T = (struct BiTNode *)malloc(sizeof(struct BiTNode));
|
||||||
|
(*T)->data = ch;
|
||||||
|
(*T)->lchild = NULL;
|
||||||
|
(*T)->rchild = NULL;
|
||||||
|
CreateBiTree(&(*T)->lchild);
|
||||||
|
CreateBiTree(&(*T)->rchild);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 先序遍历:根 -> 左 -> 右
|
||||||
|
void PreOrderTraverse(BiTree T)
|
||||||
|
{
|
||||||
|
if (!T)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 栈初始化,用栈来显性表示递归时的函数调用
|
||||||
|
int cap = 128, top = 0;
|
||||||
|
BiTree *st = (BiTree *)malloc(sizeof(BiTree) * cap);
|
||||||
|
st[top++] = T;
|
||||||
|
|
||||||
|
while (top)
|
||||||
|
{
|
||||||
|
BiTree p = st[--top];
|
||||||
|
printf("%c ", p->data);
|
||||||
|
if (p->rchild)
|
||||||
|
{
|
||||||
|
if (top >= cap)
|
||||||
|
{
|
||||||
|
cap *= 2;
|
||||||
|
st = (BiTree *)realloc(st, sizeof(BiTree) * cap);
|
||||||
|
}
|
||||||
|
st[top++] = p->rchild;
|
||||||
|
}
|
||||||
|
if (p->lchild)
|
||||||
|
{
|
||||||
|
if (top >= cap)
|
||||||
|
{
|
||||||
|
cap *= 2;
|
||||||
|
st = (BiTree *)realloc(st, sizeof(BiTree) * cap);
|
||||||
|
}
|
||||||
|
st[top++] = p->lchild;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(st);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 中序遍历:左 -> 根 -> 右
|
||||||
|
void InOrderTraverse(BiTree T)
|
||||||
|
{
|
||||||
|
int cap = 128, top = 0;
|
||||||
|
BiTree *st = (BiTree *)malloc(sizeof(BiTree) * cap);
|
||||||
|
BiTree cur = T;
|
||||||
|
while (cur || top)
|
||||||
|
{
|
||||||
|
while (cur)
|
||||||
|
{
|
||||||
|
if (top >= cap)
|
||||||
|
{
|
||||||
|
cap *= 2;
|
||||||
|
st = (BiTree *)realloc(st, sizeof(BiTree) * cap);
|
||||||
|
}
|
||||||
|
st[top++] = cur;
|
||||||
|
cur = cur->lchild;
|
||||||
|
}
|
||||||
|
BiTree p = st[--top];
|
||||||
|
printf("%c ", p->data);
|
||||||
|
cur = p->rchild;
|
||||||
|
}
|
||||||
|
free(st);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 后序遍历:左 -> 右 -> 根
|
||||||
|
void PostOrderTraverse(BiTree T)
|
||||||
|
{
|
||||||
|
int cap = 128, top = 0;
|
||||||
|
BiTree *st = (BiTree *)malloc(sizeof(BiTree) * cap);
|
||||||
|
BiTree cur = T, last = NULL;
|
||||||
|
while (cur || top)
|
||||||
|
{
|
||||||
|
while (cur)
|
||||||
|
{
|
||||||
|
if (top >= cap)
|
||||||
|
{
|
||||||
|
cap *= 2;
|
||||||
|
st = (BiTree *)realloc(st, sizeof(BiTree) * cap);
|
||||||
|
}
|
||||||
|
st[top++] = cur;
|
||||||
|
cur = cur->lchild;
|
||||||
|
}
|
||||||
|
BiTree p = st[top - 1];
|
||||||
|
if (p->rchild && last != p->rchild)
|
||||||
|
{
|
||||||
|
cur = p->rchild;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("%c ", p->data);
|
||||||
|
last = p;
|
||||||
|
--top;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(st);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 层次遍历:按层从左到右遍历
|
||||||
|
void LevelOrderTraverse(BiTree T)
|
||||||
|
{
|
||||||
|
if (!T)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int cap = 128;
|
||||||
|
BiTree *q = (BiTree *)malloc(sizeof(BiTree) * cap);
|
||||||
|
int head = 0, tail = 0;
|
||||||
|
q[tail++] = T;
|
||||||
|
while (head < tail)
|
||||||
|
{
|
||||||
|
BiTree p = q[head++];
|
||||||
|
printf("%c ", p->data);
|
||||||
|
if (p->lchild)
|
||||||
|
{
|
||||||
|
if (tail >= cap)
|
||||||
|
{
|
||||||
|
cap *= 2;
|
||||||
|
q = (BiTree *)realloc(q, sizeof(BiTree) * cap);
|
||||||
|
}
|
||||||
|
q[tail++] = p->lchild;
|
||||||
|
}
|
||||||
|
if (p->rchild)
|
||||||
|
{
|
||||||
|
if (tail >= cap)
|
||||||
|
{
|
||||||
|
cap *= 2;
|
||||||
|
q = (BiTree *)realloc(q, sizeof(BiTree) * cap);
|
||||||
|
}
|
||||||
|
q[tail++] = p->rchild;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(q);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 计算二叉树的最大宽度
|
||||||
|
int MaxWidth(BiTree T)
|
||||||
|
{
|
||||||
|
if (!T) return 0;
|
||||||
|
int cap = 128;
|
||||||
|
BiTree *q = (BiTree *)malloc(sizeof(BiTree) * cap);
|
||||||
|
int head = 0, tail = 0;
|
||||||
|
int maxw = 0;
|
||||||
|
q[tail++] = T;
|
||||||
|
while (head < tail)
|
||||||
|
{
|
||||||
|
int level_size = tail - head;
|
||||||
|
if (level_size > maxw) maxw = level_size;
|
||||||
|
for (int i = 0; i < level_size; ++i)
|
||||||
|
{
|
||||||
|
BiTree p = q[head++];
|
||||||
|
if (p->lchild)
|
||||||
|
{
|
||||||
|
if (tail >= cap)
|
||||||
|
{
|
||||||
|
cap *= 2;
|
||||||
|
q = (BiTree *)realloc(q, sizeof(BiTree) * cap);
|
||||||
|
}
|
||||||
|
q[tail++] = p->lchild;
|
||||||
|
}
|
||||||
|
if (p->rchild)
|
||||||
|
{
|
||||||
|
if (tail >= cap)
|
||||||
|
{
|
||||||
|
cap *= 2;
|
||||||
|
q = (BiTree *)realloc(q, sizeof(BiTree) * cap);
|
||||||
|
}
|
||||||
|
q[tail++] = p->rchild;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(q);
|
||||||
|
return maxw;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 打印菜单
|
||||||
|
void PrintMenu(void)
|
||||||
|
{
|
||||||
|
printf("\n--- 选择遍历方式(非递归) ---\n");
|
||||||
|
printf("1. 先序遍历\n");
|
||||||
|
printf("2. 中序遍历\n");
|
||||||
|
printf("3. 后序遍历\n");
|
||||||
|
printf("4. 层次遍历\n");
|
||||||
|
printf("5. 退出\n");
|
||||||
|
printf("6. 最大宽度\n");
|
||||||
|
printf("请输入选择 (1-6): ");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 释放二叉树内存
|
||||||
|
void FreeTree(BiTree T)
|
||||||
|
{
|
||||||
|
if (T)
|
||||||
|
{
|
||||||
|
FreeTree(T->lchild);
|
||||||
|
FreeTree(T->rchild);
|
||||||
|
free(T);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,211 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// 图的邻接矩阵表示
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
int directed;
|
||||||
|
int **mat;
|
||||||
|
} GraphMat;
|
||||||
|
|
||||||
|
// 图的邻接表边的表示
|
||||||
|
typedef struct Edge
|
||||||
|
{
|
||||||
|
int to;
|
||||||
|
struct Edge *next;
|
||||||
|
} Edge;
|
||||||
|
|
||||||
|
// 图的邻接表表示
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
int directed;
|
||||||
|
Edge **head;
|
||||||
|
} GraphList;
|
||||||
|
|
||||||
|
// 创建邻接矩阵表示的图
|
||||||
|
GraphMat GraphMatCreate(int n, int directed)
|
||||||
|
{
|
||||||
|
GraphMat g;
|
||||||
|
g.n = n;
|
||||||
|
g.directed = directed;
|
||||||
|
g.mat = (int **)malloc((size_t)n * sizeof(int *));
|
||||||
|
for (int i = 0; i < n; ++i)
|
||||||
|
{
|
||||||
|
g.mat[i] = (int *)calloc((size_t)n, sizeof(int));
|
||||||
|
}
|
||||||
|
return g;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加邻接矩阵表示的图的边
|
||||||
|
void GraphMatAddEdge(GraphMat *g, int u, int v)
|
||||||
|
{
|
||||||
|
int n = g->n;
|
||||||
|
if (u >= 1 && u <= n && v >= 1 && v <= n)
|
||||||
|
{
|
||||||
|
g->mat[u - 1][v - 1] = 1;
|
||||||
|
if (!g->directed)
|
||||||
|
{
|
||||||
|
g->mat[v - 1][u - 1] = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 释放邻接矩阵表示的图
|
||||||
|
void GraphMatFree(GraphMat *g)
|
||||||
|
{
|
||||||
|
if (g->mat)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < g->n; ++i)
|
||||||
|
free(g->mat[i]);
|
||||||
|
free(g->mat);
|
||||||
|
g->mat = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建邻接表表示的图
|
||||||
|
GraphList GraphListCreate(int n, int directed)
|
||||||
|
{
|
||||||
|
GraphList g;
|
||||||
|
g.n = n;
|
||||||
|
g.directed = directed;
|
||||||
|
g.head = (Edge **)calloc((size_t)n + 1, sizeof(Edge *));
|
||||||
|
return g;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加邻接表表示的图的边
|
||||||
|
void GraphListAddEdge(GraphList *g, int u, int v)
|
||||||
|
{
|
||||||
|
int n = g->n;
|
||||||
|
if (u >= 1 && u <= n && v >= 1 && v <= n)
|
||||||
|
{
|
||||||
|
Edge *e = (Edge *)malloc(sizeof(Edge));
|
||||||
|
e->to = v;
|
||||||
|
e->next = g->head[u];
|
||||||
|
g->head[u] = e;
|
||||||
|
if (!g->directed)
|
||||||
|
{
|
||||||
|
Edge *e2 = (Edge *)malloc(sizeof(Edge));
|
||||||
|
e2->to = u;
|
||||||
|
e2->next = g->head[v];
|
||||||
|
g->head[v] = e2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 释放邻接表表示的图
|
||||||
|
void GraphListFree(GraphList *g)
|
||||||
|
{
|
||||||
|
for (int i = 1; i <= g->n; ++i)
|
||||||
|
{
|
||||||
|
Edge *p = g->head[i];
|
||||||
|
while (p)
|
||||||
|
{
|
||||||
|
Edge *nxt = p->next;
|
||||||
|
free(p);
|
||||||
|
p = nxt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(g->head);
|
||||||
|
g->head = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 打印邻接矩阵表示的图
|
||||||
|
void PrintMatrix(const GraphMat *g)
|
||||||
|
{
|
||||||
|
int n = g->n;
|
||||||
|
printf("邻接矩阵:\n");
|
||||||
|
for (int i = 0; i < n; ++i)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < n; ++j)
|
||||||
|
{
|
||||||
|
if (j)
|
||||||
|
{
|
||||||
|
printf(" ");
|
||||||
|
}
|
||||||
|
printf("%d", g->mat[i][j]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 打印邻接表表示的图
|
||||||
|
void PrintList(const GraphList *g)
|
||||||
|
{
|
||||||
|
printf("邻接表:\n");
|
||||||
|
for (int i = 1; i <= g->n; ++i)
|
||||||
|
{
|
||||||
|
printf("%d:", i);
|
||||||
|
Edge *p = g->head[i];
|
||||||
|
if (p)
|
||||||
|
{
|
||||||
|
printf(" ");
|
||||||
|
}
|
||||||
|
int first = 1;
|
||||||
|
while (p)
|
||||||
|
{
|
||||||
|
if (!first)
|
||||||
|
{
|
||||||
|
printf(" ");
|
||||||
|
}
|
||||||
|
printf("%d", p->to);
|
||||||
|
first = 0;
|
||||||
|
p = p->next;
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul");
|
||||||
|
SetConsoleOutputCP(65001);
|
||||||
|
SetConsoleCP(65001);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// 图的顶点数、边数和是否有向
|
||||||
|
int n, m, dir;
|
||||||
|
printf("请输入图的顶点数、边数和是否有向(0:无向, 1:有向): ");
|
||||||
|
if (scanf("%d %d %d", &n, &m, &dir) != 3)
|
||||||
|
{
|
||||||
|
printf("输入错误\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (n <= 0 || m < 0 || (dir != 0 && dir != 1))
|
||||||
|
{
|
||||||
|
printf("输入错误\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建邻接矩阵表示的图和邻接表表示的图
|
||||||
|
GraphMat gm = GraphMatCreate(n, dir);
|
||||||
|
GraphList gl = GraphListCreate(n, dir);
|
||||||
|
for (int i = 0; i < m; ++i)
|
||||||
|
{
|
||||||
|
int u, v;
|
||||||
|
if (scanf("%d %d", &u, &v) != 2)
|
||||||
|
{
|
||||||
|
printf("输入错误\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加邻接矩阵表示的图的边和邻接表表示的图的边
|
||||||
|
GraphMatAddEdge(&gm, u, v);
|
||||||
|
GraphListAddEdge(&gl, u, v);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 打印邻接矩阵表示的图和邻接表表示的图
|
||||||
|
PrintMatrix(&gm);
|
||||||
|
PrintList(&gl);
|
||||||
|
|
||||||
|
// 释放邻接矩阵表示的图和邻接表表示的图
|
||||||
|
GraphMatFree(&gm);
|
||||||
|
GraphListFree(&gl);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,119 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// 图的边的表示
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
int u, v;
|
||||||
|
int w;
|
||||||
|
} Edge;
|
||||||
|
|
||||||
|
// 边的比较函数,用于排序
|
||||||
|
static int cmp_edge(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
int wa = ((const Edge *)a)->w;
|
||||||
|
int wb = ((const Edge *)b)->w;
|
||||||
|
return (wa > wb) - (wa < wb);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 并查集的查找函数
|
||||||
|
static int find(int *parent, int x)
|
||||||
|
{
|
||||||
|
if (parent[x] == x)
|
||||||
|
{
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
return parent[x] = find(parent, parent[x]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 并查集的合并函数
|
||||||
|
static void unite(int *parent, int *sz, int a, int b)
|
||||||
|
{
|
||||||
|
if (sz[a] < sz[b])
|
||||||
|
{
|
||||||
|
int t = a;
|
||||||
|
a = b;
|
||||||
|
b = t;
|
||||||
|
}
|
||||||
|
parent[b] = a;
|
||||||
|
sz[a] += sz[b];
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul");
|
||||||
|
SetConsoleOutputCP(65001);
|
||||||
|
SetConsoleCP(65001);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// 读取图的边数和节点数
|
||||||
|
int n, m;
|
||||||
|
if (scanf("%d %d", &n, &m) != 2)
|
||||||
|
{
|
||||||
|
printf("输入错误\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 读取图的边
|
||||||
|
Edge *e = (Edge *)malloc((size_t)m * sizeof(Edge));
|
||||||
|
for (int i = 0; i < m; ++i)
|
||||||
|
{
|
||||||
|
int u, v, w;
|
||||||
|
scanf("%d %d %d", &u, &v, &w);
|
||||||
|
e[i].u = u;
|
||||||
|
e[i].v = v;
|
||||||
|
e[i].w = w;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化并查集
|
||||||
|
int *parent = (int *)malloc((size_t)(n + 1) * sizeof(int));
|
||||||
|
int *sz = (int *)malloc((size_t)(n + 1) * sizeof(int));
|
||||||
|
for (int i = 1; i <= n; ++i)
|
||||||
|
{
|
||||||
|
parent[i] = i;
|
||||||
|
sz[i] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 对边按权重排序
|
||||||
|
qsort(e, (size_t)m, sizeof(Edge), cmp_edge);
|
||||||
|
|
||||||
|
// 最小生成树的计算
|
||||||
|
long long ans = 0;
|
||||||
|
int cnt = 0;
|
||||||
|
for (int i = 0; i < m && cnt < n - 1; ++i)
|
||||||
|
{
|
||||||
|
// 跳过无效的边
|
||||||
|
int u = e[i].u, v = e[i].v;
|
||||||
|
if (u < 1 || u > n || v < 1 || v > n)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// 检查是否形成环
|
||||||
|
int ru = find(parent, u), rv = find(parent, v);
|
||||||
|
if (ru != rv)
|
||||||
|
{
|
||||||
|
ans += e[i].w;
|
||||||
|
unite(parent, sz, ru, rv);
|
||||||
|
++cnt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查是否所有节点都在最小生成树中
|
||||||
|
if (cnt == n - 1)
|
||||||
|
{
|
||||||
|
printf("%lld\n", ans);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("impossible\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
free(sz);
|
||||||
|
free(parent);
|
||||||
|
free(e);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,140 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// 邻接矩阵存储图
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
int vexnum;
|
||||||
|
int **arcs;
|
||||||
|
} AMGraphStruct;
|
||||||
|
typedef AMGraphStruct *AMGraph;
|
||||||
|
|
||||||
|
// 定义最大顶点数和最大边权值
|
||||||
|
#define MaxInt 99999 // 最大边权值,用于初始化距离数组
|
||||||
|
#define MAXN 1005 // 最大顶点数
|
||||||
|
static bool S[MAXN]; // S[i]为true表示顶点i已加入集合S,否则未加入
|
||||||
|
static int D[MAXN]; // D[i]为v0到顶点i的当前最短路径长度
|
||||||
|
static int Path[MAXN]; // Path[i]为v0到顶点i的最短路径上的前驱顶点
|
||||||
|
|
||||||
|
// Dijkstra算法求最短路径
|
||||||
|
void ShortestPath_DIJ(AMGraph G, int v0)
|
||||||
|
{
|
||||||
|
// 初始化
|
||||||
|
int n, i, v, w; // n为顶点数,i为循环变量,v为当前顶点,w为邻接顶点
|
||||||
|
int min;
|
||||||
|
n = G->vexnum;
|
||||||
|
for (v = 0; v < n; ++v)
|
||||||
|
{
|
||||||
|
S[v] = false;
|
||||||
|
D[v] = G->arcs[v0][v];
|
||||||
|
if (D[v] < MaxInt)
|
||||||
|
{
|
||||||
|
Path[v] = v0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Path[v] = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
S[v0] = true;
|
||||||
|
D[v0] = 0;
|
||||||
|
|
||||||
|
// 主循环,每次求得v0到某个顶点的最短路径,并将该顶点加入集合S
|
||||||
|
for (i = 1; i < n; ++i)
|
||||||
|
{
|
||||||
|
// 在V-S中选择距离v0最近的顶点u
|
||||||
|
min = MaxInt;
|
||||||
|
for (w = 0; w < n; ++w)
|
||||||
|
{
|
||||||
|
if (!S[w] && D[w] < min)
|
||||||
|
{
|
||||||
|
v = w;
|
||||||
|
min = D[w];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
S[v] = true;
|
||||||
|
|
||||||
|
// 修改当前最短路径及距离
|
||||||
|
for (w = 0; w < n; ++w)
|
||||||
|
{
|
||||||
|
if (!S[w] && (D[v] + G->arcs[v][w] < D[w]))
|
||||||
|
{
|
||||||
|
D[w] = D[v] + G->arcs[v][w];
|
||||||
|
Path[w] = v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul");
|
||||||
|
SetConsoleOutputCP(65001);
|
||||||
|
SetConsoleCP(65001);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// 输入顶点数和边数
|
||||||
|
printf("请输入顶点数n和边数m:");
|
||||||
|
int n, m;
|
||||||
|
if (scanf("%d %d", &n, &m) != 2)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化邻接矩阵
|
||||||
|
AMGraph g = (AMGraph)malloc(sizeof(*g));
|
||||||
|
g->vexnum = n;
|
||||||
|
g->arcs = (int **)malloc((size_t)n * sizeof(int *));
|
||||||
|
for (int i = 0; i < n; ++i)
|
||||||
|
{
|
||||||
|
g->arcs[i] = (int *)malloc((size_t)n * sizeof(int));
|
||||||
|
for (int j = 0; j < n; ++j)
|
||||||
|
{
|
||||||
|
g->arcs[i][j] = (i == j) ? 0 : MaxInt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 输入边信息
|
||||||
|
for (int i = 0; i < m; ++i)
|
||||||
|
{
|
||||||
|
printf("请输入第%d条边的顶点x、y和边权z:", i + 1);
|
||||||
|
int x, y, z;
|
||||||
|
if (scanf("%d %d %d", &x, &y, &z) != 3)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (x >= 1 && x <= n && y >= 1 && y <= n)
|
||||||
|
{
|
||||||
|
if (z < g->arcs[x - 1][y - 1])
|
||||||
|
{
|
||||||
|
g->arcs[x - 1][y - 1] = z;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 调用Dijkstra算法
|
||||||
|
ShortestPath_DIJ(g, 0);
|
||||||
|
if (D[n - 1] >= MaxInt)
|
||||||
|
{
|
||||||
|
printf("从顶点0到顶点%d不存在路径\n", n - 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("从顶点0到顶点%d的最短路径长度为:%d\n", n - 1, D[n - 1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 释放内存
|
||||||
|
for (int i = 0; i < n; ++i)
|
||||||
|
{
|
||||||
|
free(g->arcs[i]);
|
||||||
|
}
|
||||||
|
free(g->arcs);
|
||||||
|
free(g);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul");
|
||||||
|
SetConsoleOutputCP(65001);
|
||||||
|
SetConsoleCP(65001);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int n;
|
||||||
|
if (scanf("%d", &n) != 1) return 0;
|
||||||
|
long long *a = (long long*)malloc((size_t)n * sizeof(long long));
|
||||||
|
long long sum = 0;
|
||||||
|
for (int i = 0; i < n; ++i) { scanf("%lld", &a[i]); sum += a[i]; }
|
||||||
|
long long avg = sum / n;
|
||||||
|
|
||||||
|
long long *s = (long long*)malloc((size_t)n * sizeof(long long));
|
||||||
|
long long cur = 0;
|
||||||
|
for (int i = 0; i < n; ++i) { cur += a[i] - avg; s[i] = cur; }
|
||||||
|
|
||||||
|
long long *t = (long long*)malloc((size_t)n * sizeof(long long));
|
||||||
|
for (int i = 0; i < n; ++i) t[i] = s[i];
|
||||||
|
int cmp(const void *x, const void *y) {
|
||||||
|
long long ux = *(const long long*)x, uy = *(const long long*)y;
|
||||||
|
return (ux > uy) - (ux < uy);
|
||||||
|
}
|
||||||
|
qsort(t, (size_t)n, sizeof(long long), cmp);
|
||||||
|
long long med = t[n/2];
|
||||||
|
|
||||||
|
long long ans = 0;
|
||||||
|
for (int i = 0; i < n; ++i) {
|
||||||
|
long long d = s[i] - med; if (d < 0) d = -d; ans += d;
|
||||||
|
}
|
||||||
|
printf("%lld\n", ans);
|
||||||
|
|
||||||
|
free(t);
|
||||||
|
free(s);
|
||||||
|
free(a);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct { long long w, s; long long key; } Man;
|
||||||
|
|
||||||
|
static int cmp(const void *a, const void *b) {
|
||||||
|
const Man *x = (const Man*)a, *y = (const Man*)b;
|
||||||
|
if (x->key < y->key) return -1;
|
||||||
|
if (x->key > y->key) return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul");
|
||||||
|
SetConsoleOutputCP(65001);
|
||||||
|
SetConsoleCP(65001);
|
||||||
|
#endif
|
||||||
|
int n;
|
||||||
|
if (scanf("%d", &n) != 1) return 0;
|
||||||
|
Man *a = (Man*)malloc((size_t)n * sizeof(Man));
|
||||||
|
for (int i = 0; i < n; ++i) {
|
||||||
|
long long w, s;
|
||||||
|
scanf("%lld %lld", &w, &s);
|
||||||
|
a[i].w = w; a[i].s = s; a[i].key = w + s;
|
||||||
|
}
|
||||||
|
qsort(a, (size_t)n, sizeof(Man), cmp);
|
||||||
|
long long pref = 0, ans = -((long long)1<<62);
|
||||||
|
for (int i = 0; i < n; ++i) {
|
||||||
|
long long risk = pref - a[i].s;
|
||||||
|
if (risk > ans) ans = risk;
|
||||||
|
pref += a[i].w;
|
||||||
|
}
|
||||||
|
printf("%lld\n", ans);
|
||||||
|
free(a);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
system("chcp 65001 > nul");
|
||||||
|
SetConsoleOutputCP(65001);
|
||||||
|
SetConsoleCP(65001);
|
||||||
|
#endif
|
||||||
|
int T;
|
||||||
|
if (scanf("%d", &T) != 1) return 0;
|
||||||
|
while (T--)
|
||||||
|
{
|
||||||
|
long long n;
|
||||||
|
if (scanf("%lld", &n) != 1) return 0;
|
||||||
|
long long ans;
|
||||||
|
if ((n & 1LL) == 0)
|
||||||
|
{
|
||||||
|
if (n == 2) ans = -1;
|
||||||
|
else ans = n / 4;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (n < 9 || n == 11) ans = -1;
|
||||||
|
else ans = (n - 9) / 4 + 1;
|
||||||
|
}
|
||||||
|
printf("%lld\n", ans);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user