zoukankan      html  css  js  c++  java
  • POJ3285 River Hopscotch(最大化最小值之二分查找)

    POJ3285 River Hopscotch

      此题是大白P142页(即POJ2456)的一个变形题,典型的最大化最小值问题.

      C(x)表示要求的最小距离为X时,此时需要删除的石子.二分枚举X,直到找到最大的X,由于c(x)=m时满足题意,所以最后输出的是ub-1或者lb(lb==ub-1

      注意相邻距离小于x的要删除(此处不是小于等于),对于相邻的距离小于x的两个石子,当删除其中一个后,又会产生其他的相邻的石子,直接计数不好计数,不妨用两个标记last,cur,其中last表示上一个石子,cur表示当前的石子

        将起点的石子和终点的石子加入数组,距离分别初始化为0,1,让last=0,cur=last+1=1开始,由于起点和终点不能删除,每次只需判断cur是否需要删除,若需要,cur++;

    再次循环时,last=cur,cur=last+1,对于循环的终止条件,当cur=n+1时,若a[cur]-a[last]<x(此时last一定不为0),前面一直删除的是右边的,此处其实删除的是左边的,对结果没影响,此时cur++,cur的值变为n+2,跳出最内层循环,再跳出外层循环.

       注意 last一定不为0的假设是基于n!=0的假设,当n=0时,可以单独判断一下.当n=0时,mid会一直减小到l,所以下面程序也没问题

      我一开始把 int c(x) 函数写成了bool类型,在调试时又把INF改小了,后来交上去就一直wa,其实不需要将ub初始化为INF,直接初始化为l+1就够了

       以下为AC代码

    /*
    * Created:     2016年03月31日 16时07分30秒 星期四
    * Author:      Akrusher
    *
    */
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <string>
    #include <vector>
    #include <deque>
    #include <list>
    #include <set>
    #include <map>
    #include <stack>
    #include <queue>
    #include <numeric>
    #include <iomanip>
    #include <bitset>
    #include <sstream>
    #include <fstream>
    using namespace std;
    #define rep(i,a,n) for (int i=a;i<n;i++)
    #define per(i,a,n) for (int i=n-1;i>=a;i--)
    #define in(n) scanf("%d",&(n))
    #define in2(x1,x2) scanf("%d%d",&(x1),&(x2))
    #define inll(n) scanf("%I64d",&(n))
    #define inll2(x1,x2) scanf("%I64d%I64d",&(x1),&(x2))
    #define inlld(n) scanf("%lld",&(n))
    #define inlld2(x1,x2) scanf("%lld%lld",&(x1),&(x2))
    #define inf(n) scanf("%f",&(n))
    #define inf2(x1,x2) scanf("%f%f",&(x1),&(x2))
    #define inlf(n) scanf("%lf",&(n))
    #define inlf2(x1,x2) scanf("%lf%lf",&(x1),&(x2))
    #define inc(str) scanf("%c",&(str))
    #define ins(str) scanf("%s",(str))
    #define out(x) printf("%d
    ",(x))
    #define out2(x1,x2) printf("%d %d
    ",(x1),(x2))
    #define outf(x) printf("%f
    ",(x))
    #define outlf(x) printf("%lf
    ",(x))
    #define outlf2(x1,x2) printf("%lf %lf
    ",(x1),(x2));
    #define outll(x) printf("%I64d
    ",(x))
    #define outlld(x) printf("%lld
    ",(x))
    #define outc(str) printf("%c
    ",(str))
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define SZ(x) ((int)(x).size())
    #define mem(X,Y) memset(X,Y,sizeof(X));
    typedef vector<int> vec;
    typedef long long ll;
    typedef pair<int,int> P;
    const int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
    const int INF=0x3f3f3f3f;
    const ll mod=1e9+7;
    ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
    const bool AC=true;
    
    int l,n,m,cnt;
    int a[50005];
    int C(int x){ //返回值是int类型,不是bool类型
        cnt=0;
        int last,cur;
        last=0;
        while(true){
        cur=last+1;
        if(cur>=n+2) break;
        while(a[cur]-a[last]<x){
        cnt++;
        cur++;
        if(cur==n+2) break;
        }
        last=cur;
      }
      return cnt;    
    }
    int main()
    {
        in2(l,n);
        in(m); 
        a[0]=0;//a[0]=0为起点
        rep(i,1,n+1){
            in(a[i]);
            }
        a[n+1]=l; //a[n+1]为终点
        sort(a,a+n+2);
        int lb,ub,mid;
        lb=0,ub=l+1;//设为l+1可以减小查找时间
        while(ub-lb>1){
        mid=(ub+lb)/2;
        if(C(mid)>m) ub=mid;//距离大了
        else if(C(mid)<=m) lb=mid;
        }
        out(lb);//或者out(ub-1);
        return 0;
    }
  • 相关阅读:
    【总结】机器学习里需要知道的12堂课
    【转载】The Mathematics Of Beauty
    Facebook的朋友推荐系统
    【转载】Netflix: System Architectures for Personalization and Recommendation
    aDev第13期#个性化推荐技术#总结(Part II:江申@百度)
    mahout 0.7关于随机选择数据的一个bug
    Dirichlet Process 和 Dirichlet Process Mixture模型
    aDev第13期#个性化推荐技术#总结(Part I:袁全@一淘)
    httpHandler实现.Net无后缀名Web访问
    还在使用Application_Start做定时器的,你们伤不起!
  • 原文地址:https://www.cnblogs.com/akrusher/p/5342512.html
Copyright © 2011-2022 走看看