【每日一题 | 2025】2.3 ~ 2.9

个人主页:GUIQU.
归属专栏:每日一题

文章目录

  • [1. 【2.3】P8784 [蓝桥杯 2022 省 B] 积木画](#1. 【2.3】P8784 [蓝桥杯 2022 省 B] 积木画)
  • [2. 【2.4】P8656 [蓝桥杯 2017 国 B] 对局匹配](#2. 【2.4】P8656 [蓝桥杯 2017 国 B] 对局匹配)
  • [3. 【2.5】[ABC365D] AtCoder Janken 3](#3. 【2.5】[ABC365D] AtCoder Janken 3)
  • [4. 【2.6】P8703 [蓝桥杯 2019 国 B] 最优包含](#4. 【2.6】P8703 [蓝桥杯 2019 国 B] 最优包含)
  • [5. 【2.7】P8624 [蓝桥杯 2015 省 AB] 垒骰子](#5. 【2.7】P8624 [蓝桥杯 2015 省 AB] 垒骰子)
  • [6. 【2.8】P8774 [蓝桥杯 2022 省 A] 爬树的甲壳虫](#6. 【2.8】P8774 [蓝桥杯 2022 省 A] 爬树的甲壳虫)
  • [7. 【2.9】[ARC189C] Balls and Boxes](#7. 【2.9】[ARC189C] Balls and Boxes)

正文

1. 【2.3】P8784 [蓝桥杯 2022 省 B] 积木画

题目链接:https://www.luogu.com.cn/problem/P8784

【AC_Code】

cpp 复制代码
#include <iostream>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

const int mod = 1e9 + 7;
int N;

int main()
{
	IOS; cin >> N;
	
	if (N == 1)
	{
		cout << 1 << "\n";
		return 0;
	}
	
	if (N == 2)
	{
		cout << 2 << "\n";
		return 0;
	}
	
	if (N == 3)
	{
		cout << 5 << "\n";
		return 0;
	}
	
	int a = 1, b = 2, c = 5, d;
	for (int i = 4; i <= N; i ++)
	{
		d = c * 2 % mod + a;
		a = b, b = c, c = d;
		a %= mod, b %= mod, c %= mod, d %= mod;
	}
	cout << d << "\n";
	
	return 0;
}

2. 【2.4】P8656 [蓝桥杯 2017 国 B] 对局匹配

题目链接:https://www.luogu.com.cn/problem/P8656

【AC_Code】

cpp 复制代码
#include <iostream>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

const int maxN = 1e5 + 10;
int N, K, index, ans, a[maxN];

int main()
{
	IOS; cin >> N >> K;
	for (int i = 0; i < N; i ++) cin >> index, a[index] ++;
	if (K == 0)
	{
		for (int i = 0; i < N; i ++) if (a[i]) ans ++;
		cout << ans << "\n";
		return 0;
	}
	for (int i = 0; i < maxN; i ++)
	{
		if (a[i] < a[i + K]) a[i + K] -= a[i];
		else a[i + K] = 0;
	}
	for (int i = 0; i < maxN - K; i ++) ans += a[i];
	cout << ans << "\n";
	
	return 0;
}

3. 【2.5】[ABC365D] AtCoder Janken 3

题目链接:https://www.luogu.com.cn/problem/AT_abc365_d

【AC_Code】

cpp 复制代码
#include <bits/stdc++.h>
#define int long long

using namespace std;

const int N = 2e5 + 10;
int n, ans, f[N][10];
string s;

int main()
{
    cin >> n >> s;
    if (s[0] == 'R') {
        f[0][1] = 0;
        f[0][2] = 1;
    }
    if (s[0] == 'P') {
        f[0][2] = 0;
        f[0][3] = 1;
    }
    if (s[0] == 'S') {
        f[0][1] = 1;
        f[0][3] = 0;
    }
    for (int i = 1; i < n; i++) {
        if (s[i] == 'R') {
            f[i][1] = max(f[i - 1][2], f[i - 1][3]);
            f[i][2] = max(f[i - 1][1], f[i - 1][3]) + 1;
        }
        if (s[i] == 'P') {
            f[i][2] = max(f[i - 1][1], f[i - 1][3]);
            f[i][3] = max(f[i - 1][1], f[i - 1][2]) + 1;
        }
        if (s[i] == 'S') {
            f[i][1] = max(f[i - 1][2], f[i - 1][3]) + 1;
            f[i][3] = max(f[i - 1][1], f[i - 1][2]);
        }
    }
    cout << max({f[n - 1][1], f[n - 1][2], f[n - 1][3]});
    
    return 0;
}

4. 【2.6】P8703 [蓝桥杯 2019 国 B] 最优包含

题目链接:https://www.luogu.com.cn/problem/P8703

【AC_Code】

cpp 复制代码
#include <iostream>
#include <cstring>
#include <algorithm>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define inf 0x3f3f3f3f

using namespace std;

const int maxn = 1010;
int dp[maxn][maxn];
string S, T;

int main()
{
	IOS; cin >> S >> T; S = " " + S, T = " " + T;
	memset(dp, inf, sizeof(dp));
	for (int i = 0; i < S.size() + 1; i ++) dp[i][0] = 0;
	for (int i = 1; i <= S.size(); i ++) for (int j = 1; j <= S.size(); j ++)
	{
		if (S[i] == T[j]) dp[i][j] = dp[i-1][j-1];
		else dp[i][j] = min(dp[i-1][j-1]+1, dp[i-1][j]);
	}
	cout << dp[S.size()][T.size()] << "\n";
	
	return 0;
}

5. 【2.7】P8624 [蓝桥杯 2015 省 AB] 垒骰子

题目链接:https://www.luogu.com.cn/problem/P8624

【AC_Code】

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
#define rep(x,y,z) for(int x=y;x<=z;x++)
typedef long long LL;
const int mod=1e9+7;
int n,m,a,b,oppo[7]={0,4,5,6,1,2,3};
bool st[7][7];
struct matrix{
    LL c[7][7];
    matrix(){memset(c,0,sizeof c);}
}A,res;
matrix operator * (matrix &x,matrix &y){
    matrix t;
    rep(i,1,6){
        rep(j,1,6){
            rep(k,1,6){
                t.c[i][j]=(t.c[i][j]+x.c[i][k]*y.c[k][j])%mod;
            }
        }
    }
    return t;
}
void fastpow(LL k){
    rep(i,1,6) res.c[1][i]=4;
    rep(i,1,6){
        rep(j,1,6){
            if(st[i][oppo[j]]) A.c[i][j]=0;
            else A.c[i][j]=4;
        }
    }
    while(k){
        if(k&1) res=res*A;
        A=A*A;
        k>>=1;
    }
}

int main()
{
    cin>>n>>m;
    while(m--){
        cin>>a>>b;
        st[a][b]=st[b][a]=1;
    }
    fastpow(n-1);
    LL ans=0;
    rep(i,1,6) ans=(ans%mod+res.c[1][i]%mod)%mod;
    cout<<ans;
    
    return 0;
}

6. 【2.8】P8774 [蓝桥杯 2022 省 A] 爬树的甲壳虫

题目链接:https://www.luogu.com.cn/problem/P8774

【AC_Code】

cpp 复制代码
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5;
const int P = 998244353;

int n, a[N], b[N];

int fp(int x, int y) {
    int res = 1;
    for (; y; y >>= 1) {
        if (y & 1) {
            res = (1ll * res * x) % P;
        }
        x = (1ll * x * x) % P;
    }
    return res;
}

int main() {
    scanf("%d", &n);

    for (int i = 1; i <= n; i++) {
        scanf("%d%d", &a[i], &b[i]);
    }

    int s1 = 1, s2 = 0, s3 = 0;

    for (int i = 1; i <= n; i++) {
        int p1 = (1ll * a[i] * fp(b[i], P - 2)) % P;
        int p2 = (1ll * (b[i] - a[i]) * fp(b[i], P - 2)) % P;

        s3 = (s3 + s1) % P;
        s2 = (s2 + 1ll * s1 * p1) % P;
        s1 = (1ll * s1 * p2) % P;
    }

    printf("%d", (1ll * s3 * fp(1 - s2 + P, P - 2)) % P);

    return 0;
}

7. 【2.9】[ARC189C] Balls and Boxes

题目链接:https://www.luogu.com.cn/problem/AT_arc189_c

【AC_Code】

cpp 复制代码
#include <bits/stdc++.h>

#define F(i, a, b) for (int i = (a); i <= (b); ++i)
#define dF(i, a, b) for (int i = (a); i >= (b); --i)

using namespace std;

typedef long long LL;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int, int> pii;

const int N = 2e5 + 5;

int n, x, a[N], b[N], p[N], q[N];
int m, k, s[N], t[N], tt[N];
bool vis[N];

int dp[N], c[N], ans;

void Update(int x, int k) {
    while (x <= n) {
        c[x] = max(c[x], k);
        x += x & -x;
    }
}

void Query(int &y, int x) {
    while (x) {
        y = max(y, c[x]);
        x -= x & -x;
    }
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    cin >> n >> x;

    F(i, 1, n) {
        cin >> a[i];
    }

    F(i, 1, n) {
        cin >> b[i];
    }

    F(i, 1, n) {
        cin >> p[i];
    }

    F(i, 1, n) {
        cin >> q[i];
    }

    int u = p[x];
    bool now = 0;
    vis[x] = 1;

    while (!vis[u]) {
        now |= a[u];
        vis[u] = 1;
        if (now) {
            s[++m] = u;
        }
        u = p[u];
    }

    F(i, 1, n) {
        if (!vis[i] && a[i]) {
            return cout << "-1", 0;
        }
    }

    F(i, 1, n) {
        vis[i] = 0;
    }

    u = q[x];
    now = 0;
    vis[x] = 1;

    while (!vis[u]) {
        now |= b[u];
        vis[u] = 1;
        if (now) {
            t[++k] = u;
        }
        u = q[u];
    }

    F(i, 1, n) {
        if (!vis[i] && b[i]) {
            return cout << "-1", 0;
        }
    }

    F(i, 1, k) {
        tt[t[i]] = i;
    }

    F(i, 1, m) {
        if (tt[s[i]]) {
            Query(dp[i], tt[s[i]]);
            Update(tt[s[i]], ++dp[i]);
        }
    }

    F(i, 1, n) {
        ans = max(ans, dp[i]);
    }

    cout << m + k - ans;

    return 0;
}

结语

感谢您的阅读!期待您的一键三连!欢迎指正!

相关推荐
极客先躯2 个月前
高级java每日一道面试题-2024年11月27日-JVM篇-JVM的永久代中会发生垃圾回收么?
java·jvm·每日一题·内存溢出·元空间·java高级面试·永久代
凉风听雪4 个月前
VsCode插件:前端每日一题
前端·ide·vscode·每日一题
C_GUIQU4 个月前
【每日一题】24.10.14 - 24.10.20
每日一题
sweetheart7-75 个月前
LeetCode54. 螺旋矩阵(2024秋季每日一题 21)
线性代数·矩阵·力扣·数组·每日一题
sweetheart7-76 个月前
LeetCode152. 乘积最大子数组(2024秋季每日一题 2)
数据结构·算法·动态规划·dp·每日一题
sweetheart7-76 个月前
LeetCode172. 阶乘后的零(2024秋季每日一题 1)
数据结构·算法·leetcode·每日一题·hot100
无名之逆8 个月前
54. 螺旋矩阵【rust题解】
开发语言·前端·算法·rust·力扣·每日一题
菜菜的小彭8 个月前
2024-5-12——吃掉 N 个橘子的最少天数
java·算法·leetcode·面试·职场和发展·每日一题
无名之逆8 个月前
超级幂积【rust题解】
开发语言·前端·算法·rust·力扣·每日一题