zoj 3590 -3+1

原创 2012年03月27日 21:18:52
-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();
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

【模拟】ZOJ-3590 -3+1

今天浙大校赛最水的一道题,纯模拟……具体见代码…… 题目 #include #include using namespace std; template T Max(T x,T y){re...

poj3590

【题意】 给定一个整数n,求所有n元置换循环最长的长度(即经过多少次置换变回1..n的序列),并输出字典序最小的该方案 【输入】 第一行一个数t表示几组数据 接下来t行每行一个数字表示数字n ...

POJ 3590 The shuffle Problem 置换+DP

题意:对每一个置换T,都存在一个T^k = e。现在让你求一个n元置换,使得它的阶最大,即当T^k = e时,k最大。若同时存在多个这样的T,那么输出其中排序最小的。 题解:由于每一个置换都可以分解...
  • Tsaid
  • Tsaid
  • 2012-03-24 00:51
  • 1444

BZOJ3590【状压DP】

转自:http://blog.csdn.net/Lethelody/article/details/45147055 orz大神凯爷 原文 SNOI2013竟然出了一道很...

pku3590 The shuffle Problem

#include #include #include #include #include #include #include #include #include #include #include #...

HDU 3590 PP and QQ(树上删边游戏 + anti-sg)

题意: N<=100颗树,每棵数有m<=100个点,双方每次可以选择一条边删去,并且把不与根相连的边一并删去,不能操作者赢N<=100颗树,每棵数有m<=100个点,双方每次可以选择一条边删去,并...
  • lwt36
  • lwt36
  • 2015-11-25 03:39
  • 213

[ZOJ 3682] E - Cup 3 (背包DP计数 + 滚动数组)

ZOJ - 3682 有S1S_1个球迷支持巴萨,S2S_2个球迷支持皇马 球场有NN个方阵,每个可以坐 kik_i个球迷 安排球迷使得一个方阵要么全是支持巴萨的 要么全是支持皇...

BFS广度优先搜索(3)--poj2251(zoj1940)(基础题)

Dungeon Master Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%lld & %llu Description ...

ZOJ 3682 E - Cup 3

二维DP,用a[i][j]表示前i个广场容纳j个球迷的方案数,这里的球迷或为Barcelona,或为Madrid。由于题目保证两队人数与广场总容量相等,则可选择人数较少的队DP。不过这样的话用来存储中...
  • zu_xu
  • zu_xu
  • 2013-03-25 22:30
  • 669

ZOJ 3682 E - Cup 3

题意: 有巴塞罗纳和马德里球迷s1,s2。同时有n个广场,每个广场能容纳一定的球迷。为了让比赛是和谐的,所以要分配两边的球迷入座这n个广场。对于单个广场,只有三中情况是和谐的:1.全是巴塞罗纳球迷 2...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)