HDU 5037 frog greedy 2014 Beijing Network Competition F

Source: Internet
Author: User

Question link; click the open link

There is a small river with m length. It can be seen as a one-dimensional axis. There are n stones in the river, and there is a small frog that can jump l meters each time, add a stone at will to ensure that the frog can jump from the beginning to the end, and ask the frog how many times it will take to use the best strategy to jump to the other side.

Idea: assume that each stone of a frog goes through a step to represent the step of the last hop of the frog. The distance between the current point and the next point is compared with that of L, if it is small, it proves that the frog can jump to this one time, update the step and the position of the frog, and the CNT remains unchanged. If it is larger, it indicates that the frog must jump at least once. If lenth <= l, skip directly. Update step = lenth, CNT ++. If lenth> L, the frog will jump through the cycle of L + 1-step, step. This is easy to prove to ensure optimal solution, then it is equivalent to translating the stone backward into x * (L + 1) units. And so on. For more information, see the code ~ WAF is crying during the competition !!!

CPP:

#include <cstdio>#include <iostream>#include <algorithm>#include <cmath>using namespace std;int data[200010],T,n,m,l;void slove(){sort(data,data+n+1);int step=l,now=0,cnt=0;for(int i=0;i<=n;i++){int lenth=data[i]-now;if(lenth==0) continue;if(lenth+step<=l){now=data[i];step=lenth+step;}else if(lenth<=l){now=data[i];step=lenth;cnt++;}else if(lenth>l){int tp=lenth%(l+1);cnt+=(lenth/(l+1))*2;if(tp+step<=l){step=tp+step;now=data[i];}else {step=tp;now=data[i];cnt++;}}}printf("%d\n",cnt);}int main(){//freopen("data.in","r",stdin);int cas=0;scanf("%d",&T);while (T--){printf("Case #%d: ",++cas);scanf("%d%d%d",&n,&m,&l);for(int i=0;i<n;i++){scanf("%d",data+i);}data[n]=m;slove();}return 0;}


HDU 5037 frog greedy 2014 Beijing Network Competition F

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.