中石大第39次CSP培训Week2题解

A.查找


Key:二分-二分查找

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 = 1e6+10, MAX = 1e9, INF = -1e9;

    int n;
    int a[N];
    int q;
    int x;

    void solve(){
        cin>>n>>q;
        for(int i=1;i<=n;i++)cin>>a[i];
        while(q--){
            cin>>x;
            int l=1;int r=n;
            while(l<r){
                int mid=(l+r)>>1;
                if(a[mid]>=x)r=mid;
                else l=mid+1;
            }
            cout<<((a[l]==x) ? l : -1)<<" ";
        }
        return ;
    }

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

B.跳石头


Key:二分-二分答案

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 len,n,m;
    int a[N];


    bool check(int mid){
        int num=0;
        int p=0;
        for(int i=1;i<=n;i++){
            if(a[i]-a[p]<mid)num++;
            else p=i;
        }
        return num<=m;
    }

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

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

C.求区间和


Key:前缀和-一维前缀和

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;
    int s[N];

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

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

D.领地选择


Key:前缀和-二位前缀和

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 = 1005, MAX = 1e9, INF = -1e9;

    int n,m,c;
    int a[N][N];
    int s[N][N];
    int ans=INF;
    int x,y;

    void solve(){
        cin>>n>>m>>c;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cin>>a[i][j];
            }
        }
        for(int i=0;i<=n;i++)s[i][0]=s[0][i]=0;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+a[i][j];
            }
        }
        for(int i=1;i+c-1<=n;i++){
            for(int j=1;j+c-1<=m;j++){
                int x0=i;int x1=i+c-1;
                int y0=j;int y1=j+c-1;
                int ds=s[x1][y1]-s[x0-1][y1]-s[x1][y0-1]+s[x0-1][y0-1];
                if(ds>ans){
                    ans=ds;
                    x=i;y=j;
                }
            }
        }
        cout<<x<<" "<<y<<endl;
        return ;
        
    }

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

E.语文成绩


Key:差分-一维差分

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,m;
    int a[N];
    int l,r,c;
    int b[N];
    int ans=MAX;

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

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

F.地毯


Key:差分-二维差分

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 = 1005, MAX = 1e9, INF = -1e9;

    int n,m;
    int b[N][N];
    int l1,r1,l2,r2;

    void insert(int x1,int y1,int x2,int y2,int e){
        b[x1][y1]+=e;
        b[x2+1][y1]-=e;
        b[x1][y2+1]-=e;
        b[x2+1][y2+1]+=e;
    }

    void solve(){
        cin>>n>>m;
        memset(b,0,sizeof b);
        while(m--){
            cin>>l1>>r1>>l2>>r2;
            insert(l1,r1,l2,r2,1);
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                b[i][j]=b[i-1][j]+b[i][j-1]-b[i-1][j-1]+b[i][j];
                cout<<b[i][j]<<" ";
            }
            cout<<endl;
        }
        return ;

    }

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

G.第31次CSP认证第二题:坐标变换(其二)


Key:前缀和

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<int,char> PIC;
    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,q;
    int i,j;
    map<int,int> mpa;
    map<int,int> mpb;
    double x,y;
    vector<double> na;
    vector<double> nb;

    void solve(){
        cin>>n>>q;
        na.pb(1.0);nb.pb(0.0);
        for(int i=1;i<=n;i++){
            cin>>j>>x;
            if(j==1){
                na.pb(na[na.size()-1]*x);
            }
            else{
                nb.pb(nb[nb.size()-1]+x);
            }
            mpa[i]=na.size()-1;
            mpb[i]=nb.size()-1;
        }
        while(q--){
            cin>>i>>j>>x>>y;
            double d=na[mpa[j]]/na[mpa[i-1]];
            x*=d;
            y*=d;
            double x0=x;
            double y0=y;
            d=nb[mpb[j]]-nb[mpb[i-1]];
            x=x0*cos(d)-y0*sin(d);
            y=x0*sin(d)+y0*cos(d);
            cout<<fixed<<setprecision(3)<<x<<" "<<y<< endl;
        }
        return ;

    }

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

H.第25次CSP认证第二题:出行计划


Key:差分

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<PII> PQ;
    typedef priority_queue<int, vector<int>, greater<int>> Q;
    const int N = 1e6+10, MAX = 1e9, INF = -1e12, M = 10005;

    int n,m,k;
    int t,c;
    int b[N];
    int e;

    void solve(){
    	cin>>n>>m>>k;
        memset(b,0,sizeof b);
    	for(int i=1;i<=n;i++){
    		cin>>t>>c;
            int l=max(1ll,t-c+1);
            int r=t;
            b[l]++;b[r+1]--;
    	}
        for(int i=1;i<N;i++){
            b[i]+=b[i-1];
        }
        while(m--){
            cin>>e;
            cout<<b[e+k]<<endl;
        }

        return ;
    }

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