标签:style blog http io ar for sp strong on
转载请注明本文链接http://blog.csdn.net/yangnanhai93/article/details/40506571
题目链接:http://ac.jobdu.com/problem.php?pid=1394
问题分析:
这个问题虽然是四星的问题,但是感觉有点过于简单了。
这个题目第一个给我们的信息是需要连续的,所以肯定会对原来的数组进行排序,接下来就是找缺多少个数了。这个题目想想会发现和找N天中最大股票的问题有点类似,就是我只关心我当前的数,往前看,我现在有几个数了,然后就知道多少个了
所以思路自然的很顺畅,从该数往前走,直到这个前面出现的数比当前的数大于等于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 ****************************************************************/
标签:style blog http io ar for sp strong on
原文地址:http://blog.csdn.net/yangnanhai93/article/details/40506571