zoukankan      html  css  js  c++  java
  • 解题:POI 2007 Weights

    题面

    这是个$O(nlog^2$ $n)$的解法,因为蒟蒻博主没有看懂$O(nlog$ $n)$的更优秀的解法

    显然从小到大装砝码是最优的方法,又显然从大到小装容器不会使得答案变劣,还显然砝码数具有单调性。于是就很好做了,先将砝码从小到大排序,每次二分答案后用一个大根堆维护容器然后按题意模拟即可

     1 // luogu-judger-enable-o2
     2 #include<queue>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<algorithm>
     6 using namespace std;
     7 const int N=100005;
     8 int ves[N],wei[N];
     9 int n,m,l,r,ans;
    10 bool check(int x)
    11 {
    12     priority_queue<int> hp; 
    13     for(int i=1;i<=n;i++) hp.push(ves[i]); 
    14     for(int i=x;i;i--)
    15     {
    16         if(hp.empty()) return false;
    17         int tmp=hp.top(); hp.pop();
    18         if(tmp>=wei[i]) hp.push(tmp-wei[i]);
    19         else return false;
    20     }
    21     return true;
    22 }
    23 int main()
    24 {
    25     scanf("%d%d",&n,&m),l=1,r=m;
    26     for(int i=1;i<=n;i++) scanf("%d",&ves[i]);
    27     for(int i=1;i<=m;i++) scanf("%d",&wei[i]);
    28     sort(wei+1,wei+1+m);
    29     while(l<=r)
    30     {
    31         int mid=(l+r)/2;
    32         if(check(mid)) l=mid+1,ans=mid;
    33         else r=mid-1;
    34     }
    35     printf("%d",ans);
    36     return 0;
    37 }
    View Code
  • 相关阅读:
    最小生成树
    线段树
    编程快捷键
    线段树的动态开点
    常用库
    线性求逆元
    文件读入
    树上倍增(LCA)
    set容器
    快读与快写
  • 原文地址:https://www.cnblogs.com/ydnhaha/p/9853206.html
Copyright © 2011-2022 走看看