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;
}