zoukankan      html  css  js  c++  java
  • bzoj 1650: [Usaco2006 Dec]River Hopscotch 跳石子【贪心+二分】

    脑子一抽写了个堆,发现不对才想起来最值用二分
    然后判断的时候贪心的把不合mid的区间打通,看打通次数是否小于等于m即可

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    const int N=50005;
    int L,n,m,a[N];
    int read()
    {
    	int r=0,f=1;
    	char p=getchar();
    	while(p>'9'||p<'0')
    	{
    		if(p=='-')
    			f=-1;
    		p=getchar();
    	}
    	while(p>='0'&&p<='9')
    	{
    		r=r*10+p-48;
    		p=getchar();
    	}
    	return r*f;
    }
    bool ok(int x)
    {
    	int tot=0,la=0;
    	for(int i=1;i<=n;i++)
    		if(a[i]-a[la]<x)
    		{
    			tot++;
    			if(tot>m)
    				return 0;
    		}
    		else 
    			la=i;
    	return 1;
    }
    int main()
    {
    	L=read(),n=read(),m=read();
    	for(int i=1;i<=n;i++)
    	    a[i]=read();
    	sort(a+1,a+n+1);
    	a[++n]=L;
    	int l=0,r=L,ans=0;
    	while(l<=r)
    	{
    		int mid=(l+r)>>1;
    		if(ok(mid))
    			ans=mid,l=mid+1;
    		else 
    			r=mid-1;
    	}
    	printf("%d
    ",ans);
    	return 0;
    }
    
  • 相关阅读:
    Two Sum
    Longest Common String
    Maximum Subarray
    Copy List with Random Pointer
    Convert Binary Search Tree to Doubly Linked List
    Fast Power
    Centos7安装ELK Cyrus
    IPv6实验 OSPFv3
    IPv6笔记和实验 RIPng
    IPv6 ICMPv6笔记
  • 原文地址:https://www.cnblogs.com/lokiii/p/8991453.html
Copyright © 2011-2022 走看看