【题解】codeforces Zuma Round #336 D.【区间DP】

该文讨论了一种算法问题,即给定一组数字,每次可以删除一个回文子串,求最少需要删除多少次。文章提到了使用区间动态规划的方法来解决,通过f[l][r]表示从1到n的数中删除的最小次数,并通过遍历所有可能的分割点进行状态转移。特殊情况下,如果区间两端的数字相等,可以同时删除。最后,代码展示了如何实现这个算法。

题意:给n个数,每次可以删除一个回文串,问最少删除的次数;

分析:看数据范围可以知道可以用区间DP。首先不妨设f[1][n]表示1-n个数删除的最小次数,那我  

        我们枚举区间中的一个位置k 就可以用f[l][r] = min{f[l][k] + f[k + 1][r]}就可以表示所有的转移,

        考虑特殊情况,如果区间长度是2并且两个数相等那么只需要一次就可以删除。如果                         w[l] == w[r] 那么位置l 和 r可以在删除l,r之间的数的时候顺便删了,就可以得出f[l][r] = f[l + 1][r - 1]

#include<iostream>
#include<stdio.h>
#include<cmath>
#include<cstring>
#include<stack>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<climits>
#define fors(i, a, b) for(int i = (a); i <= (b); ++i)
#define scanf1(a) scanf("%d",&a)
#define scanf2(a,b) scanf("%d%d",&a,&b)
#define scanf3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define scanf4(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d)
#define ll long long
#define readType int

using namespace std;
const int mx = 1e3 + 10;
const int inf = 0x3f3f3f3f;

inline readType read() {
    char c = getchar(); readType x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}

int n;
int w[mx];
int f[mx][mx];

int main2(){
    cin >> n;
    memset(f, inf, sizeof f);
    for(int i = 1; i <= n; i ++) {
        cin >> w[i];
        f[i][i] = 1;
    }
    for(int len = 2; len <= n; len ++){
        for(int l = 1; l + len - 1 <= n; l ++){
            int r = l + len - 1;
            for(int k = l; k < r; k ++){
                f[l][r] = min(f[l][r], f[l][k] + f[k + 1][r]);
            }
            if(w[l] == w[r]){
                if(len == 2) f[l][r] = 1;
                f[l][r] = min(f[l][r], f[l + 1][r - 1]);
            }
        }
    }
    cout << f[1][n] << endl;
    
    
    return 0;
}

int main(){
    //ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int _;//_ = read();
    _ = 1;
    while(_--)main2();return 0;
}

### Codeforces Round 1005 Div. 2 A-F Problem Solutions #### A. Money Change 为了处理货币转换问题,可以将所有的金额都转化为分的形式来简化计算。通过遍历输入数据并累加各个部分的金额,最后求得剩余的钱数并对100取模得到最终结果[^2]。 ```cpp #include <iostream> using namespace std; int main() { int s, xi, yi; cin >> s; int total_cents = 0; for (int i = 0; i < s; ++i) { cin >> xi >> yi; total_cents += xi * 100 + yi; } cout << (s * 100 - total_cents) % 100 << endl; } ``` #### B. Odd and Even Pairs 此题目要求找到至少一对满足条件的索引:要么是一个偶数值的位置,或者是两个奇数值位置。程序会读入测试次数`t`以及每次测试中的数组长度`n`及其元素,并尝试找出符合条件的一对索引输出;如果没有这样的组合则返回-1[^3]。 ```cpp #include <cstdio> int main() { int t, n, num; scanf("%d", &t); while (t--) { int evenIndex = 0, oddIndex1 = 0, oddIndex2 = 0; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &num); if (num % 2 == 0 && !evenIndex) evenIndex = i; else if (num % 2 != 0) { if (!oddIndex1) oddIndex1 = i; else if (!oddIndex2) oddIndex2 = i; } if ((evenIndex || (oddIndex1 && oddIndex2))) break; } if (evenIndex) printf("1\n%d\n", evenIndex); else if (oddIndex1 && oddIndex2) printf("2\n%d %d\n", oddIndex1, oddIndex2); else printf("-1\n"); } return 0; } ``` 由于仅提供了前两道题的具体描述和解决方案,在这里无法继续给出完整的C至F题解答。通常情况下,每一道竞赛编程题都有其独特的挑战性和解决方法,建议查阅官方题解或社区讨论获取更多帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值