Atcoder Beginner Contest (ABC) 237 A - E

ABC237题目

目录

A题:

B题:

C题:

D题:

E题:


A题:

水题,直接放代码

#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long n;
    cin >> n;
    if (n >= -2147483648 && n < 2147483648)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
}

B题:

就是把行和列换一下位置:

#include <bits/stdc++.h>
using namespace std;
#define N 100005
vector<int> G[N];
int main()
{
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        G[i].push_back(0);
        for (int j = 1; j <= m; j++)
        {
            int a;
            cin >> a;
            G[i].push_back(a);
        }
    }
    for (int i = 1; i <= m; i++)
    {
        for (int j = 1; j <= n; j++)
            cout << G[j][i] << " ";
        cout << endl;
    }
}

其中,我G[i]只是为了把坐标改成1

C题:

就是你一个字符串,把它加入一个字符,模拟即可

#include <bits/stdc++.h>
#define int long long
using namespace std;
int32_t main()
{

    string str;
    cin >> str;
    int x = 0;
    while (str.size() > 0 && str.back() == 'a')
    {
        str.pop_back();
        x++;
    }
    int y = 0;
    reverse(str.begin(), str.end());
    while (str.size() > 0 && str.back() == 'a')
    {

        str.pop_back();
        y++;
    }
    string res = str;
    reverse(res.begin(), res.end());
    if (res == str && x >= y)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
}

D题:

给你一个字符串,包含LR,如果s[i]是L,就把它放在i - 1的左边,

如果s[i]是R,就把它放在i - 1的右边,

这题如果用别的东西存储,复杂度就是O(n^2)的,会超时。

这题要用链表,具体见代码

#include <iostream>
using namespace std;
#define N (int)5e5 + 5
struct Node
{
    int l = -1, r = -1;
} a[N];
int main()
{
    int n;
    cin >> n;
    string s;
    cin >> s;
    int t = 0;
    for (int i = 0; i < s.size(); i++)
    {
        if (s[i] == 'L')
        {
            int pre = a[t].l;
            a[t].l = t + 1;
            a[t + 1].r = t;
            a[t + 1].l = pre;
            a[pre].r = t + 1;
            t++;
        }
        else if (s[i] == 'R')
        {
            int bac = a[t].r;
            a[t].r = t + 1;
            a[t + 1].l = t;
            a[t + 1].r = bac;
            a[bac].l = t + 1;
            t++;
        }
    }
    int beg = 0;
    for (int i = 0; i <= n; i++)
        if (a[i].l == -1)
        {
            beg = i;
            break;
        }
    while (a[beg].r != -1)
    {
        cout << beg << " ";
        beg = a[beg].r;
    }
    cout << beg << endl;
}

E题:

SPFA裸题:

#include <iostream>
#include <queue>
#include <vector>
#include <stack>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long
#define pii pair<int, int>
const int N = 500005;
const int mod = 1e9 + 7;
const int maxn = 200005;
struct edge
{
    int v, w;
};
vector<edge> e[maxn];
int h[N];
int dis[maxn], cnt[maxn], vis[maxn];
queue<int> q;
bool spfa(int n, int s)
{
    memset(dis, 63, sizeof(dis));
    dis[s] = 0, vis[s] = 1;
    q.push(s);
    while (!q.empty())
    {
        int u = q.front();
        q.pop(), vis[u] = 0;
        for (auto ed : e[u])
        {
            int v = ed.v, w = ed.w;
            if (dis[v] > dis[u] + w)
            {
                dis[v] = dis[u] + w;
                cnt[v] = cnt[u] + 1;
                if (cnt[v] >= n)
                    return false;
                if (!vis[v])
                    q.push(v), vis[v] = 1;
            }
        }
    }
    return true;
}
void solve()
{
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        cin >> h[i];
    }
    for (int i = 1; i <= m; i++)
    {
        int u, v;
        cin >> u >> v;
        int cost = h[u] - h[v];
        if (cost > 0)
        {
            e[u].push_back({v, -cost});
            e[v].push_back({u, 2 * cost});
        }
        else if (cost <= 0)
        {
            e[u].push_back({v, -2 * cost});
            e[v].push_back({u, 1 * cost});
        }
    }
    spfa(n, 1);
    int maxn = 0;
    for (int i = 1; i <= n; i++)
    {
        maxn = min(maxn, dis[i]);
    }
    cout << -maxn;
}
int main()
{
    solve();
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值