Solutions to the 12th Fuzhou University Program Design Competition, Fuzhou University Design Competition

Source: Internet
Author: User

Solutions to the 12th Fuzhou University Program Design Competition, Fuzhou University Design Competition

A: This topic uses A monotonous stack to pre-process the farthest position from each position to the bottom. Therefore, each row is considered separately. Each row is equivalent to the number of child matrices of some segments. It is maintained using A monotonous stack, calculate the part of the segment that is greater than the current segment to be imported into the stack (the height is 0) and add the answer.

B: This question can be enumerated, because a 10 ^ 15 square after the opening of the third side is 10 ^ 5 enumeration is normal, and then enumeration of a number I, it is equal to the value calculated in the range from max (a, I * I) to min (I * I, B, how many x meet the requirements of I * x in the interval, and the answer can be calculated by removing the lower and lower sides of the two sides.

C: Check the collection. Make sure that the city is the root of each merge. Finally, find the root for people and services. If the root is the city, the information is complete.

D: Water question. Use a string to store floating-point numbers for calculation. Just divide the gcd.

E: This question .. Obviously, there is no wonderful data, so we only need to determine the edge that cannot be connected to each other in a set, and then use the bipartite graph for dyeing. If there is no conflict, each time we accumulate one side of the black and white vertices in a subset, the answer is the final answer.

F: tree-like DP, dp [u] [0] and dp [u] [1] respectively indicate no return, and the minimum cost of returning to the root of the tree is as follows, the cost of returning to the root of the tree is fixed, that is, the cost of his sub-tree + edge weight. The cost of not returning is to select a sub-tree and not returning it. In this case, as long as the maximum value of this answer is recorded during the process of finding a subnode, the minimum value of dp [u] [1] can be obtained by subtracting this value from dp [u] [0 ].

G: The big white book has this question. First, we will create a BFS for the fire to calculate the spread time of each point, and then use this time to create a BFS for the person. Note that this question has a pitfall point, that is, if a person goes out directly at the exit, because the magma then spreads

H: greedy. For the y operation, it is best to put the following 1 in the front of the 0 position, so we need to find a critical value, followed by X 1, the rest are moved to the front step by step using the x operation, which is directly enumerated, and then some prefix and things of the 7th percentile are maintained. Pay attention to the details when writing. For details, see the code.

Code:

A:

#include <cstdio>#include <cstring>#include <stack>using namespace std;typedef long long ll;const int N = 2005;int n, m;char str[N];int g[N][N];stack<pair<int, int> > S;ll get(int s, int k) {    return (ll)(2 * s - (k - 1)) * k / 2;}int main() {    while (~scanf("%d%d", &n, &m)) {        for (int i = 0; i < n; i++) {            scanf("%s", str);            for (int j = 0; j < m; j++) {                if (str[j] == 'b') g[i][j] = 0;                else g[i][j] = 1;            }        }        for (int i = 0; i < m; i++) {            int pre = n;            for (int j = n - 1; j >= 0; j--) {                if (g[j][i] == 0) pre = j;                g[j][i] = pre;            }        }        for (int i = 0; i < n; i++) g[i][m] = i;        ll ans = 0;        for (int i = 0; i < n; i++) {            for (int j = 0; j <= m; j++) {                int h = g[i][j] - i, v = j, pre = j;                while (!S.empty() && S.top().first > h) {                    v = S.top().second;                    ans += get(j - v, pre - v) * (S.top().first - h);                    pre = v;                    S.pop();                }                if (h) S.push(make_pair(h, v));            }        }        printf("%I64d\n", ans);    }    return 0;}

B:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long ll;ll a, b;ll ans;int main() {    while (~scanf("%I64d%I64d", &a, &b)) {        ans = 0;        for (ll i = 1; i * i * i <= b; i++) {            ll sb = max(a, i * i * i);            ans += b / (i * i) - (sb - 1) / (i * i);        }        printf("%I64d\n", ans);    }    return 0;}

C:

#include <cstdio>#include <cstring>#include <vector>using namespace std;const int MAXN = 400005;const int N = 100000;const int M = 100000;int n, m, q;int parent[MAXN];int find(int x) {    if (x == parent[x])return x;    return parent[x] = find(parent[x]);}void uni(int a, int b) {    int pa = find(a);    int pb = find(b);    if (pb < pa) swap(pb, pa);    if (pa != pb) parent[pa] = pb;}int main() {    while (~scanf("%d%d%d", &n, &m, &q)) {        int a, b, c;        for (int i = 0; i < MAXN; i++)            parent[i] = i;        for (int i = 1; i <= n; i++) {            scanf("%d%d", &a, &b);            a--; b--;            if (b != -1) {                uni(a, b + N + M);            }        }        for (int i = 1; i <= m; i++) {            scanf("%d%d%d", &a, &b, &c);            a--; b--; c--;            uni(a + N, b);            if (c != -1) {                uni(a + N, c + N + M);                uni(b, c + N + M);            }        }        int tp,id;        while (q--) {            scanf("%d%d", &tp, &id);            id--;            if (tp == 1) id += N;            int x = find(id);            if (x >= N + M) printf("%d\n", x - (N + M) + 1);            else printf("0\n");        }    }    return 0;}

D:

#include <cstdio>#include <cstring>int t;char n[105];typedef long long ll;ll gcd(ll a, ll b) {    if (!b) return a;    return gcd(b, a % b);}int main() {    scanf("%d", &t);    while (t--) {        scanf("%s", &n);        int len = strlen(n);        ll sb = 0, fuck = 1;        for (int i = 0; i < len; i++) {            if (n[i] == '.') {                for (int j = i + 1; j < len; j++) {                    sb = sb * 10 + n[j] - '0';                    fuck *= 10;                }                break;            }            sb = sb * 10 + n[i] - '0';        }        ll d = gcd(sb, fuck);        printf("%I64d/%I64d\n", sb / d, fuck / d);    }    return 0;}

E:

#include <cstdio>#include <cstring>#include <set>#include <vector>#include <algorithm>using namespace std;typedef long long ll;const int N = 50005;int n, col[N];vector<int> g[N];struct Point {    int x, y;    void read() {        scanf("%d%d", &x, &y);    }} p[N];bool cmpx(Point a, Point b) {    return a.x < b.x;}int w[2], ans;bool dfs(int u) {    for (int i = 0; i < g[u].size(); i++) {        int v = g[u][i];        if (col[u] == col[v]) return false;        if (col[v] == -1) {            col[v] = !col[u];            w[col[v]]++;            dfs(v);        }    }    return true;}ll dis(Point a, Point b) {    ll dx = a.x - b.x;    ll dy = a.y - b.y;    return dx * dx + dy * dy;}int main() {    while (~scanf("%d", &n)) {        for (int i = 0; i < n; i++) {            p[i].read();            g[i].clear();            col[i] = -1;        }        sort(p, p + n, cmpx);        for (int i = 0; i < n; i++) {            for (int j = i + 1; p[j].x - p[i].x <= 5; j++) {                if (dis(p[j], p[i]) <= 25LL) {                    g[i].push_back(j);                    g[j].push_back(i);                }            }        }        int flag = 0;        ans = 0;        for (int i = 0; i < n; i++) {            if (col[i] == -1) {                col[i] = 0;                w[0] = 1; w[1] = 0;                if (!dfs(i)) {                    flag = 1;                    break;                }                ans += max(w[0], w[1]);            }        }        if (flag) printf("-1\n");        else printf("%d\n", ans);    }    return 0;}

F:

#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <vector>using namespace std;const int N = 100005;int n;const int INF = 0x3f3f3f3f;int cost[N], vis[N], dp[N][2];vector<int> g[N];void dfs(int u) {    dp[u][0] = dp[u][1] = 0;    if (g[u].size() == 0)        return;    int Min = -INF, Minv;    for (int i = 0; i < g[u].size(); i++) {        int v = g[u][i];        dfs(v);        if (Min < dp[v][1] - dp[v][0] + cost[v]) {            Min = dp[v][1] - dp[v][0] + cost[v];            Minv = v;        }        dp[u][1] += dp[v][1] + cost[v];    }    dp[u][0] = dp[u][1] - dp[Minv][1] - cost[Minv] + dp[Minv][0];}int main() {    while (~scanf("%d", &n)) {        int u, v, w;        for (int i = 1; i <= n; i++) {            vis[i] = 0;            g[i].clear();        }        for (int i = 0; i < n - 1; i++) {            scanf("%d%d%d", &u, &v, &w);            g[u].push_back(v);            cost[v] = w;        }        dfs(1);        printf("%d\n", dp[1][0]);    }    return 0;}

G:

#include <cstdio>#include <cstring>#include <queue>using namespace std;const int N = 1005;const int INF = 0x3f3f3f3f;char g[N][N];int t, n, m;struct Point {    int x, y;    Point() {}    Point(int x, int y){        this->x = x;        this->y = y;    }} s, e;const int d[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};int bid[N][N], vis[N][N];int main() {    scanf("%d", &t);    while (t--) {        scanf("%d%d", &n, &m);        queue<Point> Q;        for (int i = 0; i < n; i++) {            scanf("%s", g[i]);            for (int j = 0; j < m; j++) {                bid[i][j] = INF;                vis[i][j] = INF;                if (g[i][j] == 'S') {                    s.x = i;                    s.y = j;                }                if (g[i][j] == '!') {                    Q.push(Point(i, j));                    bid[i][j] = 0;                }                if (g[i][j] == 'E') {                    e.x = i;                    e.y = j;                }            }        }        while (!Q.empty()) {            Point u = Q.front(); Q.pop();            for (int i = 0; i < 4; i++) {                Point v;                v.x = u.x + d[i][0];                v.y = u.y + d[i][1];                if (v.x < 0 || v.x >= n || v.y < 0 || v.y >= m || g[v.x][v.y] == '#') continue;                if (bid[v.x][v.y] > bid[u.x][u.y] + 1) {                    bid[v.x][v.y] = bid[u.x][u.y] + 1;                    Q.push(v);                }            }        }        Q.push(s);        vis[s.x][s.y] = 0;        int flag = 0;        while (!Q.empty()) {            Point u = Q.front(); Q.pop();            for (int i = 0; i < 4; i++) {                Point v;                v.x = u.x + d[i][0];                v.y = u.y + d[i][1];                if (v.x < 0 || v.x >= n || v.y < 0 || v.y >= m || g[v.x][v.y] == '#') continue;                if (v.x == e.x && v.y == e.y && bid[v.x][v.y] >= vis[u.x][u.y] + 1) {                    flag = 1;                    break;                }                if (bid[v.x][v.y] <= vis[u.x][u.y] + 1) continue;                if (vis[v.x][v.y] > vis[u.x][u.y] + 1) {                    vis[v.x][v.y] = vis[u.x][u.y] + 1;                    Q.push(v);                }            }        }        printf("%s\n", flag ? "Yes" : "No");    }    return 0;}

H:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 100005;typedef long long ll;int t, n;char str[N];ll x, y;int one[N], zero[N], on, zn;ll sum[N], bit[N];inline int lowbit(int x){    return x&(-x);}void add(int x, int v) {    while (x <= n) {        bit[x] += v;        x += lowbit(x);    }}ll get(int x) {    ll ans = 0;    while (x) {        ans += bit[x];        x -= lowbit(x);    }    return ans;}int main() {    scanf("%d", &t);    while (t--) {        scanf("%I64d%I64d", &x, &y);        scanf("%s", str + 1);        n = strlen(str + 1);        memset(bit, 0, sizeof(bit));        on = zn = 0;        ll tot = 0;        ll sb = 0;        for (int i = 1; i <= n; i++) {            sum[i] = sum[i - 1];            if (str[i] == '1') {                tot += sb;                one[on++] = i;                sum[i] += 1;            } else {                sb++;                add(i, 1);            }        }        for (int i = n; i >= 1; i--) if (str[i] == '0') zero[zn++] = i;        ll ans = 0;        while (on && zn && one[on - 1] > zero[zn - 1]) {            if (x * tot < y + x * (tot - get(one[on - 1]) - (sum[one[on - 1] - 1] - sum[zero[zn - 1]]))) {                ans += x * tot;                break;            } else {                tot -= get(one[on - 1]) + (sum[one[on - 1] - 1] - sum[zero[zn - 1]]);                add(zero[zn - 1], -1);                zn--; on--;                ans += y;            }        }        printf("%I64d\n", ans);    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.