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. This topic to think will find and find the biggest stock in n days the problem is a bit similar, that is, I only care about my current number, look ahead, I now have a few count, and then know how many
So the idea is naturally smooth, moving forward from that number until the number above the current number is greater than or equal to 5.
#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