# zoj 3590 -3+1

-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.

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 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;
//threes = 0;

//rests += fours;
//ones += 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;
twos -= twos;
} else {
zeros += 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;
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
#else
#endif
}

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

• 本文已收录于以下专栏：

举报原因： 您举报文章：zoj 3590 -3+1 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)