设为首页 加入收藏

TOP

FZU 2178 礼物分配 (折半搜索+二分)
2015-07-20 17:22:06 来源: 作者: 【 】 浏览:3
Tags:FZU 2178 礼物 分配 搜索 +二分

题目地址:FZU 2178

由于n最大是30,一次全搜的话妥妥的超时,那么可以采用折半搜索。分成相同的两份,对左边的一堆进行预处理,然后再处理右堆,每一次都对左堆进行二分,找最接近的。由于两个人取的不能相差多于1个,所以要对每个个数分开存储。并排序,排序是为了后边的二分。

代码如下:

#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #include 
       
         #include 
         #include 
         
           #include 
          
            using namespace std; #define LL __int64 #define pi acos(-1.0) const int mod=1e9+7; const int INF=1e9; const double eqs=1e-9; int v[40], w[40], c[17][1<<15], d[17]; int bin_search(int x, int f) { int low=0, mid, high=d[f]-1, ans=-1; while(low<=high){ mid=low+high>>1; if(c[f][mid]>=x) { ans=mid; high=mid-1; } else low=mid+1; } if(ans==-1){ return abs(c[f][d[f]-1]-x); } else if(ans==0){ return abs(c[f][0]-x); } else return min(abs(c[f][ans]-x),abs(c[f][ans-1]-x)); } int main() { int t, n, i, min1, tot, m1, m2, ans1, ans2, al, ans, j, cnt; //freopen("1.txt","r",stdin); //freopen("2.txt","w",stdout); scanf("%d",&t); while(t--){ scanf("%d",&n); m1=n>>1; m2=n-m1; min1=INF; for(i=0;i
           
            

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇(hdu step 1.3.7)As Easy As A+B(.. 下一篇C++嵌套类的使用

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·微服务 Spring Boot (2025-12-26 18:20:10)
·如何调整 Redis 内存 (2025-12-26 18:20:07)
·MySQL 数据类型:从 (2025-12-26 18:20:03)
·Linux Shell脚本教程 (2025-12-26 17:51:10)
·Qt教程,Qt5编程入门 (2025-12-26 17:51:07)