A
模拟即可
void solve()
{
string s;
cin >> s;
for (int i = s.size() - 1; i >= 0; i--)
if (s[i] == '.')
{
cout << s.substr(i + 1) << endl;
return;
}
}
B
模拟,可以让下标从0开始,这样可以在%n下满足题意
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
void solve()
{
int n, m, opt;
cin >> n >> m >> opt;
vector<vector<char>> a(n, vector<char>(m, '.'));
int d = 0, x = 0, y = 0;
while (opt--)
{
if (a[x][y] == '.')
{
a[x][y] = '#';
d = (d + 1) % 4;
}
else
{
a[x][y] = '.';
d = (d - 1 + 4) % 4;
}
x = (x + dx[d] + n) % n;
y = (y + dy[d] + m) % m;
// cout << x << " " << y << endl;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
cout << a[i][j];
cout << endl;
}
}
C
从后往前吗模拟:
设当前最少有x人
如果是\(a[i]<0\),则在到达该站前至少\(abs(a[i])\)人
如果是\(a[i]>0\),则在到达该站前可以少\(abs(a[i])\)人
最后再顺着模拟一遍得到答案
void solve()
{
ll n;
cin >> n;
vector<ll> a(n + 1);
for (int i = 1; i <= n; i++)
cin >> a[i];
ll ans = 0;
for (int i = n; i >= 1; i--)
{
if (a[i] > 0)
{
ans -= a[i];
ans = max(0ll, ans);
}
else
ans -= a[i];
}
for (int i = 1; i <= n; i++)
ans += a[i];
cout << ans << endl;
}
D
同时维护两人的坐标,进行bfs
ll dist[70][70][70][70];
int d[4][4] = {{-1, 1, 0, 0}, {0, 0, -1, 1}, {-1, 1, 0, 0}, {0, 0, -1, 1}};
void solve()
{
int n, cnt = 0;
cin >> n;
array<int, 4> st;
vector<vector<char>> a(n + 1, vector<char>(n + 1));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
{
cin >> a[i][j];
if (a[i][j] == 'P')
st[cnt++] = i, st[cnt++] = j;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
for (int k = 1; k <= n; k++)
for (int l = 1; l <= n; l++)
dist[i][j][k][l] = 1e9;
auto bfs = [&](array<int, 4> x) -> void
{
queue<array<int, 4>> q;
q.push(x);
dist[x[0]][x[1]][x[2]][x[3]] = 0;
while (q.size())
{
auto t = q.front();
q.pop();
for (int i = 0; i < 4; i++) // 方向
{
array<int, 4> tmp;
for (int j = 0; j < 4; j++)
{
tmp[j] = t[j] + d[j][i];
tmp[j] = min(n, tmp[j]);
tmp[j] = max(1, tmp[j]);
}
if (a[tmp[0]][tmp[1]] == '#')
tmp[0] = t[0], tmp[1] = t[1];
if (a[tmp[2]][tmp[3]] == '#')
tmp[2] = t[2], tmp[3] = t[3];
if (dist[tmp[0]][tmp[1]][tmp[2]][tmp[3]] > dist[t[0]][t[1]][t[2]][t[3]] + 1)
{
dist[tmp[0]][tmp[1]][tmp[2]][tmp[3]] = dist[t[0]][t[1]][t[2]][t[3]] + 1;
q.push(tmp);
}
}
}
};
bfs(st);
ll ans = 1e9;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (a[i][j] == '.')
ans = min(ans, dist[i][j][i][j]);
if (ans == 1e9)
ans = -1;
cout << ans << endl;
}
E
线段树优化dp,维护转移范围内的最大值
struct node
{
ll val;
};
struct segtree
{
int n;
vector<node> a;
segtree(int _n) : n(_n * 4 + 10), a(n + 1) {}
void update(int id) { a[id].val = max(a[id * 2].val, a[id * 2 + 1].val); }
void build(int id, int l, int r, vector<int> &arr)
{
if (l == r)
a[id].val = arr[l];
else
{
int mid = l + r >> 1;
build(id * 2, l, mid, arr);
build(id * 2 + 1, mid + 1, r, arr);
update(id);
}
}
void change(int id, int l, int r, int pos, int val)
{
if (l == r) // 叶子节点
a[id].val = val;
else
{
int mid = l + r >> 1;
if (pos <= mid)
change(id * 2, l, mid, pos, val);
else
change(id * 2 + 1, mid + 1, r, pos, val);
update(id);
}
}
ll query(int id, int l, int r, int ql, int qr)
{
if (l == ql && r == qr)
return a[id].val;
int mid = l + r >> 1;
if (qr <= mid)
return query(id * 2, l, mid, ql, qr);
else if (ql > mid)
return query(id * 2 + 1, mid + 1, r, ql, qr);
else
return max(query(id * 2, l, mid, ql, mid), query(id * 2 + 1, mid + 1, r, mid + 1, qr));
}
};
void solve()
{
int n, d;
cin >> n >> d;
vector<int> a(n + 1), rec(N, 0);
for (int i = 1; i <= n; i++)
cin >> a[i];
segtree seg(N);
seg.build(1, 1, n, rec);
for (int i = 1; i <= n; i++)
{
int l = max(1, a[i] - d), r = min((int)5e5, a[i] + d);
int res = seg.query(1, 1, N, l, r) + 1;
int now = max(seg.query(1, 1, N, a[i], a[i]), 1ll);
now = max(now, res);
seg.change(1, 1, N, a[i], now);
}
ll res = 1;
for (int i = 1; i <= n; i++)
res = max(res, seg.query(1, 1, N, a[i], a[i]));
cout << res << endl;
}
标签:tmp,AtCoder,339,Beginner,int,void,mid,vector,id
From: https://www.cnblogs.com/Chesedss/p/18011402