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