SMU Summer 2024 Team Round 2(2024CCPC上海) AEJLM题解

A-无线网络整点栅格统计

思路:数据量不大,直接枚举每一个点,再枚举除上一步枚举到的点以外的所有点作为一个正方形的另一点,通过公式推出剩余两点看其是否满足题意,若满足则 arr[该点]++

AC代码:

#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,m;
int arr[101][101];

signed main() {
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n,m;
    cin>>n>>m;
    for(int i=0;i<=n;i++){
        for(int j=0;j<=m;j++){
            for(int x=0;x<=n;x++){
                for(int y=0;y<=m;y++){
                    int x1=i,y1=j,x2=x,y2=y;
                    int cha1 = x2-x1, cha2 = y2-y1;
                    int x3=x1+cha2, y3=y1-cha1;
                    int x4=x3+cha1, y4=y3+cha2;
                    if(x3>=0 && x3<=n && y3>=0 && y3<=m && x4>=0 && x4<=n && y4>=0 && y4<=m) arr[i][j]++;
                }
            }
        }
    }
    for(int i=0;i<=n;i++){
        for(int j=0;j<=m;j++) cout<<arr[i][j]-1<<" ";
        cout<<'\n';
    }
    return 0;
}

E-无线软件日

思路:把 s h a n g i 六个字母各有多少统计出来,注意a 和 h 的数量要除2,答案为各字母数目的最小值

AC代码:

#include<bits/stdc++.h>
using namespace std;
int n;
string s;
map<char,int>mp;

signed main() {
    cin >> n;
    cin >> s;
    for (int i = 0; i <= n-1; i++) {
        if(s[i] == 's' || s[i] == 'S') mp['s']++;
        if(s[i] == 'h' || s[i] == 'H') mp['h']++;
        if(s[i] == 'a' || s[i] == 'A') mp['a']++;
        if(s[i] == 'n' || s[i] == 'N') mp['n']++;
        if(s[i] == 'g' || s[i] == 'G') mp['g']++;
        if(s[i] == 'i' || s[i] == 'I') mp['i']++;
    }
    mp['h']=mp['h']/2;
    mp['a']=mp['a']/2;
    int ans = min(mp['a'],mp['h']);
    ans = min(ans,mp['s']);
    ans = min(ans,mp['n']);
    ans = min(ans,mp['i']);
    ans = min(ans,mp['g']);
    cout << ans << endl;
    return 0;
}

 J-极简合数序列

思路:答案实际上只有 -1 0 1 2 3 5种情况,选择长度为1 2 3 4的四种子串情况遍历判断即可,都没有则输出-1

AC代码:

#include<bits/stdc++.h>
using namespace std;
#define int long long
int t;
int a[1005];
int qz[1005];
bool st[10040];

signed main() {
    cin >> t;
    for (int i = 1; i <= 10000; i++) {
        for (int j = 2; j*j <= i; j++) {
            if(i % j == 0){
                st[i] = true;
                break;
            }
        }
    }st[2] = false;
    while(t--) {
        int n;
        cin >> n;
        bool stt = true;
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
            qz[i] = qz[i-1]+a[i];
            if(st[a[i]])stt = false;
        }
        if(n == 1) {
            if(st[a[1]]) {
                cout << 0 << endl;
            }else cout << -1 << endl;
            stt = false;
            continue;
        }
        else if(!stt) {
            cout << 0 << endl;
            continue;
        }
        else {
            for (int i = 1; i <= n-1; i++) {
                if(st[qz[i+1]-qz[i-1]] && stt)  {
                    cout << 1 << endl;
                    stt = false;
                    break;
                }
            }
            for (int i = 1; i <= n-2; i++) {
                if(st[qz[i+2]-qz[i-1]] && stt)  {
                    cout << 2 << endl;
                    stt = false;
                    break;
                }
            }
            for (int i = 1; i <= n-3; i++) {
                if(st[qz[i+3]-qz[i-1]] && stt)  {
                    cout << 3 << endl;
                    stt = false;
                    break;
                }
            }
            if(stt) cout << -1 << endl;
        }
    }
    return 0;
}

L-扩散模型

思路:(傅姐NB系列) 从下往上找,记录各个标记点往上走到根结点的路程,取最小值,若无则取-1

AC代码:


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=5e5+10;
#define PII pair<int,int>
signed main() {
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int T;
    cin>>T;
    while(T--){
        int n,m,k;
        cin>>n>>m>>k;
        bool g[n+10];
        int s[n+10];
        int p[n+10];
        int f[n+10];
        memset(f,0,sizeof f);
        memset(g,0,sizeof g);
        memset(s,0,sizeof s);
        memset(p,0,sizeof p);
        map<PII,int>mp;
        vector<int>a[n+10];
        queue<int>q;
        for(int i=1;i<=n;i++){
            int l;
            cin>>l;
            if(l==0) {
                q.push(i);
                continue;
            }
            while(l--){
                int h;
                cin>>h;
                f[h]=i;
                a[i].push_back(h);
            }
        }
        while(m--){
            int x,y;
            cin>>x>>y;
            mp[{x,y}]=1;
            mp[{y,x}]=1;
        }
        for(int i=0;i<k;i++) {
            int x;
            cin>>x;
            p[x]=1;
        }
        while(q.size()){
            int x=q.front();
            q.pop();
            if(g[x]) continue;
            g[x]=1;
            int sum=0;
            int ksum=0;
            int sum1=0;
            int now=N*10;
            for(int i=0;i<a[f[x]].size();i++){
                g[a[f[x]][i]]=1;
                if(p[a[f[x]][i]]){
                    sum++;
                    ksum+=s[a[f[x]][i]];
                    if(mp.count({f[x],a[f[x]][i]})!=0){
                        sum1++;
                        now=min(now,s[a[f[x]][i]]);
                    }
                }
            }
            if(sum==a[f[x]].size()) {
                if(sum!=0){
                    p[f[x]] = 1, s[f[x]] = min(ksum,now+1);
                }
                else{
                    p[f[x]] = 1, s[f[x]] = ksum;
                }

            }

            else {

                if(sum1>0) {
                    p[f[x]]=1;
                    s[f[x]]=now+1;
                }
                else p[f[x]]=0;
            }
            q.push(f[x]);
        }
        if(p[1]) cout<<s[1]<<endl;
        else cout<<-1<<endl;
    }
    return 0;
}

M-不共戴天

思路:(俞姐NB系列) 从6的样例中想到取成一个2x3的矩阵,蛙全部横着走,鸟全部竖着走,答案取两个cnt的min, 俞姐提供进一步思路 对于输入n  应该尽量凑出接近 sqrt(n) 的矩阵,从而使最后答案最大

AC代码:

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

int main(){
    int n;
    cin>>n;
    int t = sqrt(n);
    if(n-t*t>t) t++;

    int arr[t+105][t+105];
    memset(arr,0,sizeof arr);
    int cnt = 0;

    for(int i=1;i<=t+1;i++){
        for(int j=1;j<=t;j++) {
            arr[i][j] = ++cnt;
            if (cnt == n) goto aa;
        }
    }
    aa: ;

    vector<pair<int,int> > gua,niao;
    for(int i=1;i<=t+1;i++){
        for(int j=1;j<=t;j++){
            if(arr[i][j+1]) gua.push_back({arr[i][j],arr[i][j+1]});
            if(arr[i+1][j]) niao.push_back({arr[i][j],arr[i+1][j]});
        }
    }
    int cnm = min(gua.size(),niao.size());
    cout<<cnm<<endl;
    for(int i=0;i<cnm;i++) cout<<gua[i].first<<" "<<gua[i].second<<'\n';
    for(int i=0;i<cnm;i++) cout<<niao[i].first<<" "<<niao[i].second<<'\n';
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值