关闭

zoj 3590 -3+1

479人阅读 评论(0) 收藏 举报
-3+1

Time Limit: 2 Seconds      Memory Limit: 65536 KB

ZOJ is 10 years old! For celebrating, we are offering the easiest problem in the world to you.

Recently we received a long sequence. We can modify the sequence once by the following two steps.

  1. Choose any element in the sequence, say x(satisfying x ≥ 3), and subtract 3 from x.
  2. Choose any element in the sequence, say x, and add 1 to x.

Now, we want to know how many times at most the sequence can be modified.

Input

The input contains multiple test cases. For each case, the first line contains an integer n(1 ≤ n ≤ 20000). The second line contains n integers describing the sequence. All the numbers in the sequence are non-negative and not greater than 1000000.

Output

Output number of times at most the sequence can be modified, one line per case.

Sample Input

1
10
2
10 11

Sample Output

4
10

Author: ZHUANG, Junyuan
Contest: ZOJ 10th Anniversary Contest



#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>

#undef _DEBUG

#ifdef _DEBUG
#define debug_printf(...) printf(__VA_ARGS__)
#else
#define debug_printf(...) 0
#endif

#undef assert
#define assert(x) ((x) || (exit(-1), 0))

void solve()
{
    int n;
    if (scanf("%d", &n) == EOF) {
        exit(0);
    }

    long long answer = 0;

    long long fours = 0;
    long long threes = 0;
    long long twos = 0;
    long long ones = 0;
    long long zeros = 0;
    long long rests = 0;

    for (int i=0; i<n; ++i) {
        int a = 0;
        scanf("%d", &a);

        if (a == 0) {
            ++zeros;
        } else if (a == 1) {
            ++ones;
        } else if (a == 2) {
            ++twos;
        //} else if (a == 3) {
        //    ++threes;
        //} else if (a == 4) {
        //    ++fours;
        } else {
            //assert(a > 4);
            assert(a >= 3);

            rests += a / 3;
            if (a % 3 == 0) {
                ++zeros;
            } else if (a % 3 == 1) {
                ++ones;
            } else if (a % 3 == 2) {
                ++twos;
            }

            answer += a / 3 * 2;

            //if (a % 2 == 0) {
            //    rests += 2;
            //    answer += a - 2;
            //} else {
            //    rests += 1;
            //    answer += a - 1;
            //}

            //++zeros;
        }
    }

    debug_printf("\nones = %d\n", ones);
    debug_printf("twos = %d\n", twos);
    debug_printf("threes = %d\n", threes);
    debug_printf("fours = %d\n", fours);
    debug_printf("rests = %d\n", rests);
    debug_printf("zeros = %d\n", zeros);

    //rests += threes;
    //zeros += threes;
    //answer += threes + threes;
    //threes = 0;

    //rests += fours;
    //ones += fours;
    //answer += fours + fours;
    //fours = 0;

    debug_printf("\nones = %d\n", ones);
    debug_printf("twos = %d\n", twos);
    debug_printf("threes = %d\n", threes);
    debug_printf("fours = %d\n", fours);
    debug_printf("rests = %d\n", rests);
    debug_printf("zeros = %d\n", zeros);

    while (rests >= 1 && twos > 0) {
        if (rests > twos) {
            zeros += twos;
            answer += twos + twos;
            twos -= twos;
        } else {
            zeros += rests;
            answer += rests + rests;
            twos -= rests;
        }
    }

    debug_printf("\nones = %d\n", ones);
    debug_printf("twos = %d\n", twos);
    debug_printf("threes = %d\n", threes);
    debug_printf("fours = %d\n", fours);
    debug_printf("rests = %d\n", rests);
    debug_printf("zeros = %d\n", zeros);

    while (rests >= 2 && ones > 0) {
        if (rests / 2 > ones) {
            zeros += ones;
            answer += ones + ones;
            rests -= ones;
            ones -= ones;
        } else {
            zeros += rests / 2;
            answer += rests / 2 * 2;
            ones -= rests / 2;
            rests -= rests / 2;
        }
    }

    debug_printf("\nones = %d\n", ones);
    debug_printf("twos = %d\n", twos);
    debug_printf("threes = %d\n", threes);
    debug_printf("fours = %d\n", fours);
    debug_printf("rests = %d\n", rests);
    debug_printf("zeros = %d\n", zeros);

    while (rests >= 3 && zeros > 0) {
        answer += rests / 3 * 2;
        rests -= rests / 3 * 2;
    }

    assert (zeros + ones + twos + threes + fours == n);

#ifndef ONLINE_JUDGE
    printf ("%I64d\n", answer >> 1);
#else
    printf ("%lld\n", answer >> 1);
#endif
}

int main(int argc, char **argv)
{
    while (true) {
        solve();
    }
}


0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:65984次
    • 积分:1564
    • 等级:
    • 排名:千里之外
    • 原创:94篇
    • 转载:0篇
    • 译文:0篇
    • 评论:15条
    最新评论