AtCoder Beginner Contest 235 A~F

目录

  • A - Rotate
  • B - Climbing Takahashi
  • C - The Kth Time Query
  • D - Multiply and Rotate
  • E - MST + 1
  • F - Variety of Digits

A - Rotate

按照题目指示做

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    char a, b, c; cin >> a >> b >> c;
    int x1 = a-'0', x2 = b-'0', x3 = c-'0';
    cout << x1*100+x2*10+x3 + x2*100+x3*10+x1 + x3*100+x1*10+x2 << endl;
}

B - Climbing Takahashi

找到一直递增的尽头

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n; cin >> n;
    vector<int> a(n+1);
    for (int i = 1; i <= n; i++) cin >> a[i];
    int now = a[1];
    for (int i = 2; i <= n; i++) {
        if (a[i] > a[i-1]) {
            now = a[i];
        } else {
            break;
        }
    }
    cout << now << endl;
}

C - The Kth Time Query

寻找x在数列中第k次出现,用map存一下然后O(1)找

map<int, vector<int>> mp;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, q; cin >> n >> q;
    for (int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        mp[x].push_back(i);
    }
    while(q--) {
        int x, k; cin >> x >> k;
        if (k > mp[x].size()) cout << -1 << endl;
        else cout << mp[x][k-1] << endl;
    }
}

D - Multiply and Rotate

用bfs暴力找一下就可以了

struct node {
    int now, step;
};
int vis[N];
int base[7];
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    base[0] = 1;
    for (int i = 1; i <= 6; i++) base[i] = base[i-1] * 10;
    int a, n; cin >> a >> n;
    queue<node> que;
    que.push({1, 0});
    while (que.size()) {
        node now = que.front();
        que.pop();
        if (now.now == n) {
            cout << now.step << endl;
            return 0;
        }
        if (vis[now.now]) continue;
        vis[now.now] = 1;
        if (1ll*now.now*a<=3e6 && !vis[now.now*a]) que.push({now.now*a, now.step+1});
        if (now.now % 10 == 0) continue;
        int tmp = now.now;
        int dig = 0;
        while (tmp) {
            tmp /= 10;
            dig++;
        }
        int ttmp = now.now%10*base[dig-1] + now.now/10;
        if (ttmp<=3e6 && !vis[ttmp]) que.push({ttmp, now.step+1});
    }
    cout << -1 << endl;
}

E - MST + 1

最小生成树问题,考虑离线处理,将询问全部丢到边集里面,在构建最小生成树时check一下是否在树内

#include 
using namespace std;
typedef long long ll;
const int N = 3e6 + 10;
const int mod = 998244353;
struct Edge {
    int state, u, v, w;
    bool operator < (const Edge &rhs) const {
        return w < rhs.w;
    }
}e[N];
int fa[N], ans[N];
int find(int x) {return x == fa[x] ? x : fa[x] = find(fa[x]);}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m, q; cin >> n >> m >> q;
    for (int i = 1; i <= m; i++) {
        cin >> e[i].u >> e[i].v >> e[i].w;
        e[i].state = 0;
    }
    for (int i = m+1; i <= m+q; i++) {
        cin >> e[i].u >> e[i].v >> e[i].w;
        e[i].state = i - m;
    }
    for (int i = 1; i <= m+q; i++) fa[i] = i;
    sort(e+1, e+m+q+1);
    int count = 0;
    for (int i = 1; i <= m + q; i++) {
        int u = find(e[i].u);
        int v = find(e[i].v);
        if (u != v) {
            if (!e[i].state) {
                count++;
                fa[u] = v;
                if (count == n - 1) break;
            } else {
                ans[e[i].state] = 1;
            }
        }
    }
    for (int i = 1; i <= q; i++) {
        if (ans[i]) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
}

F - Variety of Digits

简单的数位DP,记 d p [ p o s ] [ s t a t e ] dp[pos][state] dp[pos][state]为当前在pos位且取到数字的状压表示为state的个数,dp2[pos][state]为和

在数位DP中分类讨论一下,如果是前导0,那么不会记录state中

#include 
using namespace std;
typedef long long ll;
const int N = 3e6 + 10;
const int mod = 998244353;
typedef pair<ll, ll> pii;
ll dp[10005][2048];
ll dp2[10005][2048];
int a[10005], st;
ll base[10005];
pii dfs(int pos, int state, bool limit, bool lead) {
    if (pos == -1) {
        if ((state & st) == st) return {1,0};
        else return {0,0};
    }
    if (!limit && !lead && dp[pos][state] != -1) return {dp[pos][state], dp2[pos][state]};
    int End = limit ? a[pos] : 9;
    ll ans1 = 0, ans2 = 0;
    for (int i = 0; i <= End; i++) {
        if (lead) {
            if (i == 0) {
                pii tmp = dfs(pos-1, state, limit && i == End, 1);
                ans1 = (ans1 + tmp.first) % mod;
                ans2 = (ans2 + i * base[pos] % mod * tmp.first % mod + tmp.second) % mod;
            }
            else {
                pii tmp = dfs(pos-1, state | (1<<i), limit && i == End, 0);
                ans1 = (ans1 + tmp.first) % mod;
                ans2 = (ans2 + i * base[pos] % mod * tmp.first % mod + tmp.second) % mod;
            }
        } else {
            pii tmp = dfs(pos-1, state | (1 << i), limit && i == End, 0);
            ans1 = (ans1 + tmp.first) % mod;
            ans2 = (ans2 + i * base[pos] % mod * tmp.first % mod + tmp.second) % mod;
        }
    }
    if (!limit && !lead) dp[pos][state] = ans1, dp2[pos][state] = ans2;
    return {ans1, ans2};
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    base[0] = 1;
    for (int i = 1; i <= 10000; i++) base[i] = base[i-1] * 10 % mod;
    string s; cin >> s;
    int len = s.size();
    int m; cin >> m;
    for (int i = 1; i <= m; i++) {
        int x; cin >> x;
        st |= 1 << x;
    }

    for (int i = 0; i < s.size(); i++) a[i] = s[len-1-i]-'0';
    memset(dp, -1, sizeof dp);
    memset(dp2, -1, sizeof dp2);
    cout << dfs(len-1, 0, true, true).second << endl;
}

你可能感兴趣的:(动态规划,c++,acm竞赛)