zoukankan      html  css  js  c++  java
  • HDU 5884 Sort

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define rep(i,a,b) for(int i=a;i<=b;++i)
     4 #define ms(arr,a) memset(arr,a,sizeof arr)
     5 const int maxn=1e5+5;
     6 int N,C;
     7 int a[maxn],b[maxn],bf,be,af;
     8 bool ok(int w)
     9 {
    10     if(w==1)return false;
    11     int sum=0;
    12     int f;
    13     if(N%(w-1))f=N%(w-1);
    14     else f=w-1;
    15     bf=be=1;
    16     af=f+1;
    17     b[bf]=0;
    18     rep(i,1,f)b[bf]+=a[i];
    19     sum+=b[be];
    20     rep(i,1,(N-f)/(w-1))
    21     {
    22         int tmp_sum=0;
    23         rep(j,1,w)
    24         {
    25             if(bf>be){tmp_sum+=a[af++];continue;}
    26             if(af>N){tmp_sum+=b[bf++];continue;}
    27             if(b[bf]<a[af])tmp_sum+=b[bf++];
    28             else tmp_sum+=a[af++];
    29         }
    30         b[++be]=tmp_sum;
    31         sum+=b[be];
    32     }
    33     if(sum<=C)return true;
    34     else return false;
    35 }
    36 int solve(int l,int r)
    37 {
    38     int m;
    39     while(l<r)
    40     {
    41         m=(l+r)/2;
    42         //printf("check on %d
    ",m);
    43         if(ok(m))r=m;
    44         else l=m+1;
    45     }
    46     return l;
    47 }
    48 int main()
    49 {
    50     int T;scanf("%d",&T);
    51     while(T--)
    52     {
    53         scanf("%d%d",&N,&C);
    54         rep(i,1,N)scanf("%d",a+i);
    55         sort(a+1,a+N+1);
    56         int ans=solve(1,N);
    57         printf("%d
    ",ans);
    58     }
    59 }
  • 相关阅读:
    拓扑排序
    数论初步(费马小定理)
    HDU
    HDU
    八月馒头
    萌新仓鼠,参上!
    新姿势
    字典树
    HDU
    Codeforce
  • 原文地址:https://www.cnblogs.com/maoruimas/p/9521003.html
Copyright © 2011-2022 走看看