题解
whyxiexieni · · 个人记录
atcoder abc249 - f 题解
题意
一开始有一个整数
思路
当
标程
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 5;
const ll INF = 1e18;
int n, k, t[N], y[N];
ll sum, ans = -INF;
priority_queue<int> q;
int main(){
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> k;
for(int i = 1; i <= n; i++){
cin >> t[i] >> y[i];
}
for(int i = n; k >= 0 && i >= 0; i--){
if(t[i] == 2){
if(y[i] >= 0){
sum += y[i];
} else{
q.push(y[i]);
}
} else{
while(q.size() > k){
sum += q.top();
q.pop();
}
k--;
ans = max(ans, sum + y[i]);
}
}
cout << ans;
return 0;
}
atcoder abc248 - f题解
题意
给定整数
标程
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 3e3 + 5;
int n, p;
ll dp[N][N][2];
int main(){
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> p;
dp[1][1][0] = dp[1][0][1] = 1;
for(int i = 2; i <= n; i++){
for(int j = 0; j < n; j++){
dp[i][j][1] = (dp[i - 1][j][0] + dp[i - 1][j][1]) % p;
if(j >= 1){
dp[i][j][0] = dp[i - 1][j - 1][0];
dp[i][j][1] = (dp[i][j][1] + 3 * dp[i - 1][j - 1][1]) % p;
}
if(j >= 2){
dp[i][j][0] = (dp[i][j][0] + 2 * dp[i - 1][j - 2][1]) % p;
}
}
}
for(int i = 1; i < n; i++){
cout << dp[n][i][1] << ' ';
}
return 0;
}
atcoder abc245 - f题解
题意
有
思路
因为
标程
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m, ind[N], c, q[N];
vector<int> g[N];
void topo(){
for(int i = 1; i <= n; i++){
if(!ind[i]){
q[++c] = i;
}
}
for(int i = 1; i <= c;){
int u = q[i];
i++;
for(int v : g[u]){
ind[v]--;
if(!ind[v]){
q[++c] = v;
}
}
}
}
int main(){
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> m;
for(int i = 1, u, v; i <= m; i++){
cin >> u >> v;
g[v].push_back(u);
ind[u]++;
}
topo();
cout << n - c;
return 0;
}
atcoder abc246 - e题解
题意
有一个 .,表示位置 #,表示 -1。
思路
我们需要使用一个
标程
#include<bits/stdc++.h>
using namespace std;
const int N = 15e2 + 5, dx[] = {1, 1, -1, -1}, dy[] = {-1, 1, -1, 1};
struct Node{
int x, y, d, dis;
};
int n, ax, ay, bx, by, vis[N][N][4];
char a[N][N];
deque<Node> q;
void bfs(){
for(int i = 0; i < 4; i++){
q.push_front({ax, ay, i, 1});
}
while(q.size()){
Node now = q.front();
q.pop_front();
int x = now.x, y = now.y, d = now.d, dis = now.dis;
if(x == bx && y == by){
cout << dis;
return ;
}
vis[x][y][d] = 1;
for(int i = 0; i < 4; i++){
int nx = x + dx[i], ny = y + dy[i];
if(nx >= 1 && ny >= 1 && nx <= n && ny <= n && !vis[nx][ny][i] && a[nx][ny] == '.'){
if(i == d){
q.push_front({nx, ny, i, dis});
} else{
q.push_back({nx, ny, i, dis + 1});
}
}
}
}
cout << -1;
}
int main(){
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> ax >> ay >> bx >> by;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
cin >> a[i][j];
}
}
bfs();
return 0;
}
atcoder abc245 - e题解
题意
有 Yes,否则输出 No。
思路
当看到set 维护当前可用盒子的长的集合,但长可能有重复,所以使用multiset。
标程
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
struct Node{
int x, y;
}a[N], b[N];
int n, m, ans;
multiset<int> s;
bool cmp(const Node &i, const Node &j){
return i.x > j.x;
}
int main(){
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> a[i].x;
}
for(int i = 1; i <= n; i++){
cin >> a[i].y;
}
for(int i = 1; i <= m; i++){
cin >> b[i].x;
}
for(int i = 1; i <= m; i++){
cin >> b[i].y;
}
sort(a + 1, a + n + 1, cmp);
sort(b + 1, b + m + 1, cmp);
for(int i = 1, j = 1; i <= n; i++){
for(; j <= m && b[j].x >= a[i].x; j++){
s.insert(b[j].y);
}
auto it = s.lower_bound(a[i].y);
if(it == s.end()){
cout << "No";
return 0;
}
s.erase(it);
}
cout << "Yes";
return 0;
}
atcoder abc245 - d 题解
题意
已知
求
思路
首先已知
我们知道
求出
时间复杂度:
标程
#include<bits/stdc++.h>
using namespace std;
const int N = 2e2 + 5;
int n, m, a[N], b[N], c[N];
int main(){
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> m;
for(int i = 0; i <= n; i++){
cin >> a[i];
}
for(int i = 0; i <= n + m; i++){
cin >> c[i];
}
for(int i = n + m; i >= n; i--){
b[i - n] = c[i] / a[n];
for(int j = i; j >= i - n; j--){
c[j] -= b[i - n] * a[n - i + j];
}
}
for(int i = 0; i <= m; i++){
cout << b[i] << ' ';
}
return 0;
}
atcoder abc243 - d
题目大意
一开始有一个无限大的完全二叉树,有一个长度为
思路
如果直接对
通过观察题目可以发现,如果在
标程
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int n;
ll x;
string s;
vector<char> v;
int main(){
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> x >> s;
s = ' ' + s;
for(int i = 1; i <= n; i++){
if(s[i] == 'L' || s[i] == 'R'){
v.push_back(s[i]);
} else{
if(v.size()){
v.pop_back();
} else{
x /= 2;
}
}
}
for(int c : v){
x = x * 2 + (c == 'R');
}
cout << x;
return 0;
}
atcoder abc242 - e题解
题目大意
有
思路
我们知道回文串的第
那么我们要先算出字典序小于
标程
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5, MOD = 998244353;
int n, ans;
string s;
void solve(){
cin >> n >> s;
ans = 0;
for(int i = 0; i < (n + 1) / 2; i++){
ans = (1ll * ans * 26 % MOD + s[i] - 'A') % MOD;
}
ans++;
for(int i = (n + 1) / 2; i < n; i++){
if(s[n - i - 1] != s[i]){
ans -= s[n - i - 1] > s[i];
break;
}
}
cout << ans % MOD << '\n';
}
int main(){
ios::sync_with_stdio(false), cin.tie(0);
int T;
cin >> T;
while(T--){
solve();
}
return 0;
}
atcoder abc242 - d题解
题目大意
给定由 A,B,C 组成的字符串串 A能变成BC,B能变成CA,C能变成AB。共
思路
我们先考虑使用暴力。
题中的操作会将字符串长度乘上
得出这一结论后,我们先用A, B, C,可以得出,每次操作后,每个字符会变成
通过以上结论,我们考虑使用递归分治解决这道题,每次
时间复杂度:
标程
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
string s;
int q, n;
ll t, k;
char dfs(ll t, ll k){
if(!t){
return s[k];
}
if(!k){
return (s[0] - 'A' + t) % 3 + 'A';
}
return (dfs(t - 1, k / 2) - 'A' + k % 2 + 1) % 3 + 'A';
}
int main(){
ios::sync_with_stdio(false), cin.tie(0);
cin >> s >> q;
while(q--){
cin >> t >> k;
k--;
cout << dfs(t, k) << '\n';
}
return 0;
}