题目链接:

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

Hometask

time limit per test:2 seconds
memory 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;
}

Codeforces Round #131 (Div. 2) B. Hometask dp的更多相关文章

  1. Codeforces Round #131 (Div. 1) B. Numbers dp

    题目链接: http://codeforces.com/problemset/problem/213/B B. Numbers time limit per test 2 secondsmemory ...

  2. Codeforces Round #131 (Div. 2) E. Relay Race dp

    题目链接: http://codeforces.com/problemset/problem/214/E Relay Race time limit per test4 secondsmemory l ...

  3. Codeforces Round #131 (Div. 2)

    A. System of Equations \(a\)的范围在\(\sqrt n\)内,所以暴力枚举即可. B. Hometask 需要被2.5整除,所以末位必然为0,如果0没有出现,则直接返回-1 ...

  4. Codeforces Round #276 (Div. 1) D. Kindergarten dp

    D. Kindergarten Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/proble ...

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

    A. Boredom Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/problem/A ...

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

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

  7. Codeforces Round #539 (Div. 2) 异或 + dp

    https://codeforces.com/contest/1113/problem/C 题意 一个n个数字的数组a[],求有多少对l,r满足\(sum[l,mid]=sum[mid+1,r]\), ...

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

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

  9. Codeforces Round #202 (Div. 1) D. Turtles DP

    D. Turtles Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/547/problem/B ...

随机推荐

  1. js一些稀奇古怪的写法-带你装逼带你飞

    //定时器的第三个参数 setInterval(function(str1,str2,num){ alert(str1+str2+num) },1000,'参数1','还可以有很多参数,不同的类型.. ...

  2. 将raw里面的数据库文件写入到data中

    package com.city.list.db; import java.io.File; import java.io.FileNotFoundException; import java.io. ...

  3. jQuery基础知识— 获得内容和属性

    jQuery拥有可操作HTML元素和属性的方法. 获得内容: text()--设置或返回所选元素的文本内容 html()--设置或返回所选元素的内容(包括HTML标记) val()--设置或返回表单字 ...

  4. 神奇的脱机 app_offline.htm

    在更新或维护网站的时候,需要给用户一个明确的提示; 如"系统正在维护中..."."系统更新中..." 等信息 这里介绍园友告知的一个超级简单的做法:  在网站根 ...

  5. 用web查看hadoop运行状态

    转载--http://www.weixuehao.com/archives/621 我们安装完hadoop,下面我们从视觉上看看hadoop怎么玩的. 我们可以在win7系统上,通过web界面,在浏览 ...

  6. 相比于汇编语言的准确性c语言延时精确度如何提升

    只要合理的运用,C还是可以达到意想不到的效果.很多朋友抱怨C效率比汇编差了很多,其实如果对Keil C的编译原理有一个较深入的理解,是可以通过恰当的语法运用,让生成的C代码达到最优化.即使这看起来不大 ...

  7. GPS之NMEA协议20160526

    NMEA 0183是美国国家海洋电子协会(National Marine Electronics Association)为海用电子设备制定的标准格式.现在已经成为GPS导航设备统一的RTCM(Rad ...

  8. Python核心编程--学习笔记--9--文件和输入输出

    本章将深入介绍Python的文件处理和相关输入输出能力,包括:文件对象(以及它的内建函数.内建方法和属性),标准文件,文件系统的访问方法,文件执行,最后简要涉及持久存储和标准库中与文件有关的模块. 1 ...

  9. java reflect 初始学习 动态加载类

    首先要理解Class类: 在java 的反射中,Class.forName("com.lilin.Office") 使用类的全名,这样获取,不仅仅表示了类的类类型,同时还代表着类的 ...

  10. oracle 归档/非归档

    1.查看oralce是归档模式还是非归档模式 SQL> select name,log_mode from v$database; NAME LOG_MODE------------------ ...