Jakarta Asia 2018 做题记录

时间:2020-03-24
本文章向大家介绍Jakarta Asia 2018 做题记录,主要包括Jakarta Asia 2018 做题记录使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

D.

细节题。

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 const int MAXN = 500 + 10;
 6 
 7 char a[MAXN][MAXN];
 8 
 9 int n, m;
10 
11 int main() {
12     cin >> n >> m;
13     for (int i = 0; i < n; ++i)
14         cin >> a[i];
15     int ans = 0;
16     if (n == 1) {
17         for (int i = 1; i < m - 1; ++i)
18             if (a[0][i] == '.')
19                 ++ans;
20     } else if (m == 1) {
21         for (int i = 1; i < n - 1; ++i)
22             if (a[i][0] == '.')
23                 ++ans;
24     } else if (n == 2) {
25         for (int i = 1; i < m - 1; ++i)
26             if (a[0][i] == '.' && a[1][i] == '.')
27                 ++ans;
28     } else if (m == 2) {
29         for (int i = 1; i < n - 1; ++i)
30             if (a[i][0] == '.' && a[i][1] == '.')
31                 ++ans;
32     } else {
33         for (int i = 1; i < n - 1; ++i)
34             for (int j = 1; j < m - 1; ++j) {
35                 if (a[i][j] == '.')
36                     ++ans;
37             }
38         int add = 1;
39         for (int i = 1; i < n - 1; ++i) {
40             if (a[i][0] == '#' || a[i][m - 1] == '#')
41                 add = 0;
42         }
43         for (int i = 1; i < m - 1; ++i) {
44             if (a[0][i] == '#' || a[m - 1][i] == '#')
45                 add = 0;
46         }
47         ans += add;
48     }
49     cout << ans << endl;
50 }
View Code

J.

签到题。贪心

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 string b2s(int64_t b) {
 6     string s = "";
 7     while (b) {
 8         if (b & 1)
 9             s.push_back('1');
10         else
11             s.push_back('0');
12         b >>= 1;
13     }
14     reverse(s.begin(), s.end());
15     return s;
16 }
17 
18 int64_t s2b(const string& s) {
19     int64_t b = 0;
20     for (auto x : s) {
21         b = b * 2 + x - '0';
22     }
23     return b;
24 }
25 
26 int main() {
27     ios::sync_with_stdio(false);
28 
29     int64_t n;
30     string bits;
31     cin >> n;
32     cin >> bits;
33     int cnt = 0;
34     while (s2b(bits) > n) {
35         for (size_t i = 1; i < bits.size(); ++i) {
36             if (bits[i] == '1') {
37                 bits.erase(bits.begin() + i);
38                 goto END_OF_WHILE;
39             }
40         }
41         bits.pop_back();
42       END_OF_WHILE:
43         cnt++;
44     }
45     cout << cnt << endl;
46 }
View Code

K.

对图 dfs,过程贪心选择。可以证明是最优解。

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 const int MAXN = 100000 + 10;
 6 
 7 int dfn[MAXN], alloc;
 8 struct Tuple {
 9     int a, b, c;
10     Tuple(int a=0, int b=0, int c=0): a(a), b(b), c(c) {}
11 };
12 vector<Tuple> ans;
13 
14 struct Graph {
15     struct Edge {
16         int v, n;
17     } e[MAXN << 1];
18     int G[MAXN], edgeCnt;
19     void _add_edge(int u, int v) {
20         e[++edgeCnt] = (Edge){v, G[u]};
21         G[u] = edgeCnt;
22     }
23     void add_edge(int u, int v) {
24         _add_edge(u, v);
25         _add_edge(v, u);
26     }
27     bool dfs(int u, int par=-1) {
28         dfn[u] = ++alloc;
29         int temp = -1;
30         for (int i = G[u]; i; i = e[i].n) if (e[i].v != par) {
31             if (dfn[e[i].v]) {
32                 if (dfn[e[i].v] > dfn[u])
33                     continue;
34                 if (~temp) {
35                     ans.push_back(Tuple(temp, u, e[i].v));
36                     temp = -1;
37                 } else {
38                     temp = e[i].v;
39                 }
40             } else {
41                 if (dfs(e[i].v, u)) {
42                     if (~temp) {
43                         ans.push_back(Tuple(temp, u, e[i].v));
44                         temp = -1;
45                     } else {
46                         temp = e[i].v;
47                     }
48                 }
49             }
50         }
51         if (temp == -1) {
52             return true;
53         }
54         if (par != -1) {
55             ans.push_back(Tuple(temp, u, par));
56         }
57         return false;
58     }
59 } graph;
60 
61 int main() {
62     ios::sync_with_stdio(false);
63     int n, m;
64     cin >> n >> m;
65     for (int i = 1; i <= m; ++i) {
66         int u, v;
67         cin >> u >> v;
68         graph.add_edge(u, v);
69     }
70     for (int i = 1; i <= n; ++i) {
71         if (!dfn[i]) {
72             graph.dfs(i);
73         }
74     }
75     cout << ans.size() << endl;
76     for (auto x : ans) {
77         cout << x.a << ' ' << x.b << ' ' << x.c << endl;
78     }
79 }
View Code

原文地址:https://www.cnblogs.com/uuzhateteee/p/12559826.html