-3 + 1

Source: Internet
Author: User
Question details

There is a series where all numbers are non-negative integers. You can perform one operation as follows (note that the following two steps must be completed in one complete operation ):

(1) Select a number not less than 3 and reduce it by 3.

(2) increase the number by 1.

How many operations can be performed at most?

Input Format:

Multiple groups of data. The first row of each group of data is a positive integer N, indicating the number of numbers in the series. (1 <= n <= 20000)

The second row contains N non-negative integers separated by spaces. Each integer cannot exceed 1000000.

Output Format:

Output a row of data in each group, indicating the maximum number of operations that can be performed.


Input example:

1

10

2

10 11

Output example:

4

10



Solutions

For each number X: Use sum to record the numbers x/3 that can be added. Use an array to save the remainder (0, 1, 2) after each number % 3.

It is required that the operation be performed every 3-3 times in sum, and the maximum number of operations plus 1 in 0, 1, 2 is allowed.

The greedy idea is used to finish all 2 operations first, then finish all 1 operations, and finally consider the case of 0.


# Include <iostream> # include <cstdio> # include <cstring> # include <cstdlib> # include <cmath> using namespace STD; typedef long ll; ll sum, A [3]; int main () {// freopen ("C: \ Users \ Administrator \ Desktop \ kd.txt", "r", stdin ); ll N; while (~ Scanf ("% LLD", & N) {sum = 0; ll res = 0; A [1] = A [2] = 0; For (INT I = 0; I <n; I ++) {ll X; scanf ("% LLD", & X); sum + = x/3; X % = 3; A [x] ++; // statistics} res = sum; If (sum> 0) RES + = A [2]; while (1) // 1 {ll TMP = min (sum> 1, a [1]); Res + = TMP; sum-= TMP; A [1]-= TMP; if (sum <1 | A [1] <1) break;} while (1) // 0 {ll TMP = sum/3; sum % = 3; sum + = TMP; Res + = TMP; If (sum <= 2) break;} printf ("% LLD \ n", Res);} return 0 ;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.