Nine degree title 1394: Five Combo array

Source: Internet
Author: User

Reprint please indicate this article link http://blog.csdn.net/yangnanhai93/article/details/40506571

Title Link: http://ac.jobdu.com/problem.php?pid=1394


Problem Analysis:

This problem is a four-star problem. But it feels a little too simple.

The first message to us is that it needs to be sequential, so we'll definitely sort the original array, and then we'll find out how many are missing. The problem is a bit similar to the question of finding the biggest stock in n days, I just care about my current number and look ahead. I've got a few numbers now, and I know how many.

So the idea is naturally very smooth. Go forward from that number. The number that appears before this is greater than or equal to 5 than the current number.

#include <stdio.h> #include <algorithm>using namespace Std;int main () {    //freopen ("data.in", "R", stdin );    int n,a[1000],result;    Short b[1000];    while (scanf ("%d", &n)!=eof)    {        result=5;        for (int i=0;i<n;i++)        {            scanf ("%d", &a[i]);            b[i]=0;        }        Sort (a,a+n);        for (int i=0;i<n;i++)        {            int j=i;            while (j>=0&&a[i]-a[j]<5)            {                b[i]++;                j--;            }            if (5-b[i]<result)                result=5-b[i];            if (result==0) break                ;        }        printf ("%d\n", result);    }    return 0;} /**************************************************************    problem:1394    user:vincent_ynh    language:c++    result:accepted    time:0 ms    memory:1020 kb******************************************** ********************/


Nine degree title 1394: Five Combo array

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.