AtCoder Beginner Contest 384
A - aaaadaa
题意
给定长为\(n\)的字符串\(s\),和两个字符\(c_1\)、\(c_2\),把\(s\)中不是\(c_1\)的字符替换成$ c_2$
思路
模拟
代码
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
const int mxn = 1e6 + 5;
void solve()
{
int n;
char a, b;
string s;
cin >> n >> a >> b >> s;
for (int i = 0; i < s.size(); i++)
{
if (s[i] != a)
{
s[i] = b;
}
}
cout << s << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T = 1;
// cin >> T;
while (T--)
{
solve();
}
return 0;
}
B - ARC Division
题意
\(n\)场比赛,已知初始分数\(r\),每场比赛有\(d_i\)与\(a_i\)两个属性,分别表示分区与分数变动。当前分数处在对应分区分数才改变,否则不变。分区\(1\):\([1600,2799]\),分区\(2\):\([1200,2399]\)。求最终分数
思路
模拟
代码
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
const int mxn = 1e6 + 5;
void solve()
{
int n, r;
cin >> n >> r;
for (int i = 0; i < n; i++)
{
int d, a;
cin >> d >> a;
if (d == 1 && r >= 1600 && r <= 2799)
{
r += a;
}
else if (d == 2 && r >= 1200 && r <= 2399)
{
r += a;
}
}
cout << r << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T = 1;
// cin >> T;
while (T--)
{
solve();
}
return 0;
}
C - Perfect Standings
题意
总共\(5\)个题\(ABCDE\),给定每个题对应的分数\(abcde\),输出所有\(31\)种出题情况分高的在前,分一样按出题的字典序排。
思路
求出每种情况的分数,带上字符串排序。
代码
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, string> pii;
const int mxn = 1e6 + 5;
int a, b, c, d, e;
string s = "ABCDE";
vector<pii> ans;
bool cmp(const pii& a, const pii& b)
{
if (a.first == b.first)
{
return a.second < b.second;
}
return a.first > b.first;
}
void solve()
{
vector<int> SC(5);
for (int i = 0; i < 5; i++)
{
cin >> SC[i];
}
for (int i = 1; i < (1LL << 5); i++) // 每个题都有出跟不出两种情况,没有一个题都没出的情况,所以是<2^5
{
int sc = 0;
string name = "";
for (int j = 0; j < 5; j++)
{
if (i & (1LL << j))
{
name.push_back(s[j]);
sc += SC[j];
}
}
ans.push_back({ sc, name });
}
sort(ans.begin(), ans.end(), cmp);
for (int i = 0; i < ans.size(); i++)
{
cout << ans[i].second << endl;
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T = 1;
// cin >> T;
while (T--)
{
solve();
}
return 0;
}
D - Repeated Sequence
题意
给定周期为\(n\)的无穷序列的前\(n\)项,问该无穷序列是否存在连续子序列,使得这个子序列之和等于\(s\)。
思路
因为是连续子序列,很容易想到前缀和,但是这个序列有周期,所以可能出现\(s = a_4+a_5(a_1)+a_6(a_2)\)(假设周期为\(4\)),所以至少需要\(2\)个周期长度的前缀和。并且只需要看\(s%sum\)(\(sum\)是一个周期之和),即剩下的这部分是否与一个区间和相等即可。由于处理所有区间和需要\(O(n^2)\)的复杂度(\(T\)了),所以将\(pre[i] - pre[j] = s\)转化为\(pre[j] = pre[i] - s\),用\(set\)来维护,从而简化到\(O(n\ log\ n)\)
代码
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve()
{
int n, s;
cin >> n >> s;
vector<int> a(n + 1), pre(2 * (n + 1));
set<int> st;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
sum += a[i];
pre[i] = pre[i - 1] + a[i];
}
for (int i = n + 1; i <= 2 * n; i++)
{
pre[i] = pre[i - 1] + a[i - n];
}
st.insert(0);
s %= pre[n];
if (!s)
{
cout << "Yes" << endl;
return;
}
for (int i = 1; i <= 2 * n; i++)
{
if (st.count(pre[i] - s))
{
cout << "Yes" << endl;
return;
}
st.insert(pre[i]);
}
cout << "No" << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
solve();
return 0;
}
E - Takahashi is Slime 2
题意
给定\(h\)×\(w\)的网格,\((i,j)\)有一团强度为\(S_{i,j}\)的粘液,最初有一团粘液在\((p,q)\),他会吸收与之相邻且强度严格小于它的粘液。求最后的粘液强度。
思路
\(BFS\)但需要用小根堆,每次取最小的吸收,当没有可以吸收(队头强度大于等于当前强度)或全部吸收完时结束。
代码
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
const int mxn = 5e2 + 5;
struct node
{
int x, y, w;
bool operator < (const node& a)const
{
return w > a.w;
}
};
int h, w, X, a, b, now;
int mp[mxn][mxn];
bool vis[mxn][mxn];
int dx[] = { 1,-1,0,0 };
int dy[] = { 0,0,1,-1 };
void solve()
{
cin >> h >> w >> X >> a >> b;
for (int i = 1; i <= h; i++)
{
for (int j = 1; j <= w; j++)
{
cin >> mp[i][j];
}
}
priority_queue<node> q;
q.push({ a,b,mp[a][b] });
vis[a][b] = true;
bool f = false;
while (q.size())
{
int x = q.top().x;
int y = q.top().y;
if ((int128)mp[x][y] * X >= now&& f) // 注意这里直接乘会爆,要不就int128,要不就直接除也能过
{
break;
}
f = true;
now += q.top().w;
q.pop();
for (int i = 0; i < 4; i++)
{
int tx = x + dx[i];
int ty = y + dy[i];
if (tx > 0 && ty > 0 && tx <= h && ty <= w && !vis[tx][ty])
{
q.push({ tx,ty,mp[tx][ty] });
vis[tx][ty] = true;
}
}
}
cout << now << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
solve();
return 0;
}



浙公网安备 33010602011771号