题目链接: 传送门

Hometask

Time Limit: 2 seconds     Memory Limit: 256 megabytes

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

1
0

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

8
3 2 5 1 5 2 2 3

Sample Output

0

5554443330

-1

思路:

题目大意:给一个都是数字的集合,组成一个尽量大的数使之能被2,3,5整除。
找出集合中是否有9,没有则停止判断。对于给出的数字集合,看总和是否能被3整除,能直接从大到小输出,不行的话,对3取模,对取模结果(1或2)单独判断,删除集合中某些数字是否能被3整除,不行则停止。输出是忽略前导0。(训练赛时在对模结果为1的特判中折腾了好久。模结果为1,则可以删去集合中一个对3取模也为1的数,或是删去两个对3取模为2的数,模结果为2同理。 官方题解

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef __int64 LL;
struct Node
{
    LL cnt,val;
    bool flag;
    Node():cnt(0),val(0),flag(false) {}
};
bool cmp(Node x,Node y)
{
    return x.val > y.val;
}

int main()
{
    LL N,tmp,maxx = 0,sum = 0;
    bool Isz = false;
    Node num[15];
    scanf("%I64d",&N);
    for (int i = 0; i < N; i++)
    {
        scanf("%I64d",&tmp);
        sum += tmp;
        num[tmp].val = tmp;
        num[tmp].cnt++;
        num[tmp].flag = true;
        if (tmp > maxx)
        {
            maxx = tmp;
        }
        if (tmp == 0)
        {
            Isz = true;
        }
    }
    if (!Isz)
    {
        printf("-1\n");
    }
    else
    {
        if (sum % 3 == 0)
        {
            if (num[maxx].val == 0)
            {
                printf("0\n");
            }
            else
            {
                for (int i = maxx; i >= 0; i--)
                {
                    if(num[i].flag)
                    {
                        while (num[i].cnt--)
                        {
                            printf("%I64d",num[i].val);
                        }
                    }
                }
                printf("\n");
            }
        }
        else
        {
            LL r = sum % 3;
            bool Can = false;
            LL cnt = 0;
            if (r == 1)
            {
                for (int i = 1; i <= maxx ; i+=3)
                {
                    if (num[i].flag && num[i].cnt)
                    {
                        Can = true;
                        num[i].cnt--;
                        if (num[i].cnt == 0)
                        {
                            num[i].flag = false;
                            num[i].val = 0;
                        }
                        break;
                    }
                }
                if(!Can)
                {
                    for (int i = 2;i <= maxx;i+=3)
                    {
                        if (num[i].flag && num[i].cnt)
                        {
                            while (num[i].cnt--)
                            {
                                cnt++;
                                if (num[i].cnt == 0)
                                {
                                    num[i].flag = false;
                                }
                                if (cnt == 2 || num[i].cnt == 0)
                                {
                                    if (cnt == 2)
                                    {
                                        Can = true;
                                    }
                                    break;
                                }
                            }
                            if (cnt == 2)
                            {
                                Can = true;
                                break;
                            }
                        }
                    }
                }
            }
            else if (r == 2)
            {
                for (int i = 2; i <= maxx; i+=3)
                {
                    if (num[i].flag && num[i].cnt)
                    {
                        Can = true;
                        num[i].cnt--;
                        if(num[i].cnt == 0)
                        {
                            num[i].flag = false;
                            num[i].val = 0;
                        }
                        break;
                    }
                }
                if (!Can)
                {
                    for (int i = 1; i <= maxx; i+=3)
                    {
                        if (num[i].flag && num[i].cnt)
                        {
                            while (num[i].cnt--)
                            {
                                cnt++;
                                if (num[i].cnt == 0)
                                {
                                    num[i].flag = false;
                                }
                                if (cnt == 2 || num[i].cnt == 0)
                                {
                                    if (cnt == 2)
                                    {
                                        Can = true;
                                    }
                                    break;
                                }
                            }
                        }
                        if (cnt == 2)
                        {
                            Can = true;
                            break;
                        }
                    }
                }
            }
            if (!Can)
            {
                printf("-1\n");
            }
            else
            {
                bool Noz = false;
                for (int i = 0;i <= maxx;i++)
                {
                    if (num[i].flag && num[i].val)
                    {
                        Noz = true;
                        break;
                    }
                }

                if (Noz)
                {
                    for (int i = maxx;i >= 0;i--)
                    {
                        if (num[i].flag && num[i].cnt)
                        {
                            while (num[i].cnt--)
                            {
                                printf("%I64d",num[i].val);
                            }
                        }
                    }
                    printf("\n");
                }
                else
                {
                    printf("0\n");
                }
            }
        }
    }
    return 0;
}
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

int main()
{
    int N;
    while (~scanf("%d",&N))
    {
        int ans[10] = {0};
        int tmp,sum = 0;
        bool flag = false;
        for (int i = 0;i < N;i++)
        {
            scanf("%d",&tmp);
            ans[tmp]++;
            sum += tmp;
        }
        if (ans[0] == 0)
        {
            printf("-1\n");
        }
        else
        {
            flag = true;
            if (sum % 3 == 1)
            {
                if (ans[1] + ans[4] + ans[7])
                {
                    for (int i = 1;i <= 7;i += 3)
                    {
                        if (ans[i])
                        {
                            ans[i]--;
                            break;
                        }
                    }
                }
                else if (ans[2] + ans[5] + ans[8] >= 2)
                {
                    for (int i = 2,j = 1;i <= 8;i+=3)
                    {
                        while (ans[i] && j <= 2)
                        {
                            ans[i]--;
                            j++;
                        }
                    }
                }
            }
            else if (sum % 3 == 2)
            {
                if (ans[2] + ans[5] + ans[8])
                {
                    for (int i = 2;i <= 8;i += 3)
                    {
                        if (ans[i])
                        {
                            ans[i]--;
                            break;
                        }
                    }
                }
                else if (ans[1] + ans[4] + ans[7] >= 2)
                {
                    for (int i = 1,j = 1;i <= 7;i += 3)
                    {
                        while (ans[i] && j <= 2)
                        {
                            ans[i]--;
                            j++;
                        }
                    }
                }
            }
        }

        if (flag)
        {
            if (ans[1] + ans[2] + ans[3] + ans[4] + ans[5] + ans[6] + ans[7] + ans[8] + ans[9])
            {
                for (int i = 9;i >= 0;i--)
                {
                    while (ans[i]--)
                    {
                        printf("%d",i);
                    }
                }
            }
            else
            {
                printf("0");
            }
            printf("\n");
        }
    }
    return 0;
}

后来参考了别人代码= =果然自己太native,运用一点数学思想可以省略很多特判= =还是要注重多思考啊。

CF 214B Hometask(想法题)的更多相关文章

  1. HDU 4972 Bisharp and Charizard 想法题

    Bisharp and Charizard Time Limit: 1 Sec  Memory Limit: 256 MB Description Dragon is watching NBA. He ...

  2. CodeForces 111B - Petya and Divisors 统计..想法题

    找每个数的约数(暴力就够了...1~x^0.5)....看这约数的倍数最后是哪个数...若距离大于了y..统计++...然后将这个约数的最后倍数赋值为当前位置...好叼的想法题.... Program ...

  3. HDU - 5806 NanoApe Loves Sequence Ⅱ 想法题

    http://acm.hdu.edu.cn/showproblem.php?pid=5806 题意:给你一个n元素序列,求第k大的数大于等于m的子序列的个数. 题解:题目要求很奇怪,很多头绪但写不出, ...

  4. CF 701B Cells Not Under Attack(想法题)

    题目链接: 传送门 Cells Not Under Attack time limit per test:2 second     memory limit per test:256 megabyte ...

  5. CF 405C Unusual Product(想法题)

    题目链接: 传送门 Domino Effect time limit per test:1 second     memory limit per test:256 megabytes Descrip ...

  6. CF 405B Domino Effect(想法题)

    题目链接: 传送门 Domino Effect time limit per test:1 second     memory limit per test:256 megabytes Descrip ...

  7. HDU - 5969 最大的位或 想法题

    http://acm.hdu.edu.cn/showproblem.php?pid=5969 (合肥)区域赛签到题...orz 题意:给你l,r,求x|y的max,x,y满足l<=x<=y ...

  8. Codeforces Round #354 (Div. 2)-C. Vasya and String,区间dp问题,好几次cf都有这种题,看来的好好学学;

    C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. cf Canada cup A题

    A. Jumping Ball time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

随机推荐

  1. JavaScript高级程序设计笔记 事件冒泡和事件捕获

    1.事件冒泡 要理解事件冒泡,就得先知道事件流.事件流描述的是从页面接收事件的顺序,比如如下的代码: <body> <div> click me! </div> & ...

  2. <实训|第九天>掌握linux中普通的权限控制和三种特殊的权限(sst),做合格的运维工程师

    linux中,权限的学习是必不可少的,不论是作为一名运维工程师或者是单一的管理者,学习好linux中的权限控制,你就可以保护好自己的隐私同时规划好你所管理的一切. 权限的学习是很多的,不要认为自己已经 ...

  3. 拦截PHP各种异常和错误,发生致命错误时进行报警,万事防患于未然

    在日常开发中,大多数人的做法是在开发环境时开启调试模式,在产品环境关闭调试模式.在开发的时候可以查看各种错误.异常,但是在线上就把错误显示的关闭. 上面的情形看似很科学,有人解释为这样很安全,别人看不 ...

  4. SQLServer数据导入Mongodb

    一.思路 MongoVUE免费版支持MySQL导入Mongo,所以思路是SQLServer导入MySQL,再从MySQL导入Mongo. 二.准备 1,安装mysql数据库(我用的是WAMP,集成my ...

  5. 安装.NET Framework后程序无法启动的错误处理

    最近发现一直在使用的Database.NET软件无法正常使用了,表现为当尝试进行Sql Server的连接创建时,直接报错 在事件查看器具体错误信息为: 日志名称:          Applicat ...

  6. android之短信拦截器

    下面通过短信拦截器来介绍短信中的广播 布局文件 在布局文件中可以设置需要拦截的号码 <?xml version="1.0" encoding="utf-8" ...

  7. Android应用崩溃后异常捕获并重启并写入日志

    在Android开发时,有时会因为一些异常导致应用报错,偶尔会因为错误 而崩溃,导致用户体验下降,为了解决这问题,我们就要对这样的异常处理: 代码如下: CrashHandler.java impor ...

  8. 二、处理MVC多级目录问题——以ABP为基础架构的一个中等规模的OA开发日志

    就个人感觉而言.ASP.NET MVC是一种非常反人类的设计.(我没有接触过Java的MVC,不知道两者是否一样.如果一样,那么搞Java的同学也挺可怜.)尤其是MVC的路由机制,灰常灰常反动.路由所 ...

  9. SpringMVC 部署项目静态资源文件访问问题

    问题:采用SpringMVC 部署项目后程序加载或用浏览器访问时出现类似的警告,2011-01-19 10:52:51,646 WARN [org.springframework.web.servle ...

  10. eclipse-插件安装的三种方式

    (前两种安装方式以多国语言包的安装为例) 1.  普通安装:用直接解压的安装方式来实现 解压插件到某个文件夹 将下载的插件文件解压到 Eclipse 的安装目录下 如插件文件为多国语言包: NLpac ...