首页 > 其他分享 >ABC 276 ABCDE(dfs)

ABC 276 ABCDE(dfs)

时间:2022-11-08 18:49:44浏览次数:37  
标签:typedef ABC const LL ABCDE cin dfs long tie

A - Rightmost

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL INF=1e9;
const LL N=5000200,M=2002;
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        string s;
        cin>>s;
        s=" "+s;
        LL idx=-1;
        for(LL i=1;i<s.size();i++)
        {
            if(s[i]=='a') idx=max(idx,i);
        }
        cout<<idx<<endl;
    }
    return 0;
}

B - Adjacency List

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL INF=1e9;
const LL N=5000200,M=2002;
vector<LL> g[N];
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        LL n,m;
        cin>>n>>m;
        for(int i=1;i<=m;i++)
        {
            LL x,y;
            cin>>x>>y;
            g[x].push_back(y);
            g[y].push_back(x);
        }
        for(int i=1;i<=n;i++)
        {
            sort(g[i].begin(),g[i].end());
            cout<<g[i].size()<<" ";
            for(int j=0;j<g[i].size();j++)
                cout<<g[i][j]<<" ";
            cout<<endl;
        }
    }
    return 0;
}

C - Previous Permutation

题目大意:

给定一个长度为n的全排列中的某一个,问我们它之前的那个全排列是啥?
Sample Input 1 
3
3 1 2
Sample Output 1 
2 3 1

(1,2,3)
(1,3,2)
(2,1,3)
(2,3,1)
(3,1,2)
(3,2,1)

这个题目一开始我想用暴力跑,后来发现这数字竟然这么大!(普及:next_permutation大概只能跑【9-11】个数字的全排列哦~)
我寻思着不能连个C都写不出来吧,看了下样例,发现欸嘿! 有规律,规律详情见代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL INF=1e9;
const LL N=5000200,M=2002;
LL a[N];
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        LL n;
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
        }
        LL idx=-1;
        vector<LL> v;
        for(int i=n;i>=1;i--)
        {
            if(a[i]<a[i-1])
            {
                idx=i-1;
                v.push_back(a[i]);
                v.push_back(a[idx]);
                break;
            }
            else v.push_back(a[i]);
        }
        sort(v.rbegin(),v.rend());
        LL flag;
        for(int i=0;i<v.size();i++)
        {
            if(v[i]<a[idx])
            {
                flag=v[i];
                break;
            }
        }
        for(int i=1;i<idx;i++)
        {
            cout<<a[i]<<" ";
        }
        cout<<flag<<" ";
        for(int i=0;i<v.size();i++)
        {
            if(v[i]!=flag)
                cout<<v[i]<<" ";
        }
        cout<<endl;
    }
    return 0;
}

D - Divide by 2 or 3

题目大意:

给定长度为n的数列a,对每个数字可以进行/2或者/3的操作(在取模为0的情况下进行)

问我们最少多少次步骤可以让数组a里面的数字全部都变得一模一样?
Sample Input 1 
3
1 4 3
Sample Output 1  
3

输麻了这题,思路完全一样,但是就是一开始没有直接变成1。卡我半天,找不出bug,最后换个写法就过了
【到现在还不知道原写法哪里wa了】

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL INF=1e9;
const LL N=5000200,M=2002;
LL a[N];
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        LL n;
        cin>>n;
        LL gd;
        for(LL i=1;i<=n;i++)
        {
            cin>>a[i];
            if(i==1) gd=a[i];
            else gd=__gcd(gd,a[i]);
        }
        //cout<<gd<<" ";
        LL sum=0;
        bool flag=true;
        for(LL i=1;i<=n;i++)
        {
            a[i]/=gd;
            while(a[i]%3==0) a[i]/=3,sum++;
            while(a[i]%2==0) a[i]/=2,sum++;
            if(a[i]!=1) flag=false;
        }
        if(flag==false) cout<<"-1"<<endl;
        else cout<<sum<<endl;
    }
    return 0;
}

E - Round Trip

题目大意:

给定一个长度为n*m的.#矩阵,问我们能不能从起点S走一个步数>=4的圈圈回到原地?

注意,走的这条路其中的点不能够走两次。
Sample Input 1 
4 4
....
#.#.
.S..
.##.
Sample Output 1 
Yes
The path (3,2)→(2,2)→(1,2)→(1,3)→(1,4)→(2,4)→(3,4)→(3,3)→(3,2) satisfies the conditions.

这个题目要格外注意图的存储,一般的二维存不下,采用vector a[N]可以避免RE的情况。
【注意状态的判定以及回归】

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL INF=1e9;
const LL N=1000200,M=2002;
vector<char> a[N];
vector<LL> st[N];
LL n,m,sx,sy;
LL dx[]={-1,0,0,1},dy[]={0,1,-1,0};
bool flag=false;
void dfs(LL x,LL y,LL num)
{
    if(flag==true) return ;

    st[x][y]=num;
    for(LL i=0;i<4;i++)
    {
        LL xx=dx[i]+x,yy=dy[i]+y;
        if(xx<1||xx>n||yy<1||yy>m||a[xx][yy]=='#') continue;
        if(xx==sx&&yy==sy&&abs(st[x][y]-st[sx][sy])>=3) flag=true;
        if(st[xx][yy]!=0) continue;
        dfs(xx,yy,num+1);
    }
    return ;
}
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        flag=false;
        cin>>n>>m;
        for(LL i=1;i<=n;i++)
        {
            a[i].push_back('#');
            st[i].push_back(0);
            for(LL j=1;j<=m;j++)
            {
                char op;
                cin>>op;
                a[i].push_back(op);
                if(a[i][j]=='S') sx=i,sy=j;
                st[i].push_back(0);
            }
        }
        dfs(sx,sy,1);
        if(flag==true) cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
    }
    return 0;
}

标签:typedef,ABC,const,LL,ABCDE,cin,dfs,long,tie
From: https://www.cnblogs.com/Vivian-0918/p/16866991.html

相关文章

  • HDFS Java API
    HDFS(HadoopDistributedFileSystem)是Hadoop项目的核心子项目,在大数据开发中通过分布式计算对海量数据进行存储与管理。它基于流数据模式访问和处理超大文件的需求而开发......
  • ABC248G
    套路题。设\(M=10^5\)。设\(f(i)\)表示路径的\(\gcd\)恰好为\(i\)时候的贡献,则答案为\(\sum_{i=1}^Mi\timesf(i)\)。套路的,将限制变成路径的\(\gcd\)为\(i......
  • 第2-1-4章 SpringBoot整合FastDFS文件存储服务
    目录5SpringBoot整合5.1操作步骤5.2项目依赖5.3客户端开发5.3.1FastDFS配置5.3.2FastDFS配置类5.3.3文件工具类5.3.4文件上传配置5.3.5配置Swagger25.3.6API接口......
  • ABC248F
    设\(f_{i,j,0/1}\)表示考虑前\(i\)列,删去了\(j\)条边,目前上方和下方连不连通的方案数。则有转移:\[f_{i,j,1}=f_{i-1,j,1}+3\timesf_{i-1,j-1,1}+f_{i-1,j,0}\]\[......
  • [数学记录]abc276G Count Sequences
    题意:对满足以下条件的序列计数,膜\(998244353\):\(0\leqa_0\leqa_1\cdots\leqa_n\leqm\)$a_i\not\equiva_j\pmod3$这题的难点在于发现它是简单题。想了......
  • ACWING 第 76 场周赛 ABC
    https://www.acwing.com/activity/content/2589/这场周赛也很简单,除了C我在赛场上写的时候有点小bug,赛时没改出来,哎,真废啊4713.反转字符串#include<bits/stdc++.h>u......
  • dfs 序
    dfs序可以\(O(1)\)判断书上两个点的从属关系TreeQueries题面翻译给你一个以\(1\)为根的有根树.每回询问\(k\)个节点\({v_1,v_2\cdotsv_k}\)求出是否有一条以根节......
  • 第2-1-3章 docker-compose安装FastDFS,实现文件存储服务
    目录4docker-compose安装FastDFS4.1docker-compose-fastdfs.yml4.2nginx.conf4.3storage.conf4.4测试4docker-compose安装FastDFS需要注意:network_mode必须是ho......
  • dfs序及其应用
    dfs序前置知识:线段树,树状数组,LCA,树的存储,树的基础问题类型1.点修改,子树查询2.子树修改,点查询3.子树修改,子树查询4.链修改,点查询5.点修改,链查询6.链修改,子树查询7.子树修改,......
  • 题解 [ABC259Ex] Yet Another Path Counting
    首先,每种颜色互不干扰,因此考虑对每种颜色统计答案。有两种解法:枚举起始格子\((x,y)\)和结尾格子\((z,w)\),由组合数易知共有\(\binom{z-x+w-y}{z-x}\)种路径。时间......