(GPLT)上海理工大学校内选拔赛(重现赛)

题目链接

A.

记录最大值和个数,和次大值
遍历数组,不是最大值,输出该数与最大值的和
是最大值,则讨论最大值个数,若只有一个,输出该数与次大值的和,否则输出最大值的两倍

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N=1e5+10;

void solve() {
    int n;cin>>n;
    vector<int>a(n+1);
    int ma=-1,cnt_ma=0,tm=-1;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        if(a[i]>ma){
            tm=ma;
            ma=a[i];
            cnt_ma=1;
        }else if(a[i]==ma){
            cnt_ma++;
        }else if(a[i]>tm){
         //   cout<<"tm="<<a[i]<<'\n';
            tm=a[i];
        }
    }
   // cout<<ma<<' '<<tm;
    for(int i=1;i<=n;i++){
        if(a[i]<ma){
            cout<<a[i]+ma;
        }else if(a[i]==ma){
            if(cnt_ma>1){
                cout<<2*ma;
            }else cout<<a[i]+tm;
        }
        if(i!=n)cout<<' ';
    }
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int left=1;
    //cin>>left;
    while(left--){
        solve();
    }
}

B.

快速幂即可

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N=1e5+10;

int ksm(int a,int b,int p){
    int ans=1;
    while(b){
        if(b&1)ans=(ans*a)%p;
        a=(a*a)%p;
        b/=2;
    }
    return ans%p;
}
void solve() {
    int n;cin>>n;
    cout<<ksm(n,n,n+2);
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int left=1;
    //cin>>left;
    while(left--){
        solve();
    }
}

C.

从字符串里处理出字符串并计数,用map实现

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N=1e5+10;

void solve() {
    string s;getline(cin,s);
    //cout<<s;
    map<string,int>mp;
    string tmp="";
    int sum=0;
    for(int i=0;i<s.size();i++){
        if(s[i]>='a'&&s[i]<='z'||s[i]>='A'&&s[i]<='Z')tmp+=s[i];
        else if(s[i]>='0'&&s[i]<='9')sum=sum*10+(s[i]-'0');
        else if(s[i]==' '){
            if(sum){
                mp[tmp]+=sum;
                sum=0;
                tmp="";
            }
        }
        if(i==s.size()-1){
            mp[tmp]+=sum;
        }
    }
    int tt;cin>>tt;
    string t;
    while(tt--){
        cin>>t;
        cout<<mp[t]<<'\n';
    }
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int left=1;
    //cin>>left;
    while(left--){
        solve();
    }
}

D.

K数列就是进制的定义
类比二进制串,想象一个k进制串
把n先转成二进制串,再转为k进制串即可

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N=1e5+10;

const int mod=1e9+7;


void solve() {
    int n,k;cin>>n>>k;
    int ans=0,t=1;
    while(n){
        if(n&1)ans=(ans+t)%mod;
        t=(t*k)%mod;
        n/=2;
    }
    cout<<ans;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int left=1;
    //cin>>left;
    while(left--){
        solve();
    }
}

E.

bfs

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N=300+10;

int g[N][N];
int n,m,k,a,b,c,d;
int vis1[N][N],vis2[N][N];
struct node{
    int x,y,dep;
};
int dx[10]={-2,-2,-1,-1,2,2,1,1};
int dy[10]={1,-1,2,-2,1,-1,2,-2};
int cx[10]={-1,-1,0,0,1,1,0,0};
int cy[10]={0,0,1,-1,0,0,1,-1};
int bfs1(){
    queue<node>q;
    while(q.size())q.pop();
    if(a==c&&b==d)return 0;
    q.push({a,b,0});
    vis1[a][b]=1;
    while(q.size()){
        auto t=q.front();q.pop();
        for(int i=0;i<8;i++){
            int xx=t.x+dx[i],yy=t.y+dy[i];
            if(xx>=1&&xx<=n&&yy>=1&&yy<=m){
                if(vis1[xx][yy]||g[xx][yy])continue;
                vis1[xx][yy]=1;
                if(xx==c&&yy==d)return t.dep+1;
                q.push({xx,yy,t.dep+1});
            }
        }
    }
    return -1;
}
int bfs2(){
    queue<node>q;
    while(q.size())q.pop();
    if(a==c&&b==d)return 0;
    q.push({a,b,0});
    vis2[a][b]=1;
    while(q.size()){
        auto t=q.front();q.pop();
        for(int i=0;i<8;i++){
            int xx=t.x+dx[i],yy=t.y+dy[i];
            int tx=t.x+cx[i],ty=t.y+cy[i];
            if(xx<1||xx>n||yy<1||yy>m)continue;
            if(g[xx][yy]||g[tx][ty])continue;
            if(vis2[xx][yy])continue;
            vis2[xx][yy]=1;
            if(xx==c&&yy==d)return t.dep+1;
            q.push({xx,yy,t.dep+1});
        }
    }
    return -1;
}
void solve() {
    for(int i=1;i<N;i++){
        for(int j=1;j<N;j++){
            g[i][j]=0;vis1[i][j]=0;vis2[i][j]=0;
        }
    }
    cin>>n>>m>>k>>a>>b>>c>>d;
    while(k--){
        int x,y;cin>>x>>y;
        g[x][y]=1;
    }
    cout<<bfs1()<<' '<<bfs2()<<'\n';
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int left=1;
    cin>>left;
    while(left--){
        solve();
    }
}

F.

最大配最小

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N=1e5+10;


void solve() {
    int n;cin>>n;
    vector<int>a(n+1),b(n+1);
    for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=1;i<=n;i++)cin>>b[i];
    sort(a.begin()+1,a.end());
    sort(b.begin()+1,b.end());
    int ans=0;
    for(int i=1;i<=n;i++){
        ans+=a[i]*b[n-i+1];
    }
    cout<<ans<<'\n';
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int left=1;
    cin>>left;
    while(left--){
        solve();
    }
}

J.

dij板子

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N=1e5+10;

vector<pair<int,int>>g[N];
#define inf 0x3f3f3f3f3f3f3f3f
int dist[N];
bool st[N];
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;
void dij(){
    memset(dist,inf,sizeof(dist));
    dist[1]=0;
    q.push({0,1});
    while(q.size()){
        auto k=q.top();q.pop();
        int ver=k.second,distance=k.first;
        if(st[ver])continue;
        st[ver]=true;
        for(int i=0;i<g[ver].size();i++){
            int j=g[ver][i].first;
            if(dist[j]>distance+g[ver][i].second){
                dist[j]=distance+g[ver][i].second;
                q.push({dist[j],j});
            }
        }
    }
}
void solve() {
    int n,m;cin>>n>>m;
    for(int i=1,v,w,d;i<=m;i++){
        cin>>v>>w>>d;
        g[v].push_back({w,d});
        g[w].push_back({v,d});
    }
    dij();
    int tt;cin>>tt;
    while(tt--){
        int s,t;cin>>s>>t;
        cout<<dist[s]+dist[t]<<'\n';
    }
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int left=1;
    //cin>>left;
    while(left--){
        solve();
    }
}

posted @ 2024-01-31 17:19  WW爆米花  阅读(15)  评论(0编辑  收藏  举报