-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6220281
commit 4a6e6aa
Showing
8 changed files
with
259 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# A1038 Recover the Smallest Number | ||
|
||
## 1.题意理解 | ||
把给定的序列拼接到一起,拼成一个最小的数 | ||
|
||
## 2.思路分析 | ||
用字符串拼接,要求拼接结果最小(按字符串和按整数规则是等效的),按照这个思想来排序。 | ||
|
||
## 3.参考代码 | ||
```cpp | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
bool cmp(string a, string b) | ||
{ | ||
return a + b < b + a; // 很巧妙的比较函数 | ||
} | ||
|
||
int main() | ||
{ | ||
int n; | ||
scanf("%d", &n); | ||
string s; | ||
vector<string> v; | ||
|
||
for (int i = 0; i < n; i++) | ||
{ | ||
cin >> s; | ||
v.push_back(s); | ||
} | ||
|
||
sort(v.begin(), v.end(), cmp); | ||
string ans = ""; | ||
for (string t : v) | ||
ans += t; | ||
while (ans.length() && ans[0] == '0') | ||
ans.erase(ans.begin()); // 去掉前导0 | ||
if (ans.length() == 0) | ||
cout << 0; | ||
else | ||
cout << ans << endl; | ||
return 0; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# A1094 The Largest Generation | ||
|
||
## 1.题意理解 | ||
求一棵树的最大宽度,以及是哪一层 | ||
|
||
## 2.思路分析 | ||
遍历这棵树,把每一层的结点数统计出来,找最大的 | ||
|
||
## 3.参考代码 | ||
```cpp | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
const int N = 128; | ||
int n, m; | ||
int ans[N] = {0}; | ||
vector<int> child[N]; | ||
|
||
int maxdepth = 0; | ||
void dfs(int root, int depth) | ||
{ | ||
if (child[root].size() == 0) | ||
{ | ||
ans[depth]++; | ||
maxdepth = max(depth, maxdepth); | ||
return; | ||
} | ||
|
||
ans[depth]++; | ||
for (int i = 0; i < child[root].size(); i++) | ||
dfs(child[root][i], depth + 1); | ||
} | ||
|
||
int main() | ||
{ | ||
int k, id, c; | ||
scanf("%d%d", &n, &m); | ||
|
||
for (int i = 0; i < m; i++) | ||
{ | ||
scanf("%d %d", &id, &k); | ||
for (int j = 0; j < k; j++) | ||
{ | ||
scanf("%d", &c); | ||
child[id].push_back(c); | ||
} | ||
} | ||
|
||
dfs(1, 1); | ||
|
||
int ansLevel = 1, ansNum = 1; | ||
for (int i = 1; i <= maxdepth; i++) | ||
if (ans[i] > ansNum) | ||
{ | ||
ansLevel = i; | ||
ansNum = ans[i]; | ||
} | ||
printf("%d %d\n", ansNum, ansLevel); | ||
return 0; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# A1097 Deduplication on a Linked List | ||
|
||
## 1.题意理解 | ||
给原始链表去重,重复的结点按顺序组成一个新链表 | ||
|
||
## 2.思路分析 | ||
记录关键字绝对值是否出现,第一次出现的结点加入result,出现过的加入removed | ||
|
||
## 3.参考代码 | ||
```cpp | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
const int N = 100100; | ||
int n, k; | ||
|
||
struct node | ||
{ | ||
int Address, Key, Next; | ||
node() {} | ||
node(int addr, int key, int next) : Address(addr), Key(key), Next(next) {} | ||
}; | ||
vector<node> nodes(N); | ||
vector<node> result, removed, lis; | ||
bool vis[N] = {false}; | ||
|
||
int main() | ||
{ | ||
int head, addr, key, next; | ||
scanf("%d %d", &head, &n); | ||
|
||
for (int i = 0; i < n; i++) | ||
{ | ||
scanf("%d %d %d", &addr, &key, &next); | ||
nodes[addr] = node(addr, key, next); | ||
} | ||
|
||
for (int i = head; i != -1; i = nodes[i].Next) | ||
lis.push_back(nodes[i]); // 提取有效节点 | ||
|
||
for (node it : lis) | ||
{ | ||
int temp = abs(it.Key); | ||
if (vis[temp]) | ||
removed.push_back(it); | ||
else | ||
{ | ||
result.push_back(it); | ||
vis[temp] = true; | ||
} | ||
} | ||
|
||
for (int i = 0; i < result.size() - 1; i++) | ||
printf("%05d %d %05d\n", result[i].Address, result[i].Key, result[i + 1].Address); | ||
printf("%05d %d -1\n", result.back().Address, result.back().Key); | ||
|
||
if (removed.size()) | ||
{ | ||
for (int i = 0; i < removed.size() - 1; i++) | ||
printf("%05d %d %05d\n", removed[i].Address, removed[i].Key, removed[i + 1].Address); | ||
printf("%05d %d -1\n", removed.back().Address, removed.back().Key); | ||
} | ||
return 0; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# A1101 Quick Sort | ||
|
||
## 1.题意理解 | ||
快速排序算法的核心——partition操作每次会确定一个主元(pivot)的位置,现在给序列让找出所有可能的主元。 | ||
|
||
## 2.思路分析 | ||
主元需要满足的条件:**左边最大数 <= 主元 <= 右边最小数** | ||
|
||
开一个leftMax和一个rightMin数组,从左到右填充leftMax,从右到左填充rightMin | ||
|
||
## 3.参考代码(晴神版) | ||
```cpp | ||
#include <iostream> | ||
#include <cstdlib> | ||
#include <algorithm> | ||
#include <vector> | ||
using namespace std; | ||
const int N = 1e5 + 10; | ||
const int INF = 0x3f3f3f3f; | ||
int A[N] = {}, leftMax[N] = {}, rightMin[N] = {}; | ||
vector<int> ans; | ||
|
||
int main() | ||
{ | ||
int n, cnt = 0; | ||
scanf("%d", &n); | ||
for (int i = 0; i < n; i++) | ||
scanf("%d", &A[i]); | ||
|
||
leftMax[0] = 0; // 初始化为一个足够小的数 | ||
for (int i = 1; i < n; i++) | ||
leftMax[i] = max(A[i - 1], leftMax[i - 1]); | ||
|
||
rightMin[n - 1] = INF; // 初始化为一个足够大的数 | ||
for (int i = n - 2; i >= 0; i--) | ||
rightMin[i] = min(A[i + 1], rightMin[i + 1]); | ||
|
||
for (int i = 0; i < n; i++) | ||
if (leftMax[i] < A[i] && rightMin[i] > A[i]) // 是否满足主元必要条件 | ||
ans.push_back(A[i]); | ||
|
||
printf("%d\n", ans.size()); | ||
for (int i = 0; i < ans.size(); i++) | ||
{ | ||
printf("%d", ans[i]); | ||
if (i < ans.size() - 1) | ||
printf(" "); | ||
} | ||
printf("\n"); | ||
return 0; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters