【解題報告】Codeforces Round #367 (Div. 2)

題目鏈接


A. Beru-taxi(Codeforces 706A)

思路

我們可以枚舉出租車,然後用兩點間距離公式計算用時的用時更新最優解。

代碼

#include <bits/stdc++.h>
using namespace std;

int n;
double a, b, x, y, v, ans;

double sqr(double x) {
    return x * x;
}

double dis(int x1, int y1, int x2, int y2) {
    return sqrt(sqr(x1 - x2) + sqr(y1 - y2));
}

int main() {
    cin >> a >> b >> n;
    ans = 1e9;
    for(int i = 1; i <= n; i++) {
        cin >> x >> y >> v;
        ans = min(ans, dis(a, b, x, y) / v);
    }
    printf("%.10f\n", ans);
    return 0;
}

B. Interesting drink(Codeforces 706B)

思路

本題需要我們動態地在給定的序列 x 中求出比 m 小的數的個數 num 。由於我們可以用二分查找(前提是對 x 排序)找到 m 在序列 x 中的位置 idx (準確地說是 x 中的只包含 m 的左閉右開區間的上界)。從而得到我們要的 num (值與 idx 相等)。

代碼

#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e5 + 5;
int n, q, m, idx, a[maxn];

int main() {
    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    sort(a, a + n);
    scanf("%d", &q);
    while(q--) {
        scanf("%d", &m);
        idx = upper_bound(a, a + n, m) - a;
        printf("%d\n", idx);
    }
    return 0;
}

C. Hard problem(Codeforces 706C)

思路

如果我們能知道到第 i1 個字符串爲止,在不反轉和反轉第 i1 個字符串情況下的最優解 d[i1][0]d[i1][1] ,我們就能知道 d[i][0]d[i][1] 的情況。因此我們可以用個動態規劃來解決這個問題。首先預處理出第i個原串 s[i][0] 和其反轉後的字符串 s[i][1] ,然後進行初始化 d[1][0]=0,d[1][1]=c[1] ,接着按照方程

d[i][j]=min(d[i][j],d[i1][k]+j×c[i]),if(s[i][j]s[i1][k])

進行遞推,最後 min(d[n][0],d[n][1]) 就是答案。

代碼

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn = 1e5 + 5;
const ll INF = (1LL << 60);
int n;
ll ans, cost, c[maxn], d[maxn][2];
string s[maxn][2];

int main() {
    ios::sync_with_stdio(false);
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> c[i];
    }
    for(int i = 1; i <= n; i++) {
        cin >> s[i][0];
        s[i][1] = s[i][0];
        // 預處理出反轉後的字符串
        reverse(s[i][1].begin(), s[i][1].end());
    }
    d[1][1] = c[1];
    for(int i = 2; i <= n; i++) {
        // 初始化d[i][0]和d[i][1]
        d[i][0] = d[i][1] = INF;
        // 枚舉第i個字符串的擺放方式(反轉還是不反轉)
        for(int j = 0; j <= 1; j++) {
            // 枚舉第i-1個字符串的擺放方式
            for(int k = 0; k <= 1; k++) {
                if(s[i][j] < s[i-1][k]) {
                    continue;
                }
                cost = j * c[i];
                d[i][j] = min(d[i][j], d[i-1][k] + cost);
            }
        }
    }
    ans = min(d[n][0], d[n][1]);
    cout << (ans == INF ? -1 : ans) << endl;
    return 0;
}

D. Vasiliy’s Multiset(Codeforces 706D)

思路

首先,可以用 map 來維護多重集。其次,可以用 Trie 來維護一個集合,並且能夠對給定的 x 快速找出集合中的y使得 xy 最大。所以對每個操作更新這兩個數據結構就行了。

代碼

#include <bits/stdc++.h>
using namespace std;

const int maxNode = 1e7;
char s[10];
int q, x, ans;
map <int, int> mp;

// Trie的模板
struct trie {
    int sz, val[maxNode], ch[maxNode][2];
    void init() {
        memset(ch[0], 0, sizeof(ch[0]));
        sz = 1;
    }
    void update(int num, int v) {
        int u = 0;
        for(int i = 30; i >= 0; i--) {
            int c = (num >> i) & 1;
            if(ch[u][c] == 0) {
                memset(ch[sz], 0, sizeof(ch[sz]));
                val[sz] = 0;
                ch[u][c] = sz++;
            }
            u = ch[u][c];
            val[u] += v;
        }
    }
    int match(int num) {
        int u = 0, ans = 0;
        for(int i = 30; i >= 0; i--) {
            int c = (num >> i) & 1;
            if(ch[u][c^1] && val[ch[u][c^1]]) {
                ans |= (1 << i);
                u = ch[u][c^1];
            }
            else u = ch[u][c];
        }
        return ans;
    }
}o;

int main() {
    o.init();
    o.update(0, 1);
    scanf("%d", &q);
    while(q--) {
        scanf("%s%d", s, &x);
        if(s[0] == '+') {
            if(mp[x]++ == 0) {
                o.update(x, 1);
            }
        }
        if(s[0] == '-') {
            if(--mp[x] == 0) {
                o.update(x, -1);
            }
        }
        if(s[0] == '?') {
            printf("%d\n", o.match(x));
        }
    }
    return 0;
}

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