Summer Training Mania Brush Series--lightoj 1084-winter BFS

Source: Internet
Author: User

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

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.