Codeforces Round #131 (Div. 2) B. Hometask dp

本文介绍了一道来自CodeForces的编程题214B的解决方案,该题要求从一组数字中找出能够组成最大整数的组合,该整数需同时被2、3、5整除。文章详细解析了如何使用数位DP来解决这个问题,包括排序、状态转移等关键步骤。

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

题目链接:

http://codeforces.com/problemset/problem/214/B

Hometask


time limit per test:2 secondsmemory limit per test:256 megabytes
问题描述

Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?

You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.

Each digit is allowed to occur in the number the same number of times it occurs in the set.

输入

A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space.

输出

On a single line print the answer to the problem. If such number does not exist, then you should print -1.

样例

sample input
11
3 4 5 4 5 3 5 3 4 4 0

sample output
5554443330

题意

给你一n个数x1,...,xn(0<=xi<=9)。挑出若干个拼在一起,使得它的值最大。

题解

题目相当于是求从n个数中挑出最多的数,它们的和能被3整除,并且它们中要有至少一个0,如果有多种方法挑出最多的数就优先选大的数挑。
可以用数位dp做:dp[i][j]表示考虑到第i个数,前缀和%3==j的方案数。
先对原序列排个序(为了转移的时候贪心挑最大的数),从左到右扫一遍dp,用pre[i][j]记录一下路径。

#include<iostream> 
#include<cstdio>
#include<cstring>
#include<vector> 
#include<string>
#include<algorithm>
using namespace std;

const int maxn = 1e5 + 10;
typedef long long LL;

string str;
int arr[maxn];
int dp[maxn][3], pre[maxn][3];
vector<int> ans;
int n, m;

int main() {
    int zero = 0;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &arr[i]);
    }
    sort(arr, arr + n);
    memset(dp, -1, sizeof(dp));
    for (int i = 0; i<maxn; i++) dp[i][0] = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j<3; j++) {
            dp[i][j] = dp[i - 1][j];
            pre[i][j] = j;
            int ne = ((j - arr[i]) % 3 + 3) % 3;
            if (dp[i - 1][ne] >= 0 && dp[i][j] <= dp[i - 1][ne] + 1) {
                dp[i][j] = dp[i - 1][ne] + 1;
                pre[i][j] = ne;
            }
        }
    }
    int p = 0;
    for (int i = n; i >= 1; i--) {
        int bef = pre[i][p];
        if (dp[i - 1][bef] + 1 == dp[i][p]) ans.push_back(arr[i]);
        p = bef;
    }
    sort(ans.begin(), ans.end());
    if (ans[0] != 0) {
        puts("-1");
        return 0;
    }
    int i = ans.size() - 1;
    for (; i>0 && ans[i] == 0; i--);
    for (; i >= 0; i--) printf("%d", ans[i]);
    puts("");
    return 0;
}

转载于:https://www.cnblogs.com/fenice/p/5683286.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值