团体程序设计天梯赛-练习集所有L2题目全集

团体程序设计天梯赛-练习集所有L2题目全集

标号 标题 分数
L2-001 紧急救援 25
L2-002 链表去重 25
L2-003 月饼 25
L2-004 这是二叉搜索树吗? 25
L2-005 集合相似度 25
L2-006 树的遍历 25
L2-007 家庭房产 25
L2-008 最长对称子串 25
L2-009 抢红包 25
L2-010 排座位 25
L2-011 玩转二叉树 25
L2-012 关于堆的判断 25
L2-013 红色警报 25
L2-014 列车调度 25
L2-015 互评成绩 25
L2-016 愿天下有情人都是失散多年的兄妹 25
L2-017 人以群分 25
L2-018 多项式A除以B 25
L2-019 悄悄关注 25
L2-020 功夫传人 25
L2-021 点赞狂魔 25
L2-022 重排链表 25
L2-023 图着色问题 25
L2-024 部落 25
L2-025 分而治之 25
L2-026 小字辈 25
L2-027 名人堂与代金券 25
L2-028 秀恩爱分得快 25
L2-029 特立独行的幸福 25
L2-030 冰岛人 25
L2-031 深入虎穴 25
L2-032 彩虹瓶 25
L2-033 简单计算器 25
L2-034 口罩发放 25
L2-035 完全二叉树的层序遍历 25
L2-036 网红点打卡攻略 25
L2-037 包装机 25
L2-038 病毒溯源 25
L2-039 清点代码库 25
L2-040 哲哲打游戏 25
L2-041 插松枝 25
L2-042 老板的作息表 25
L2-043 龙龙送外卖 25
L2-044 大众情人 25
L2-045 堆宝塔 25
L2-046 天梯赛的赛场安排 25
L2-047 锦标赛 25
L2-048 寻宝图 25

L2-001 紧急救援

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <bits/stdc++.h>

#define x first
#define y second;
#define LL long long

using namespace std;

typedef pair<int, int> PII;
const int N = 510;

int n, m, S, E;
LL v[N];
int g[N][N];
int dist[N];
LL cnt[N], w[N];
bool st[N];
int mp[N];

void dijkstra()
{
memset(dist, 0x3f, sizeof dist);
dist[S] = 0;
cnt[S] = 1;
for(int i = 1; i <= n; i ++ ) w[i] = v[i];
priority_queue<PII, vector<PII>, greater<PII> > q;
q.push({0, S});
while(!q.empty())
{
auto t = q.top();
q.pop();
int dis = t.x, u = t.y;

if(st[u]) continue;
st[u] = true;

for(int i = 1; i <= n; i ++ )
{
if(g[u][i] == 0x3f3f3f3f) continue;
if(u == i) continue;
int t = g[u][i] + dis;
if(dist[i] > t)
{
mp[i] = u;
dist[i] = t;
cnt[i] = cnt[u];
w[i] = w[u] + v[i];
q.push({dist[i], i});
}
else if(dist[i] == t)
{
cnt[i] += cnt[u];
if(w[u] + v[i] > w[i])
{
w[i] = w[u] + v[i];
mp[i] = u;
}
}
}
}
}

int main()
{
cin >> n >> m >> S >> E;
S ++; E ++;
for(int i = 1; i <= n; i ++ )
cin >> v[i];
memset(g, 0x3f, sizeof g);
while(m -- )
{
int u, v, c;
cin >> u >> v >> c;
u ++; v ++;
g[u][v] = min(g[u][v], c);
g[v][u] = min(g[v][u], c);
}

mp[S] = -1;
dijkstra();

cout << cnt[E] << ' ' << w[E] << endl;
vector<int> ans;
for(int i = E; ~i; i = mp[i]) ans.push_back(i);
for(int i = ans.size() - 1; i >= 0; i -- )
cout << ans[i] - 1 << " \n"[i == 0];
return 0;
}

L2-002 链表去重

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
62
#include <bits/stdc++.h>
using namespace std;

int head;
int n;
map<int, int> l, r;
map<int, int> cnt;
map<int, int> w;
vector<int> res;
bool st[1000000];

int main()
{
cin >> head >> n;
l[head] = -1;
while(n -- )
{
int u, v;
int d;
cin >> u >> d >> v;
// printf("%d %d %d\n", u, v, d);
r[u] = v;
w[u] = d;
l[v] = u;
if(d == 0) w[u] = 1e9;
}

for(int i = head; i != -1; i = r[i])
{
cnt[abs(w[i])] ++;
if(cnt[abs(w[i])] > 1)
{
res.push_back(i);
r[l[i]] = r[i];
l[r[i]] = l[i];
}
}

for(int i = head; ~i; i = r[i])
{
if(r[i] != -1)
printf("%05d %d %05d\n", i, (w[i] == 1e9 ? 0:w[i]), r[i]);
else
{
printf("%05d %d %d", i, (w[i] == 1e9 ? 0:w[i]), r[i]);
if(res.size()) cout << endl;
}
st[i] = true;
}

int len = res.size();
for(int i = 0; i < len; i ++ )
{
if(i != len - 1)
printf("%05d %d %05d\n", res[i], (w[res[i]] == 1e9 ? 0:w[res[i]]), res[i + 1]);
else
printf("%05d %d %d\n", res[i], (w[res[i]] == 1e9 ? 0:w[res[i]]), -1);
st[res[i]] = true;
}

return 0;
}

L2-003 月饼

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
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
struct node{
double x, y;
bool operator <(const node &a)const {
return x * a.y < a.x * y;
}
}f[N];
signed main()
{
int n, m;
cin >> n >> m;
for(int i = 0; i < n; i++)
cin >> f[i].x;
for(int i = 0; i < n; i++)
cin >> f[i].y;
sort(f, f + n);
double res = 0;
int l = 0;
while(l < n && m >= f[l].x)
res += f[l].y, m -= f[l++].x;
if(l < n) res += m * f[l].y / f[l].x;
printf("%.2lf", res);
}

L2-004 这是二叉搜索树吗?

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <bits/stdc++.h>
using namespace std;

const int N = 1010;

int n;
int tr[N];
struct Node{
int date;
Node *left;
Node *right;
};
bool f = true;
int cnt;

Node *dfs(int *tr, int len, int p)
{
if(tr == NULL || len == 0)
return NULL;

Node *root = new Node;
root->date = *tr;
if(len == 1) return root;
int pos = 1;
while(pos < len)
{
if(p == 0 && *(tr + pos) >= *tr) break;
if(p == 1 && *(tr + pos) < *tr) break;
pos ++;
}
for(int i = pos; i < len; i ++ )
if(p == 0 && *(tr + i) < *tr)
{
f = false;
return NULL;
}
else if(p == 1 && *(tr + i) >= *tr)
{
f = false;
return NULL;
}
if(pos - 1 == 0)
root->left = NULL;
else
root->left = dfs(tr + 1, pos - 1, p);

if(len - pos == 0)
root->right = NULL;
else
root->right = dfs(tr + pos, len - pos, p);

return root;
}

void out(Node *root)
{
if(root->left != NULL)
out(root->left);
if(root->right != NULL)
out(root->right);
if(cnt) cout << ' ' ;
cnt = 1;
cout << root->date;
}

int main()
{
cin >> n;
for(int i = 1; i <= n; i ++ )
cin >> tr[i];

Node *root = dfs(tr + 1, n, 0);
if(f)
{
puts("YES");
out(root); return 0;
}
f = true;
root = dfs(tr + 1, n, 1);
if(f)
{
puts("YES");
out(root); return 0;
}
puts("NO");
return 0;
}

L2-005 集合相似度

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
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> PII;
const int N = 55, P = 131;

int n, m;
set<int> s[N];

int main()
{
cin >> n;
for(int i = 1; i <= n; i ++ )
{
cin >> m;
for(int j = 0; j < m; j ++ )
{
int x; cin >> x;
s[i].insert(x);
}
}

cin >> m;
while(m -- )
{
int a, b;
cin >> a >> b;
int f = s[a].size() + s[b].size();
int cnt = 0;
for(auto x : s[a])
if(s[b].find(x) != s[b].end())
{
cnt ++;
}
printf("%.2lf%\n", cnt * 100.0 / (f - cnt));
}

return 0;
}

L2-006 树的遍历

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
62
63
64
65
66
67
68
69
#include <bits/stdc++.h>
using namespace std;

const int N = 33;

int n;
struct Node{
int date;
Node *left;
Node *right;
};
int mid[N], suff[N];

Node *get_tree(int *suff, int *mid, int len)
{
if(suff == NULL || len <= 0)
return NULL;

Node *root = new Node;
root->date = *(suff + len - 1);
int pos = 0;
while(pos < len)
{
if(*(mid + pos) == root->date) break;
pos ++;
}

if(pos <= 0)
root->left = NULL;
else{
root->left = get_tree(suff, mid, pos);
}

if(len - pos - 1 <= 0)
root->right = NULL;
else{
root->right = get_tree(suff + pos, mid + pos + 1, len - pos - 1);
}
return root;
}

void out(Node *root)
{
queue<Node*> q;
q.push(root);
int cnt = 0;
while(!q.empty())
{
Node *now = q.front();
q.pop();
cnt ++;
cout << now->date << " \n"[cnt == n];
if(now->left != NULL) q.push(now->left);
if(now->right != NULL) q.push(now->right);
}
}

int main()
{
cin >> n;
for(int i = 0; i < n; i ++ )
cin >> suff[i];
for(int i = 0; i < n; i ++ )
cin >> mid[i];

Node *root = get_tree(suff, mid, n);

out(root);
}

L2-007 家庭房产

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
62
63
64
65
66
67
68
69
70
71
72
73
#include <bits/stdc++.h>
using namespace std;

const int N = 10000;

int n;
int d[N], cnt[N], s[N], p[N];
int mp[N];
struct Node{
int id, t;
double c, s;
};

int find(int x)
{
if(x != p[x]) p[x] = find(p[x]);
return p[x];
}

bool cmp(Node &x, Node &y)
{
if(x.s == y.s) return x.id < y.id;
return x.s > y.s;
}

void merge(int a, int b)
{
if(d[a] == -1) d[a] = 1;
if(b != -1 && d[b] == -1) d[b] = 1;
if(b == -1) return ;
int fa = find(a), fb = find(b);
if(fa == fb) return ;
d[fb] += d[fa];
cnt[fb] += cnt[fa];
s[fb] += s[fa];
mp[fb] = min(mp[fb], mp[fa]);
p[fa] = p[fb];
}

int main()
{
cin >> n;
for(int i = 1; i < 10000; i ++ )
mp[i] = i, p[i] = i;
memset(d, -1, sizeof d);
for(int i = 0; i < n; i ++ )
{
int id, f, m, k;
cin >> id >> f >> m >> k;
merge(id, f); merge(id, m);
for(int j = 0; j < k; j ++ )
{
int x; cin >> x;
merge(id, x);
}
int x, y;
cin >> x >> y;
int fid = find(id);
cnt[fid] += x; s[fid] += y;
}

vector<Node> v;
for(int i = 0; i < N; i ++ )
if(d[i] != -1 && i == find(i))
{
v.push_back({mp[i], d[i], cnt[i] * 1.0 / d[i], s[i] * 1.0 / d[i]});
}
sort(v.begin(), v.end(), cmp);
cout << v.size() << endl;
for(int i = 0; i < v.size(); i ++ )
printf("%04d %d %.3lf %.3lf\n", v[i].id, v[i].t, v[i].c, v[i].s);
return 0;
}

L2-008 最长对称子串

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 <bits/stdc++.h>
using namespace std;

const int N = 1010;

int n;
char s[N];
bool f[N][N];

int main()
{
scanf("%[^\n]\n", s + 1);
int n = strlen(s + 1);

int res = 1;
for(int i = 1; i <= n; i ++ ) f[i][i] = true;
for(int i = 2; i <= n; i ++ )
for(int l = 1; l <= n - i + 1; l ++ )
{
int r = l + i - 1;
if(i == 2) f[l][r] = (s[l] == s[r]);
else f[l][r] = (s[l] == s[r] && f[l + 1][r - 1]);
if(f[l][r])
res = max(res, i);
}
cout << res;
}

L2-009 抢红包

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
#include <bits/stdc++.h>
using namespace std;

const int N = 1e4 + 10;

int n;
struct Node{
int id, cnt;
double money;
}a[N];

bool cmp(Node &x, Node &y)
{
if(x.money == y.money)
if(x.cnt == y.cnt) return x.id < y.id;
else return x.cnt > y.cnt;
else return x.money > y.money;
}

int main()
{
cin >> n;
for(int i = 1; i <= n; i ++ )
a[i] = {i, 0};
for(int i = 1; i <= n; i ++ )
{
int k; cin >> k;
for(int j = 0; j < k; j ++ )
{
int id; double p;
cin >> id >> p;
p /= 100;
a[id].cnt ++;
a[id].money += p;
a[i].money -= p;
}
}
sort(a + 1, a + n + 1, cmp);
for(int i = 1; i <= n; i ++ )
printf("%d %.2lf\n", a[i].id, a[i].money);
return 0;
}

L2-010 排座位

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
#include <bits/stdc++.h>
#define PII pair<int,int>
using namespace std;

const int N = 110;

int n, m, k;
int p[N];
map<PII, int> st;

int find(int x)
{
if(x != p[x]) p[x] = find(p[x]);
return p[x];
}

int main()
{
cin >> n >> m >> k;
for(int i = 1; i <= n; i ++ ) p[i] = i;
while(m -- )
{
int x, y, c;
cin >> x >> y >> c;
if(x > y) swap(x, y);
if(c == 1)
{
int fx = find(x), fy = find(y);
if(fx != fy) p[fx] = p[fy];
}
else
st[{x, y}] = -1;
}

while(k -- )
{
int x, y;
cin >> x >> y;
if(x > y) swap(x, y);
int fx = find(x), fy = find(y);
if(st[{x, y}] == -1)
{
if(fx == fy) puts("OK but...");
else puts("No way");
}
else
{
if(fx != fy) puts("OK");
else puts("No problem");
}
}
return 0;
}

L2-011 玩转二叉树

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <bits/stdc++.h>
using namespace std;

const int N = 33;

int n;
struct Node{
int date;
Node *left;
Node *right;
};
int mid[N], pre[N];

Node *get_tree(int *pre, int *mid, int len)
{
if(pre == NULL || len <= 0)
return NULL;

Node *root = new Node;
root->date = *pre;
int pos = 0;
while(pos < len)
{
if(*(mid + pos) == root->date) break;
pos ++;
}

if(pos <= 0)
root->left = NULL;
else{
root->left = get_tree(pre + 1, mid, pos);
}

if(len - pos - 1 <= 0)
root->right = NULL;
else{
root->right = get_tree(pre + pos + 1, mid + pos + 1, len - pos - 1);
}
return root;
}

void exchange(Node *root)
{
if(root->left == NULL && root->right == NULL)
return ;
Node *t = root->left;
root->left = root->right;
root->right = t;
if(root->left != NULL) exchange(root->left);
if(root->right != NULL) exchange(root->right);
}

void out(Node *root)
{
queue<Node*> q;
q.push(root);
int cnt = 0;
while(!q.empty())
{
Node *now = q.front();
q.pop();
cnt ++;
int k = log2(cnt) + 1;
cout << now->date << " \n"[cnt == n];
if(now->left != NULL) q.push(now->left);
if(now->right != NULL) q.push(now->right);
}
}

int main()
{
cin >> n;
for(int i = 0; i < n; i ++ )
cin >> mid[i];
for(int i = 0; i < n; i ++ )
cin >> pre[i];

Node *root = get_tree(pre, mid, n);

exchange(root);

out(root);
}

L2-012 关于堆的判断

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
62
63
64
65
66
#include <bits/stdc++.h>
using namespace std;

int n, m;
int heap[1010];
int a[30000];

void create(int x, int i)
{
heap[i] = x;
while(i > 1 && heap[i] < heap[i / 2])
{
swap(heap[i], heap[i / 2]);
i /= 2;
}
}

int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i ++ )
{
int x; cin >> x;
create(x, i);
}

for(int i = 1; i <= n; i ++ )
a[heap[i] + 10000] = i;

while(m -- )
{
int x, y;
cin >> x;
x += 10000;
char str[50];
scanf("%s", str);
if(str[0] == 'a')
{
scanf("%d %s %s", &y, str, str);
y += 10000;
if(a[x] / 2 == a[y] / 2) puts("T");
else puts("F");
}else{
scanf("%s", str);
if(str[0] == 'a')
{
scanf("%s %s %d", str, str, &y);
y += 10000;
if(a[x] / 2 == a[y]) puts("T");
else puts("F");
}else{
scanf("%s", str);
if(str[0] == 'r'){
if(a[x] == 1) puts("T");
else puts("F");
}else{
scanf("%s %d\n", str, &y);
y += 10000;
if(a[x] == a[y] / 2) puts("T");
else puts("F");
}
}
}
}
return 0;
}

L2-013 红色警报

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
62
63
#include <bits/stdc++.h>
#define PII pair<int,int>
using namespace std;

const int N = 510, M = 5010;

int n, m;
PII e[M];
int p[N];
bool st[N];

int find(int x)
{
if(p[x] != x) p[x] = find(p[x]);
return p[x];
}

int check()
{
for(int i = 0; i < n; i ++ ) p[i] = i;
for(int i = 0; i < m; i ++ )
{
int u = e[i].first, v = e[i].second;
if(st[u] || st[v]) continue;
int fu = find(u), fv = find(v);
if(fu != fv) p[fu] = p[fv];
}
int res = 0;
for(int i = 0; i < n; i ++ )
if(!st[i] && i == find(i))
res ++;
return res;
}

int main()
{
cin >> n >> m;
for(int i = 0; i < m; i ++ )
{
int u, v;
cin >> u >> v;
e[i] = make_pair(u, v);
}

int k; cin >> k;
int cnt = check();
while(k -- )
{
int x; cin >> x;
st[x] = true;
int t = check();
if(t > cnt)
printf("Red Alert: City %d is lost!\n", x), cnt = t;
else
{
printf("City %d is lost.\n", x);
cnt = t;
}
}
cnt = check();
if(!cnt) puts("Game Over.");
return 0;
}

L2-014 列车调度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> PII;
const int N = 1e5 + 10;

int n;
set<int> s;

int main()
{
cin >> n;
for(int i = 0; i < n; i ++ )
{
int x; cin >> x;
set<int>::iterator it = s.lower_bound(x);
if(it != s.end())
s.erase(it);
s.insert(x);
}
cout << s.size();
return 0;
}

L2-015 互评成绩

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 <bits/stdc++.h>
using namespace std;

int n, m, k;
vector<double> v;

int main()
{
cin >> n >> m >> k;
for(int i = 0; i < n; i ++ )
{
double avg = 0;
int mx = 0, mn = 1e9;
for(int j = 0; j < m; j ++ )
{
int x; cin >> x;
avg = avg + x;
mx = max(mx, x); mn = min(mn, x);
}
avg = (avg - mx - mn) / (m - 2);
v.push_back(avg);
}
sort(v.begin(), v.end());
for(int i = k; i > 0; i -- )
printf("%.3lf%c", v[n - i], " \n"[i == 1]);
return 0;
}

L2-016 愿天下有情人都是失散多年的兄妹

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
62
63
64
65
66
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6;

int n;
vector<int> e[N];
int vis[N];
char sex[N];
bool f;

void dfs(int u, int cnt)
{
if(cnt >= 4) return;
for(int i = 0; i < e[u].size(); i ++ )
{
if(vis[e[u][i]]) f = false;
else{
vis[e[u][i]] = 1;
dfs(e[u][i], cnt + 1);
}
}
}

int main()
{
cin >> n;
memset(sex, '1', sizeof sex);
for(int i = 0; i < n; i ++ )
{
int id; cin >> id;
cin >> sex[id];
int fa, ma;
cin >> fa >> ma;
if(fa != -1)
{
e[id].push_back(fa);
sex[fa] = 0;
}
if(ma != -1)
{
e[id].push_back(ma);
sex[ma] = 1;
}
}

int k; cin >> k;
while(k -- )
{
int id1, id2;
cin >> id1 >> id2;
if(sex[id1] == '1' || sex[id2] == '1') continue;
if(sex[id1] == sex[id2])
{
puts("Never Mind");
continue;
}
memset(vis, 0, sizeof vis);
vis[id1] = 1;
vis[id2] = 1;
f = true;
dfs(id1, 0); dfs(id2, 0);
if(f) puts("Yes");
else puts("No");
}
return 0;
}

L2-017 人以群分

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
#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10;

int n;
int a[N];

int main()
{
cin >> n;
for(int i = 0; i < n; i ++ )
cin >> a[i];
sort(a, a + n);
int s1, s2;
s1 = s2 = 0;
for(int i = 0, j = n - 1; i <= j; i ++ , j -- )
{
s2 += a[j];
if(i != j) s1 += a[i];
}
printf("Outgoing #: %d\n", (n + 1) / 2);
printf("Introverted #: %d\n", n / 2);
printf("Diff = %d", s2 - s1);
return 0;
}

L2-018 多项式A除以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
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>
#include<math.h>
#include<map>

using namespace std;

int main() {
int n, e,max;
double c;
map<int, double, greater<int>> A, B, C, Q, R;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> e >> c;
A[e] = c;
}
cin >> n;
for (int i = 0; i < n; i++) {
cin >> e >> c;
B[e] = c;
}
while (A.begin()->first>= B.begin()->first) {
e = A.begin()->first - B.begin()->first;
c = A.begin()->second / B.begin()->second;
C[e] += c;
for (auto&it : B)
A[it.first + e] -= it.second*c;
A.erase(A.begin());
}
for (auto& it : A)
if (abs(it.second) >= 0.05)
R[it.first] = it.second;
for (auto& it : C)
if (abs(it.second) >= 0.05)
Q[it.first] = it.second;
printf("%d", Q.size());
if (Q.size())
for (auto& it : Q)
printf(" %d %.1lf", it.first, it.second);
else
printf(" 0 0.0");
printf("\n%d", R.size());
if (R.size())
for (auto& it : R)
printf(" %d %.1lf", it.first, it.second);
else
printf(" 0 0.0");
return 0;
}

L2-019 悄悄关注

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;

map<string, int> st;
vector<string> ans;
pair<string, int> a[10010];

int main()
{
int n; cin >> n;
for(int i = 0; i < n; i ++ )
{
string s; cin >> s;
st[s] = 1;
}
int m; cin >> m;
int avg = 0;
for(int i = 0; i < m; i ++ )
{
cin >> a[i].first >> a[i].second;
avg += a[i].second;
}
avg = (avg + m - 1) / m;
for(int i = 0; i < m; i ++ )
if(a[i].second >= avg && !st[a[i].first])
ans.push_back(a[i].first);

if(ans.size())
{
sort(ans.begin(), ans.end());
for(int i = 0; i < ans.size(); i ++ )
cout << ans[i] << endl;
}
else puts("Bing Mei You");
return 0;
}

L2-020 功夫传人

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
#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10;

int n;
double z, r, ans;
vector<int> tr[N];
int bs[N];

void dfs(int u, double z)
{
if(tr[u].size() == 0)
ans += z * bs[u];
else{
int len = tr[u].size();
for(int i = 0; i < len; i ++ )
dfs(tr[u][i], z * (1 - r));
}
}

int main()
{
cin >> n >> z >> r;
r /= 100;
for(int i = 0; i < n; i ++ )
{
int m;
cin >> m;
if(m == 0)
{
int x; cin >> x;
bs[i] = x;
continue;
}
while(m -- )
{
int x; cin >> x;
tr[i].push_back(x);
}
}

dfs(0, z);

printf("%ld", (long long)ans);
return 0;
}

L2-021 点赞狂魔

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
#include <bits/stdc++.h>
using namespace std;

const int N = 110;

int n;
struct Node{
int a;
double b;
string s;
bool operator < (const Node &x) const{
if(this->a == x.a)
return this->b < x.b;
return this->a > x.a;
}
};
Node ans[N];

int main()
{
cin >> n;
for(int i = 0; i < n; i ++ )
{
string name; int k;
cin >> name >> k;
set<int> s;
for(int j = 0; j < k; j ++ )
{
int x; cin >> x;
s.insert(x);
}
int len = s.size();
ans[i] = {len, k * 1.0 / len, name};
}
sort(ans, ans + n);
for(int i = 0; i < 3; i ++ )
if(i < n)
cout << ans[i].s << " \n"[i == 2];
else cout << '-' << " \n"[i == 2];
return 0;
}

L2-022 重排链表

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
#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10;

int h, n;
map<int, int> ne, w;

int main()
{
cin >> h >> n;
for(int i = 1, idx; i <= n; i ++ )
cin >> idx >> w[idx] >> ne[idx];
vector<int> v;
n = 0;
for(int i = h; ~i; i = ne[i])
v.push_back(i), n ++;
vector<int> res;
for(int i = n - 1, j = 0; i >= j; i --, j ++ )
{
res.push_back(v[i]);
if(i != j) res.push_back(v[j]);
}
for(int i = 0; i < n; i ++ )
if(i != n - 1)
printf("%05d %d %05d\n", res[i], w[res[i]], res[i + 1]);
else
printf("%05d %d -1\n", res[i], w[res[i]]);

}

L2-023 图着色问题

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
62
63
64
65
66
67
68
#include <bits/stdc++.h>
using namespace std;

const int N = 510;

int n, m, k;
int e[250010], ne[250010], h[N], idx;
int c[N];
bool st[N];

void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}

bool dfs(int u)
{
if(st[u]) return true;
st[u] = true;
for(int i = h[u]; ~i; i = ne[i])
{
int j = e[i];
if(c[u] == c[j])
{
// printf("%d----%d %d-----%d\n", u, c[u], j, c[j]);
return false;
}
if(!dfs(j)) return false;
}
return true;
}

int main()
{
cin >> n >> m >> k;
memset(h, -1, sizeof h);
while(m -- )
{
int a, b;
cin >> a >> b;
add(a, b);
add(b, a);
}
int q; cin >> q;
while(q -- )
{
for(int i = 1; i <= n; i ++ ) st[i] = false;
set<int> s;
for(int i = 1; i <= n; i ++ ){
cin >> c[i];
s.insert(c[i]);
}
if(s.size() == k)
{
bool f = true;
for(int i = 1; i <= n; i ++ )
if(!st[i])
{
f = dfs(i);
if(!f) break;
}
if(f) puts("Yes");
else puts("No");
}
else puts("No");
}
return 0;
}

L2-024 部落

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
#include <bits/stdc++.h>
using namespace std;

const int N = 1e4 + 10;

int n;
int p[N];
set<int> s;

int find(int x)
{
if(p[x] != x) p[x] = find(p[x]);
return p[x];
}

int main()
{
cin >> n;
for(int i = 1; i < N; i ++ ) p[i] = i;
for(int i = 0; i < n; i ++ )
{
int k; cin >> k;
int fa = -1;
for(int j = 0; j < k; j ++ )
{
int x; cin >> x;
s.insert(x);
int fx = find(x);
if(fa == -1) fa = find(x);
else if(fx != fa)
p[fx] = fa;
}
}

cout << s.size() << ' ';
int cnt = 0;
for(auto x : s)
if(find(p[x]) == x)
cnt ++;
cout << cnt << endl;
int q; cin >> q;
while(q -- )
{
int a, b;
cin >> a >> b;
int fa = find(p[a]);
int fb = find(p[b]);
if(fa == fb) cout << 'Y' << endl;
else cout << 'N' << endl;
}
return 0;
}

L2-025 分而治之

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
#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10;

int n, m;
int e[N], ne[N], h[N], idx;
bool st[N];

void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}

int main()
{
cin >> n >> m;
memset(h, -1, sizeof h);
while(m -- )
{
int a, b;
cin >> a >> b;
add(a, b);
add(b, a);
}

int q; cin >> q;
while(q -- )
{
int k; cin >> k;
for(int i = 1; i <= n; i ++ ) st[i] = false;
for(int i = 0; i < k; i ++ )
{
int x; cin >> x;
st[x] = true;
}
bool flag = true;
for(int i = 1; i <= n; i ++ )
if(!st[i])
{
for(int j = h[i]; ~j; j = ne[j])
if(e[j] != i && !st[e[j]])
{
// cout << i << "=======" << e[j] << endl;
flag = false;
break;
}
if(!flag) break;
}
if(flag) puts("YES");
else puts("NO");
}
return 0;
}

L2-026 小字辈

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
#include <bits/stdc++.h>
using namespace std;

const int N = 1e5+10;

int n;
vector<int> ans;
vector<int> tr[N];
int root, res = 0;

void dfs(int u, int deep)
{
if(tr[u].size() == 0)
{
if(deep == res) ans.push_back(u);
else if(deep > res)
{
res = deep;
ans.clear();
ans.push_back(u);
}
}
else
{
for(int i = 0; i < tr[u].size(); i ++ )
dfs(tr[u][i], deep + 1);
}
}

int main()
{
cin >> n;
for(int i = 1; i <= n; i ++ )
{
int x; cin >> x;
if(x == -1) root = i;
else tr[x].push_back(i);
}
dfs(root, 1);
sort(ans.begin(), ans.end());
cout << res << endl;
for(int i = 0; i < ans.size(); i ++ )
printf("%d%c", ans[i], " \n"[i == ans.size() - 1]);
return 0;
}

L2-027 名人堂与代金券

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 <bits/stdc++.h>
using namespace std;

int n, g, k;
int rak[10010];
struct Node{
int x; string s;

bool operator < (const Node& q) const{
if(this->x == q.x) return this->s < q.s;
return this->x > q.x;
}
}a[10010];
int res;

int main()
{
cin >> n >> g >> k;
for(int i = 0; i < n; i ++ )
{
cin >> a[i].s >> a[i].x;
if(a[i].x >= g) res += 50;
else if(a[i].x <= g && a[i].x >= 60) res += 20;
}
sort(a, a + n);
cout << res << endl;
for(int i = 0, j = 1; i < n; i ++ )
{
rak[i] = j;
if(a[i].x > a[i + 1].x) j = max(j + 1, i + 2);
}
for(int i = 0; rak[i] <= k && i < n; i ++ )
cout << rak[i] << ' ' << a[i].s << ' ' << a[i].x << endl;
return 0;
}

L2-028 秀恩爱分得快

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <bits/stdc++.h>
using namespace std;

const int N = 1010;

int n, m;
double g[N][N];
char sex[N];
struct Node{
double q;
int id;
bool operator < (const Node& x) const{
if(x.q == this->q) return this->id < x.id;
return this->q > x.q;
}
};

int read()
{
char c = getchar();
int f = 1, x = 0;
while(c < '0' || c > '9')
{
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9'){
x = x * 10 + c - '0';
c = getchar();
}
x ++;
return f * x;
}

int main()
{
cin >> n >> m;
for(int i = 0; i < m; i ++ )
{
int k; cin >> k;
vector<int> v;
for(int j = 0; j < k; j ++ )
{
int x = read();
if(x < 0)
{
sex[-x] = 'S';
v.push_back(-x);
}
else
{
sex[x] = 'M';
v.push_back(x);
}
}
for(int j = 0; j < k; j ++ )
for(int z = j + 1; z < k; z ++ )
g[v[j]][v[z]] += 1.0 / k, g[v[z]][v[j]] += 1.0 / k;
}

int x = read(), y = read();
int a = abs(x), b = abs(y);
bool f1, f2;
f1 = f2 = false;
vector<Node> pa, pb;
for(int i = 1; i <= n; i ++ )
{
if(sex[i] != sex[a])
pa.push_back({g[a][i], i});
if(sex[i] != sex[b])
pb.push_back({g[b][i], i});
}
sort(pa.begin(), pa.end());
sort(pb.begin(), pb.end());
int la = pa.size(), lb = pb.size();
for(int i = 0; i < la; i ++ )
if(pa[i].q == pa[0].q && pa[i].id == b)
f1 = true;
for(int i = 0; i < lb; i ++ )
if(pb[i].q == pb[0].q && pb[i].id == a)
f2 = true;
if(f1 && f2)
{
if(x < 0) cout << '-' << abs(x) - 1;
else cout << x - 1;
cout << ' ' ;
if(y < 0) cout << '-' << abs(y) - 1 << endl;
else cout << y - 1 << endl;
}
else
{
for(int i = 0; i < la; i ++ )
if(pa[i].q == pa[0].q)
{
int id = pa[i].id;
if(x < 0) cout << '-' << abs(x) - 1;
else cout << x - 1;
cout << ' ' ;
if(sex[id] == 'S') cout << '-' << id - 1 << endl;
else cout << id - 1 << endl;
}
for(int i = 0; i < lb; i ++ )
if(pb[i].q == pb[0].q)
{
int id = pb[i].id;
if(y < 0) cout << '-' << abs(y) - 1;
else cout << y - 1;
cout << ' ' ;
if(sex[id] == 'S') cout << '-' << id - 1 << endl;
else cout << id - 1 << endl;
}
}
return 0;
}

L2-029 特立独行的幸福

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
#include <bits/stdc++.h>
using namespace std;

const int N = 1e4 + 10;

int l, r;
int isapper[N];
int ans[N];

bool isprime(int x)
{
if(x < 2) return false;
for(int i = 2; i <= x / i; i ++ )
if(x % i == 0)
return false;
return true;
}

int main()
{
cin >> l >> r;
for(int i = l; i <= r; i ++ )
{
int x = i;
vector<int> a;
while(x != 1)
{
int t = x, y = 0;
while(t)
{
int tt = t % 10;
y += tt * tt;
t /= 10;
}
if(find(a.begin(), a.end(), y) != a.end())
break;
a.push_back(y);
isapper[y] = 1;
x = y;
}
if(x == 1) ans[i] = a.size();
}

int cnt = 0;
for(int i = l; i <= r; i ++ )
if(!isapper[i] && ans[i] && ++ cnt)
{
cout << i << ' ' << ans[i] * (1 + isprime(i)) << endl;
}
if(!cnt) puts("SAD");
return 0;
}

L2-030 冰岛人

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
#include<iostream>
#include<map>
#include<string>
using namespace std;
struct Peoson {
char sex;
string father;
};
map<string, Peoson> people;
int judge(string a, string b) {
int i = 1, j;
for (string A = a; !A.empty(); A = people[A].father, i++) {
j = 1;
for (string B = b; !B.empty(); B = people[B].father, j++) {
if (i >= 5 && j >= 5)
break;
if (A == B && (i < 5 || j < 5))
return 0;
}
}
return 1;
}
int main() {
int n, m;
string str, a, b;
cin.sync_with_stdio(false);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a >> b;
if (b.back() == 'n')
people[a] = { 'm',b.substr(0,b.size() - 4) };
else if (b.back() == 'r')
people[a] = { 'f',b.substr(0, b.size() - 7) };
else
people[a].sex = b.back();
}
cin >> m;
for (int i = 0; i < m; i++) {
cin >> a >> str >> b >> str;
if (people.find(a) == people.end() || people.find(b) == people.end())
printf("NA\n");
else if (people[a].sex == people[b].sex)
printf("Whatever\n");
else
printf("%s\n", judge(a, b) ? "Yes" : "No");
}
return 0;
}

L2-031 深入虎穴

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
#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
vector<int> v[N];
int n, st[N], t, res;
void dfs(int u, int deep){
if(deep > t) res = u, t = deep;
for(int i = 0; i < v[u].size(); i++)
dfs(v[u][i], deep + 1);
}
int main()
{
cin >> n;
for(int i = 1; i <= n; i++)
{
int k; cin >> k;
while(k--)
{
int a; cin >> a;
v[i].push_back(a);
st[a] = 1;
}
}
int root = 1;
while(st[root]) root++;
dfs(root, 1);
cout << res << endl;
return 0;
}

L2-032 彩虹瓶

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<bits/stdc++.h>
using namespace std;
const int N = 1010;
int n, m, k, f[N];
int main()
{
cin >> n >> m >> k;
for(int i = 0; i < k; i++)
{
for(int j = 0; j < n; j++)
cin >> f[j];
int cnt = 1,idx = 0;
stack<int> st;
while(cnt <= n)
{
if(f[idx]==cnt)
{
cnt ++;
idx ++;
}
else
{
if(st.size()!=0&&st.top()==cnt)
{
st.pop();
cnt ++;
}
else if(st.size()<m)
st.push(f[idx++]);
else break;
}

}
if(cnt > n) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}

L2-033 简单计算器

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
#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
int main()
{
int n; cin >> n;
stack<int> st0;
stack<char> st1;
for(int i = 0; i < n; i++)
{
int a; cin >> a;
st0.push(a);
}
for(int i = 0; i < n-1; i++)
{
char a; cin >> a;
st1.push(a);
}
int a = st0.top();
st0.pop();
while(st0.size())
{
int b = st0.top();
st0.pop();
char c = st1.top();
st1.pop();
if(c == '+') a = b + a;
if(c == '-') a = b - a;
if(c == '*') a = b * a;
if(c == '/')
{
if(a == 0){
printf("ERROR: %d/0", b);
return 0;
}
a = b / a;
}
}
cout << a << endl;
return 0;
}

L2-034 口罩发放

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
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
struct node{
string a, b, c;
int id;
bool operator < (const node &x)const {
if(c != x.c) return c < x.c;
return id < x.id;
}
};
bool check(string num)
{
if(num.size()!=18)
return false;
for(int i = 0; i < 18; i++)
if(num[i]<'0'||num[i]>'9')
return false;
return true;
}
vector<node> v;
int main()
{
int d, p;
cin >> d >> p;
map<string, int> time, mp;
for(int i = 1; i <= d; i++)
{
for(auto &aa : time)
if(aa.second!=0) aa.second--;
int n, m;
cin >> n >> m;
vector<node> now;
for(int j = 1; j <= n; j++)
{
string a, b, c, d;
cin >> a >> b >> c >> d;
if(check(b)&& !mp[b] && c == "1")
{
v.push_back({a, b, d, j});
mp[b] = 1;
}
if(check(b))
now.push_back({a, b, d, j});
}
sort(now.begin(), now.end());
int cnt = 0;
for(int i = 0; i < now.size()&&cnt < m; i++)
{
if(time[now[i].b]) continue;
cout << now[i].a << " " << now[i].b << endl;
cnt ++;
time[now[i].b] += p + 1;
}
}
for(int i = 0; i < v.size(); i++)
cout << v[i].a << " " << v[i].b << endl;
}

L2-035 完全二叉树的层序遍历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
int n, tree[31];
void dfs(int i)
{
if(i > n) return;
dfs(2 * i);
dfs(2 * i + 1);
scanf("%d", &tree[i]);
}
int main()
{
scanf("%d", &n);
dfs(1);
printf("%d",tree[1]);
for(int i = 2; i <= n; i++)
printf(" %d",tree[i]);
return 0;
}

L2-036 网红点打卡攻略

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
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1010;
int d[N][N];
int n, m;
signed main()
{
cin >> n >> m;
while(m--)
{
int a, b, c;
cin >> a >> b >> c;
d[a][b] = d[b][a] = c;
}
int k; cin >> k;
int ma = 0x3f3f3f3f, res = 0, cnt = 0;
for(int i = 1; i <= k; i++)
{
int a, la = 0, sum = 0, flag = 1;
cin >> a;
map<int, int> mp;
for(int j = 0; j < a; j++)
{
int b; cin >> b;
if(mp[b]) flag = 0;
mp[b] = 1;
if(!d[la][b]) flag = 0;
sum += d[la][b];
la = b;
}
sum += d[la][0];
if(!d[la][0]) flag = 0;
if(flag && a==n )
{
cnt ++;
if(ma > sum) ma = sum, res = i;
}

}
cout << cnt << endl;
cout << res << " " << ma << endl;
return 0;
}

L2-037 包装机

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
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
char s[N][N];
int l[N], n, m ,S;
stack<char> st; // 筐
vector<char> res; // 流水线
void op0()
{
if(st.size())
{
res.push_back(st.top());
st.pop();
}
}
void op(int x)
{
if(l[x] >= m) return;
st.push(s[x][l[x]]);
l[x]++;
}
int main()
{
cin >> n >> m >> S;
for(int i = 0; i < n; i++)
cin >> s[i];
int a;
while(cin >> a && a != -1)
{
if(a == 0) op0();
else
{
if(st.size() == S && l[a-1]<m)
op0();
op(a - 1);
}
}
for(int i = 0; i < res.size(); i++)
cout << res[i];
return 0;
}

L2-038 病毒溯源

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
62
63
64
#include <bits/stdc++.h>
using namespace std;
const int N = 10100;
vector<int> v[N];
int cnt[N], path[N], d[N];
int ma = 0;
void bfs(int x)
{
queue<int> q;
q.push(x);
path[x] = -1;
while(q.size())
{
int t = q.front();
q.pop();
for(int i = 0; i < v[t].size(); i++)
{
int j = v[t][i];
q.push(j);
d[j] = d[t] + 1;
path[j] = t;
ma = max(ma, d[j]);
}
}
}
int main()
{
int n; cin >> n;
for(int i = 0; i < n; i++)
{
int k; cin >> k;
for(int j = 0; j < k; j++)
{
int a; cin >> a;
v[i].push_back(a);
cnt[a] ++;
}
}
int root = 0;
while(cnt[root]) root++;
bfs(root);
set<vector<int> > st;
for(int i = 0; i < n; i++)
{
vector<int> a;
int j = i;
if(d[i] == ma)
{
while(j != -1)
{
a.push_back(j);
j = path[j];
}
reverse(a.begin(), a.end());
st.insert(a);
}
}
vector<int> t = *st.begin();
cout << t.size() << endl;
cout << t[0];
for(int i = 1; i < t.size(); i++)
cout << " " << t[i];
return 0;
}

L2-039 清点代码库

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
#include <bits/stdc++.h>
#define fr first
#define se second
using namespace std;
const int N = 100100;
typedef pair<int, vector<int> > PIV;
PIV f[N];
int n, m;
bool cmp(PIV a, PIV b)
{
if(a.fr!=b.fr) return a.fr > b.fr;
return a.se < b.se;
}
int main()
{
cin >> n >> m;
map<vector<int>, int> mp;
for(int i = 0; i < n; i++)
{
vector<int> a(m);
for(int i = 0; i < m; i++)
cin >> a[i];
mp[a] ++;
f[i].fr = mp[a];
f[i].se = a;
}
sort(f, f + n, cmp);
cout << mp.size() << endl;
for(int i = 0; i < n; i++)
{
if(mp[f[i].se])
{
cout << mp[f[i].se];
for(int j = 0; j < m; j++)
cout << " " << f[i].se[j];
cout << endl;
mp[f[i].se] = 0;
}
}
}

L2-040 哲哲打游戏

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 <bits/stdc++.h>
using namespace std;
const int N = 100100;
vector<int> v[N]; // 每个剧情点可选值
int f[N]; // 存档
int main()
{
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
int k; cin >> k;
for(int j = 1; j <= k; j++)
{
int a; cin >> a;
v[i].push_back(a);
}
}
// 当前点t
int t = 1;
for(int i = 0; i < m; i++)
{
int op, j;
cin >> op >> j;
if(op == 1)
{
f[j] = t;
cout << t << endl;
}
else if(op == 0) t = v[t][j-1];
else t = f[j];
}
cout << t << endl;
}

L2-041 插松枝

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
#include<bits/stdc++.h>
#define fr first
#define se second
using namespace std;
void print(vector<int> &v)
{
cout << v[0];
for(int i = 1; i < v.size(); i++)
cout << " " << v[i];
cout << endl;
v.clear();
}
int main()
{
int n, m, k;
cin >> n >> m >> k;
queue<int> q;
for(int i = 0; i < n; i++)
{
int a; cin >> a;
q.push(a);
}
stack<int> st;
vector<int> v;
while(q.size() || st.size())
{
while(!st.empty()&&(v.empty()||st.top()<=v.back()))
{
v.push_back(st.top());
if(v.size() == k) print(v);
st.pop();
}
if(q.empty() && v.size()) print(v);

if(!q.empty()&&(v.empty()||v.back()>=q.front()))
{
v.push_back(q.front());
if(v.size() == k) print(v);
q.pop();
}
else
{
while(!q.empty() && q.front()>v.back() && st.size()<m)
{
st.push(q.front());
q.pop();
}
if(v.size() && !q.empty() && q.front() > v.back())
print(v);
}
}
if(v.size()) print(v);
}

L2-042 老板的作息表

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
#include <bits/stdc++.h>
using namespace std;
const int N = 86400 + 10;
int p[N];
int main()
{
int n; cin >> n;
for(int i = 0; i < n; i++)
{
int a, b, c, d, e, f;
scanf("%d:%d:%d - %d:%d:%d", &a, &b, &c, &d, &e, &f);
int t1 = a * 3600 + b * 60 + c;
int t2 = d * 3600 + e * 60 + f;
p[t1] ++; p[t2] --;
}
int aa = -1, bb = -1;
for(int i = 0; i < 86400; i++)
if(i) p[i] += p[i-1];
for(int i = 0; i < 86400 - 1;)
{
int j = i;
while(j < 86400 - 1 && !p[j]) j++;
if(i != j) printf("%02d:%02d:%02d - %02d:%02d:%02d\n",
i / 3600, i % 3600 / 60, i % 60,
j / 3600, j % 3600 / 60, j % 60);
i = j + 1;
}
return 0;
}

L2-043 龙龙送外卖

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 <bits/stdc++.h>
using namespace std;
const int N = 100010;
int st[N], f[N], d[N];
int t, cnt;
void dfs(int u)
{
if(f[u] == -1 || st[u]) return;
cnt ++; st[u] = 1;
dfs(f[u]);
d[u] = d[f[u]] + 1;
}
int main()
{
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
int a; cin >> a;
f[i] = a;
}
int res = 0, mx = 0;
for(int i = 0; i < m; i++)
{
cnt = 0;
cin >> t;
dfs(t);
mx = max(mx, d[t]);
res += cnt * 2;
cout << res - mx << endl;
}
return 0;
}

L2-044 大众情人

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
#include <bits/stdc++.h>
using namespace std;

typedef pair<int,int> PII;
const int N = 510;

int n;
int dist[N][N];
char sex[N];
vector<PII> man, woman;

int main()
{
cin >> n;
memset(dist, 0x3f, sizeof dist);
for(int i = 1; i <= n; i ++ )
{
cin >> sex[i];
int k; cin >> k;
for(int j = 0; j < k; j ++ )
{
int x, d;
scanf("%d:%d", &x, &d);
dist[x][i] = d;
}
}

for(int k = 1; k <= n; k ++ )
for(int i = 1; i <= n; i ++ )
for(int j = 1; j <= n; j ++ )
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);

for(int i = 1; i <= n; i ++ )
{
int d = 0;
for(int j = 1; j <= n; j ++ )
if(j != i && sex[i] != sex[j])
d = max(d, dist[i][j]);
if(sex[i] == 'M') man.push_back({d, i});
else woman.push_back({d, i});
}
sort(man.begin(), man.end());
sort(woman.begin(), woman.end());
int t = 0;
cout << woman[t].second;
for(int i = 1; i < woman.size(); i ++ )
if(woman[i].first == woman[t].first)
cout << ' ' << woman[i].second;
cout << endl;
t = 0;
cout << man[t].second;
for(int i = 1; i < man.size(); i ++ )
if(man[i].first == man[t].first)
cout << ' ' << man[i].second;
cout << endl;
}

L2-045 堆宝塔

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 <bits/stdc++.h>
using namespace std;
int main()
{
int n; cin >> n;
vector<int> a, b;
int cnt = 0, res = 0;
for(int i = 0; i < n; i++)
{
int x; cin >> x;
if(a.empty() || a.back() > x)
a.push_back(x);
else if(b.empty() || b.back() < x)
b.push_back(x);
else
{
cnt ++;
res = max(res, (int)a.size());
a.clear();
for(int i = b.size() - 1; i >= 0; i--)
{
if(b[i] > x)
{
a.push_back(b[i]);
b.pop_back();
}
else break;
}
a.push_back(x);
}
}
if(!a.empty()) cnt ++, res = max(res, (int)a.size());
if(!b.empty()) cnt ++, res = max(res, (int)b.size());;
cout << cnt << " " << res << endl;
}

L2-046 天梯赛的赛场安排

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 <bits/stdc++.h>
using namespace std;
const int N = 10000;
string s[N];
int a[N];
int main()
{
int n, c;
cin >> n >> c;
int cnt = 0;
vector<int> v;
for(int i = 0; i < n; i++)
{
cin >> s[i] >> a[i];
cnt += a[i] / c;
cout << s[i] << " " << ceil(a[i] *1.0/ c) << endl;
a[i] %= c;
}
sort(a, a + n, greater<>());
for(int i = 0; i < n; i++)
{
for(int j = 0; j < v.size(); j++)
{
if(a[i] + v[j] <= c) {
v[j] += a[i];
a[i] = 0;
break;
}
}
if(a[i] != 0)
v.push_back(a[i]);
}
cout << cnt + v.size() << endl;
}

L2-047 锦标赛

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
#include <iostream>
#include <algorithm>
#include <cmath>
#define N 1000010
using namespace std;
int t[N],maxx[N],k,w,n,st,flag;
void build(int rt){
if(rt>n/2){
maxx[rt]=t[rt];
return;
}
build(rt*2);
build(rt*2+1);
maxx[rt]=max(maxx[rt*2],maxx[rt*2+1]);
maxx[rt]=max(maxx[rt],t[rt]);
}
void solve(int rt){
if(flag) return;
if(rt>n/2) return;
if(t[rt]<maxx[rt]){
flag=1;return;
}
if(t[rt*2]<maxx[rt*2]&&t[rt*2]<maxx[rt*2+1]){
flag=1;return;
}
if(t[rt*2]<maxx[rt*2]){
t[rt*2+1]=t[rt*2];
t[rt*2]=t[rt];
}
else{
t[rt*2+1]=t[rt];
}
solve(rt*2);
solve(rt*2+1);
}
int main(){
scanf("%d",&k);
n=pow(2,k+1)-1;
st=n+1;
for(int i=0;i<=k;i++){
st/=2;
for(int j=0;j*2<st;j++){
scanf("%d",&w);
t[st+j*2]=w;
}
}
build(1);
solve(1);
if(flag) cout<<"No Solution";
else{
for(int i=n/2+1;i<n;i++) cout<<t[i]<<" ";
cout<<t[n];
}
cout<<endl;
return 0;
}

L2-048 寻宝图

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 = 100010;
string v[N];
int cnt0, cnt1, flag, n, m;
int tx[4] = {0, 0, 1, -1}, ty[4] = {1, -1, 0, 0};
void dfs(int x, int y)
{
if(v[x][y]!='1') flag = 1;
v[x][y] = '0';
for(int i = 0; i < 4; i++)
{
int nx = tx[i] + x, ny = ty[i] + y;
if(nx < 0 || ny < 0 || nx >= n || ny >= m || v[nx][ny]=='0')
continue;
dfs(nx, ny);
}
}
int main()
{
cin >> n >> m;
for(int i = 0; i < n; i++)
cin >> v[i];
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
{
flag = 0;
if(v[i][j] != '0'){
cnt0++;
dfs(i, j);
}
if(flag) cnt1 ++;
}
cout << cnt0 << " " << cnt1 << endl;
return 0;
}

团体程序设计天梯赛-练习集所有L2题目全集
https://leaf-domain.gitee.io/2024/03/26/pta-GPLT-L2/
作者
叶域
发布于
2024年3月26日
许可协议