"2017 today's headline" Headlines (JAVA) __ Online Programming Questions

Source: Internet
Author: User

The title of the headline 2017 school recruit began. For this school recruit, we organized a large scale of the Test team, each of the subjects have a number of interesting topics, and we now want to combine these topics into a number of tests out, before the topic, we have a blind trial of the topic, and set out the difficulty of each problem system. A test contains 3 open topics, assuming that their difficulty from small to large is a,b,c, we hope that these 3 questions can meet the following conditions:
A<=b<=c
b-a<=10
c-b<=10
All the questions were given a "n" Open topic. Now we want to distribute these n questions to a number of exams (1 or more games, each question must be used and can only be used once, however, due to the above conditions, there may be some exams can not be enough to gather 3 questions, so the author will need to have some more appropriate difficulty of the subject to allow each test to meet the requirements, However, we have been very tired of the question, you can calculate that we need to do at least a few more questions. Enter a description:

The first line of input contains an integer n, which indicates the number of titles that are currently in good order.

the second line gives the difficulty coefficient of each topic d1,d2,..., DN. 

data range

for 30% of the data, 1≤n,di≤5;

For 100% of the data, 1≤n≤10^5,1≤di≤100.

in the sample, a feasible solution is to add 2 problems with 20 and 50 difficulty, which can be combined into two tests: (20 20 23) and (35,40,50).
Output Description:
The output includes only one row, the answer that is being asked.
Example 1 input
4  
20 35 23 40
Output
2

Answer Description

1. The difficulty of the degree of sorting, from small to large, starting from the second number of traversal.

2. Define a sign t that represents the first digit of the current number as the number of three digits.

3. Compare the interval between the current number and the previous number.

4. If the interval is less than 10, then t++, skip, do not do processing. If T is greater than or equal to 3, the whole set is already pooled, and the T is assigned a value of 1.

5. If the interval is less than 20 and the previous number is the first number, then you need to insert only one number in the middle to make a group. At this point, count++,t = 3;

6. If the previous number's tag value is t = = 1, and the interval is greater than 20, it means that you need to add two digits after the sum of money to make the previous number a group.

7. If money a number of tagged value t = = 2; And the interval is greater than 10, it means that you need to add a number after the sum of money to make the first two numbers into a group.

8. The last number may not be processed in the loop, so it needs to be added outside the loop.


Import Java.util.Arrays;
Import Java.util.Scanner;

public class Main {public
    static void Main (string[] args) {
        Scanner Scanner = new Scanner (system.in);
        int n = scanner.nextint ();

        Int[] D = new Int[n];

        Arrays.sort (d);
        int t = 1;
        int count = 0;
        for (int i = 1; i < n; i++) {
            if (T < 3) {
                if (D[i]-d[i-1] <=) {
                    t++;
                } else if (t = =  1 && d[i]-d[i-1] <=20) {
                    count++;
                    t = 3;
                } else {
                    count+= 3-t;
                    t = 1;
                }
            } else {
                t = 1;
            }
        }
        Count + = 3-t;
        System.out.println (count);
    }
}





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.