SMU Spring 2023 Trial Contest Round 9

发布时间 2023-04-23 17:24:37作者: bible_w

SMU Spring 2023 Trial Contest Round 9

A - Wrong Subtraction

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>PII;
typedef pair<string,int>PSI;
const int N=1e5+5,INF=0x3f3f3f3f,Mod=1e6;

const double eps=1e-6;
typedef long long ll;

int n,k;

int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n>>k;
    while(k--){
        int m=n%10;
        if(m)n--;
        else n/=10;
    }
    cout<<n;
    return 0;
}
View Code

 

B - Two-gram

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>PII;
typedef pair<string,int>PSI;
const int N=1e5+5,INF=0x3f3f3f3f,Mod=1e6;

const double eps=1e-6;
typedef long long ll;

int n,ma;
string ans;
unordered_map<string,int>mp;
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n;
    string s;
    cin>>s;
    for(int i=0;i<n-1;++i){
        string a="";
        a+=s[i];
        a+=s[i+1];
        mp[a]++;
        if(mp[a]>ma){
            ma=mp[a];
            ans=a;
        }
    }
    cout<<ans;
    return 0;
}
View Code

 

C - Less or Equal

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>PII;
typedef pair<string,int>PSI;
const int N=2e5+5,INF=0x3f3f3f3f,Mod=1e6;

const double eps=1e-6;
typedef long long ll;

int n,a[N],k;

int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n>>k;
    for(int i=0;i<n;++i){
        cin>>a[i];
    }
    sort(a,a+n);
    if(k==0){
        if(a[0]==1)cout<<-1;
        else cout<<1;
    }
    else{
        if(a[k-1]==a[k])cout<<-1;
        else cout<<a[k-1];
    }

    return 0;
}
View Code

 

D - Divide by three, multiply by two

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>PII;
typedef pair<string,int>PSI;
const int N=2e2+5,INF=0x3f3f3f3f,Mod=1e6;

const double eps=1e-6;
typedef long long ll;

int n;
ll a[N],p;
unordered_map<ll,ll>mp,mm;
vector<ll>ans;
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n;
    for(int i=0;i<n;++i){
        cin>>a[i];
        mp[a[i]]++;
    }
    for(int i=0;i<n;++i){
        ll x=a[i]*2;
        if(mp[x])mm[x]=a[i];
        else if(a[i]%3==0&&mp[a[i]/3])mm[a[i]/3]=a[i];
        else p=a[i];
        
    }
    ans.push_back(p);

    for(int i=0;i<n-1;++i){
        
        ans.push_back(mm[p]);
        p=mm[p];
    }
    for(int i=n-1;i>=0;--i)cout<<ans[i]<<' ';
    return 0;
}
View Code

 

E - Cyclic Components

思路:并查集求每个连通块,若有节点的入度不为2说明不在规定的环内

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>PII;
typedef pair<string,int>PSI;
const int N=2e5+5,INF=0x3f3f3f3f,Mod=1e6;

const double eps=1e-6;
typedef long long ll;

int n,m,fa[N],ru[N];
vector<int>ve[N];
unordered_set<int>se;
bool a[N];

int find(int x){
    if(x!=fa[x])fa[x]=find(fa[x]);
    return fa[x];
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;++i)fa[i]=i;
    for(int i=0,u,v;i<m;++i){
        cin>>u>>v;
        ru[u]++,ru[v]++;
        u=find(u),v=find(v);
        if(u!=v)fa[u]=v;
    }
    for(int i=1;i<=n;++i){
        int x=find(i);
        if(x==i){
            se.insert(i);
        }
        if(ru[i]!=2)a[x]=true;
    }
    int ans=0;
    for(auto v:se){
        if(!a[v])ans++;
    }
    cout<<ans;
    return 0;
}
View Code

 

 F - Consecutive Subsequence

思路:dp,f[i]状态表示:以i为结尾的集合,计算:数量最大值,f[i]=max(f[i],f[i-1]+1),由于数的范围为1e9,用map存

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>PII;
typedef pair<string,int>PSI;
const int N=2e5+5,INF=0x3f3f3f3f,Mod=1e6;

const double eps=1e-6;
typedef long long ll;

int n,cnt,p,a[N];
map<int,int>mp;
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n;
    for(int i=0;i<n;++i){
        cin>>a[i];
        mp[a[i]]=max(mp[a[i]],mp[a[i]-1]+1);
        if(mp[a[i]]>cnt){
            cnt=mp[a[i]];
            p=a[i];
        }
    }
    cout<<cnt<<'\n';
    p=p-cnt+1;
    for(int i=0;i<n;++i){
        if(a[i]==p){
            cout<<i+1<<' ';p++;
        }
    }
    return 0;
}
View Code