1、算法练习专栏——牛客练习——天梯备赛02

A-1_算法练习专栏02 (nowcoder.com)

B-2_算法练习专栏02 (nowcoder.com)

C-3_算法练习专栏02 (nowcoder.com)

D-4_算法练习专栏02 (nowcoder.com)

E-5_算法练习专栏02 (nowcoder.com)

F-6_算法练习专栏02 (nowcoder.com)

G-7(补充题,堆的知识)_算法练习专栏02 (nowcoder.com)

A

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<iostream>
using namespace std;
const int N=1e5+10;
int st[N],mine;
int n;
void Init()
{
mine=-1;
}
int main()
{
cin>>n;
Init();
while(n--)
{
string s;
cin>>s;
if(s=="push")
{
int x;
cin>>x;
st[++mine]=x;
}
else if(mine==-1)
cout<<"error\n";
else if(s=="pop")
{
cout<<st[mine]<<endl;;
mine--;
}
else
{
cout<<st[mine] << endl;
}
}
return 0;
}

B

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
long long q, op, x, k, top, arr[N];

int gcd(int a,int b) {
return (b ? gcd(b, a % b): a);
}

int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin >> q;
while(q--)
{
cin>>op;
if(op == 1) {
cin >> x;
arr[++top] = x;
}
if(op == 2) top--;
if(op == 3) cout<< arr[top]<<'\n';
if(op == 4)
{
int b = arr[top];
cin >> k;
for(int i = top;i >= top - k + 1; i--)
{
b = gcd(b, arr[i - 1]);
if(b == 1) break;
}
for(int i = top;i >= top - k + 1; i --) arr[i] = b;
}
}
return 0;
}

C

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <queue>

using namespace std;
const int N = 1e5 + 10;
int n;
queue<int> Q;

int main()
{
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int n;
cin >> n;
while (n--) {
string s;
cin >> s;
if (s == "push") {
int x;
cin >> x;
Q.push(x);
} else if (Q.empty()) {
cout << "error\n";
} else if (s == "pop") {
cout << Q.front() << '\n';
Q.pop();
} else {
cout << Q.front() << '\n';
}
}


return 0;
}

D

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
using namespace std;
const int N = 1e6 + 10;
int p[N], top_1,top_2;
int a;
string s;

int main()
{
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int n, q;
cin >> n >> q;
while (q--) {
cin >> s;
if (s == "push"){
int x;
cin >> x;
if (top_2 - top_1 == n) {
cout << "full\n";
} else {
p[++top_2] = x;
}
} else if (top_2 == top_1) {
cout << "empty\n";
} else if (s == "front") {
cout << p[top_1 + 1] << '\n';
} else {
cout << p[top_1 + 1] << '\n';
top_1++;
}

}

return 0;
}

E

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <set>

using namespace std;

int T, n;

int main()
{
cin >> T;
while (T--)
{
set<int> q;
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
int x;
cin >> x;
q.insert(x);
}
cout << q.size() << '\n';
}

return 0;
}

F

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <unordered_map>
#include <cstring>

using namespace std;
const int N = 5e4 + 10;
int T,n;
int a[N];

int main()
{
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin >> T;
while (T--)
{
memset(a,0,sizeof a);
unordered_map<int, int> Map;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a[i];
if (Map[a[i]] == 0)
{
Map[a[i]] = 1;
cout << a[i] << " ";
}
}
cout << '\n';
}



return 0;
}

G

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;
const int N = 1e5 + 10;
int n, k, sz;
int h[N];

void down(int x)
{
int t = x;
if (2 * t <= sz && h[2 * t] > h[x]) x = 2 * t;
if (2 * t + 1 <= sz && h[2 * t + 1] > h[x]) x = 2 * t + 1;
if (t != x)
{
swap(h[t],h[x]);
down(x);
}
}

void up(int x) {
while (x / 2 && h[x / 2] < h[x]) {
swap(h[x / 2],h[x]);
x /= 2;
}
}

int main()
{
int n;
char op[6];
cin >> n;
while (n--)
{
cin >> op;

if (!strcmp(op, "push")){
int x;
cin >> x;
++sz;
h[sz] = x;
up(sz);
} else if (!strcmp(op, "top")) {
if (sz != 0) cout << h[1] << '\n';
else cout << "empty\n";
} else {
if (sz != 0)
{
cout << h[1] << '\n';
h[1] = h[sz];

--sz;

down(1);
}
else cout << "empty\n";
}
}
return 0;
}