Codeforces Round #574 (Div. 2)题解

这篇博客详细解析了Codeforces Round #574 (Div. 2)的比赛题目,涵盖了A至E五道题。针对每道题,博主阐述了题意、解题思路,并提供了代码实现。A题关键在于优先满足偶数口味需求;B题通过二分查找确定操作次数;C题采用动态规划寻找最大身高组合;D题涉及位运算和贡献计算;E题利用单调队列求子矩阵最小值之和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

比赛链接

传送门

A题

题意

n n n个人每个人都有自己喜欢喝的 v e c h o r k a vechorka vechorka口味,现在给你 ⌈ n / 2 ⌉ \lceil n/2\rceil n/2 v e c h o r k a vechorka vechorka,每箱有两瓶,问最多能有多少个人能拿到自己喜欢的口味。

思路

我们首先记录每个口味有多少个人喜欢,然后要想拿到自己喜欢的口味最大那么一定要优先考虑能凑偶数的,把偶数考虑完后剩余的口味一定都是 1 1 1,就不管怎么分都只能满足一半的人。

代码实现如下

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
 
typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
 
#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://Code//in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0)
 
const double eps = 1e-8;
const int mod = 1e9 + 7;
const int maxn = 1e6 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
 
int n, k;
int a[maxn], num[maxn];
 
int main() {
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
        num[a[i]]++;
    }
    int ans = 0;
    int tot = (n + 1) / 2;
    for(int i = 1; i <= k; ++i) {
        ans += num[i] / 2 * 2;
        tot -= num[i] / 2;
        num[i] %= 2;
    }
    ans += tot;
    printf("%d\n", ans);
    return 0;
}

B题

题意

要你使用恰好 n n n次操作使得总糖果数为 k k k,操作分为两种:

  • 增加上一次增加的数量 + 1 +1 +1个糖果;
  • 减少 1 1 1个糖果。

思路

二分 c h e c k check check

代码实现如下

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
 
typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
 
#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://Code//in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0)
 
const double eps = 1e-8;
const int mod = 1e9 + 7;
const int maxn = 1e6 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
 
int n, k;
 
bool check(int x) {
    return (1LL * x * x + 3LL * x) / 2 - n >= k;
}
 
int main() {
    scanf("%d%d", &n, &k);
    int ub = n, lb = 1, mid, ans = 0;
    while(ub >= lb) {
        mid = (ub + lb) >> 1;
        if(check(mid)) {
            ans = mid;
            ub = mid - 1;
        } else {
            lb = mid + 1;
        }
    }
    printf("%d\n", n - ans);
    return 0;
}

C题

题意

总共有 2 n 2n 2n个人,第一排的编号从 1 1 1 n n n,第二排也是,现在要你选择任意多个人使得总身高最大,但是注意同一个编号只能有一个人,编号相邻的话不能是同一排的。

思路

d p [ i ] [ j ] dp[i][j] dp[i][j]表示编号为 i i i的人选择状态为 j j j时的最大身高, j = 0 j=0 j=0表示从第一排选, j = 1 j=1 j=1从第二排, j = 2 j=2 j=2为不选,则 d p [ i ] [ 0 ] = m a x ( d p [ i − 1 ] [ 1 ] , d p [ i − 1 ] [ 2 ] ) + a [ i ] , d p [ i ] [ 1 ] = m a x ( d p [ i − 1 ] [ 0 ] , d p [ i − 1 ] [ 2 ] ) + b [ i ] , d p [ i ] [ 2 ] = m a x ( d p [ i − 1 ] [ 0 ] , d p [ i − 1 ] [ 1 ] , d p [ i − 1 ] [ 2 ] ) dp[i][0]=max(dp[i-1][1],dp[i-1][2])+a[i],dp[i][1]=max(dp[i-1][0],dp[i-1][2])+b[i],dp[i][2]=max(dp[i-1][0],dp[i-1][1],dp[i-1][2]) dp[i][0]=max(dp[i1][1],dp[i1][2])+a[i],dp[i][1]=max(dp[i1][0],dp[i1][2])+b[i],dp[i][2]=max(dp[i1][0],dp[i1][1],dp[i1][2])

代码实现如下

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
 
typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
 
#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://Code//in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0)
 
const double eps = 1e-8;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
 
int n;
int a[maxn], b[maxn];
LL dp[maxn][3];
 
int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
    }
    for(int i = 1; i <= n; ++i) {
        scanf("%d", &b[i]);
    }
    for(int i = 1; i <= n; ++i) {
        dp[i][0] = max(dp[i-1][1], dp[i-1][2]) + a[i];
        dp[i][1] = max(dp[i-1][0], dp[i-1][2]) + b[i];
        dp[i][2] = max(dp[i-1][0], max(dp[i-1][1], dp[i-1][2]));
    }
    printf("%lld\n", max(dp[n][0], max(dp[n][1], dp[n][2])));
    return 0;
}

D题

题意

定义 f f f函数为
如果 p ≥ q p\geq q pq f ( a 1 … a p , b 1 … b q ) = a 1 a 2 … a p − q + 1 b 1 a p − q + 2 b 2 … a p − 1 b q − 1 a p b q f(a1…ap,b1…bq)=a_1a_2\dots a_{p−q+1}b_1a{p−q+2}b_2\dots a_{p−1}b_{q−1}a_pb_q f(a1ap,b1bq)=a1a2apq+1b1apq+2b2ap1bq1apbq;
如果 p &lt; q p&lt;q p<q f ( a 1 … a p , b 1 … b q ) = b 1 b 2 … b q − p a 1 b q − p + 1 a 2 … a p − 1 b q − 1 a p b q f(a_1\dots a_p,b_1\dots b_q)=b_1b_2…b_{q−p}a_1b_{q−p+1}a_2\dots a_{p−1}b{q−1}a_pb_q f(a1ap,b1bq)=b1b2bqpa1bqp+1a2ap1bq1apbq.

思路

按位算贡献,先预处理出 a i a_i ai与长度 l e n len len的数进行 f f f函数的贡献然后乘以长度为 l e n len len的数的个数。

代码实现如下

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
 
typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
 
#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://Code//in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0)
 
const double eps = 1e-8;
const int mod = 998244353;
const int maxn = 1e5 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
 
int n;
int a[maxn], pw[105], cnt[30];
LL dp[maxn][30];
 
int main() {
    scanf("%d", &n);
    pw[0] = 1;
    for(int i = 1; i < 30; ++i) {
        pw[i] = 1LL * pw[i-1] * 10 % mod;
    }
    for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
    for(int i = 1; i <= n; ++i) {
        int x = a[i], len = 0;
        while(x) {
            ++len;
            x /= 10;
        }
        cnt[len]++;
        for(int j = 1; j <= 10; ++j) {
            if(len >= j) {
                int num = a[i];
                for(int k = 0; k < j; ++k) {
                    int x = num % 10;
                    num /= 10;
                    dp[i][j] = (((dp[i][j] + 1LL * x * pw[k*2] % mod) % mod) + 1LL * x * pw[k*2+1] % mod) % mod;
                }
                int pp = 2 * j;;
                for(int k = j; k < len; ++k) {
                    int x = num % 10;
                    num /= 10;
                    dp[i][j] = (dp[i][j] + 2LL * x * pw[pp] % mod) % mod;
                    ++pp;
                }
            } else {
                int num = a[i];
                for(int k = 0; k < len; ++k) {
                    int x = num % 10;
                    num /= 10;
                    dp[i][j] = (((dp[i][j] + 1LL * x * pw[k*2] % mod) % mod) + 1LL * x * pw[k*2+1] % mod) % mod;
                }
            }
        }
    }
    LL ans = 0;
    for(int i = 1; i <= n; ++i) {
        for(int j = 1; j <= 10; ++j) {
            ans = (ans + 1LL * dp[i][j] * cnt[j] % mod) % mod;
        }
    }
    printf("%lld\n", ans);
    return 0;
}

E题

题意

给你构造 n × m n\times m n×m的矩阵的公式然后要你求所有大小为 a × b a\times b a×b的子矩阵内最小值的和。

思路

首先我们先通过暴力将矩阵构造出来,然后对每一行用单调队列求出最小值,然后再把这个值当成 m p [ i ] [ j ] mp[i][j] mp[i][j]再对每一列求一次然后累加即可。

代码实现如下

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
 
typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
 
#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://Code//in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0)
 
const double eps = 1e-8;
const int mod = 998244353;
const int maxn = 1e5 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
 
int n, m, a, b;
LL g, x, y, z;
int mp[3002][3002], dp[3002][3002];
deque<pii> q;
 
int main() {
    scanf("%d%d%d%d", &n, &m, &a, &b);;
    scanf("%lld%lld%lld%lld", &g, &x, &y, &z);
    for(int i = 1; i <= n; ++i) {
        for(int j = 1; j <= m; ++j) {
            mp[i][j] = g;
            g = (1LL * g * x % z + y) % z;
        }
    }
    for(int i = 1; i <= n; ++i) {
        while(!q.empty()) q.pop_back();
        for(int j = m; j >= 1; --j) {
            while(!q.empty() && mp[i][j] < q.front().first) q.pop_front();
            q.push_front({mp[i][j], j});
            dp[i][j] = q.back().first;
            while(!q.empty() && q.back().second >= j + b - 1) q.pop_back();
        }
    }
    for(int i = 1; i <= n; ++i) {
        for(int j = 1; j <= m; ++j) {
            mp[i][j] = dp[i][j];
        }
    }
    for(int j = 1; j <= m; ++j) {
        while(!q.empty()) q.pop_back();
        for(int i = n; i >= 1; --i) {
            while(!q.empty() && mp[i][j] < q.front().first) q.pop_front();
            q.push_front({mp[i][j], i});
            dp[i][j] = q.back().first;
            while(!q.empty() && q.back().second >= i + a - 1) q.pop_back();
        }
    }
    LL ans = 0;
    for(int i = 1; i <= n - a + 1; ++i) {
        for(int j = 1; j <= m - b + 1; ++j) {
            ans += dp[i][j];
        }
    }
    printf("%lld\n", ans);
    return 0;
}
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值