1144 The Missing Number (20 分)
Sample Input:
10
5 -25 9 6 1 3 4 2 5 17
Sample Output:
7
Note
- 找到第一个没有出现的整数
Code
#include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<string>
#include<fstream>
#include<map>
using namespace std;
const int maxn = 1e6;
int visited[maxn];
int main(){
#ifdef _DEBUG
ifstream cin("data.txt");
#endif
int num, i;
cin >> num;
for(i = 0; i < num; i++){
int temp;
cin >> temp;
if(temp >= 0 && temp < maxn){
visited[temp] = 1;
}
}
for(i = 1; i < maxn; i++){
if(visited[i] == 0 ) break;
}
cout << i;
#ifdef _DEBUG
cin.close();
#endif
return 0;
}
1145 Hashing - Average Search Time (25 分)
Sample Input:
4 5 4
10 6 4 15 11
11 4 15 2
Sample Output:
15 cannot be inserted.
2.8
Note
- 题意输入 tablesize num queries,下一行存入myhash,存不进去的记 cannot be inserted, 最后一行输入查询序列,输出平均查询长度
- 注意: 查找元素时,如果位置上没有元素则查询结束
Code
#include<iostream>
#include<algorithm>
#include<vector>
#include<fstream>
#include<queue>
#include<string>
#include<cstring>
#include<cmath>
#include<map>
using namespace std;
const int maxn = 1e7;
bool isprime(int n ){
if (n <= 1 ) return 0;
for(int i = 2; i <= sqrt(n); i++){
if(n % i == 0) return 0;
}
return 1;
}
int main(){
#ifdef _DEBUG
ifstream cin("data2.txt");
#endif
int num, i, tablesize, quries;
cin >> tablesize >> num >> quries;
while(isprime(tablesize) == 0) tablesize++;
vector<int> myhash(tablesize, -1);
for(i = 0; i < num; i++){
int temp;
cin >> temp;
int key = temp % tablesize, cnt = 1;
while(myhash[key] != -1 && cnt < tablesize){
key =( temp % tablesize + cnt * cnt) % tablesize;
cnt++;
}
if(cnt < tablesize) myhash[key] = temp;
else if(cnt == tablesize)
printf("%d cannot be inserted.\n", temp);
}
int finalcnt = 0;
for(i = 0; i < quries; i++){
int temp;
cin >> temp;
int key = temp % tablesize, cnt = 1;
finalcnt++;
while(myhash[key] != temp && cnt <= tablesize && myhash[key] != -1){
key =( temp % tablesize + cnt * cnt) % tablesize;
cnt++;
finalcnt++;
}
}
printf("%.1lf", finalcnt * 1.0 / quries);
#ifdef _DEBUG
cin.close();
#endif
return 0;
}
1146 Topological Order (25 分)
Sample Input:
6 8
1 2
1 3
5 2
5 4
2 3
2 6
3 4
6 4
5
1 5 2 3 6 4
5 1 2 6 3 4
5 1 2 3 6 4
5 2 1 6 3 4
1 2 3 4 5 6
Sample Output:
3 4
Note
- 题意: 输入有向图,问queries个序列是否为拓扑排序。
- 思路: 记录每个节点的入度indegree(num), 以及每个节点的出边inta[maxn],检测每个节点输出时入度是否为零,并将该节点的出边节点入度–
- 敲的过程中又犯了最基础的输入没完break的问题。。。
Code
#include<iostream>
#include<vector>
#include<map>
#include<fstream>
using namespace std;
const int maxn = 1e6;
vector<int> a[maxn];
vector<int> indegree;
int main(){
#ifdef _DEBUG
ifstream cin("data3.txt");
#endif
int i, j, k, num, edge, queries, cnt = 0, tempa, tempb;
cin >> num >> edge;
indegree.resize(num + 1);
for(i = 0; i < edge; i++){
cin >> tempa >> tempb;
indegree[tempb]++;
a[tempa].push_back(tempb);
}
cin >> queries;
for(i = 0; i < queries; i++){
vector <int> tempdegree, save(num);
tempdegree = indegree;
for(j = 0; j < num; j++)
cin >> save[j];
for(j = 0; j < num; j++){
tempa = save[j];
if(tempdegree[tempa] != 0 ) {
if(cnt == 0) cout << i;
else cout << " " << i;
cnt++;
break;
}
for(int k = 0; k < a[tempa].size(); k++){
tempdegree[a[tempa][k]]--;
}
}
}
#ifdef _DEBUG
cin.close();
#endif
return 0;
}
1147 Heaps (30 分)
Sample Input:
3 8
98 72 86 60 65 12 23 50
8 38 25 58 52 82 70 60
10 28 15 12 34 9 8 56
Sample Output:
Max Heap
50 60 65 72 12 23 86 98
Min Heap
60 58 52 38 82 70 25 8
Not Heap
56 12 34 28 9 8 15 10
Note
- 题意:判断是否为大顶堆小顶堆,输入的是层次遍历结果,输出后序遍历结果
- 思路: 判断是否为堆利用了insert的主要逻辑 a[i] > a[i/2],BST的层次序列转后序,利用递归左右子树刚好为 *2 和 *2 + 1
- 近些年最后的题目码量上都相对变少,主要还是考的思路和简化
Code
#include<iostream>
#include<algorithm>
#include<vector>
#include<fstream>
using namespace std;
void PreTraverse(vector <int> a, int index){
if(index >= a.size()) return;
PreTraverse(a, index * 2 );
PreTraverse(a, index * 2 +1);
if(index != 1) cout << a[index] << " ";
else cout << a[index] << endl;
}
int main(){
#ifdef _DEBUG
ifstream cin("data4.txt");
#endif
int treenum, num, i, j, index;
cin >> treenum >> num;
while(treenum--){
vector <int> a(num + 1);
int flag = 0;
for(i = 1; i <= num; i++) cin >> a[i];
if(a[1] > a[num] ){
flag = 1;
for(j = 2; j <= num; j++)
if(a[j] >a[j/2]){
flag = 0;
break;
}
}else{
flag = 2;
for(j = 2; j <= num; j++)
if(a[j] < a[j/2]){
flag = 0;
break;
}
}
if(flag == 0) cout << "Not Heap";
else if(flag == 1) cout << "Max Heap";
else cout << "Min Heap";
cout << endl;
PreTraverse(a, 1);
}
#ifdef _DEBUG
cin.close();
#endif
return 0;
}
Summary
- ( temp % tablesize + cnt * cnt) % tablesize与(temp + cnt * cnt ) % tablesize;相等吗,为什么两种写法都可过呢
- 前中后层次序列之间的转换: