中石大第38次CSP培训Week1题解

A.[NOIP 2006 普及组] 明明的随机数

知识点

STL——set

AC代码

    #include<bits/stdc++.h>
    #define endl '\n'
    #define int int long long
    #define pb push_back
    #define bs bitset
    #define val(a) (a<'a' ? (a-'A'+'a') : a)
    #define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
    using namespace std;

    typedef pair<char,int> PCI;
    typedef pair<int,int> PII;
    typedef pair<long long, long long> PLL;
    typedef priority_queue<int> PQ;
    typedef priority_queue<int, vector<int>, greater<int>> Q;
    const int N = 2e5+10, MAX = 1e9, INF = -1e9;

    int n;
    int e;
    set<int> st;

    void solve(){
        cin>>n;
        for(int i=1;i<=n;i++){
            cin>>e;
            st.insert(e);
        }
        cout<<st.size()<<endl;
        for(auto i:st){
            cout<<i<<" ";
        }
        return ;
    }

    signed main()
    {
        fast();
        
        int t=1;
        //cin>>t;
        while(t--){
            solve();
        }
        return 0;
    }

B.【模板】字符串哈希

知识点

STL——map,string

AC代码

    #include<bits/stdc++.h>
    #define endl '\n'
    #define int int long long
    #define pb push_back
    #define bs bitset
    #define val(a) (a<'a' ? (a-'A'+'a') : a)
    #define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
    using namespace std;

    typedef pair<char,int> PCI;
    typedef pair<int,int> PII;
    typedef pair<long long, long long> PLL;
    typedef priority_queue<int> PQ;
    typedef priority_queue<int, vector<int>, greater<int>> Q;
    const int N = 2e5+10, MAX = 1e9, INF = -1e9;

    int n;
    string s;
    map<string,int> mp;

    void solve(){
        cin>>n;
        for(int i=1;i<=n;i++){
            cin>>s;
            mp[s]++;
        }
        cout<<mp.size()<<endl;
        return ;
    }

    signed main()
    {
        fast();
        
        int t=1;
        //cin>>t;
        while(t--){
            solve();
        }
        return 0;
    }

C.【深进1.例1】求区间和

知识点

前缀和

AC代码

    #include<bits/stdc++.h>
    #define endl '\n'
    #define int int long long
    #define pb push_back
    #define bs bitset
    #define val(a) (a<'a' ? (a-'A'+'a') : a)
    #define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
    using namespace std;

    typedef pair<char,int> PCI;
    typedef pair<int,int> PII;
    typedef pair<long long, long long> PLL;
    typedef priority_queue<int> PQ;
    typedef priority_queue<int, vector<int>, greater<int>> Q;
    const int N = 2e5+10, MAX = 1e9, INF = -1e9;

    int n;
    int a[N];
    int q;
    int l,r;

    void solve(){
        cin>>n;
        a[0]=0;
        for(int i=1;i<=n;i++){
            cin>>a[i];
            a[i]+=a[i-1];
        }
        cin>>q;
        while(q--){
            cin>>l>>r;
            cout<<a[r]-a[l-1]<<endl;
        }
        return ;
        
    }

    signed main()
    {
        fast();
        
        int t=1;
        //cin>>t;
        while(t--){
            solve();
        }
        return 0;
    }

D.语文成绩

知识点

差分

AC代码

    #include<bits/stdc++.h>
    #define endl '\n'
    #define int int long long
    #define pb push_back
    #define bs bitset
    #define val(a) (a<'a' ? (a-'A'+'a') : a)
    #define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
    using namespace std;

    typedef pair<char,int> PCI;
    typedef pair<int,int> PII;
    typedef pair<long long, long long> PLL;
    typedef priority_queue<int> PQ;
    typedef priority_queue<int, vector<int>, greater<int>> Q;
    const int N = 5e6+10, MAX = 1e9, INF = -1e9;

    int n;
    int a[N];
    int q;
    int l,r,c;

    void solve(){
        cin>>n>>q;
        a[0]=0;
        for(int i=1;i<=n;i++){
            cin>>a[i];
        }
        for(int i=n;i>=1;i--){
            a[i]-=a[i-1];
        }
        while(q--){
            cin>>l>>r>>c;
            a[l]+=c;
            a[r+1]-=c;
        }
        for(int i=1;i<=n;i++){
            a[i]+=a[i-1];
        }
        int ans=MAX;
        for(int i=1;i<=n;i++){
            ans=min(ans,a[i]);
        }
        cout<<ans<<endl;
        return ;
        
    }

    signed main()
    {
        fast();
        
        int t=1;
        //cin>>t;
        while(t--){
            solve();
        }
        return 0;
    }

E.连续自然数和

知识点

二分

AC代码

    #include<bits/stdc++.h>
    #define endl '\n'
    #define int int long long
    #define pb push_back
    #define bs bitset
    #define val(a) (a<'a' ? (a-'A'+'a') : a)
    #define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
    using namespace std;

    typedef pair<char,int> PCI;
    typedef pair<int,int> PII;
    typedef pair<long long, long long> PLL;
    typedef priority_queue<int> PQ;
    typedef priority_queue<int, vector<int>, greater<int>> Q;
    const int N = 1e5+10, MAX = 1e9, INF = -1e12;

    int m;
    int f(int a,int b){
    	return (b*b+b+a-a*a)/2;
    }

    void solve(){
    	cin>>m;
    	for(int i=1;i<=m;i++){
    		int l=i+1;int r=m;
    		while(l<r){
    			int mid=(l+r)>>1;
    			if(f(i,mid)>=m)r=mid;
    			else l=mid+1;
    		}
    		if(f(i,l)==m)cout<<i<<" "<<l<<endl;
    	}
    }

    signed main()
    {
        fast();
        
        int t=1;
        //cin>>t;
        while(t--){
            solve();
        }
        return 0;
    }

F.[NOIP 2005 普及组] 采药

知识点

01背包

AC代码

    #include<bits/stdc++.h>
    #define endl '\n'
    #define int int long long
    #define pb push_back
    #define bs bitset
    #define val(a) (a<'a' ? (a-'A'+'a') : a)
    #define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
    using namespace std;

    typedef pair<char,int> PCI;
    typedef pair<int,int> PII;
    typedef pair<long long, long long> PLL;
    typedef priority_queue<int> PQ;
    typedef priority_queue<int, vector<int>, greater<int>> Q;
    const int N = 105, MAX = 1e9, INF = -1e12, M = 1005;

    int n,V;
    int v[N];
    int w[N];
    int dp[N][M];

    void solve(){
    	cin>>V>>n;
    	for(int i=1;i<=n;i++){
    		cin>>v[i]>>w[i];
    	}
    	memset(dp,0,sizeof dp);
    	for(int i=1;i<=n;i++){
    		for(int j=1;j<=V;j++){
    			dp[i][j]=dp[i-1][j];
    			if(j-v[i]>=0)dp[i][j]=max(dp[i][j],dp[i-1][j-v[i]]+w[i]);
    		}
    	}
    	cout<<dp[n][V]<<endl;

    }

    signed main()
    {
        fast();
        
        int t=1;
        //cin>>t;
        while(t--){
            solve();
        }
        return 0;
    }

G.质因数分解

知识点

质数唯一分解定理(数论)

AC代码

    #include<bits/stdc++.h>
    #define endl '\n'
    #define int int long long
    #define pb push_back
    #define bs bitset
    #define val(a) (a<'a' ? (a-'A'+'a') : a)
    #define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
    using namespace std;

    typedef pair<char,int> PCI;
    typedef pair<int,int> PII;
    typedef pair<long long, long long> PLL;
    typedef priority_queue<int> PQ;
    typedef priority_queue<int, vector<int>, greater<int>> Q;
    const int N = 105, MAX = 1e9, INF = -1e12, M = 1005;

    int n;
    vector<int> v;

    void solve(){
    	cin>>n;
    	for(int i=2;i*i<=n;i++){
    		if(n%i==0){
    			v.pb(i);
    		}
    		while(n%i==0){
    			n/=i;
    		}
    	}
    	if(n!=1)v.pb(n);
    	cout<<v[v.size()-1]<<endl;

    }

    signed main()
    {
        fast();
        
        int t=1;
        //cin>>t;
        while(t--){
            solve();
        }
        return 0;
    }

H.矩阵乘法

知识点

线性代数

AC代码

    #include<bits/stdc++.h>
    #define endl '\n'
    #define int int long long
    #define pb push_back
    #define bs bitset
    #define val(a) (a<'a' ? (a-'A'+'a') : a)
    #define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
    using namespace std;

    typedef pair<char,int> PCI;
    typedef pair<int,int> PII;
    typedef pair<long long, long long> PLL;
    typedef priority_queue<int> PQ;
    typedef priority_queue<int, vector<int>, greater<int>> Q;
    const int N = 105, MAX = 1e9, INF = -1e12, M = 1005;

    int n,m,k;
    int a[N][N];
    int b[N][N];
    int c[N][N];

    void solve(){
    	cin>>n>>m>>k;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cin>>a[i][j];
            }
        }
        for(int i=1;i<=m;i++){
            for(int j=1;j<=k;j++){
                cin>>b[i][j];
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=k;j++){
                c[i][j]=0;
                for(int q=1;q<=m;q++){
                    c[i][j]+=a[i][q]*b[q][j];
                }
                cout<<c[i][j]<<" ";
            }
            cout<<endl;
        }
    	return ;
    }

    signed main()
    {
        fast();
        
        int t=1;
        //cin>>t;
        while(t--){
            solve();
        }
        return 0;
    }

I.[蓝桥杯 2025 省 A/Python B 第二场] 消消乐

知识点

双指针

AC代码

    #include<bits/stdc++.h>
    #define endl '\n'
    #define int int long long
    #define pb push_back
    #define bs bitset
    #define val(a) (a<'a' ? (a-'A'+'a') : a)
    #define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
    using namespace std;

    typedef pair<char,int> PCI;
    typedef pair<int,int> PII;
    typedef pair<long long, long long> PLL;
    typedef priority_queue<int> PQ;
    typedef priority_queue<int, vector<int>, greater<int>> Q;
    const int N = 105, MAX = 1e9, INF = -1e12, M = 1005;

    string s;
    int ans=0;

    void solve(){
    	cin>>s;
    	int n=s.size();
    	s=" "+s;
    	int l=1;int r=n;
    	while(l<r){
    		while(s[l]=='B')l++;
    		while(s[r]=='A')r--;
    		if(l<r&&s[l]=='A'&&s[r]=='B'){
    			ans+=2;
    			l++;
    			r--;
    		}
    	}
    	cout<<n-ans<<endl;
    	return ;
    }

    signed main()
    {
        fast();
        
        int t=1;
        //cin>>t;
        while(t--){
            solve();
        }
        return 0;
    }
posted @ 2025-05-24 17:38  Oaths  阅读(193)  评论(0)    收藏  举报