2012-11-04 17:40:49Morris

[ITSA] 18th 總解答

一個人比賽,還用得著去寫旋轉就太瘋了,果斷打表。

#include <stdio.h>
char num[10][6][6] = {
    {"01110", "10001", "10001", "10001", "01110"},//0
    {"00100", "00100", "00100", "00100", "00100"},//1
    {"01110", "10001", "00010", "00100", "11111"},//2
    {"01110", "10001", "00110", "10001", "01110"},//3
    {"10010", "10010", "01111", "00010", "00010"},//4
    {"11111", "10000", "11110", "00001", "11110"},//5
    {"00010", "00100", "01110", "10001", "01110"},//6
    {"01110", "10001", "00010", "00010", "00010"},//7
    {"01110", "10001", "01110", "10001", "01110"},//8
    {"01110", "10001", "01110", "00010", "00010"},//9
};
int main() {
    int t;
    scanf("%d", &t);
    while(t--) {
        int i, j;
        int a, b, c, x;
        int g[5][5];
        for(i = 0; i < 5; i++) {
            for(j = 0; j < 5; j++) {
                scanf("%d", &x);
                g[i][j] = x+'0';
            }
        }
        int tmp, ans = 0;
        for(i = 0; i < 30; i++) {
            for(j = 0; j < 10; j++) {
                for(a = 0; a < 10; a++) {
                    int cnt = 0;
                    for(b = 0; b < 5; b++)
                        for(c = 0; c < 5; c++)
                            if(g[b][c] == num[a][b][c])
                                cnt++;
                    if(cnt == 25)
                        ans = a;
                }
                tmp = g[1][1];
                g[1][1] = g[1][2];
                g[1][2] = g[1][3];
                g[1][3] = g[2][3];
                g[2][3] = g[3][3];
                g[3][3] = g[3][2];
                g[3][2] = g[3][1];
                g[3][1] = g[2][1];
                g[2][1] = tmp;
            }
            tmp = g[0][0];
            g[0][0] = g[0][1];
            g[0][0] = g[0][1];
            g[0][1] = g[0][2];
            g[0][2] = g[0][3];
            g[0][3] = g[0][4];
            g[0][4] = g[1][4];
            g[1][4] = g[2][4];
            g[2][4] = g[3][4];
            g[3][4] = g[4][4];
            g[4][4] = g[4][3];
            g[4][3] = g[4][2];
            g[4][2] = g[4][1];
            g[4][1] = g[4][0];
            g[4][0] = g[3][0];
            g[3][0] = g[2][0];
            g[2][0] = g[1][0];
            g[1][0] = tmp;
        }
        printf("%d\n", ans);
    }
    return 0;
}

樹形 dp,事實上這題在比賽的時候,測資是錯誤的,用錯誤的方法也會過,根據通過的隊伍獲得資料,測資筆數只有一筆,而且 n 還小於 10,由於主辦單位給了多餘的輸入,以至於多輸出了答案,無語現場摔鍵盤。
很多人都說測資錯了,主辦單位還不信,只好認命點了,這次沒破台。

#include <stdio.h>
long long dp1[90005], dp2[90005];
int p[90005], son[90005];
int main() {
    int n, i;
    while(scanf("%d", &n) == 1) {
        for(i = 1; i < n; i++) {
            scanf("%d", &p[i]);
            dp1[i] = dp2[i] = son[i] = 0;
        }
        dp1[0] = dp2[0] = son[0] = 0;
        for(i = n-1; i > 0; i--) {
            son[i]++;
            dp1[p[i]] += dp1[i] + son[i];
            son[p[i]] += son[i];
        }
        for(i = 1; i < n; i++) {
            dp2[i] = (dp2[p[i]]+n-son[i]) + (dp1[p[i]]-dp1[i]-son[i]);
        }
        long long ans = dp1[0]+dp2[0];
        int lab = 0;
        for(i = 1; i < n; i++) {
            if(dp1[i]+dp2[i] < ans)
                ans = dp1[i]+dp2[i], lab = i;
        }
        printf("%d\n", lab);
    }
    return 0;
}

這題大家肯定是水了他。

#include <stdio.h>

int main() {
    char s[1000];
    int i;
    while(gets(s)) {
        int cnt[26] = {};
        for(i = 0; s[i]; i++) {
            if(s[i] >= 'A' && s[i] <= 'Z')
                cnt[s[i]-'A']++;
            else
            if(s[i] >= 'a' && s[i] <= 'z')
                cnt[s[i]-'a']++;
        }
        for(i = 0; i < 26; i++) {
            if(cnt[i])
                printf("%c %d\n", i+'a', cnt[i]);
        }
    }
    return 0;
}

其實這題答案不會超過 n,證明可以簡單地得到,而且好像用 greedy 就可以知道答案為多少,但是我居然用 dfs 跑,而且還能通過,我就沒多說什麼了。

#include <stdio.h>
int A[100], i, n;
int ans;
void dfs(int dep) {
    if(dep >= ans)
        return;
    int flag = 0, i, tmp, j;
    for(i = 0; i < n; i++) {
        if(A[i] != i) {
            j = A[i];
            tmp = A[i], A[i] = A[j], A[j] = tmp;
            dfs(dep+1);
            flag = 1;
            tmp = A[i], A[i] = A[j], A[j] = tmp;
        }
    }
    if(flag == 0) {
        if(ans > dep)
            ans = dep;
    }
}
int main() {
    while(scanf("%d", &n) == 1) {
        ans = n*n;
        for(i = 0; i < n; i++) {
            scanf("%d", &A[i]);
        }
        dfs(0);
        printf("%d\n", ans);
    }
    return 0;
}

這一題是你猜我猜,連工作人員都在猜,在看不懂題目的意思下,猜一猜賭賭看囉。

#include <stdio.h>

int main() {
    int n;
    while(scanf("%d", &n) == 1) {
        int deg[90005] = {}, x, y, i;
        for(i = 1; i < n; i++) {
            scanf("%d %d", &x, &y);
            deg[x]++, deg[y]++;
        }
        int ans = 0;
        for(i = 0; i < n; i++) {
            if(deg[i] >= 2)
                ans++;
        }
        printf("%d\n", ans);
    }
    return 0;
}