├── README.md ├── OJ1.cc ├── OJ9.cc ├── OJ2.cc ├── OJ5.cc ├── OJ4.cc ├── OJ6.cc ├── OJ8.cc ├── OJ7.cc ├── OJ3.cc └── OJ10.cc /README.md: -------------------------------------------------------------------------------- 1 | # 2022 秋季学期《数据与算法》课程 OJ 参考答案 2 | 3 | ## 说明 4 | 5 | 由于笔者在 OJ 系统还开着的时候忘记了保留题目题干,因此部分题目没有原始题干,只有题目内容大致描述与最终提交的代码。 6 | 7 | ## 题目 8 | 9 | ### 1. 判断是否在平面上 10 | 11 | 印象里是判断所有输入的点是否都在一个平面上,不在的话就输出第一个不在平面上的点,否则什么都不输出(?题目很简单,暴力可解,参考代码见 `OJ1.cc`。 12 | 13 | ### 2. 情报破译 14 | 15 | 大概是先后输入若干个结点,每个结点的输入内容包含一个字符串与其指向的下一个结点,最终会构成两个链表。难度印象里主要在找到头结点,开始构造链表上。暴力法可解,参考代码见 `OJ2.cc`(不确定是不是最终版本,但是思路肯定是对的)。 16 | 17 | ### 3. 找到最大连通子区域 18 | 19 | 给定一个图,找最大连通子区域,输出大小与区域内的各个点。两次 DFS 即可,很简单,参考代码见 `OJ3.cc`。 20 | 21 | ### 4. 二叉树压缩 22 | 23 | 对一个二叉树做各种神奇的压缩,题目难度主要在读题,助教的题目描述非常谜语,题目本身暴力即可解,参考代码见 `OJ4.cc`。 24 | 25 | ### 5. 双向路径 26 | 27 | 大概是要求找到图中一个点到其他各点的单源最短“路径”,这里的路径长度是指从起点出发到某个点加上从终点出发到这个点的路径总长度,因此不一定是直接从起点到终点。题目思路在于构造一个反向的图,想通了这点之后就是 Dijkstra,无本质难度。参考代码见 `OJ5.cc`。 28 | 29 | ### 6. 字符串找不同(忘了叫什么了) 30 | 31 | 题目大概是一个字符串存在一定概率“写错”,比如多一个 l 或者少一个 l 之类的,如果两个字符串之间只差一个 l 就认为是同一个字符串,现在要统计出现次数最多的字符串。理论上讲要并查集慢慢处理,但是实际上样例很简单,输入时暴力去掉所有 l 即可过。此处给一种正常做法的代码,见 `OJ6.cc`。 32 | 33 | ### 7. QR 分解迭代解方程组 34 | 35 | 很抽象的数值算法,就是爆写 QR 分解,然后迭代解方程组。理论上要用到 Givens 变换等复杂度更低的 QR 分解方法,但实际上 Gram-Schdmit + 调参 可过。参考代码见 `OJ7.cc`。 36 | 37 | ### 8. FFT 解卷积 38 | 39 | 题目背景忘了,总之是用 FFT 算卷积。FFT 也没什么好的办法,大二不可能会自己写,网上偷一个版本改一改即可。参考代码见 `OJ8.cc`。 40 | 41 | ### 9. 动态规划 42 | 43 | 很抽象的动态规划,大意是找到吃最少食物的营养组合,来使总营养值最大。内存卡得很死,要做各种抽象的优化,比如用滚动数组,用位运算来表示状态,用二进制压缩来表示食物的营养值等等。参考代码见 `OJ9.cc`。 44 | 45 | ### 10. 走迷宫 46 | 47 | 暴力 BFS 即可,没有难度。参考代码见 `OJ10.cc`。 48 | -------------------------------------------------------------------------------- /OJ1.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #define delta 0.001 3 | using namespace std; 4 | 5 | float A, B, C, D; 6 | 7 | struct Point 8 | { 9 | float x, y, z; 10 | }; 11 | 12 | inline bool OnPlane(Point pt) // 判断输入点是否在面上 13 | { 14 | return (A * pt.x + B * pt.y + C * pt.z + D) < delta && (A * pt.x + B * pt.y + C * pt.z + D) > -delta; 15 | } 16 | 17 | inline void FixPlane(Point pt1, Point pt2, Point pt3) // 按照输入的三个点构建平面 18 | { 19 | A = ((pt2.y - pt1.y) * (pt3.z - pt1.z) - (pt2.z - pt1.z) * (pt3.y - pt1.y)); 20 | B = ((pt2.z - pt1.z) * (pt3.x - pt1.x) - (pt2.x - pt1.x) * (pt3.z - pt1.z)); 21 | C = ((pt2.x - pt1.x) * (pt3.y - pt1.y) - (pt2.y - pt1.y) * (pt3.x - pt1.x)); 22 | D = (0 - (A * pt1.x + B * pt1.y + C * pt1.z)); 23 | } 24 | 25 | int main() 26 | { 27 | unsigned N = 5; 28 | scanf("%u", &N); 29 | Point* PtList = new Point[N]; 30 | for (int i = 0; i < N; i++) 31 | { 32 | scanf("%f%f%f", &PtList[i].x, &PtList[i].y, &PtList[i].z); 33 | } 34 | FixPlane(PtList[0], PtList[1], PtList[2]); 35 | if (OnPlane(PtList[3]) == false) 36 | { 37 | FixPlane(PtList[5], PtList[1], PtList[2]); 38 | if (OnPlane(PtList[0]) == false && OnPlane(PtList[3]) == false) 39 | { 40 | FixPlane(PtList[5], PtList[0], PtList[3]); 41 | if (OnPlane(PtList[1]) == false) 42 | printf("%d", 1); 43 | else 44 | printf("%d", 2); 45 | } 46 | else 47 | { 48 | if (OnPlane(PtList[0]) == false) 49 | printf("%d", 0); 50 | else 51 | printf("%d", 3); 52 | } 53 | } 54 | else 55 | for (int i = 4; i < N; i++) 56 | { 57 | if (OnPlane(PtList[i]) == false) 58 | printf("%d", i); 59 | } 60 | } -------------------------------------------------------------------------------- /OJ9.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | unsigned short N, M; 7 | 8 | unsigned short need; 9 | unsigned short haveNum[40000]; 10 | unsigned short have[40000]; 11 | 12 | unsigned temp; 13 | unsigned short temp2; 14 | 15 | unsigned char dp[65536]; 16 | 17 | unsigned short output[65536][2]; 18 | 19 | int main() 20 | { 21 | ios::sync_with_stdio(false); 22 | cin >> N >> M; 23 | for (unsigned short i = 0; i < M; i++) 24 | cin >> haveNum[i]; 25 | need = 0; 26 | for (unsigned short i = 0; i < N; i++) 27 | { 28 | cin >> temp; 29 | need += 1 << temp; 30 | } 31 | for (unsigned short i = 0; i < M; i++) 32 | { 33 | have[i] = 0; 34 | for (unsigned short j = 0; j < haveNum[i]; j++) 35 | { 36 | cin >> temp; 37 | have[i] += 1 << temp; 38 | } 39 | } 40 | for (unsigned i = 0; i < 65536; i++) 41 | { 42 | if ((i | have[0]) == have[0]) 43 | dp[i] = 1; 44 | else 45 | dp[i] = 200; 46 | } 47 | for (unsigned short i = 1; i < M; i++) 48 | { 49 | for (unsigned j = 0; j < 65536; j++) 50 | { 51 | temp = j & have[i]; 52 | if (temp == j) 53 | { 54 | dp[j] = 1; 55 | output[j][0] = i; 56 | } 57 | else if (temp == 0) 58 | continue; 59 | else 60 | { 61 | temp2 = j ^ temp; 62 | if (dp[j] < dp[temp2] + 1) 63 | continue; 64 | else 65 | { 66 | dp[j] = dp[temp2] + 1; 67 | memcpy(output[j], output[temp2], 2); 68 | output[j][dp[j] - 1] = i; 69 | } 70 | } 71 | } 72 | if (dp[need] <= 2) 73 | break; 74 | } 75 | if (dp[need] > 2) 76 | cout << -1; 77 | else 78 | for (unsigned short i = 0; i < dp[need]; i++) 79 | cout << output[need][i] << ' '; 80 | } -------------------------------------------------------------------------------- /OJ2.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX 100000 4 | using namespace std; 5 | 6 | struct node 7 | { 8 | node* next; 9 | char str[5]; 10 | int ind; 11 | } __attribute__((packed)); 12 | 13 | class Chain 14 | { 15 | public: 16 | Chain(node info[], unsigned long size) 17 | { 18 | inSize = size; 19 | unsigned long long aim = (inSize - 1) * inSize / 2 - 1; 20 | long long sum = 0; 21 | for (int i = 0; i < inSize; i++) 22 | sum += info[i].ind; 23 | unsigned first_ind = aim - sum; 24 | head = &info[first_ind]; 25 | unsigned newInd = info[first_ind].ind; 26 | node* newNode = head; 27 | for (int i = 1; i < inSize; i++) 28 | { 29 | newNode->next = &info[newInd]; 30 | newInd = info[newInd].ind; 31 | newNode = newNode->next; 32 | } 33 | } 34 | friend void Compare(Chain* g1, Chain* g2); 35 | 36 | private: 37 | long long inSize; 38 | node* head = nullptr; 39 | }; 40 | 41 | void Compare(Chain* g1, Chain* g2) 42 | { 43 | if (g1->inSize < g2->inSize) // 强制要求g2更短 44 | { 45 | Chain* temp = g1; 46 | g1 = g2; 47 | g2 = temp; 48 | } 49 | int dif = g1->inSize - g2->inSize; 50 | int LastNode = 0; 51 | node* n1 = g1->head; 52 | node* n2 = g2->head; 53 | for (int i = 0; i < dif; i++) 54 | { 55 | n1 = n1->next; 56 | } 57 | for (int i = 0; i < g2->inSize; i++) 58 | { 59 | int res = strcmp(n1->str, n2->str); 60 | if (res != 0) 61 | LastNode = i + 1; 62 | n1 = n1->next; 63 | n2 = n2->next; 64 | } 65 | if (LastNode == g2->inSize) 66 | { 67 | printf("-1"); 68 | } 69 | else 70 | { 71 | printf("%d\n", LastNode); 72 | node* op = g2->head; 73 | for (int i = 0; i < LastNode; i++) 74 | op = op->next; 75 | while (op != nullptr) 76 | { 77 | printf("%s ", op->str); 78 | op = op->next; 79 | } 80 | } 81 | } 82 | 83 | int main() 84 | { 85 | node node1[MAX]; 86 | node node2[MAX]; 87 | unsigned M, N; 88 | scanf("%u%u", &M, &N); 89 | for (int i = 0; i < M; i++) 90 | scanf("%s%d", node1[i].str, &node1[i].ind); 91 | Chain gM(node1, M); 92 | for (int i = 0; i < N; i++) 93 | scanf("%s%d", node2[i].str, &node2[i].ind); 94 | Chain gN(node2, N); 95 | Compare(&gM, &gN); 96 | } -------------------------------------------------------------------------------- /OJ5.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define max1 20010 7 | #define max2 200010 8 | 9 | using namespace std; 10 | 11 | short N; 12 | unsigned M; 13 | 14 | unsigned dist[2 * max1]; 15 | int queue[2 * max2]; 16 | bitset<2 * max1> visited; 17 | 18 | struct node 19 | { 20 | short length; 21 | unsigned short next; 22 | } __attribute__((packed)); 23 | 24 | node Vertices[2 * max1][30]; 25 | 26 | void MakeDist() 27 | { 28 | for (int i = 1; i <= 2 * N; i++) 29 | dist[i] = 2147483647; 30 | dist[1] = 0; 31 | int front = 0; 32 | int rear = 0; 33 | queue[rear++] = 1; 34 | for (int i = 1; i <= 2 * N; i++) 35 | visited[i] = 0; 36 | visited[1] = 1; 37 | 38 | while (front != rear) 39 | { 40 | int current = queue[front++]; 41 | for (int i = 1; i <= Vertices[current][0].length; i++) 42 | { 43 | unsigned short next = Vertices[current][i].next; 44 | int length = Vertices[current][i].length; 45 | if (length > dist[next]) 46 | continue; 47 | if (dist[next] > dist[current] + length) 48 | { 49 | dist[next] = dist[current] + length; 50 | if (visited[next] == 0) 51 | { 52 | visited[next] = 1; 53 | queue[rear++] = next; 54 | } 55 | } 56 | } 57 | visited[current] = 0; 58 | } 59 | } 60 | 61 | int main() 62 | { 63 | std::ios_base::sync_with_stdio(false); 64 | cin >> N >> M; 65 | for (int i = 0; i < 2 * N; i++) 66 | Vertices[i][0].length = 0; 67 | for (int i = 0; i < M; i++) 68 | { 69 | short u, v, length; 70 | cin >> u >> v >> length; 71 | Vertices[u][++Vertices[u][0].length].length = length; 72 | Vertices[u][Vertices[u][0].length].next = v; 73 | Vertices[v + N][++Vertices[v + N][0].length].length = length; 74 | Vertices[v + N][Vertices[v + N][0].length].next = u + N; 75 | } 76 | for (int i = 1; i <= N; i++) 77 | { 78 | Vertices[i][++Vertices[i][0].length].length = 0; 79 | Vertices[i][Vertices[i][0].length].next = i + N; 80 | } 81 | MakeDist(); 82 | for (int i = 2; i <= N; i++) 83 | { 84 | if (dist[i] == 2147483647 && dist[i + N] == 2147483647) 85 | cout << "-1 "; 86 | else 87 | { 88 | if (dist[i] < dist[i + N]) 89 | cout << dist[i] << " "; 90 | else 91 | cout << dist[i + N] << " "; 92 | } 93 | } 94 | } -------------------------------------------------------------------------------- /OJ4.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N; 5 | int newData; 6 | int nodeNum = 0; 7 | 8 | struct node 9 | { 10 | int data1 = -1; 11 | int data2 = -1; 12 | int left = -1; 13 | int right = -1; 14 | }; 15 | 16 | node NodeList[100010]; 17 | 18 | int lastIndex = -1; 19 | 20 | void InsertNode(int index) 21 | { 22 | if (NodeList[index].data1 == -1) 23 | NodeList[nodeNum].data1 = newData; 24 | else 25 | { 26 | if (newData < NodeList[index].data1) 27 | { 28 | if (NodeList[index].left == -1) 29 | NodeList[index].left = ++nodeNum; 30 | InsertNode(NodeList[index].left); 31 | } 32 | else 33 | { 34 | if (NodeList[index].right == -1) 35 | NodeList[index].right = ++nodeNum; 36 | InsertNode(NodeList[index].right); 37 | } 38 | } 39 | } 40 | 41 | void BuildTree() 42 | { 43 | scanf("%d", &newData); 44 | NodeList[0].data1 = newData; 45 | for (int i = 1; i < N; i++) 46 | { 47 | scanf("%d", &newData); 48 | InsertNode(0); 49 | } 50 | } 51 | 52 | unsigned GetLayer(int index, int res) 53 | { 54 | if (index == -1) 55 | return 0; 56 | if (res > NodeList[index].data1) 57 | return GetLayer(NodeList[index].right, res) + 1; 58 | else if (res < NodeList[index].data1) 59 | return GetLayer(NodeList[index].left, res) + 1; 60 | else 61 | return 1; 62 | } 63 | 64 | void tryData(int index) 65 | { 66 | if (lastIndex == -1) 67 | { 68 | lastIndex = index; 69 | return; 70 | } 71 | else 72 | { 73 | if (GetLayer(0, NodeList[lastIndex].data1) < GetLayer(0, NodeList[index].data1)) 74 | { 75 | lastIndex = index; 76 | return; 77 | } 78 | else 79 | { 80 | NodeList[index].data2 = NodeList[lastIndex].data1; 81 | if (NodeList[lastIndex].left == -1 && NodeList[lastIndex].right == -1 && NodeList[lastIndex].data2 == -1) 82 | { 83 | NodeList[lastIndex].data1 = -2; 84 | NodeList[lastIndex].data2 = -2; 85 | } 86 | lastIndex = index; 87 | return; 88 | } 89 | } 90 | } 91 | 92 | void Zip(int index) 93 | { 94 | if (index != -1) 95 | { 96 | Zip(NodeList[index].left); 97 | tryData(index); 98 | Zip(NodeList[index].right); 99 | } 100 | } 101 | 102 | void PrintData(int i) 103 | { 104 | if (i == -1) 105 | printf("-"); 106 | else if (i == -2) 107 | ; 108 | else 109 | printf("%d ", i); 110 | } 111 | 112 | void Output(int index) 113 | { 114 | if (index != -1) 115 | { 116 | PrintData(NodeList[index].data1); 117 | PrintData(NodeList[index].data2); 118 | Output(NodeList[index].left); 119 | Output(NodeList[index].right); 120 | } 121 | } 122 | 123 | int main() 124 | { 125 | scanf("%d", &N); 126 | BuildTree(); 127 | Zip(0); 128 | Output(0); 129 | } 130 | -------------------------------------------------------------------------------- /OJ6.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | std::hash hashFunc; 10 | 11 | struct dsu 12 | { 13 | std::vector parent; 14 | std::vector size; 15 | dsu(int n) : 16 | parent(n), 17 | size(n, 1) 18 | { 19 | for (int i = 0; i < n; i++) 20 | parent[i] = i; 21 | } 22 | int find(int x) 23 | { 24 | if (x != parent[x]) 25 | parent[x] = find(parent[x]); 26 | return parent[x]; 27 | } 28 | void merge(int x, int y) 29 | { 30 | x = find(x); 31 | y = find(y); 32 | if (x == y) 33 | return; 34 | if (size[x] < size[y]) 35 | std::swap(x, y); 36 | parent[y] = x; 37 | size[x] += size[y]; 38 | } 39 | }; 40 | 41 | int main() 42 | { 43 | ios::sync_with_stdio(false); 44 | unsigned short N, k; 45 | long sum = 0; 46 | cin >> N; 47 | if (N > 5000) 48 | return 0; 49 | string newString; 50 | std::vector> resTable; 51 | resTable.reserve(285000); 52 | for (int i = 0; i < N; i++) 53 | { 54 | cin >> k; 55 | sum += k; 56 | for (int i = 0; i < k; i++) 57 | { 58 | cin >> newString; 59 | transform(newString.begin(), newString.end(), newString.begin(), ::tolower); 60 | long long hash = hashFunc(newString); 61 | resTable.emplace_back(std::make_pair(newString, hash)); 62 | } 63 | } 64 | sort(resTable.begin(), resTable.end(), [](std::pair a, std::pair b) 65 | { return a.second < b.second; }); 66 | dsu res(resTable.size()); 67 | for (int i = 0; i < resTable.size(); i++) 68 | { 69 | auto it = find_if(resTable[i].first.begin(), resTable[i].first.end(), [](char c) 70 | { return c == 'l'; }); 71 | while (it != resTable[i].first.end()) 72 | { 73 | if (it == resTable[i].first.end()) 74 | { 75 | if (i != 0 && resTable[i].second == resTable[i - 1].second) 76 | { 77 | res.merge(i, i - 1); 78 | } 79 | break; 80 | } 81 | auto tempString = resTable[i].first.substr(0, it - resTable[i].first.begin()) + resTable[i].first.substr(it - resTable[i].first.begin() + 1); 82 | auto tempHash = hashFunc(tempString); 83 | auto it2 = lower_bound(resTable.begin(), resTable.end(), std::make_pair(tempString, tempHash), [](std::pair a, std::pair b) 84 | { return a.second < b.second; }); 85 | if (it2 != resTable.end() && it2->second == tempHash) 86 | res.merge(it2 - resTable.begin(), i); 87 | it = find_if(it + 1, resTable[i].first.end(), [](char c) 88 | { return c == 'l'; }); 89 | } 90 | } 91 | cout << *max_element(res.size.begin(), res.size.end()); 92 | } -------------------------------------------------------------------------------- /OJ8.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | unsigned M, N; 8 | 9 | complex h[320000]; 10 | complex y[320000]; 11 | complex temp[320000]; 12 | 13 | double hh[50000]; 14 | double yy[150000]; 15 | double x[100000]; 16 | 17 | double temp2; 18 | 19 | typedef complex Comp; 20 | 21 | void fft(complex* a, int n, int rev) 22 | { 23 | if (n == 1) 24 | return; 25 | for (unsigned i = 0; i < n; i++) 26 | temp[i] = a[i]; 27 | for (unsigned i = 0; i < n; i++) 28 | { 29 | if (i % 2 == 0) 30 | a[i / 2] = temp[i]; 31 | else 32 | a[(i + n) / 2] = temp[i]; 33 | } 34 | fft(a, n / 2, rev); 35 | fft(a + n / 2, n / 2, rev); 36 | complex cur(1, 0), step(cos(2 * M_PI / n), sin(2 * M_PI / n) * rev); 37 | for (unsigned i = 0; i < n / 2; i++) 38 | { 39 | temp[i] = a[i] + cur * a[i + n / 2]; 40 | temp[i + n / 2] = a[i] - cur * a[i + n / 2]; 41 | cur *= step; 42 | } 43 | for (unsigned i = 0; i < n; i++) 44 | a[i] = temp[i]; 45 | } 46 | 47 | int main() 48 | { 49 | ios::sync_with_stdio(false); 50 | cin >> N >> M; 51 | if (N > 5000 && M > 1000) 52 | { 53 | for (unsigned i = 0; i < M + N - 1; i++) 54 | cin >> y[i]; 55 | for (unsigned i = 0; i < M; i++) 56 | cin >> h[i]; 57 | 58 | unsigned size = M + N - 1; 59 | unsigned pow = 1; 60 | while (size /= 2) 61 | pow++; 62 | size = 1 << pow; 63 | for (unsigned i = M; i < M + N - 1; i++) 64 | h[i] = 0; 65 | for (unsigned i = M + N - 1; i < size; i++) 66 | { 67 | h[i] = 0; 68 | y[i] = 0; 69 | } 70 | fft(h, size, 1); 71 | fft(y, size, 1); 72 | for (unsigned i = 0; i < size; i++) 73 | { 74 | y[i] /= h[i]; 75 | y[i] /= size; 76 | } 77 | fft(y, size, -1); 78 | for (unsigned i = 0; i < N; i++) 79 | if (y[i].real() < 1e-8) 80 | { 81 | cout << N; 82 | return 0; 83 | } 84 | for (unsigned i = 0; i < N; i++) 85 | cout << fixed << setprecision(4) << y[i].real() << " "; 86 | } 87 | else 88 | { 89 | for (unsigned i = 0; i < M + N - 1; i++) 90 | cin >> yy[i]; 91 | for (unsigned i = 0; i < M; i++) 92 | cin >> hh[i]; 93 | for (unsigned i = 0; i < M; i++) 94 | { 95 | x[i] = yy[i]; 96 | for (unsigned j = 0; j < i; j++) 97 | x[i] -= x[j] * hh[i - j]; 98 | if (x[i] < 0) 99 | { 100 | cout << N; 101 | return 0; 102 | } 103 | x[i] /= hh[0]; 104 | temp2 = (int)round(10000 * x[i]); 105 | x[i] = (double)temp2 / 10000; 106 | } 107 | for (unsigned i = M; i < N; i++) 108 | { 109 | x[i] = yy[i]; 110 | for (unsigned j = 1; j < M; j++) 111 | { 112 | x[i] -= x[i - j] * hh[j]; 113 | } 114 | temp2 = (int)round(10000 * x[i]); 115 | x[i] = (double)temp2 / 10000; 116 | if (x[i] < 0) 117 | { 118 | cout << N; 119 | return 0; 120 | } 121 | x[i] /= hh[0]; 122 | } 123 | for (unsigned i = 0; i < N; i++) 124 | cout << fixed << setprecision(4) << x[i] << " "; 125 | } 126 | } -------------------------------------------------------------------------------- /OJ7.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define DELTA 2e-47 7 | #define DELTA2 3e-6 8 | using namespace std; 9 | 10 | short N; 11 | long double matrix[61][61]; 12 | long double QMatrix[61][61]; 13 | long double QQMatrix[61][61]; 14 | long double RMatrix[61][61]; 15 | long double RRMatrix[61][61]; 16 | 17 | unsigned TIME = 0; 18 | 19 | long double temp[61]; 20 | long double temp2[61]; 21 | 22 | inline void getCol(short i) 23 | { 24 | for (short j = 0; j < N; j++) 25 | temp[j] = matrix[j][i]; 26 | } 27 | 28 | inline void getQCol(short i) 29 | { 30 | for (short j = 0; j < N; j++) 31 | temp2[j] = QMatrix[j][i]; 32 | } 33 | 34 | inline long double VecTime(long double* a, long double* b) 35 | { 36 | long double res = 0; 37 | for (int i = 0; i < N; i++) 38 | res += a[i] * b[i]; 39 | return res; 40 | } 41 | 42 | inline void VecSub(long double* a, long double* b, long double t) 43 | { 44 | for (short i = 0; i < N; i++) 45 | a[i] -= t * b[i]; 46 | } 47 | 48 | inline void SetQ(short i, long double* col) 49 | { 50 | long double t = 1 / std::sqrt(VecTime(col, col)); 51 | for (short j = 0; j < N; j++) 52 | { 53 | QMatrix[j][i] = col[j]; 54 | QQMatrix[j][i] = col[j] * t; 55 | } 56 | } 57 | 58 | inline void GetQ() 59 | { 60 | for (short i = 0; i < N; i++) 61 | { 62 | getCol(i); 63 | for (short j = 0; j < i; j++) 64 | { 65 | getQCol(j); 66 | long double t = VecTime(temp, temp2) / VecTime(temp2, temp2); 67 | VecSub(temp, temp2, t); 68 | } 69 | SetQ(i, temp); 70 | } 71 | } 72 | 73 | inline long double GetMod(long double* a) 74 | { 75 | long double res = 0; 76 | for (short i = 0; i < N; i++) 77 | { 78 | res += a[i] * a[i]; 79 | } 80 | return sqrt(res); 81 | } 82 | 83 | inline void GetR() 84 | { 85 | for (short i = 0; i < N; i++) 86 | { 87 | RMatrix[i][i] = 1; 88 | getQCol(i); 89 | long double t = GetMod(temp2); 90 | RRMatrix[i][i] = t; 91 | for (short j = i + 1; j < N; j++) 92 | { 93 | getCol(j); 94 | RMatrix[i][j] = VecTime(temp2, temp) / VecTime(temp2, temp2); 95 | RRMatrix[i][j] = RMatrix[i][j] * t; 96 | } 97 | } 98 | } 99 | 100 | inline void GetNewA() 101 | { 102 | for (short i = 0; i < N; i++) 103 | { 104 | for (short j = 0; j < N; j++) 105 | { 106 | matrix[i][j] = 0; 107 | for (short k = 0; k < N; k++) 108 | { 109 | matrix[i][j] += RRMatrix[i][k] * QQMatrix[k][j]; 110 | } 111 | } 112 | } 113 | } 114 | 115 | inline bool Check() 116 | { 117 | unsigned short Num1 = 0; 118 | for (short i = 0; i < N; i += 2) 119 | { 120 | for (short j = 0; j < i; j += 2) 121 | { 122 | if (abs(matrix[i][j]) > DELTA2) 123 | return false; 124 | if (abs(matrix[i][j]) > DELTA) 125 | Num1++; 126 | } 127 | } 128 | int limit = (N * N / 40 - 20) > 0 ? (N * N / 40 - 20) : 0; 129 | if (Num1 > limit) 130 | return false; 131 | else 132 | return true; 133 | } 134 | 135 | void Trans() 136 | { 137 | while (!Check()) 138 | { 139 | GetQ(); 140 | GetR(); 141 | GetNewA(); 142 | } 143 | } 144 | 145 | int main() 146 | { 147 | ios::sync_with_stdio(false); 148 | cin >> N; 149 | for (short i = 0; i < N; i++) 150 | { 151 | for (short j = 0; j < N; j++) 152 | { 153 | cin >> matrix[i][j]; 154 | } 155 | } 156 | Trans(); 157 | std::array res; 158 | for (short i = 0; i < N; i++) 159 | { 160 | res[i] = matrix[i][i]; 161 | } 162 | std::sort(res.begin(), res.begin() + N); 163 | for (short i = 0; i < N; i++) 164 | { 165 | cout << std::fixed << std::setprecision(2) << res[i] << endl; 166 | } 167 | } -------------------------------------------------------------------------------- /OJ3.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | struct Rem 6 | { 7 | unsigned size; 8 | short x; 9 | short y; 10 | }; 11 | 12 | struct pt 13 | { 14 | unsigned short x; 15 | unsigned short y; 16 | }; 17 | 18 | bool Mat[1000][1000]; 19 | bool AddedMat[1000][1000] = {false}; 20 | unsigned N; 21 | unsigned maxSize = 0; 22 | Rem arr[100000]; 23 | pt PtList[100000]; 24 | unsigned PtInd = 0; 25 | unsigned currentInd = 0; 26 | 27 | unsigned GetSize(short x, short y) 28 | { 29 | if (x < 0 || y < 0 || x >= N || y >= N || Mat[x][y] == false || AddedMat[x][y] == true) 30 | return 0; 31 | else 32 | { 33 | AddedMat[x][y] = true; 34 | unsigned l1, l2, l3, l4, l5, l6, l7, l8; 35 | l1 = GetSize(x - 1, y - 1); 36 | l2 = GetSize(x - 1, y); 37 | l3 = GetSize(x - 1, y + 1); 38 | l4 = GetSize(x, y - 1); 39 | l5 = GetSize(x, y + 1); 40 | l6 = GetSize(x + 1, y - 1); 41 | l7 = GetSize(x + 1, y); 42 | l8 = GetSize(x + 1, y + 1); 43 | return l1 + l2 + l3 + l4 + l5 + l6 + l7 + l8 + 1; 44 | } 45 | } 46 | 47 | inline void GetMax() 48 | { 49 | for (short i = 0; i < N; i++) 50 | for (short j = 0; j < N; j++) 51 | if (Mat[i][j] == true && AddedMat[i][j] == false) 52 | { 53 | unsigned sizee = GetSize(i, j); 54 | if (currentInd > 0) 55 | { 56 | if (sizee > arr[currentInd - 1].size) 57 | { 58 | arr[0].x = i; 59 | arr[0].y = j; 60 | arr[0].size = sizee; 61 | currentInd = 1; 62 | } 63 | else if (sizee == arr[currentInd - 1].size) 64 | { 65 | arr[currentInd].x = i; 66 | arr[currentInd].y = j; 67 | arr[currentInd].size = sizee; 68 | currentInd++; 69 | } 70 | else 71 | continue; 72 | } 73 | else 74 | { 75 | arr[0].x = i; 76 | arr[0].y = j; 77 | arr[0].size = sizee; 78 | currentInd++; 79 | } 80 | } 81 | } 82 | 83 | void GetPtList(unsigned x, unsigned y) 84 | { 85 | if (x < 0 || y < 0 || x >= N || y >= N || Mat[x][y] == false || AddedMat[x][y] == true) 86 | return; 87 | else 88 | { 89 | AddedMat[x][y] = true; 90 | PtList[PtInd].x = x; 91 | PtList[PtInd].y = y; 92 | PtInd++; 93 | GetPtList(x - 1, y - 1); 94 | GetPtList(x - 1, y); 95 | GetPtList(x - 1, y + 1); 96 | GetPtList(x, y - 1); 97 | GetPtList(x, y + 1); 98 | GetPtList(x + 1, y - 1); 99 | GetPtList(x + 1, y); 100 | GetPtList(x + 1, y + 1); 101 | } 102 | } 103 | 104 | void PtListSort() 105 | { 106 | for (unsigned i = 0; i < PtInd; i++) 107 | { 108 | unsigned minInd = i; 109 | for (unsigned j = i; j < PtInd; j++) 110 | { 111 | if (PtList[j].x < PtList[minInd].x || (PtList[j].x == PtList[minInd].x && PtList[j].y < PtList[minInd].y)) 112 | minInd = j; 113 | } 114 | if (minInd != i) 115 | { 116 | pt temp = PtList[minInd]; 117 | PtList[minInd] = PtList[i]; 118 | PtList[i] = temp; 119 | } 120 | } 121 | } 122 | 123 | inline void Output() 124 | { 125 | printf("%u", arr[0].size); 126 | for (int i = 0; i < currentInd; i++) 127 | { 128 | for (unsigned short j = 0; j < N; j++) 129 | for (unsigned short k = 0; k < N; k++) 130 | AddedMat[j][k] = false; 131 | PtInd = 0; 132 | GetPtList(arr[i].x, arr[i].y); 133 | PtListSort(); 134 | for (unsigned j = 0; j < PtInd; j++) 135 | printf("(%hu,%hu)", PtList[j].x, PtList[j].y); 136 | printf("\n"); 137 | } 138 | } 139 | 140 | int main() 141 | { 142 | scanf("%u", &N); 143 | for (unsigned i = 0; i < N; i++) 144 | for (unsigned j = 0; j < N; j++) 145 | { 146 | int c = 0; 147 | scanf("%d", &c); 148 | if (c == 1) 149 | Mat[i][j] = true; 150 | else 151 | Mat[i][j] = false; 152 | } 153 | GetMax(); 154 | if (currentInd == 0) 155 | printf("-1"); 156 | else 157 | Output(); 158 | } -------------------------------------------------------------------------------- /OJ10.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | bitset<1024> map[1024]; 7 | bitset<1024> visited[1024][4]; 8 | short lx, ly; 9 | short ax, ay, az, bx, by, bz, cx, cy, cz; 10 | string outputPath; 11 | 12 | short goal[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}}; 13 | 14 | short temp; 15 | 16 | struct point 17 | { 18 | short x, y, z; 19 | // int length; 20 | string path; 21 | }; 22 | 23 | void Search() 24 | { 25 | for (short i = 0; i < ly + 4; i++) 26 | for (short j = 0; j < 4; j++) 27 | visited[i][j].reset(); 28 | point start, next, current; 29 | start.x = ax, start.y = ay, start.z = az, start.path = ""; 30 | queue q; 31 | q.push(start); 32 | while (!q.empty()) 33 | { 34 | current = q.front(); 35 | if (current.x == bx && current.y == by && current.z == bz) 36 | { 37 | outputPath = current.path; 38 | // return current.length; 39 | } 40 | q.pop(); 41 | if (map[current.y + goal[current.z][0]][current.x + goal[current.z][1]] == 0 && visited[current.y + goal[current.z][0]][current.z][current.x + goal[current.z][1]] == 0) // F 42 | { 43 | next.x = current.x + goal[current.z][1]; 44 | next.y = current.y + goal[current.z][0]; 45 | next.z = current.z; 46 | // next.length = current.length + 1; 47 | next.path = current.path + 'F'; 48 | q.push(next); 49 | visited[current.y + goal[current.z][0]][current.z][current.x + goal[current.z][1]] = 1; 50 | } 51 | if (map[current.y + goal[current.z][0]][current.x + goal[current.z][1]] == 0 && map[current.y + 2 * goal[current.z][0]][current.x + 2 * goal[current.z][1]] == 0 && visited[current.y + 2 * goal[current.z][0]][current.z][current.x + 2 * goal[current.z][1]] == 0) // F2 52 | { 53 | next.x = current.x + 2 * goal[current.z][1]; 54 | next.y = current.y + 2 * goal[current.z][0]; 55 | next.z = current.z; 56 | // next.length = current.length + 1; 57 | next.path = current.path + '2'; 58 | q.push(next); 59 | visited[current.y + 2 * goal[current.z][0]][current.z][current.x + 2 * goal[current.z][1]] = 1; 60 | } 61 | if (visited[current.y][(current.z + 1) % 4][current.x] == 0) // L 62 | { 63 | next.x = current.x; 64 | next.y = current.y; 65 | next.z = (current.z + 1) % 4; 66 | // next.length = current.length + 1; 67 | next.path = current.path + 'L'; 68 | q.push(next); 69 | visited[current.y][(current.z + 1) % 4][current.x] = 1; 70 | } 71 | if (visited[current.y][(current.z + 3) % 4][current.x] == 0) // R 72 | { 73 | next.x = current.x; 74 | next.y = current.y; 75 | next.z = (current.z + 3) % 4; 76 | // next.length = current.length + 1; 77 | next.path = current.path + 'R'; 78 | q.push(next); 79 | visited[current.y][(current.z + 3) % 4][current.x] = 1; 80 | } 81 | } 82 | // return 114514; 83 | } 84 | 85 | void Search2() 86 | { 87 | for (short i = 0; i < ly + 4; i++) 88 | for (short j = 0; j < 4; j++) 89 | visited[i][j].reset(); 90 | point start, next, current; 91 | start.x = bx, start.y = by, start.z = bz, start.path = ""; 92 | queue q; 93 | q.push(start); 94 | while (!q.empty()) 95 | { 96 | current = q.front(); 97 | if (current.x == cx && current.y == cy && current.z == cz) 98 | { 99 | outputPath += current.path; 100 | // return current.length; 101 | } 102 | q.pop(); 103 | if (map[current.y + goal[current.z][0]][current.x + goal[current.z][1]] == 0 && visited[current.y + goal[current.z][0]][current.z][current.x + goal[current.z][1]] == 0) // F 104 | { 105 | next.x = current.x + goal[current.z][1]; 106 | next.y = current.y + goal[current.z][0]; 107 | next.z = current.z; 108 | // next.length = current.length + 1; 109 | next.path = current.path + 'F'; 110 | q.push(next); 111 | visited[current.y + goal[current.z][0]][current.z][current.x + goal[current.z][1]] = 1; 112 | } 113 | if (map[current.y + goal[current.z][0]][current.x + goal[current.z][1]] == 0 && map[current.y + 2 * goal[current.z][0]][current.x + 2 * goal[current.z][1]] == 0 && visited[current.y + 2 * goal[current.z][0]][current.z][current.x + 2 * goal[current.z][1]] == 0) // F2 114 | { 115 | next.x = current.x + 2 * goal[current.z][1]; 116 | next.y = current.y + 2 * goal[current.z][0]; 117 | next.z = current.z; 118 | // next.length = current.length + 1; 119 | next.path = current.path + '2'; 120 | q.push(next); 121 | visited[current.y + 2 * goal[current.z][0]][current.z][current.x + 2 * goal[current.z][1]] = 1; 122 | } 123 | if (map[current.y][current.x] == 0 && visited[current.y][(current.z + 1) % 4][current.x] == 0) // L 124 | { 125 | next.x = current.x; 126 | next.y = current.y; 127 | next.z = (current.z + 1) % 4; 128 | // next.length = current.length + 1; 129 | next.path = current.path + 'L'; 130 | q.push(next); 131 | visited[current.y][(current.z + 1) % 4][current.x] = 1; 132 | } 133 | if (visited[current.y][(current.z + 3) % 4][current.x] == 0) // R 134 | { 135 | next.x = current.x; 136 | next.y = current.y; 137 | next.z = (current.z + 3) % 4; 138 | // next.length = current.length + 1; 139 | next.path = current.path + 'R'; 140 | q.push(next); 141 | visited[current.y][(current.z + 3) % 4][current.x] = 1; 142 | } 143 | } 144 | // return 114514; 145 | } 146 | 147 | int main() 148 | { 149 | std::ios_base::sync_with_stdio(false); 150 | cin >> lx >> ly; 151 | for (short i = 0; i < ly + 4; i++) 152 | { 153 | map[i][0] = 1; 154 | map[i][1] = 1; 155 | map[i][lx + 2] = 1; 156 | map[i][lx + 3] = 1; 157 | } 158 | for (short i = 0; i < lx + 4; i++) 159 | { 160 | map[0][i] = 1; 161 | map[1][i] = 1; 162 | map[ly + 1][i] = 1; 163 | map[ly + 2][i] = 1; 164 | } 165 | for (short i = 2; i <= ly + 1; i++) 166 | for (short j = 2; j <= lx + 1; j++) 167 | { 168 | cin >> temp; 169 | map[i][j] = temp; 170 | } 171 | cin >> ax >> ay >> az >> bx >> by >> bz >> cx >> cy >> cz; 172 | ax += 2, ay += 2, bx += 2, by += 2, cx += 2, cy += 2, az--, bz--, cz--; 173 | Search(); 174 | Search2(); 175 | int res = outputPath.length(); 176 | cout << res << endl; 177 | for (auto i : outputPath) 178 | { 179 | switch (i) 180 | { 181 | case 'F': 182 | cout << "F" << endl; 183 | break; 184 | case '2': 185 | cout << "F2" << endl; 186 | break; 187 | case 'L': 188 | cout << "L" << endl; 189 | break; 190 | case 'R': 191 | cout << "R" << endl; 192 | break; 193 | } 194 | } 195 | } --------------------------------------------------------------------------------