5月26日--floyd进阶

· · 个人记录

第一题:D45445 小红的好串

其实本题非常简单。
可以说在这个阶段,不会写的可以退役了。
我的代码其实是把这个复杂化了:

题目正解思路:

直接穷举可能,判断。

我的思路:

使用map遍历字符串是记录字符,最后判断map的长度。
本方法优点:可以处理不定字符串的问题,时间复杂度O(n log n)

#include<bits/stdc++.h>
using namespace std;
//const int mx=;
string str;
map<char,bool>mp;
int main()//signed
{
    //ios::sync_with_stdio(false);
    //cin.tie(nullptr);
    //cout.tie(nullptr);
    freopen("as01.in","r",stdin);
    freopen("as01.out","w",stdout);
    cin>>str;
    for(char i:str)
    {
        mp[i]=true;
    }
    if(mp.size()==2)
    {
        cout<<"Yes";
    }
    else
    {
        cout<<"No";
    }
    return 0;
}
/*



*/

第二题:D45446 小红的好串计数

本题主要就是反向的思路:
首先我们可以算出总字串个数:

\frac{n(n+1)}{2}

然后,我们求出连续的1或0的字串长度,通过公式 \frac{n(n+1)}{2}算出不符合要求的子串,最后相减得到答案。

#include<bits/stdc++.h>
#define int long long
//十年OI一场空,不开long long见祖宗
using namespace std;
const int mx=1e6+5;
char c[mx];
char ch;
int n;
int cnt;
int sum;
signed main()//signed
{
    //ios::sync_with_stdio(false);
    //cin.tie(nullptr);
    //cout.tie(nullptr);
    freopen("as02.in","r",stdin);
    freopen("as02.out","w",stdout);
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>c[i];
    }
    for(int i=1;i<=n;i++)
    {
        if(c[i]!=ch)
        {
            sum+=cnt*(cnt+1)/2;
            cnt=1;
            ch=c[i];
        }
        else
        {
            cnt++;
        }
    }
    sum+=cnt*(cnt+1)/2;
    cout<<n*(n+1)/2-sum;
//  cout<<sum;
    return 0;
}
/*



*/

第三题:D45447 神奇的计算器

这一题比较难,个人认为比T4更难一些。
这一题是本次floyd测试第一道与floyd搭边的题。

首先,我们先考虑如何快速求解:要在O(1)时间内得到操作次数。

我们可以直接使用floyd求解出操作次数。

注意事项:

多组数据需要清空dis数组。

dis_{k,k}\neq0$:需要特殊处理,因为$x$和$y$不一定是$0

随后,就是求解部分:

我们直接循环遍历,求解过程:不断加上变化步骤:

最后,输出答案。 ##### 注意事项: 需要`-1`,因为只需要一次的话不需要增加数字 如果途中$dis_{s_{i-1},s_i}$等于初始值(极大值)说明无法到达,输出`-1` ```cpp #include<bits/stdc++.h> using namespace std; const int mx=15; string str; int dis[mx][mx]; int as(int x,int y) { int ans=0; for(int i=0;i<=9;i++) { for(int j=0;j<=9;j++) { dis[i][j]=5e8; } } for(int i=0;i<=9;i++) { dis[i][(i+x)%10]=dis[i][(i+y)%10]=1; } for(int k=0;k<=9;k++) { for(int i=0;i<=9;i++) { for(int j=0;j<=9;j++) { dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]); } } } for(int i=1;i<str.size();i++) { if(dis[str[i-1]-'0'][str[i]-'0']==5e8) { return -1; } ans+=dis[str[i-1]-'0'][str[i]-'0']-1; } return ans; } int main()//signed { //ios::sync_with_stdio(false); //cin.tie(nullptr); //cout.tie(nullptr); //freopen("","r",stdin); //freopen("","w",stdout); cin>>str; for(int x=0;x<=9;x++) { for(int y=0;y<=9;y++) { cout<<as(x,y)<<' '; } cout<<'\n'; } return 0; } /* */ ``` # 第四题:45451 来追梦国的城市规划 本题我的方法是: 使用$vis$数组标记建了机场的点,然后,使用$dist$数组表示两点之间的最短路径。 ### 核心算法: 加点$floyd

核心思路:

在遇到操作1时,直接加边,跑加点floyd,两个点都要跑。
遇到操作2,我们可以直接循环遍历一次,然后如果可以加边就加边,跑floyd。
遇到操作3直接求解即可。

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mx=505;
int n,m,k,t,q;
int u,v,w,op;
int dist[mx][mx];
bool vis[mx];
inline int ans()
{
    int as=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
//          cout<<dist[i][j]<<' ';
            if(dist[i][j]==0x3f3f3f3f3f3f3f3f)
            {
                continue;
            }
            as+=dist[i][j];
        }
//      cout<<'\n';
     } 
    return as;
}
inline void floyd(int k)
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
        }
    }
}
signed main()//signed
{
//  ios::sync_with_stdio(false);
//  cin.tie(nullptr);
//  cout.tie(nullptr);
    freopen("as04.in","r",stdin);
    freopen("as04.out","w",stdout);
    cin>>n>>m;
    memset(dist,0x3f,sizeof(dist));
    for(int i=1;i<=n;i++)
    {
        dist[i][i]=0;
    }
    for(int i=1;i<=m;i++)
    {
        cin>>u>>v>>w;
        dist[u][v]=dist[v][u]=min(dist[u][v],w);
    }
    cin>>k>>t;
    for(int i=1;i<=k;i++)
    {
        cin>>u;
        vis[u]=true;
    }
    for(int i=1;i<=n;i++)
    {
        if(vis[i]==false)
        {
            continue;
         } 
        for(int j=1;j<=n;j++)
        {
            if(vis[j]==false)
            {
                continue;
            }
            dist[i][j]=min(dist[i][j],t);
        }
    }
    for(int i=1;i<=n;i++)
    {
        floyd(i);
    }
    cin>>q;
    for(int i=1;i<=q;i++)
    {
        cin>>op;
        if(op==1)
        {
            cin>>u>>v>>w;
            if(dist[u][v]>w)
            {
                dist[u][v]=dist[v][u]=w;
                floyd(u);
                floyd(v);
            }
         } 
        else if(op==2)
        {
            cin>>u;
            vis[u]=true;
            for(int j=1;j<=n;j++)
            {
                if(vis[j])
                {
                    if(dist[u][j]>t)
                    {
                        dist[u][j]=dist[j][u]=t;
                        floyd(j);
                    }
                }
            }
            floyd(u);
        }
        else
        {
//          cout<<"test"<<i<<'\n';
            cout<<ans()<<'\n';
        }
    }
    return 0;
}
/*

4 1
1 2 10
2 100
1 3
5
3
1 2 3 60
3
2 4
3


*/