【NOIP模擬】 20151007模擬

題目

戳這裏看題目,密碼:qbf4
喪心病狂的你居然想看我們的成績
今天的題比昨天少了一題,難度變得異常平均。。

T1 GALAXY

兩遍bfs
第一遍從任一節點出發,找一個離它最遠的點,記作s,這個s節點顯然是一個葉節點
第二遍從s點出發,找到的離它最遠的點t,s到t的距離就是樹上最長鏈

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a ,b) for(int i = (a); i <= (b); i++)
#define red(i, a, b) for(int i = (a); i >= (b); i--)
#define ll long long

const int inf = 1000000000, N = 100000;
struct edge{
    int from, to, nxt;
}e[N];
int head[N], d[N], vis[N];
int n, tail = 0;

void addedge(int x, int y) {
    e[++tail].from = x;
    e[tail].to = y;
    e[tail].nxt = head[x];
    head[x] = tail;
}

int bfs(int s) {
    rep(i, 0, n - 1) d[i] = inf;
    memset(vis, 0, sizeof(vis));
    queue<int> q;
    q.push(s); d[s] = 0;
    vis[s] = 1;
    while(!q.empty()) {
        int u = q.front(); q.pop();
        for(int i = head[u]; i != -1; i = e[i].nxt) {
            int v = e[i].to;
            if (!vis[v]) {
                d[v] = d[u] + 1;
                vis[v] = 1;
                q.push(v);
            }
        }
    }
    int len = 0, ans = -1;
    rep(i, 0, n - 1) if (d[i] > len) len = d[i], ans = i;
    return ans;
}

int main() {
    freopen("galaxy.in", "r", stdin);
    freopen("galaxy.out", "w", stdout);

    scanf("%d", &n);
    rep(i, 0, n - 1) head[i] = -1;
    rep(i, 0, n - 1) {
        int x; 
        scanf("%d", &x);
        while(x != -1) {
            addedge(i, x);
            addedge(x, i);
            scanf("%d", &x);
        }
    }
    int s = bfs(0);
    int t = bfs(s);
    printf("%d\n", d[t]);
    return 0;
}

T2 BLACKandWHITE

第一次在題目名稱裏出現小寫,因爲它實在太長了
這個題目首先可以做一個預處理:B[i,j]表示從(1,1)到(i,j)之間的1有多少個。這是一種很常見的預處理方法。B[i,j]=color[i,j]+B[i-1,j]+B[i,j-1]-B[i-1,j-1]。
然後可以進行普通的分治。可以用O(1)的時間求出,當前的那一塊是不是全白或者全黑(全白,1的總數爲0,全黑,1的總數爲邊長的平方),如果是這樣的,則可以直接輸出,否則按照題目分成四段,分別考慮。
輸出最後多一個空格。SR你就不能去掉行末空格?你就不能按照基本法來?以後自己也當個心吧。

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = (a); i <= (b); i++)
#define red(i, a, b) for(int i = (a); i >= (b); i--)
#define ll long long

const int N = 2000;
int a[N][N], s[N][N];
int n;

void dfs(int X1, int Y1, int X2, int Y2) {
    int num = s[X2][Y2] - s[X2][Y1 - 1] - s[X1 - 1][Y2] + s[X1 - 1][Y1 - 1];
    int square = (X2 - X1 + 1) * (Y2 - Y1 + 1);
    if (num == square) { putchar('b'); putchar(' '); }
    else if (num == 0) { putchar('w'); putchar(' '); }
    else {
        putchar('g'); putchar(' ');
        dfs(X1, (Y1 + Y2) / 2 + 1, (X1 + X2) / 2,  Y2);
        dfs(X1, Y1, (X1 + X2) / 2, (Y1 + Y2) / 2);
        dfs((X1 + X2) / 2 + 1, Y1, X2, (Y1 + Y2) / 2);
        dfs((X1 + X2) / 2 + 1, (Y1 + Y2) / 2 + 1, X2, Y2);
    }
}

int main() {
    freopen("blackandwhite.in", "r", stdin);
    freopen("blackandwhite.out", "w", stdout);

    scanf("%d", &n);
    rep(i, 1, n) {
        char ch = getchar();
        while(!isdigit(ch)) ch = getchar();
        rep(j, 1, n) {
            a[i][j] = ch - '0';
            ch = getchar();
        }
    }
    memset(s, 0, sizeof(s));
    rep(i, 1, n)
        rep(j, 1, n)
            s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];
    dfs(1, 1, n, n);
    putchar('\n');
    return 0;
}

T3 CODE

本來一位要自己想怎麼編碼的,結果它把哈夫曼編碼說出來了2333
當然,我們只能夠用O(N)的時間來建立這棵哈夫曼樹,就像NOIP2004那種方法。我們可以利用兩個有序表的合併來進行操作,從而構造樹。在構造了樹以後,我們可以用廣度遍歷的方法將所有字符編碼長度求出來,得到解。
然而懶癌晚期的我使用堆完成建樹的。。學校裏A了。。在家裏居然T了一個點

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = (a); i <= (b); i++)
#define red(i, a, b) for(int i = (a); i >= (b); i--)
#define ll long long
#define ld __float128

const int N = 300000;
struct node{
    int num, tag, len, lc, rc;
}p[N << 1];
priority_queue<node , vector<node> , greater<node> > q;
queue<int> lst;
int n, L, m;

bool operator < (node a, node b) {
    return a.num == b.num ? a.tag < b.tag : a.num < b.num;
}
bool operator > (node a, node b) {
    return a.num == b.num ? a.tag > b.tag : a.num > b.num;
}

inline int read() {
    char c = getchar();
    while(!isdigit(c)) c = getchar();
    int x = 0;
    while(isdigit(c)) {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x;
}

int main() {
    freopen("code.in", "r", stdin);
    freopen("code.out", "w", stdout);

    scanf("%d%d", &n, &L);
    rep(i, 1, n) {
        p[i].num = read();
        p[i].tag = i;
        p[i].lc = -1;
        p[i].rc = -1;
        q.push(p[i]);
    }
    m = n;
    for(int rest = n; rest > 1;) {
        node min1 = q.top(); q.pop();
        node min2 = q.top(); q.pop();
        p[++m].lc = min1.tag;
        p[m].rc = min2.tag;
        p[m].num = min1.num + min2.num;
        p[m].tag = m;
        q.push(p[m]);
        rest--;
    }
    node root = q.top();
    lst.push(root.tag);
    p[root.tag].len = 0;
    ld ans = 0;
    while(!lst.empty()) {
        int pos = lst.front(); lst.pop();
        node now = p[pos]; int temp = 0;
        if (~now.lc) {
            lst.push(now.lc);
            p[now.lc].len = p[pos].len + 1;
            temp = 1;
        }
        if (~now.rc) {
            lst.push(now.rc);
            p[now.rc].len = p[pos].len + 1;
            temp = 1;
        }
        if (temp) continue;
        ans += (ld)(now.len) / (ld)(L) * (ld)(now.num) * 1.00;
    }
    double _ = (double)ans;
    printf("%.6lf\n", _);
    return 0;
}

T4 TICKET

原來是CEOI的一道題,好像題解寫的太複雜了?
稍微用一點貪心,對每個位置i都考慮儘量滿足契合是更優的,然後在前面長度爲L的區間中找一個終點轉移過來。dp結束後特判總訂購數不足的情況

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = (a); i <= (b); i++)
#define red(i, a, b) for(int i = (a); i >= (b); i--)
#define ll long long
#define PII pair<int, int>

const int N = 150000;
const int M = 33333;
const int inf = 1000000000;
int n, m, L;
int a[N];
PII dp[N];

inline int read() {
    char c = getchar();
    while(!isdigit(c)) c = getchar();
    int x = 0;
    while(isdigit(c)) {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x;
}

int main() {
    freopen("ticket.in", "r", stdin);
    freopen("ticket.out", "w", stdout);

    scanf("%d%d", &m, &L);
    scanf("%d", &n);
    rep(i, 1, n) a[i] = read();
    sort(a + 1, a + n + 1);
    a[n + 1] = inf;

    PII ans(0, 0);
    int pos = 1;
    rep(i, 1, m - L + 1) {
        while(a[pos] < i) pos++;
        PII cur(0, 0);
        for(int j = i - L; j >= 0 && j >= i - L - L; j--) cur = max(cur, dp[j]);
        dp[i] = cur;
        if (a[pos] == i) dp[i].first++;
        else dp[i].second++;
        ans = max(ans, dp[i]);
    }

    int ans1 = ans.first, ans2 = min(n - ans1, ans.second);
    printf("%d\n", ans1 * 2 + ans2);
    return 0;
}

尾聲

國慶的訓練就這麼結束了
拜在SR的NC失誤所賜,顯得略有瑕疵
下次自己也要注意這樣的問題
中歐人果然嚴謹,一道貪心題被寫成了很長的dp
考試的時候建了一個“×(Z)×(R)×(L)AK啦”的文件夾,flag效果顯著,ZRL最後一題成功WA了一個點,圓滿完成了395分的任務
那就這樣吧,初賽要加油咯哇咔咔

End.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章