题解:LuckyIII Beginner Contest 2 - She has come

· · 题解

比赛链接

总结:比赛出水了,难度主要都在 D 和 E 上了。

A. Sandwich

难度:橙。
思路:就是贪心。

从左到右遍历字符串,只要当前位置开头的三个字符首尾字符不同,就立刻选取这个 Sandwich,并跳过后续 2 个字符;若不符合条件,则右移一位继续检查。

贪心正确性证明:若当前位置可以形成合法三元组,选取它一定不会比不选更差。因为选取该三元组后,剩余字符串的起始位置更早(可用空间更多),能选出的三元组数量不会少于跳过当前位置的方案。

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

int main() {
    ios::sync_with_stdio(false);
    string s;
    cin >> s;
    int len = s.size(), ans = 0;
    for (int i = 0; i <= n - 3; )
        if (s[i] != s[i + 2]) {
            ans++;
            i += 3;
        } else
            i++;
    cout << ans;
    return 0;
}

B. Dance Partners

难度:黄。
思路:考虑使用并查集,然后看 A 公司中和 M 有朋友关系的人数和 B 公司中和 N 有朋友关系的人数,取最小值输出。

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

int fa[40005], n, m, p, q;

int find(int x) {
    if (x == fa[x]) return x;
    return fa[x] = find(fa[x]);
}

void Merge(int x, int y) {
    int fx = find(x);
    int fy = find(y);
    if (fx != fy) {
        fa[fy] = fx;
    }
}

int main() {
    scanf("%d%d%d%d", &n, &m, &p, &q);
    for (int i = 1; i <= n + m; i++) {
        fa[i] = i;
    }
    for (int i = 1, x, y; i <= p; i++) {
        scanf("%d%d", &x, &y);
        Merge(x, y);
    }
    for (int i = 1, x, y; i <= q; i++) {
        scanf("%d%d", &x, &y);
        x *= -1, y *= -1;
        x += n, y += n;
        Merge(x, y);
    }
    int xm = 0, xh = 0;
    for (int i = 1; i <= n; i++) {
        if (find(i) == find(1)) {
            xm++;
        }
    }
    for (int i = n + 1; i <= n + m; i++) {
        if (find(i) == find(n + 1)) {
            xh++;
        }
    }
    printf("%d", min(xm, xh));
    return 0;
}

C. 8-Digits Problem

难度:绿。
思路:BFS。考虑依题意直接搜索+转移。整体没有思维难度,难度主要集中在实现上。本人觉得实现也没什么难度啊。

#include<iostream>
using namespace std;
#include<string>
#include<queue>
#include<unordered_map>
#define map unordered_map

string swaped(string orii, int i, int j){
    string ori = orii;
    swap(ori[i],ori[j]);
    return ori;
}
int bfs(string st, string en){
    queue<string>go;
    go.push(st);
    map<string,bool>vis;
    map<string,int>dis;
    vis.insert(make_pair(st,true));
    vis.insert(make_pair(st,0));
    while(!go.empty()){
        string Top = go.front();
        if(Top == en){

            return dis[Top];
        }
        int id0;
        for(int i=0;i<Top.size();i++){
            if(Top[i] == '0'){
                id0 = i;
                break;
            }
        }
        #define mygo(x,y) \
            do{\
            if(vis.find(swaped(Top,(x),(y)))==vis.end())\
            {\
                vis.insert(make_pair(swaped(Top,(x),(y)),true));\
                dis.insert(make_pair(swaped(Top,(x),(y)),dis[Top]+1));\
                go.push(swaped(Top,(x),(y)));\
            }\
            }while(0)
        switch(id0){
            case 0:{
                mygo(0,1);
                mygo(0,3);
                break;
            }
            case 1:{
                mygo(1,0);
                mygo(1,2);
                mygo(1,4);
                break;
            }
            case 2:{
                mygo(2,1);
                mygo(2,5);
                break;
            }
            case 3:{
                mygo(3,0);
                mygo(3,4);
                mygo(3,6);
                break;
            }
            case 4:{
                mygo(4,1);
                mygo(4,3);
                mygo(4,5);
                mygo(4,7);
                break;
            }
            case 5:{
                mygo(5,2);
                mygo(5,4);
                mygo(5,8);
                break;
            }
            case 6:{
                mygo(6,3);
                mygo(6,7);
                break;
            }
            case 7:{
                mygo(7,6);
                mygo(7,4);
                mygo(7,8);
                break;
            }
            case 8:{
                mygo(8,5);
                mygo(8,7);
                break;
            }
            default:{
                break;
            }
        }
        go.pop();
    }
    return -1;
}

int main(){
    string st;

    cin >> st;
    cout << bfs(st, "123804765");
    return 0;
}

D. Cubic

难度:黄。

思路:众所周知

1^3+2^3+\cdots+n^3=(\frac{n(n+1)}2)^2

,所以令 F(n)=\left(\frac{n(n+1)}2\right)^2,则

\sum^b_{i=a}i^3=F(b)-F(a-1)

。

当 p 为奇质数(p \neq 2)时,模 p 下 2 的逆元存在,则

F(n) \bmod p = \left( \frac{n(n+1)}{2} \bmod p \right)^2 \bmod p

。

当 p=2 时,2 没有逆元,但注意到模 2 下 i^3 \equiv i \pmod 2,因此

\sum_{i=a}^{b} i^3 \equiv \sum_{i=a}^{b} i \pmod 2

。区间和 \sum_{i=a}^{b} i 的奇偶性等于区间内奇数个数的奇偶性,即

\text{奇数个数} = \left\lfloor\frac{b+1}{2}\right\rfloor - \left\lfloor\frac{a}{2}\right\rfloor

,答案即为该值 \bmod 2。

::::success[因为数据过水导致的非正解 AC 代码]

#include<iostream>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        long long x,y,z,usto1,usto2;
        cin>>x>>y>>z;
        x=x%z;y=y%z;
        usto1=(y)*(y+1)/2;
        usto1=(usto1%z)*(usto1%z)%z;
        usto2=(x)*(x-1)/2;
        usto2=(usto2%z)*(usto2%z)%z;
        cout<<(usto1-usto2+z)%z<<endl;;
    }
    exit(0);
}

::::

Code from 出题人

//std
#include<bits/stdc++.h>
#define int long long
using namespace std;

int calc(int a, int p)
{
    //if(a <= 0) return 0;

    int a1 = a % p;
    int a2 = (a + 1) % p;
    int inv2 = (p + 1) / 2; //p is odd prime.
    int ans = a1 * a2 % p * inv2 % p;
    return ans;
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        int a, b, p;
        cin >> a >> b >> p;
        if(p == 2)
        {
            int cnt = (b + 1) / 2 - a / 2;
            cout << (cnt & 1) << "\n";
            continue;
        }

        int v1 = calc(b, p);
        int v2 = calc(a - 1, p);

        int ans = (v1 * v1 % p - v2 * v2 % p + p) % p;
        cout << ans << "\n";
    }
    return 0;
}

Code from 题解作者

#include <bits/stdc++.h>
using namespace std;
#define int long long

int sum_cube_mod(int n, int p) {
    if (n <= 0) return 0;
    int inv2 = (p + 1) / 2;
    int x = (n % p) * ((n + 1) % p) % p;
    x = x * inv2 % p;

    return x * x % p;
}

signed main() {
    ios :: sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--) {
        int a, b, p;
        cin >> a >> b >> p;
        int ans;
        if (p == 2) {
            int odd = (b + 1) / 2 - a / 2;
            ans = odd & 1;
        } else {
            int sb = sum_cube_mod(b, p);
            int sa = sum_cube_mod(a - 1, p);
            ans = (sb - sa + p) % p;
        }
        cout << ans << '\n';
    }
    return 0;
}

E. Journey in the Backrooms

难度:青。

思路:我感觉这个除了压缩都不难想吧?

将每扇门抽象成一个点,每扇门往前一扇门连一条单向边,门之间的传送关系也连边(本题解中称作特殊边),边权均为 1,然后求最短路。

但是会超时。

注意到未上锁的门很少(仅不超过 1000 扇)。

对于每条特殊边,我们将其连接的两点标记起来,称作关键点,未被标记的称作非关键点。

对于中间没有关键点的两点,移动方式只能是 i\rightarrow i-1。

所以我们就会发现,对于两个相邻的关键点 u<v,从 v 到 u 需要走 v-u 分钟。

由于本题的编号设定是倒序的,因此考虑建反图,所以上面说的就是从 u 到 v 需要走 v-u 分钟。

假设所有关键点为 a_1<a_2<\cdots<a_k(k\leqslant2000),则建图就容易了,对于相邻关键点直接建边权为 a_{i+1}-a_i 的边,对于传送关系直接建边权为 1 的边。这样的话整个图就只有 k+传送关系数量 条边了。

所以我们就以每个关键点为起点跑一遍 Dijkstra 就行了。

假设 dist_{i,j} 是 i 到 j 的最短路。

对于询问 (x,y),变换为 (y,x),我们需要:

用二分找到第一个 \geqslant y 的关键点 ps 和最后一个 \leqslant x 的关键点 pe,如果有哪个找不到或者 ps>pe 则说明这两个点之间根本没有关键点,答案就是 x-y;否则答案就是 (a_{ps}-y)+dist_{ps,pe}+(x-a_{pe})。

#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
int n,t,a[2005],x,y,d[2005][2005],ans,cnt;
vector<pair<int,int> >g[100005];//存储跳到的数字和排序后的编号
struct node
{
    int v,w;
};
bool operator<(node a,node b)//小顶堆
{
    return a.w>b.w;
}
priority_queue<node>q;
bool vis[2005];
void dij(int idx)
{
    q.push({idx,0});
    while(!q.empty())
    {
        x=q.top().v;
        y=q.top().w;
        q.pop();
        if(x>cnt)
        {
            continue;
        }
        if(d[idx][x]<=y)
        {
            continue;
        }
        d[idx][x]=y;
        for(int i=0;i<g[a[x]].size();i++)//方式二:遍历所有x号节点能跳到的地方
        {
            q.push({g[a[x]][i].second,y+1});
        }
        //方式1:下一个可以走到的点加入队列
        q.push({x+1,y+(a[x+1]-a[x])});
    }
}
int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n>>t;
    for(int i=1;i<=n;i++)
    {
        cin>>x;
        if(x)
        {
            g[x].push_back({i,0});
            a[++cnt]=x;
            a[++cnt]=i;
        }
    }
    sort(a+1,a+1+cnt);
    cnt=unique(a+1,a+1+cnt)-a-1;//计算剩余有效边
    for(int i=1;i<=cnt;i++)//计算排序后,每条边能跳到的数字编号
    {
        int u=a[i];//取出i号节点对应的数字
        for(int j=0;j<g[u].size();j++)//遍历这个节点能够跳到的节点
        {
            int v=g[u][j].first;
            int p=lower_bound(a+1,a+1+cnt,v)-a;//找到第i号节点能跳到的数字v节点的编号
            g[u][j].second=p;//记录数字为v的节点对应的编号p
        }
    }
    memset(d,0x3f,sizeof(d));
    for(int i=1;i<=cnt;i++)
    {
        dij(i);
    }
    while(t--)
    {
        cin>>y>>x;
        int ps=lower_bound(a+1,a+cnt+1,x)-a;//找大于等于起点的节点编号
        if(ps>=cnt)//起点比最大的编号还大
        {
            cout<<y-x<<endl;
            continue;
        }
        int pe=upper_bound(a+1,a+1+cnt,y)-a-1;//找到大于终点的节点编号的前一个编号 
        if(pe<=1||pe<=ps)
        {
            cout<<y-x<<endl;
            continue;
        }
        ans=a[ps]-x+d[ps][pe]+y-a[pe];
        cout<<ans<<endl;
    }
    return 0;
}