ABC 276 ABCDE(dfs)

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;
}
posted @ 2022-11-08 18:28  高尔赛凡尔娟  阅读(30)  评论(0编辑  收藏  举报