Codeforce 214 Div 2 B.Hometask

题目描述:

Description

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.

Input

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.

Output

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

Sample Input

Input

1
0

Output

0

Input

11
3 4 5 4 5 3 5 3 4 4 0

Output

5554443330

Input

8
3 2 5 1 5 2 2 3

Output

-1

就是给你n个数,让你求由这n个数组成的,能被2,3,5整除的的最大的数。

思路:

这道题本来是用来练手速的,没想到一做就是一个下午。最后还是看了BMan的代码才勉强AC的,不过看了BMan的代码之后,真的学到了很多的东西!例如,学到了CF里面有一个ONLINE_JUDGE的宏变量,可以利用条件编译来方便测设与提交!最重要的是学到了一个思路,那就是预处理的优美之处!

具体做法是:因为答案要能被3整除,所以各位数之和必须为3的倍数!如果是,直接输出;否则的话有两种可能:

1. sum % 3 == 1 这种情况只需在数列中找到一个num[i] % 3 == 1 的数去掉就可以咯,如果没有就找两个num[] % 3 == 2 去掉就可以了!

2..sum % 3 == 2,这种情况跟上一种是基本一致的,就不在赘述了!

这个题目有一个易错点,就是在判断前导0,BMan给出了一个很强大的做法,具体的做法请看代码实现:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#define N 1000010

using namespace std;

void debug()
{
#ifdef ONLINE_JUDGE
#else
    freopen( "in.txt", "r", stdin );
#endif // JUDGE_ONLINE
}

int main()
{
    debug();
    //freopen( "in.txt", "r", stdin );
    int n;
    while( scanf( "%d", &n ) != EOF )
    {
        int num[N] = {0};
        int vis[N];
        int sum = 0;
        memset( vis, 0, sizeof( vis ) );
        for( int i = 0; i < n; i++ )
        {
            scanf( "%d", &num[i] );
            sum += num[i];
        }
        sort( num, num + n, greater<int>() );
        if( sum % 3 == 1 )
        {
            for( int i = n-1; i >= 0; i-- )
            {
                if( num[i] % 3 == 1 )
                {
                    vis[i] = 1;
                    sum -= num[i];
                    break;
                }
            }
            int two = 0;
            if( sum % 3 == 1 )//说明找不到num[i] % 3 == 1 的num
            {
                for( int i = n-1; i >= 0; i-- )
                {
                    if( num[i] % 3 == 2 )
                    {
                        vis[i] = 1;
                        if( ++two >= 2 )
                        {
                            break;
                        }
                    }
                }
                if( two != 2 )
                {
                    printf( "-1\n" );
                    continue;
                }
            }
        }
        if( sum % 3 == 2 )
        {
            for( int i = n-1; i >= 0; i-- )
            {
                if( num[i] % 3 == 2 )
                {
                    vis[i] = 1;
                    sum -= num[i];
                    break;
                }
            }
            int one = 0;
            if( sum % 3 == 2 )
            {
                for( int i = n-1; i >= 0; i-- )
                {
                    if( num[i] % 3 == 1 )
                    {
                        vis[i] = 1;
                        if( ++one >= 2 )
                        {
                            break;
                        }
                    }
                }
                if( one != 2 )
                {
                    printf( "-1\n" );
                    continue;
                }
            }
        }
        int cnt = 0;
        for( int i = 0; i < n; i++ )
        {
            if( !vis[i] )
            {
                num[cnt++] = num[i];
            }
        }
        n = cnt;
        if( n == 0 || num[n-1] != 0 )
        {
            printf( "-1" );
        }
        else
        {
            int i = 0;
            while( i < n - 1 && num[i] == 0 )//判断前导0,如果一开始num[0]就等于0的话,由于已经进行了降序的排序,所以后面一定是0,这样的话与后面的代码结合就能够保证
                                                                    //前导0的只输出一个0
            {
                i++;
            }
            for( ; i < n; i++ )
            {
                printf( "%d", num[i] );
            }
        }
        putchar( 10 );
    }
    return 0;
}

通过今天的学习,我发现学习大神们的代码对提升自己的编码能力是有着很大的帮助的!要多看别人的代码!!!

One Day One Step!




Codeforce 214 Div 2 B.Hometask,布布扣,bubuko.com

时间: 2024-10-12 00:30:45

Codeforce 214 Div 2 B.Hometask的相关文章

Codeforces Round #214 (Div. 2)——Dima and Salad

题目链接 题意: 一行a[i],一行b[i],a和b是一一对应的.选取任意个数对,使得sigma(a)/ sigma(b)等于k,求这时候sigma(a)的最大值 分析: 这个题目关键在于对sigma(a)/ sigma(b)== k的处理.对于这种式子,用每个数的比值显然是不行的,因为没法累加:而且是double型,没法DP 考虑一个每个数对对这个式子的影响,如果每个数都是a = k * b,那么显然是可以的:如果a小于k * b,那么在整体中,当前数对少的数肯定要有一些数对来补偿,也就是说,

Codeforces Round #214 (Div. 2)---C. Dima and Salad

Dima, Inna and Seryozha have gathered in a room. That's right, someone's got to go. To cheer Seryozha up and inspire him to have a walk, Inna decided to cook something. Dima and Seryozha have n fruits in the fridge. Each fruit has two parameters: the

Codeforces Round #214 (Div. 2) C. Dima and Salad 背包

C. Dima and Salad Dima, Inna and Seryozha have gathered in a room. That's right, someone's got to go. To cheer Seryozha up and inspire him to have a walk, Inna decided to cook something. Dima and Seryozha have n fruits in the fridge. Each fruit has t

codeforce 379(div.2)

A.B略 C题 --贪心,二分查找: 对于每一个a[i], 在d中二分查找 s-b[i],注意不要忘记计算速度为x时需要花费的最小时间,以及整数范围为64位整数 1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 5 using namespace std; 6 const int maxn = 2*100000+10; 7 typedef long long LL; 8 9 int n,

codeforce 380(div.2)

A B 略 C:二分,贪心 设d(i, v)为 剩余油量为v时,车开距离i 所需要的最小时间,使用线性规划不难算出: if v < i return INF; //无法到达 if v > 2*i return i; if i <= v <= 2*i return LL(3)*i - v; 那么一辆车开到终点的最短时间等于 ∑d(g[i]-g[i-1], v)这样只需要二分能开到终点的最小油量,取其中价格最低的即是答案代码如下: 1 #include <cstdio> 2

codeforce Round304 div.2 D

题意: Two soldiers are playing a game. At the beginning first of them chooses a positive integer n and gives it to the second soldier. Then the second one tries to make maximum possible number of rounds. Each round consists of choosing a positive integ

「日常训练」Skills(Codeforce Round Div.2 #339 D)

题意(CodeForces 614D) 每个人有\(n(n\le 10^5)\)个技能,技能等级都在\([0,10^9]\)的范围,每个技能有一个当前等级,所有技能的最高等级都为A.一个人的力量被记做以下两项的和: 1. 顶级技能的个数 *cf 2. 最低等级的技能 *cm 每个单位的钱能够提升一级力量.我们希望花尽可能少的钱,使得力量尽可能高. 分析 我二分的功力还是不足,要多努力.这题其实是一个非常明显的暴力:我们枚举提高到A的等级的个数(到不能提升为止),枚举这种情况下,我们能够令把多少人

DIV+CSS实现仿京东商城导航条效果

1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3 <html xmlns="http://www.w3.org/1999/xhtml"> 4 <head> 5 <meta http-equiv=&quo

PostCSS 实战

专题截图: 项目截图: 目录说明: dest/ 发布代码文件夹: src/              预编译代码文件夹; node_modules    node 插件; gulpfile.js           gulp的配置文件; package.json      npm 配置文件; src/待编译文件夹: c/        css文件目录; i/ img文件目录; dest/发布文件夹: c/        css文件目录; i/ img文件目录; package.json 文件展示