Topic Connection:
http://www.lightoj.com/volume_showproblem.php?problem=1084
Main topic:
There are n points in a zero-based axis, each point can move a maximum of K, asked to finally be able to gather all the points in a set greater than or equal to three points, if the minimum need for a few such a collection?
Problem Solving Ideas:
Beginning to see the topic feel good simple, began the sort then greedy journey, this is the beginning of the wrong. Finally found that this does not work, and then Alex's reminder to use the Monotone queue optimization, in my ignorance of the understanding of the written BFS, submitted unexpectedly AC, surprise~~~
1#include <bits/stdc++.h>2 using namespacestd;3 4 Const intMAXN =100005;5 Const intINF =0x3f3f3f3f;6 intA[MAXN], VIS[MAXN], N, K;7 structnode8 {9 intx, index;Ten }; One A intSolve () - { -Queue <node>Q; the node p, q; - -memset (Vis,0,sizeof(Vis)); -P.index =0; +p.x =0; - Q.push (p); +vis[0] =1; A at while(!q.empty ()) - { -p =Q.front (); - Q.pop (); - - if(P.index >=N) in returnp.x; - to inttemp =P.index; + while(Temp<n && a[temp]-a[p.index]<=2*k) -Temp + +; the * intnum = Temp-P.index; $ if(Num >=3&&!Vis[temp])Panax Notoginseng { -Q.index =temp; theq.x = p.x +1; + Q.push (Q); AVis[temp] =1; the } + if(Num >=4&&!vis[temp-1]) - { $Q.index = temp-1; $q.x = p.x +1; - Q.push (Q); -vis[temp-1] =1; the } - if(Num >=5&&!vis[temp-2])Wuyi { theQ.index = temp-2; -q.x = p.x +1; Wu Q.push (Q); -vis[temp-2] =1; About } $ } - return-1; - } - intMain () A { + intT, L =0; thescanf ("%d", &t); - while(T--) $ { thescanf ("%d%d", &n, &k); the for(intI=0; i<n; i++) thescanf ("%d", &a[i]); theSort (A, A +n); - in intres =solve (); theprintf ("Case %d:%d\n", ++L, RES); the } About}
Summer Training Mania Brush Series--lightoj 1084-winter BFS