2023-3-19 QAU蓝桥杯训练-代码答案

1.

#include<bits/stdc++.h>
using namespace std;
string s[100005];
int main()
{
    int n=0;
    while(cin>>s[++n],s[n]!="."){
    }
    n--;
    if(n<=1){
        cout<<"Momo... No one is for you ...";
    }
    else
    if(n>=2&&n<=13){
        cout<<s[2]<<" is the only one for you...";
    }
    else cout<<s[2]<<" and "<<s[14]<<" are inviting you to dinner...";
    
}

2.

#include<bits/stdc++.h>
using namespace std;
int w[100005];
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>w[i];
        w[i]+=w[i-1];
    }
    while(m--)
    {
        int l,r;
        cin>>l>>r;
        cout<<w[r]-w[l-1]<<"\n";
    }
}

3

#include<bits/stdc++.h>
using namespace std;
int w[100005];
void insert(int l,int r,int c)
{
    w[r+1]-=c;
    w[l]+=c;
}
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        int x;
        cin>>x;
        insert(i,i,x);
    }
    while(m--)
    {
        int l,r,c;
        cin>>l>>r>>c;
        insert(l,r,c);
    }
    int res=0;
    for(int i=1;i<=n;i++){
        res+=w[i];
        cout<<res<<" ";
    }
}

4

#include<bits/stdc++.h>
using namespace std;
int n,cnt,primes[100005];
bool st[1000005];
void get_prime(int n)
{
    for(int i=2;i<=n;i++){
        if(!st[i])primes[cnt++]=i;
        for(int j=0;primes[j]<=n/i;j++){
            st[primes[j]*i]=1;
            if(i%primes[j]==0)break;
        }
    }
}
int main()
{
    get_prime(100000);
    int t;
    cin>>t;
    while(t--)
    {
        int l,r;
        cin>>l>>r;
        bool ok=false;
        for(int i=0;primes[i]<=r;i++){
            if(primes[i]>=l){
                cout<<primes[i]<<" ";
                ok=true;
            }
        }
        if(!ok)cout<<"-1 ";
        cout<<"\n";
    }
}

5

#include<bits/stdc++.h>
using namespace std;
int qmi(int a,int b,int mod)
{
    int res=1;
    while(b)
    {
        if(b&1)res=(long long)res*a%mod;
        a=(long long)a*a%mod;
        b>>=1;
    }
    return res;
}
int main()
{
    int a,b,m;
    cin>>a>>b>>m;
    cout<<qmi(a,b,m);
}

6

#include<bits/stdc++.h>
using namespace std;
struct node{
    int a,b,c;
    bool operator<(const node&t)const{
        return c<t.c;
    }
}edge[1000005];
int p[1000005];
int find(int x)
{
    if(p[x]!=x)p[x]=find(p[x]);
    return p[x];
}
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        edge[i]={a,b,c};
    }
    sort(edge+1,edge+m+1);
    int res=0;
    for(int i=1;i<=n;i++)p[i]=i;
    for(int i=1;i<=m;i++){
        int a=edge[i].a;
        int b=edge[i].b;
        int c=edge[i].c;
        if(find(a)==find(b))continue;
        p[find(a)]=find(b);
        res+=c;
    }
    cout<<res;
}

7

#include<bits/stdc++.h>
using namespace std;
int n;
char x;
double a[1005],sum;
int main()
{
	cin>>n>>x;
	int s=1;
	int i;
	for(i=3;;i=i+2)
	{
		if(s+i*2<=n)
		s+=i*2;
		else 
		break;
	}
	i=i-2;
	int u=i;	
	
	for(u;u>0;u=u-2)
	{
		for(int j=(i-u)/2;j>0;j--)
		cout<<" ";
		for(int j=u;j>0;j--)
		cout<<x;
		cout<<endl;
	}
	for(u=3;u<i+2;u=u+2)
	{
		for(int j=(i-u)/2;j>0;j--)
		cout<<" ";
		for(int j=u;j>0;j--)
		cout<<x;
		cout<<endl;
	}
	cout<<n-s;
}

8

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    double pi=3.14159265358979323846;
    while(t--)
    {
        int r,R,H,v;
        cin>>r>>R>>H>>v;
        double ll=0,rr=H;
        while(ll<rr)
        {
            double mid=(ll+rr)/2;
            double tR=r+(R-r)*mid/H;
            if(1.0/3*pi*(mid)*(tR*tR+tR*r+r*r)>=v)rr=mid-1e-10;
            else ll=mid+1e-10;
        }
        printf("%.6lf\n",(ll+rr)/2);
    }
}

9

#include<bits/stdc++.h>
using namespace std;
int n;
string s;
int main()
{
    cin>>n;
    getchar();
    getline(cin,s);
    int m=s.length();
    for(int i=1;i<=n;i++)s+=" ";
    for(int i=1;i<=n;i++){
    	for(int j=(m+(n-1))/n;j>=1;j--){
    		cout<<s[(j-1)*n+(i-1)];
		}
		cout<<"\n";
	}
}

10

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
int n,m,s,d;
int w[505];
int e[250005],ne[250005],h[505],len[250005],idx;
void add(int a,int b,int c)
{
	e[idx]=b;len[idx]=c;ne[idx]=h[a];h[a]=idx++;
}
int dist[505];
int path[505],people[505],score[505];
bool st[505];
void dijkstra(int first,int finsh)
{
	priority_queue<PII,vector<PII>,greater<PII>>heap;
	heap.push({0,first});
	memset(dist,0x3f,sizeof dist);
	path[first]=-1;
	people[first]=w[first];
	score[first]=1;
	dist[first]=0;
	while(!heap.empty())
	{
		int u=heap.top().second;
		heap.pop();
		if(st[u])continue;
		st[u]=true;
		for(int i=h[u];i!=-1;i=ne[i])
		{
			int j=e[i];
			if(dist[j]>dist[u]+len[i]){
				dist[j]=dist[u]+len[i];
				path[j]=u;
				people[j]=people[u]+w[j];
				score[j]=score[u];
				heap.push({dist[j],j});
			}
			else if(dist[j]==dist[u]+len[i]){
				if(people[j]<people[u]+w[j]){
					people[j]=people[u]+w[j];
					path[j]=u;
				}
				score[j]+=score[u];
				heap.push({dist[j],j});
			}
		}
	}
}
int main()
{
	cin>>n>>m>>s>>d;
	for(int i=0;i<n;i++)cin>>w[i];
	memset(h,-1,sizeof h);
	for(int i=1;i<=m;i++){
		int a,b,c;
		cin>>a>>b>>c;
		add(a,b,c);
		add(b,a,c);
	}
	dijkstra(s,d);
	cout<<score[d]<<" "<<people[d]<<"\n";
	vector<int>res;
	while(d!=-1){
		res.push_back(d);
		d=path[d];
	}
	for(int i=res.size()-1;i>=0;i--){
		cout<<res[i];
		if(i)cout<<" ";
	}
}

11

#include<bits/stdc++.h>
using namespace std;
int n;
int p[10005];
int find(int x)
{
    if(p[x]!=x)p[x]=find(p[x]);
    return p[x];
}
int main()
{
	int t;
    cin>>t;
    map<int,int>mp;
    while(t--)
    {
        int k;
        cin>>k;
        int first;
        cin>>first;
        if(!mp[first])mp[first]=first,p[first]=first;
        for(int i=1;i<=k-1;i++){
            int x;
            cin>>x;
            if(!mp[x])mp[x]=1,p[x]=x;
            p[find(x)]=find(first);
        }
    }
    cout<<mp.size()<<" ";
    set<int>s;
    for(auto eg:mp){
        if(s.find(find(eg.first))==s.end()){
            s.insert(find(eg.first));
        }
    }
    cout<<s.size()<<"\n";
    int m;
    cin>>m;
    while(m--)
    {
        int a,b;
        cin>>a>>b;
        if(find(a)==find(b))cout<<"Y";
        else cout<<"N";
        cout<<"\n";
    }
}

12

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>PII;
int n,m;
int e[1000005],ne[1000005],h[1005],w[1000005],idx;
void add(int a,int b,int c)
{
    e[idx]=b;w[idx]=c;ne[idx]=h[a];h[a]=idx++;
}
int dist[1005];
bool st[1005];
void dijkstra()
{
    priority_queue<PII,vector<PII>,greater<PII>>heap;
    heap.push({0,1});
    memset(dist,0x3f,sizeof dist);
    dist[1]=0;
    while(!heap.empty())
    {
        int u=heap.top().second;heap.pop();
        if(st[u])continue;
        st[u]=true;
        for(int i=h[u];i!=-1;i=ne[i]){
            int j=e[i];
            if(dist[j]>dist[u]+w[i]){
                dist[j]=dist[u]+w[i];
                heap.push({dist[j],j});
            }
        }
    }
}
int main()
{
    cin>>n>>m;
    memset(h,-1,sizeof h);
    for(int i=1;i<=m;i++){
        int a,b,c;
        cin>>a>>b>>c;
        if(a<b)add(a,b,c);
        else if(a>b)add(b,a,c);
    }
    dijkstra();
    cout<<dist[n];
}

13

#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> PII;
int n,m,q;
int e[200005],ne[200005],h[100005],idx,w[200005];
void add(int a,int b,int c)
{
	e[idx]=b;w[idx]=c;ne[idx]=h[a];h[a]=idx++;
}
struct node{
	int a,b,c,d;
}edge[200005];
int dist1[100005];
int dist2[100005];
bool st[100005];
void dijkstra(int first,int dist[])
{
	memset(dist,0x3f,sizeof dist1);
	dist[first]=0;
	memset(st,0,sizeof st);
	priority_queue<PII,vector<PII>,greater<PII>>heap;
	heap.push({dist[first],first});
	while(!heap.empty())
	{
		int u=heap.top().second;heap.pop();
		if(st[u])continue;
		st[u]=true;
		for(int i=h[u];i!=-1;i=ne[i])
		{
			int j=e[i];
			if(dist[j]>dist[u]+w[i]){
				dist[j]=dist[u]+w[i];
				heap.push({dist[j],j});
			}
		}
	}
}
int a[100005];
signed main()
{
	cin>>n>>m>>q;
	memset(h,-1,sizeof h);
	for(int i=1;i<=m;i++){
		int a,b,c,d;
		cin>>a>>b>>c>>d;
		edge[i]={a,b,c,d};
	}
	for(int i=1;i<=m;i++){
		int a=edge[i].a;
		int b=edge[i].b;
		int c=edge[i].c;
		add(a,b,c);
	}
	dijkstra(1,dist1);
	memset(h,-1,sizeof h);idx=0;
	for(int i=1;i<=m;i++){
		int a=edge[i].a;
		int b=edge[i].b;
		int c=edge[i].d;
		add(b,a,c);
	}
	dijkstra(n,dist2);
	multiset<int>S;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		int x=a[i];
		if(dist1[i]!=0x3f3f3f3f3f3f3f3f&&dist2[i]!=0x3f3f3f3f3f3f3f3f){
			S.insert(dist1[i]+(dist2[i]+x-1)/x);
		}
	}
	while(q--)
	{
		int x,k;
		cin>>x>>k;
		if(dist1[x]!=0x3f3f3f3f3f3f3f3f&&dist2[x]!=0x3f3f3f3f3f3f3f3f){
			S.erase(S.find(dist1[x]+(dist2[x]+a[x]-1)/a[x]));
			a[x]=k;
			S.insert(dist1[x]+(dist2[x]+a[x]-1)/a[x]);
		}
		cout<<*S.begin()<<"\n";
	}
} 

14

#include<bits/stdc++.h>
using namespace std;
int n,m,k;
long long num[100005];
long long pin[105][10005];
long long q[100005];
int b[105];
int come[10005];
bool flag;
bool ed[105];
void dfs(int u,int finish,int temp)
{
	if(flag)return;
	if(u==finish-1)
	{
		for(int i=1;i<=temp-1;i++)
		{
			cout<<come[i];
			if(i!=temp-1)
			cout<<" ";
		}
		flag=1;
		return;
	}
	for(int i=1;i<=m;i++)
	{
		bool ok=true;
		int cnt=1;
		if(!ed[i])
		{
			for(int j=u+1;j<=u+b[i];j++)
			{
				if(pin[i][cnt]*q[j]==num[j])
				{
					cnt++;
					continue;
				}
				ok=false;
			}
			if(ok)
			{
				ed[i]=1;
				come[temp]=i;
				dfs(u+b[i]-1,finish,temp+1);
				ed[i]=0;
			}
		}
	}
}
int main()
{
	cin>>n;
	q[0]=1;
	for(int i=1;i<=n;i++)
	{
		q[i]=q[i-1]*131;
	}
	for(int i=1;i<=n;i++)
	{
		cin>>num[i];
		num[i]=num[i]*q[i];
	}
	cin>>m;
	for(int i=1;i<=m;i++)
	{
		int k;
		cin>>k;
		b[i]=k;
		for(int j=1;j<=k;j++)
		cin>>pin[i][j];
	}
	dfs(0,n,1);
}

15

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
int e[200005],ne[200005],idx,w[200005],h[205];
void add(int a,int b,int c)
{
	e[idx]=b;w[idx]=c;ne[idx]=h[a];h[a]=idx++;
}
int dist[205],score[205];
bool st[205];
int num[205],kil[205],path[205];
map<string,int>mp;
map<int,int>people;
void dijkstra()
{
	memset(dist,0x3f,sizeof dist);
	dist[1]=0;
	path[1]=-1;
	priority_queue<PII,vector<PII>,greater<PII>>heap;
	heap.push({dist[1],1});
	score[1]=1;
	while(!heap.empty())
	{
		int u=heap.top().second;heap.pop();
		if(st[u])continue;
		st[u]=true;
		for(int i=h[u];i!=-1;i=ne[i]){
			int j=e[i];
			if(dist[j]>dist[u]+w[i]){
				dist[j]=dist[u]+w[i];
				kil[j]=kil[u]+people[j];
				num[j]=num[u]+1;
				path[j]=u;
				score[j]=score[u];
				heap.push({dist[j],j});
			}
			else if(dist[j]==dist[u]+w[i]){
				if(num[j]<num[u]+1){
					num[j]=num[u]+1;
					kil[j]=kil[u]+people[j];
					path[j]=u;
				}
				else if(num[j]==num[u]+1){
					if(kil[u]+people[j]>kil[j]){
						kil[j]=kil[u]+people[j];
						path[j]=u;
					}
				}
				score[j]+=score[u];
				heap.push({dist[j],j});
			}
		}
	} 
}
int main()
{
	int cnt=0;
	int n,k;
	cin>>n>>k;
	string my,enemy;
	cin>>my>>enemy;
	map<int,string>get;
	mp[my]=++cnt;
	get[cnt]=my;
	mp[enemy]=++cnt;
	get[cnt]=enemy;
	for(int i=1;i<=n-1;i++){
		string s;
		int m;
		cin>>s>>m;
		if(!mp[s])mp[s]=++cnt;
		get[mp[s]]=s;
		people[mp[s]]=m;
	}
	memset(h,-1,sizeof h);
	for(int i=1;i<=k;i++){
		string a,b;
		int c;
		cin>>a>>b>>c;
		add(mp[a],mp[b],c);
		add(mp[b],mp[a],c);
	}
	dijkstra();
	int res=2;
	vector<string>ans;
	while(res!=-1){
		ans.push_back(get[res]);
		res=path[res];
	}
	for(int i=ans.size()-1;i>=0;i--){
		cout<<ans[i];
		if(i!=0)cout<<"->";
	}
	cout<<"\n";
	cout<<score[2]<<" "<<dist[2]<<" "<<kil[2];
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值