5-stroke Array

Source: Internet
Author: User

Preface
You must read the algorithm questions every day to feel at ease. After all, it is a crucial moment for you to find a job in March. Recently, I was so busy that my eyes would have been very congested from a week to Friday. There were too many things to learn and review, plus project tasks, list several major tasks:
After reading the off offoffoff, it must be completed before January 1, July 14.
While learning the redis source code, the project must complete the server code before January 1, July 21.
Learn about the php kernel. Let's take a look at the time.
Information retrieval, taking into account the thesis. It is estimated that I will need to take out a week to attack the thesis from January 1, July 14-7 to September 21, so leave is enough.
My deepest feeling at work has always been loneliness. One of my friends from the Chinese Emy of Sciences feels similar to me. I found myself a talented person with great patience and willpower, and can hold on to it, at this stage of graduate students, the key to opening up the gap is whether you stick to what you want to learn and how to behave (ps: Except for genius)


Question
[Html] Description:
In any array, if you can find five or more continuous elements, we call it a five-stroke array. However, this array does not often exist. Your task is to add the least elements to make an array a five-click array.
Input:
Each test file contains multiple test cases, and each test case contains two rows.
The first line indicates the number of elements in the input array N, where 0 <N <= 1000.
The second row contains N non-negative integers, representing the elements of the array.
Output:
The output line of each test case represents the minimum number of elements to be added, making the array a five-click array.
Sample input:
3
1 3 4
1
1
Sample output:
2
4

Description:
In any array, if you can find five or more continuous elements, we call it a five-stroke array. However, this array does not often exist. Your task is to add the least elements to make an array a five-click array.
Input:
Each test file contains multiple test cases, and each test case contains two rows.
The first line indicates the number of elements in the input array N, where 0 <N <= 1000.
The second row contains N non-negative integers, representing the elements of the array.
Output:
The output line of each test case represents the minimum number of elements to be added, making the array a five-click array.
Sample input:
3
1 3 4
1
1
Sample output:
2
4

Ideas
A difficult topic for Samsung, because there is a negative number, it is not convenient to directly use the hash array, so you can consider:
Quick sorting
Traverse each number in sequence. The minimum number of records can be num + 4, and the output is enough.
I simulated the Quick Sort myself and reviewed it as an algorithm.


AC code
 

# Include <stdio. h> # include <stdlib. h> int partition t_partition (int * arr, int left, int right); void quick_sort (int * arr, int begin, int end); int in_array (int * arr, int n, int data); int main (void) {int I, j, min, count, n, * arr; while (scanf ("% d", & n )! = EOF) {arr = (int *) malloc (sizeof (int) * n); for (I = 0; I <n; I ++) scanf ("% d", arr + I); quick_sort (arr, 0, n-1);/* test for (I = 0; I <n; I ++) printf ("% d", arr [I]); printf ("\ n"); */for (I = 0; I <n; I ++) {for (j = arr [I], count = 0; j <= arr [I] + 4; j ++) {if (in_array (arr, n, j )) continue; else count ++;} if (I = 0) {min = count;} else {min = (min <count )? Min: count ;}} printf ("% d \ n", min); free (arr);} return 0 ;}int in_array (int * arr, int n, int data) {int I, flag = 0; for (I = 0; I <n; I ++) {if (arr [I] = data) {flag = 1; break;} return flag;} void quick_sort (int * arr, int begin, int end) {int begin; if (begin <end) {partition = maid (arr, begin, end); quick_sort (arr, begin, begin-1); quick_sort (arr, begin + 1, end);} I Nt effect_partition (int * arr, int left, int right) {int stand = arr [left]; while (left <right) {while (left <right & arr [right]> = stand) right --; if (left <right) arr [left ++] = arr [right]; while (left <right & arr [left] <= stand) left ++; if (left <right) arr [right --] = arr [left];} arr [left] = stand; return left ;} /*************************************** * *********************** Proble M: 1394 User: wangzhengyi Language: C Result: Accepted Time: 0 MS Memory: 912 kb ************************************** * *************************/# include <stdio. h> # include <stdlib. h> int partition t_partition (int * arr, int left, int right); void quick_sort (int * arr, int begin, int end); int in_array (int * arr, int n, int data); int main (void) {int I, j, min, count, n, * arr; while (scanf ("% d", & n )! = EOF) {arr = (int *) malloc (sizeof (int) * n); for (I = 0; I <n; I ++) scanf ("% d", arr + I); quick_sort (arr, 0, n-1);/* test for (I = 0; I <n; I ++) printf ("% d", arr [I]); printf ("\ n"); */for (I = 0; I <n; I ++) {for (j = arr [I], count = 0; j <= arr [I] + 4; j ++) {if (in_array (arr, n, j )) continue; else count ++;} if (I = 0) {min = count;} else {min = (min <count )? Min: count ;}} printf ("% d \ n", min); free (arr);} return 0 ;}int in_array (int * arr, int n, int data) {int I, flag = 0; for (I = 0; I <n; I ++) {if (arr [I] = data) {flag = 1; break;} return flag;} void quick_sort (int * arr, int begin, int end) {int begin; if (begin <end) {partition = maid (arr, begin, end); quick_sort (arr, begin, begin-1); quick_sort (arr, begin + 1, end );}} int effect_partition (int * arr, int left, int right) {int stand = arr [left]; while (left <right) {while (left <right & arr [right]> = stand) right --; if (left <right) arr [left ++] = arr [right]; while (left <right & arr [left] <= stand) left ++; if (left <right) arr [right --] = arr [left];} arr [left] = stand; return left ;} /*************************************** * *********************** Problem: 1394 User: wangzhengyi Language: C Result: Accepted Time: 0 MS Memory: 912 kb ************************************** **************************/

 

Related Article

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.