2021牛客寒假算法基础集训营6

题目连接

A回文括号序列计数

懵逼的签到题

#include <bits/stdc++.h>
#define pb push_back//vector,deque
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N=1e5+5;
void solve()
{
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int res=n==0?1:0;
        cout<<res;
        cout<<'\n';
    }
}
int main() {
    solve();
    return 0;
}

C末三位

签到题

#include <bits/stdc++.h>
#define pb push_back//vector,deque
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N=1e5+5;
ll q_pow(ll a,ll b)
{
    ll res=1;
    while(b){
        if(b&1)res=res*a%1000;
        a=a*a%1000;
        b>>=1;
    }
    return res;
}
void solve()
{
    ll n;
    while(scanf("%lld",&n)!=EOF){
        ll res=q_pow(5,n);
        printf("%03lld\n",res);
    }
}
int main() {
    solve();
    return 0;
}
/*
5
125
6
625
7
125
*/ 

D划数

实际上只需要把cnt(只有一个!!!)给踢出去,剩下求和取模即可。wa了好几发,最后发现是等于cnt的我全剔除出去了。

#include <bits/stdc++.h>
#define pb push_back//vector,deque
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N=2e6+5;
int a[N];
void solve()
{
    int n,cnt;
    while(scanf("%d%d",&n,&cnt)!=EOF){
        int x,cot=0;
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        bool flag=true;
        int res=0;
        if(n==2){
            if(a[1]==cnt)printf("%d\n",a[2]);
            else printf("%d\n",a[1]);
            continue;
        }
        for(int i=1;i<=n;i++){
            if(flag&&a[i]==cnt){
                a[i]=0;flag=false;
            }
            res+=a[i];
            res%=11;
        }
        printf("%d\n",res);
    }
}
int main() {
    solve();
    return 0;
}

I贪吃蛇

签到题

#include <bits/stdc++.h>
#define pb push_back//vector,deque
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N=1e5+5;
string e[105];
struct node{
    int x,y,step;
};
queue<node>q;
int res=-1,dir[4][2]={0,1,1,0,0,-1,-1,0},vis[105][105];
void bfs(int ex,int ey,int n,int m)
{
    while(q.size()){
        node h=q.front();
        q.pop();
        if(h.x==ex-1&&h.y==ey-1){
            res=h.step*100;
            break;
        }
        for(int i=0;i<4;i++){
            int xx=dir[i][0]+h.x;
            int yy=dir[i][1]+h.y;
            if(xx>=0&&xx<n&&yy>=0&&yy<m&&!vis[xx][yy]&&e[xx][yy]!='#'){
                vis[xx][yy]=1;
                q.push({xx,yy,h.step+1});
            }
        }
    }
}
void solve()
{
    int n,m,sx,sy,ex,ey;
    cin>>n>>m>>sx>>sy>>ex>>ey;
    for(int i=0;i<n;i++)cin>>e[i];
    q.push({sx-1,sy-1,0});
    vis[sx-1][sy-1]=1;
    bfs(ex,ey,n,m);
    cout<<res<<'\n';
}
int main() {
    solve();
    return 0;
} 

J天空之城

最小生成树板子,不过需要注意的是先把字符串映射成节点数字。然后就行了。最后NO的情况实际就是cnt!=n-1。因为这样他走遍不了所有城市。

#include <bits/stdc++.h>
#define pb push_back//vector,deque
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N=4e5+5;
struct node{
    int u,v;
    ll val;
    bool operator<(const node&other){//重载小于运算符 
		return val<other.val;
	}
}e[N];
int pre[N],cnt,n;
void add(int u,int v,ll val)
{
    cnt++;
	e[cnt].u=u;
	e[cnt].v=v;
	e[cnt].val=val;
}
int find(int x)//并查集 
{
	return x==pre[x]?x:pre[x]=find(pre[x]);
}
void solve()
{
    int n,q,cot=0;
    while(cin>>n>>q){
        unordered_map<string,int>mp;
        string s,t;
        cot=0;cnt=0;
        ll val;
        cin>>s;
        mp[s]=++cot;
        for(int i=0;i<q;i++){
            cin>>s>>t>>val;
            if(mp.count(s)==0)mp[s]=++cot;
            if(mp.count(t)==0)mp[t]=++cot;
            add(mp[s],mp[t],val);
        }
        for(int i=1;i<=n;i++)pre[i]=i;
        ll res=0;
        int edge=0;
        sort(e+1,e+1+cnt);
        for(int i=1;i<=cnt;i++){
            int fx=find(e[i].u),fy=find(e[i].v);
            if(fx==fy)continue;
            pre[fx]=fy;
            res+=e[i].val;
            if(++edge==n-1)break;
        }
        if(edge==n-1)cout<<res;
        else cout<<"No!";
        cout<<'\n';
        // for(auto v:mp){
        //     cout<<v.second<<'\n';
        // }
    }
}
int main() {
    solve();
    return 0;
}
/*
5 5
Orz
Ada Aed 5
Orz Ada 6
Apq Aed 8
Akk Apq 12
Aed Orz 3

5 5
Orz
Ada Aed 5
Orz Ada 6
Ada Aed 8
Akk Apq 12
Aed Orz 3
*/

猜你喜欢

转载自blog.csdn.net/qq_43566782/article/details/114032189