首页 > 其他分享 >ABC339 题解(A~G)

ABC339 题解(A~G)

时间:2024-02-03 21:44:19浏览次数:24  
标签:ABC339 int 题解 ans cin long dx dy

A

从后向前找到第一个 . 就行了。

B

按照题意模拟,设当前位置 \(x, y\) 移动方向 \(dx, dy\)。那么下一步为 \((x+dx,y+dy)\)

设新的移动方向为 \(dx',dy'\)

如果顺时针旋转,则有 \(dy'\gets -dx, dx'\gets dy\);

如果逆时针,则有 \(dx'\gets -dy, dy'\gets dx\)。

C

鉴定为除 A 以外第一简单。

设原来没有乘客,按题意模拟找到乘客人数 \(x\) 的最小值 \(x_{min}\),最后乘客个数 \(x_n\)。

当然,开始时的乘客不能是负数。

于是有 \(ans=x_n+max(0,-x_{min})\)

D

比 EFG 难(

注意到状态数最多只有 \(60^4\),每次转移代价是 1,每个状态可以向另外 4 个状态转移,于是直接 bfs,复杂度为 \(O(n^4)\)。

但是一开始以为最终情况只能是棋子被移到边界处才能重合,喜提一发法师。

可以考虑把两个棋子四个坐标压成一个整数储存,然后就是普通 bfs。

E

显然有基础的 dp:\(f_i=\max\limits_{1\le j\le n,a_j\in[a_i-d,a_i+d]}f_j+1\)。但是复杂度 \(O(n^2)\),过不了。

考虑线段树优化 dp。

表示 \([l,r]\) 的结点存 \(\max\limits_{a_i\in[l,r]}f_i\)。修改和查询显然。

时间复杂度为 \(O(n\log n)\)。

F

最简单想法是枚举 \(i,j\),用 map 存储 \(A_k\) 的值,实现 \(O(n^2)\) 或 \(O(n^2\log n)\)。

但是注意到 \(A_i\le 10^{1000}\)。高精度乘法 \(O(n\log n)\)(最快)复杂度也要上天(\(O(n^3\log n)\)) 考虑能不能把数变小。考虑取模。

把这串数 \(A_i\) 分别模大质数 \(p_j\),得到 \(a_{i,j}\)。

因为 \(A_i\times A_j = A_k \Rightarrow \forall o,a_{i,o}\times a_{j,o} \equiv a_{k,o} \bmod p_j\)。

但是 \(\exists A_i\times A_j \neq A_k, a_{i,o}\times a_{j,o} \equiv a_{k,o} \bmod p_j\)?

毕竟概率很小。

因为 Atcoder 不卡模数 只要模两到三个大质数,错误率就会很低,所以使用 pair<long long, long long>tuple<int, int, int> 存储一个数模 2 或 3 个大质数后的值。

然后就用一开始的方法愉快过题。复杂度还是 \(O(n^2)\) 或 \(O(n^2\log n)\)。

G

板,太板了。二维数点板子。

先过 Luogu P3834

注意到,这题 比 P3834 简单 和 P3834 的区别在于一个求第 \(k\) 小,一个是求小于某个数的数的和。

于是稍作修改即可(指将计数改为加上代表的值,然后查询和普通线段树差不多)。


代码

A

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

signed main()
{
    ios::sync_with_stdio(0);cin.tie(0);
    string s; cin >> s;
    reverse(s.begin(), s.end());
    string ans;
    for(char c : s)
    {
        if(c == '.') break;
        ans += c;
    }
    reverse(ans.begin(), ans.end());
    cout << ans;
    return 0;
}

B

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int N = 105;
int col[N][N], n, m, q;

signed main()
{
    ios::sync_with_stdio(0);cin.tie(0);
    cin >> n >> m >> q;
    int x = 1, y = 1, dx = -1, dy = 0;
    while(q --)
    {
        if(!col[x][y])
        {
            col[x][y] = 1;
            int nx = dy, ny = -dx;
            dx = nx, dy = ny;
        }
        else
        {
            col[x][y] = 0;
            int nx = -dy, ny = dx;
            dx = nx, dy = ny;
        }
        x = (x + dx + n - 1) % n + 1;
        y = (y + dy + m - 1) % m + 1;
    }
    for(int i = 1; i <= n; i ++, cout << "\n")
    for(int j = 1; j <= m; j ++)
    {
        if(col[i][j]) cout << "#";
        else cout << ".";
    }

    return 0;
}

C

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

#define int ll

const int N = 2e5 + 5;
int a[N], mn = 0, n;

signed main()
{
    ios::sync_with_stdio(0);cin.tie(0);
    cin >> n;
    int now = 0;
    for(int i = 1; i <= n; i ++)
    {
        cin >> a[i];
        now += a[i];
        mn = min(mn, now);
    }
    cout << -mn + now;

    return 0;
}

D

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

struct node {int x[2], y[2];};
const int B = 60, N = 65;
int n; char a[N][N];
node decode(int o) 
{
    int x, y, u, v;
    v = o % B; o /= B;
    u = o % B; o /= B;
    y = o % B; o /= B;
    x = o % B;
    return {{x, u}, {y, v}};
}
inline int encode(int x, int y, int u, int v)
{
    return ((x * B + y) * B + u) * B + v;
}
int dis[B * B * B * B];
int dx[4] = {0, 1, -1, 0}, dy[4] = {1, 0, 0, -1};
inline bool chk(int x, int y)
{
    return x >= 0 && x < n && y >= 0 && y < n && a[x][y] != '#';
}
void bfs(int f)
{
    memset(dis, 0x3f, sizeof dis);
    dis[f] = 0;
    queue<int> q;
    q.push(f);
    while(q.size())
    {
        int t = q.front(); q.pop();
        node o = decode(t);
        for(int i = 0; i < 4; i ++)
        {
            int x = o.x[0], y = o.y[0], u = o.x[1], v = o.y[1];
            if(chk(x + dx[i], y + dy[i])) x += dx[i], y += dy[i];
            if(chk(u + dx[i], v + dy[i])) u += dx[i], v += dy[i];
            int id = encode(x, y, u, v);
            if(dis[id] > dis[t] + 1)
            {
                dis[id] = dis[t] + 1;
                q.push(id);
            }
        }
    }
}

signed main()
{
    ios::sync_with_stdio(0);cin.tie(0);
    cin >> n;
    int x, y, u, v, cnt = 0;
    for(int i = 0; i < n; i ++)
    {
        cin >> a[i];
        for(int j = 0; j < n; j ++)
        {
            if(a[i][j] == 'P')
            {
                if(cnt == 1) u = i, v = j;
                else x = i, y = j, cnt ++;
            }
        }
    }
    bfs(encode(x, y, u, v));
    int ans = 2e9;
    for(int i = 0; i < n; i ++)
    for(int j = 0; j < n; j ++)
    {
        ans = min(ans, dis[encode(i, j, i, j)]);
    }
    if(ans > 1e9) cout << -1;
    else cout << ans;

    return 0;
}

E

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

#define int ll

const int N = 5e5 + 5;
struct sgt
{
    int a[N << 2];
    void pu(int x) {a[x] = max(a[x << 1], a[x << 1 | 1]);}
    void upd(int q, int l, int r, int x, int v)
    {
        if(l == r) return a[x] = max(a[x], v), void();
        int mid = l + r >> 1;
        if(mid >= q) upd(q, l, mid, x << 1, v);
        else upd(q, mid + 1, r, x << 1 | 1, v);
        pu(x);
    }
    int qry(int ql, int qr, int l, int r, int x)
    {
        if(ql <= l && r <= qr) return a[x];
        int mid = l + r >> 1, ans = 0;
        if(mid >= ql) ans = max(ans, qry(ql, qr, l, mid, x << 1));
        if(mid < qr) ans = max(ans, qry(ql, qr, mid + 1, r, x << 1 | 1));
        return ans;
    }
}t;
int f[N], n, a[N], d;

const int V = 5e5;

signed main()
{
    ios::sync_with_stdio(0);cin.tie(0);
    cin >> n >> d;
    for(int i = 1; i <= n; i ++) cin >> a[i];
    for(int i = 1; i <= n; i ++)
    {
        f[i] = t.qry(max(1ll, a[i] - d), min(V, a[i] + d), 1, V, 1) + 1;
        t.upd(a[i], 1, V, 1, f[i]);
    }
    int ans = 0;
    for(int i = 1; i <= n; i ++)
        ans = max(ans, f[i]);
    cout << ans;

    return 0;
}

F

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

#define int ll

const int N = 1005;

int p[2] = {100000000001423ll,
100000000001467ll};

map<pair<int, int>, int> mp;

int n, a[N][2];

int getnum(string &s, int p)
{
    int x = 0;
    for(char c : s)
        x = (x * 10 + c - '0') % p;
    return x;
}

signed main()
{
    ios::sync_with_stdio(0);cin.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i ++)
    {
        string s; cin >> s;
        a[i][0] = getnum(s, p[0]);
        a[i][1] = getnum(s, p[1]);
        mp[{a[i][0], a[i][1]}] ++;
    }
    int ans = 0;
    for(int i = 1; i <= n; i ++)
    {
        for(int j = 1; j <= n; j ++)
        {
            int x = (__int128) a[i][0] * a[j][0] % p[0];
            int y = (__int128) a[i][1] * a[j][1] % p[1];
            if(mp.count({x, y})) ans += mp[{x, y}];
        }
    }
    cout << ans;

    return 0;
}

G

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

#define int ll

const int N = 2e5 + 5;

struct sgt
{
    int idx, a[N << 5], ls[N << 5], rs[N << 5], rt[N];
    void pup(int x) {a[x] = a[ls[x]] + a[rs[x]];}
    int nw(int &x) {return x ? x : x = ++idx;}
    int upd(int q, int l, int r, int x)
    {
        int u = ++idx; a[u] = a[x]; ls[u] = ls[x], rs[u] = rs[x];
        if(l == r) return a[u] += l, u;
        int mid = l + r >> 1;
        if(mid >= q) ls[u] = upd(q, l, mid, ls[x]);
        else rs[u] = upd(q, mid + 1, r, rs[x]);
        pup(u);return u;
    }
    int qry(int ql, int qr, int l, int r, int xl, int xr)
    {
        if(ql <= l && r <= qr) return a[xr] - a[xl];
        int mid = l + r >> 1, ans = 0;
        if(mid >= ql) ans += qry(ql, qr, l, mid, ls[xl], ls[xr]);
        if(mid < qr) ans += qry(ql, qr, mid + 1, r, rs[xl], rs[xr]);
        return ans;
    }
}t;

int n, q, a[N];

const int V = 1e9;

signed main()
{
    ios::sync_with_stdio(0);cin.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i ++)
    {
        cin >> a[i];
        t.rt[i] = t.upd(a[i], 0, V, t.rt[i - 1]);
    }
    cin >> q;
    int la = 0;
    while(q --)
    {
        int l, r, k;cin >> l >> r >> k;
        l ^= la, r ^= la, k ^= la;
        cout << (la = t.qry(0, k, 0, V, t.rt[l - 1], t.rt[r])) << "\n";
    }

    return 0;
}

标签:ABC339,int,题解,ans,cin,long,dx,dy
From: https://www.cnblogs.com/adam01/p/18005266

相关文章

  • P7119 Mivik 的游戏 题解
    先从一个例子开始假如硬币开始是这样的:HHHHHTHH然后就可以将这个反面硬币\(T\)左边的硬币全都反过来,共需\(5\)步。然后就变成了:TTTTTTHH最后再将最右边两个反过来就可以了,共需\(5+2=7\)步。如果\(p\)为这个反面的硬币位置的话,那么答案\(as=2p-1\)。推导......
  • 洛谷题解-P1194 买礼物
    https://www.luogu.com.cn/problem/P1194题目描述又到了一年一度的明明生日了,明明想要买BBB样东西,巧的是,这BBB样东西价格都是AAA元。但是,商店老板说最近有促销活动,也就是:如果你买了第III样东西,再买第JJJ样,那么就可以只花KI,JK_{I,J}KI,J​元,更巧的是,KI,JK_{I,J}K......
  • [ABC279G] At Most 2 Colors 题解
    题目链接题目大意有一个\(1\timesN\)的格子和\(c\)种颜色,每个格子可以染上\(c\)种颜色中的一种。求任意相邻\(k\)个格子染色种类不超过\(2\)种的方案数。思路很明显,这是一个计数DP的题设\(f_i\)表示前\(i\)个格子染色的方案数,考虑第\(i\)个格子的染色情......
  • 0129-0203部分校赛题解复盘
    vj第一场A题https://codeforces.com/gym/103480/problem/A该题让我们可以从回文串的特点入手,即两个相同的字母便可增加长度2,所以并不用思考该回文串要如何排序出来,而是看有多少对相同的字母,使用map<char,int>来记录字母出现的次数,再计算可以除以2的次数即可。点击查看代码#i......
  • CF1454F Array Partition 题解
    题目链接:CF或者洛谷感觉很多人写太复杂了,其实感觉这题性质很好的。。询问是否可以分为三段\(max_1=min_2=max_3\)。考虑枚举\(max_1\),由于后缀\(max_3\)具有单调性,所以我们可以双指针轻松拿到这样一个模型:因为后缀\(max\)具有单调性,通过双指针我们可以拿到\(j\)后缀......
  • CF1789F Serval and Brain Power 题解
    题目链接点击打开链接题目解法好有技巧性的题(感觉\(n\le80\)这个数据范围就很奇怪啊)首先可以发现\(k\le3\)是好做的,只要枚举断点,然后\(dp\)做一遍\(lcs\)即可,时间复杂度为\(O(n^{2k+1})\),不过严重跑不满,所以可以跑\(k=4\)的情况和\(k=2\)的情况是相同的,所以不......
  • [ABC328G] Cut and Reorder 题解
    [ABC328G]CutandReorder题解状压fw实锤思路观察到排列操作只会做一次,答案的编号一定是一段一段的。所以可以考虑\(f_s\)表示前\(popcount(s)+1\)个\(a\)元素,放进\(b\)中\(s\)的最小代价转移可以考虑放置一段,每放一段需要\(c\)的代价。专业看起来复杂度非......
  • U404643 帕鲁大陆染色的一天 题解
    题目链接:帕鲁大陆染色的一天注意到每种颜色是独立的,所以我们能够比较容易地得知每种颜色在一系列操作中的出现和消失时间。我们注意到每个操作加入以后会有两个影响,对它后面的操作显然颜色总数都会\(+1\),对前面操作的影响来说,显然会覆盖掉某些颜色,导致某些颜色消失,换句话来讲消......
  • P2178 [NOI2015] 品酒大会 题解
    P2178[NOI2015]品酒大会题解纪念一下第一道完全自己想出来的紫NOI题。思路由于r相似有单调性的性质,题目中也提示了这一点,考虑按\(height_i\)从大到小把所有相邻的\(sa\)数组内两个后缀合并,用并查集维护,发现第一问的答案就是\[\sum_{i是并查集的根}\binom{size_i}{2}......
  • P10118 『STA - R4』And 题解
    题目看到位运算,直接二进制拆分考虑。首先\(x\operatorname{AND}y=B\),设\(x=B+m\),\(y=B+n\),知道\(x+y=A\),所以设\(W=n+m=A-2\timesB\),\(y-x\)等价于\(n-m\)。因为已知\(x\operatorname{AND}y=B\),所以\(n\operatorname{AND}m=0\),着意味着在二进制下\(n\)和\(m\)不......