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()的,回超时。
这题要用链表,具体见代码
#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;
}
|