题目链接:

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

Codeforces Round #131 (Div. 2) B. Hometask dp相关推荐

  1. Codeforces Round #533 (Div. 2) C.思维dp D. 多源BFS

    题目链接:https://codeforces.com/contest/1105 C. Ayoub and Lost Array 题目大意:一个长度为n的数组,数组的元素都在[L,R]之间,并且数组全 ...

  2. Codeforces Round #374 (Div. 2) C. Journey DP

    C. Journey 题目连接: http://codeforces.com/contest/721/problem/C Description Recently Irina arrived to o ...

  3. Codeforces Round #260 (Div. 1) A - Boredom DP

    点击打开链接 题意: 给你n个数,你每次可以选择删除去一个数x,但是等于x+1和等于x-1的数都得删去 你每一次操作可以得x分 思路一: dp[i]表示到i后能够得到的最大分数 dp[i]=max(d ...

  4. Codeforces Round #131(Div. 2)

    表示水出A和B,C没看懂,E看懂了以前貌似还见过不会做,CE应该都是DP...唉,DP,啊....A题3分钟,B题卡了错了4次,很多2B错误..把trick都想清楚了,这个基本属于乱搞题了...43分 ...

  5. Codeforces Round #131 (Div. 2)------AB

    非常郁闷,一大早起来看结果,本来以为这次rating应该能升到1600以上的,可惜结果很惨,B题由于一个小失误,导致被别人hack成功了.由于家庭原因,老妈不准熬夜做,做了39min把AB两道简单题做 ...

  6. Codeforces Round #387 (Div. 2) 747F(数位DP)

    题目大意 给出整数k和t,需要产生一个满足以下要求的第k个十六进制数 即十六进制数每一位上的数出现的次数不超过t 首先我们先这样考虑,如果给你了0~f每个数字可以使用的次数num[i],如何求长度为L ...

  7. Codeforces Round #699 (Div. 2) F - AB Tree(贪心、树上DP)超级清晰,良心题解,看不懂来打我 ~

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #699 (Div. 2) F - AB Tree Problem ...

  8. Codeforces Round #459 (Div. 2) C 思维,贪心 D 记忆化dp

    Codeforces Round #459 (Div. 2) C. The Monster 题意:定义正确的括号串,是能够全部匹配的左右括号串. 给出一个字符串,有 (.). ? 三种字符, ? 可以 ...

  9. Codeforces Round #507 (Div. 1) D. You Are Given a Tree 根号分治 + dp

    传送门 题意: 有一颗nnn个节点的树,其中一个简单路径集合被称为kkk合法当且仅当: 树的每个节点至多属于一条路径,且每条路径恰好包含kkk个点. 对于k∈[1,n]k\in [1,n]k∈[1,n ...

最新文章

  1. Pytorch 多 GPU 并行处理机制
  2. 计算机会计课程试题及答案,计算机会计第2次作业_报表_附答案
  3. 2021年第十六届智能车竞赛线上决赛之前大家的提问
  4. 经常使用的时间同步server地址
  5. 用VC开发串口通信dll控件
  6. agv ti 毫米波雷达_激光雷达VS毫米波雷达 谁才是自动驾驶“头号玩家”?
  7. 新课推荐 | 用 Django 快速搭建 API 测试工具
  8. axure 素材_Axure原型:超漂亮的系统首页
  9. java 8.0 sinffer_jpcap 配置方法,问题解决,模拟sniffer程序。(附JAVA程序,jar,dll包等环境)...
  10. python打印二进制内容_在python中打印出c类型的二进制表示
  11. IOS炫酷的引导界面
  12. 20190915:(leetcode习题)对称二叉树
  13. 数据结构排序系列详解之四 快速排序
  14. RS485转USB插电脑上通讯不上
  15. PXE工作流程(PXE启动报错处理)
  16. BP神经网络算法基本原理,bp神经网络算法详解
  17. 计算机学院运动会海报,《图文》运动会宣传海报资料
  18. 用prototype 方法$A() uncheck radio button
  19. joycon手柄拆解_任天堂switch手柄怎么拆解图文教程 教你如何拆joycon
  20. 树莓派开发板入门学习笔记2:[转]树莓派系统在VM中能做什么

热门文章

  1. 哪吒的师父太乙真人在神仙界是什么咖位的?
  2. 在职场,没有永远的朋友,也没有永远的敌人,因为只有利益
  3. davfs挂载与使用缺陷
  4. [leetcode]326. Power of Three(c语言)
  5. android各版本市场占有率报告,你用的是哪个版本 Android系统报告:果冻豆市占率升至62%...
  6. tfs 文件系统部署_使用SQL Server数据工具和使用自定义工作流文件的TFS部署到多个数据库
  7. sql查询非ascii字符_SQL替换:如何在SQL Server中替换ASCII特殊字符
  8. 使用数据库维护计划SQL Server更新统计信息
  9. 使用开源框架Sqlsugar结合mysql开发一个小demo
  10. Python学习 Day7 Python3 函数