中石大第41次CSP培训Week5/6后三题题解

模拟2A.大炮


思路

模拟

AC代码

    void solve(){
        int n;
        cin >> n;
        int a[n + 1];
        for(int i = 1; i <= n ; i ++ ){
            cin >> a[i];
        }
        int ans = 1;
        int h = 1;
        for(int i = 1; i <= n - 1; i ++ ){
            if(h + 1 <= a[i + 1])h ++;
            else{
                h = 1;
                ans ++;
                i --;
            }
        }
        cout << ans << endl;
    }

模拟2B.商店


思路

贪心——排序

80分代码

    void solve(){
        int n;
        cin >> n;
        int a[n + 1];
        int m = 0;
        for(int i = 1; i <= n ; i ++ ){
            cin >> a[i];
            m = max(m, a[i]);
        }
        int ans = 0;
        for(int k = 0; k <= m; k ++ ){
            int res = 0;
            for(int i = 1; i <= n ; i ++ ){
                if(a[i] >= k)res += k;
            }
            ans = max(ans, res);
        }
        cout << ans << endl;
        return ;
    }

AC代码

    void solve(){
        int n;
        cin >> n;
        int a[n + 1];
        for(int i = 1; i <= n ; i ++ ){
            cin >> a[i];
        }
        sort(a + 1, a + 1 + n);
        int ans = 0;
        for(int i = n; i >= 1; i -- ){
            ans = max(ans, (n - i + 1) * a[i]);
        }
        cout << ans << endl;
        return ;
    }

模拟2D.献给女士的礼物


思路

DFS

30分代码

    int ans = 0;
    int n, k;
    PII a[N];
    bool st[N];

    void dfs(int x){
        if(x > n){
            int key = 0;
            int res = 0;
            for(int i = 1; i <= n; i ++ ){
                if(st[i]){
                    key |= a[i].first;
                    res += a[i].second;
                }
            }
            if(key < k){
                ans = max(ans, res);
            }
            return ;
        }

        st[x] = true;
        dfs(x + 1);
        st[x] = false;
        dfs(x + 1);
        return ;
    }

    void solve(){
        cin >> n >> k;
        for(int i = 1; i <= n ; i ++ ){
            cin >> a[i].first >> a[i].second;
        }
        memset(st, false, sizeof st);
        dfs(1);
        cout << ans << endl;
        return ;
    }
posted @ 2026-03-12 14:58  Oaths  阅读(1)  评论(0)    收藏  举报