Codeforces Round #131 (Div. 2) B. Hometask dp
题目链接:
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;
}
Codeforces Round #131 (Div. 2) B. Hometask dp的更多相关文章
- Codeforces Round #131 (Div. 1) B. Numbers dp
题目链接: http://codeforces.com/problemset/problem/213/B B. Numbers time limit per test 2 secondsmemory ...
- Codeforces Round #131 (Div. 2) E. Relay Race dp
题目链接: http://codeforces.com/problemset/problem/214/E Relay Race time limit per test4 secondsmemory l ...
- Codeforces Round #131 (Div. 2)
A. System of Equations \(a\)的范围在\(\sqrt n\)内,所以暴力枚举即可. B. Hometask 需要被2.5整除,所以末位必然为0,如果0没有出现,则直接返回-1 ...
- Codeforces Round #276 (Div. 1) D. Kindergarten dp
D. Kindergarten Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/proble ...
- 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 ...
- Codeforces Round #533 (Div. 2) C.思维dp D. 多源BFS
题目链接:https://codeforces.com/contest/1105 C. Ayoub and Lost Array 题目大意:一个长度为n的数组,数组的元素都在[L,R]之间,并且数组全 ...
- Codeforces Round #539 (Div. 2) 异或 + dp
https://codeforces.com/contest/1113/problem/C 题意 一个n个数字的数组a[],求有多少对l,r满足\(sum[l,mid]=sum[mid+1,r]\), ...
- Codeforces Round #374 (Div. 2) C. Journey DP
C. Journey 题目连接: http://codeforces.com/contest/721/problem/C Description Recently Irina arrived to o ...
- 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 ...
随机推荐
- luigi学习7--running from command line
最简单去运行一个luigi task的方式是通过luigi命令行工具. 示例代码: # my_module.py, available in your sys.path import luigi cl ...
- php字符串截取问题
希望将一个字符串限长显示,如果该字符串超过一定长数,就截取前n个字符,后加省略号. 但是在英文和汉字混合的情况下会出现如下问题: 如果有这样一个字符串 $str="这是一个字符串" ...
- php5.5新函数array_column
php5.5新增了一个新的数组函数,感觉挺使用的,低版本的实现按照如下实现 if(!function_exists('array_column')){ function array_column($i ...
- Android环境配置Sencha Touch
转自http://www.phonegap100.com/portal.php?mod=view&aid=19 作为你开发的一部分,为安卓设备开发的 Sencha Touch框架应该在安卓虚拟 ...
- div+css的优势在哪?
1.符合W3C标准.微软等公司都是他的支持者. 2.所搜引擎更加友好. 3.样式调整更加方便. 4.css简洁的代码,减少了带宽. 5.表现和结构分离.在团队开发中更容易分工 并不是取代table,t ...
- Delphi 泛型对象类
{ 很早就写了. 只针对delphixe 以上的版本可用. 希望不是自己在造轮子. } 1 unit U_ClassUtility; interface uses generics.defaults, ...
- 信驰达蓝牙4.0模块全面升级 v2.20 U最新发布
作为国际蓝牙联盟成员之一,德州仪器(TI)于2012年强势推出CC254X系列单芯片(SOC)低功耗蓝牙收发器,经典51内核,最强优势在于丰富的外围(21个IO,UART,SPI,USB2.0,PWM ...
- ORACLE-用户常用数据字典的查询使用方法
一.用户 查看当前用户的缺省表空间 SQL> select username,default_tablespace from user_users; USERNAME DEFAULT_TABLE ...
- db2新建数据库
一.建表空间和数据库 1.在db2ad.db2db和db2ap上均执行: [sql] view plaincopyprint? db2set db2comm=tcpip db2set db2codep ...
- hdu 4941 Magical Forest
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4941 Magical Forest Description There is a forest can ...