其他分享
首页 > 其他分享> > Atcoder Beginner Contest 257

Atcoder Beginner Contest 257

作者:互联网

A - A to Z String 2

代码:

void solve(int Case) {
    int n;
    string s = " ";
    cin >> n;
    int k;
    cin >> k;
    for (int i = 'A'; i <= 'Z'; i++) {
        for (int j = 1; j <= n; j++) {
            s.push_back(i);
        }
    }
    cout << s[k] << nline;
 
 
 
 
 
}

B - 1D Pawn

代码:

const int N=10010;
int a[N];
int p[N];
void solve(int Case) {
 
    int n;
    cin >> n;
    int m, q;
    cin >> m >> q;
    for (int i = 1; i <= m; i++) {
        int x;
        cin >> x;
        p[x] = 1;
    }
    for (; q--;) {
        int l;
        cin >> l;
        for (int i = 1; i <= n; i++) {
            l -= p[i];
            if (!l) {
                if (i != n and !p[i + 1]) {
                    p[i] = 0, p[i + 1] = 1;
                    break;
                }
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        if (p[i]) cout << i << ' ';
    }
 
 
 
}

C - Robot Takahashi ]

思路:

前缀和

代码:

const int N = 200010;
int sum[N];
string str;
struct T {
    int w, s;
    bool operator<(const T &t) const {
        return w < t.w;
    }
} a[N];
vector<int> v;
int find(int x) {
    return lower_bound(all(v), x) - v.begin();
}
void solve(int Case) {
    int n;
    cin >> n;
    cin >> str;
    str = " " + str;
    for (int i = 1; i <= n; i++) {
        auto &[w, s] = a[i];
        cin >> w;
        s = (str[i] == '1');
    }
    sort(a + 1, a + 1 + n);
    for (int i = 1; i <= n; i++) {
        v.push_back(a[i].w);
        sum[i] = sum[i - 1] + a[i].s;
    }
    int res = 0;
    for (int i = 1; i <= n; i++) {
        int w = a[i].w;
        int l = find(w);
        res = max(res, l - sum[l] + (sum[n] - sum[l]));
    }
    res = max({res, sum[n],n-sum[n]});
    cout << res << nline;
 
 
 
 
 
}

D - Jumping Takahashi 2

思路:

暴力二分bfs,预处理边权跑floyd也可以

代码:

const int N = 220;
using PII = pair<int, int> ;
PII a[N];
int s[N];
bool vis[N];
int n;
bool check(int mid) {
    for (int i = 1; i <= n; i++) {
        queue<int> q;
        q.push(i);
        for (int i = 1; i <= n; i++) vis[i] = 0;
        vis[i] = 1;
        while (q.size()) {
            auto t = q.front();
        
            q.pop();
            for (int i = 1; i <= n; i++) {
                if (vis[i]) continue;
                auto [x, y] = a[t];
                auto [x1, y1] = a[i];
                int d = abs(x - x1) + abs(y - y1);
                if (mid >= (d + s[t] - 1) / s[t]) {
                    vis[i] = true;
                    q.push(i);
                }
            }
        }
        int cnt = 0;
        for (int i = 1; i <= n; i++) cnt += (!vis[i]);
        
        if (!cnt) return true;
    }
    return false;
}
void solve(int Case) {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        auto &[x, y] = a[i];
        cin >> x >> y >> s[i];
    }
    int l = 1, r = 2e18;
    while (l < r) {
        int mid = l + r >> 1;
        if (check(mid)) r = mid;
        else l = mid + 1;
    }
 
    cout << r << nline;
 
 
 
}

E - Addition and Multiplication 2

思路:

在选择最多个数的情况下选择最大,跑一遍完全背包,然后倒推出方案

代码:

const int N = 1000010;
int f[10][N];
int c[N];
void solve(int Case) {
    int n;
    cin >> n;
    for (int i = 1; i <= 9; i++) cin >> c[i];
    for (int i = 1; i <= 9; i++) {
        for (int j = 0; j <= n; j++) {
            if (j >= c[i])
                f[i][j] = max(f[i - 1][j], f[i][j - c[i]] + 1);
            else f[i][j]=f[i-1][j];
        }
    }
    
    string s;
    int i = 9;
    int j = n;
   
    while (i >= 1) {
        while (j >= c[i] and f[i][j - c[i]] + 1 >= f[i][j]) {
           
            s.push_back(i + '0');
            j -= c[i];
        }
        i--;
    }
    cout << s << nline;
 
 
 
 
 
 
}
 

F - Teleporter Setting

思路:

题目暗示很明显,建立虚拟原点,第i次询问结果是min(d[1][n],d[1][0]+d[i][n],d[1][i]+d[0][n]);

代码:

const int N = 600010;
int d[3][N];
using PII = pair<int, int>;
vector<PII> h[N];
bool vis[N];
int n;
void dijkstra(int s, int d[]) {
    for (int i = 0; i <= n; i++) d[i] = 0x3f3f3f3f;
    for (int i = 0; i <= n; i++) vis[i] = 0;
    priority_queue<PII, vector<PII>, greater<PII>> q;
    d[s] = 0;
    q.push({d[s], s});
    while (q.size()) {
        auto [w, t] = q.top();
        q.pop();
        if (vis[t]) continue;
        vis[t] = 1;
        for (auto [ver, nw] : h[t]) {
            if (d[ver] > nw + w) {
                d[ver] = nw + w;
                q.push({d[ver], ver});
            }
        }
    }
}
void solve(int Case) {
    int  m;
    cin >> n >> m;
    for (; m--;) {
        int u, v;
        cin >> u >> v;
        h[u].push_back({v, 1});
        h[v].push_back({u, 1});
    }
    dijkstra(1, d[1]);
    dijkstra(0, d[0]);
    dijkstra(n, d[2]);
    for (int i = 1; i <= n; i++) {
        int res = min({d[1][n], d[1][i] + d[0][n], d[1][0] + d[2][i]});
        if (res >= 0x3f3f3f3f) res = -1;
        cout << res << ' ';
    }





}

标签:Atcoder,const,Beginner,int,void,cin,push,ver,257
来源: https://www.cnblogs.com/koto-k/p/16413021.html