zoukankan      html  css  js  c++  java
  • codeforces 732/D 二分

    给出考试时间和考试需要准备的时间,问最早考完所有科目的时间 

    二分答案 NlogN

    二分抄神犇的写法 感觉挺舒服的嘻嘻嘻

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int MAXN=1e5+5;
     4 int N,M,d[MAXN],w[MAXN],cnt[MAXN];
     5 void read(int &x){
     6     x=0;
     7     register char c=getchar();
     8     while(c<48||c>57)
     9         c=getchar();
    10     for(;c>=48&&c<=57;c=getchar())
    11         x=(x<<1)+(x<<3)+c-48;
    12 }
    13 bool check(int x)
    14 {
    15     memset(cnt,0,sizeof(cnt));
    16     int ans = 0,val = 0;
    17     for (int i=x; i>=1; i--)
    18         if (d[i]&&++cnt[d[i]]==1&&w[d[i]]<i-M+ans+1)
    19         {
    20             ans++;
    21             val+=w[d[i]];
    22         }
    23    return ans == M && val+M<=x;
    24 }
    25 int main()
    26 {
    27     read(N),read(M);
    28     for (int i=1; i<=N; i++)
    29         read(d[i]);
    30     for (int i=1; i<=M; i++)
    31         read(w[i]);
    32     int first = 0;
    33     int last = N;
    34     int mid;
    35     int best = -1;
    36     while (first <= last)
    37     {
    38         mid = (first + last)/2;
    39         if (check(mid))
    40         {
    41             last = mid-1;
    42             best = mid;
    43         }
    44         else
    45         {
    46             first = mid+1;
    47         }
    48     }
    49     printf("%d
    ",best);
    50     return 0;
    51 }
    View Code
  • 相关阅读:
    Python类的继承(进阶5)
    面向对象编程基础(进阶4)
    Python模块(进阶3)
    Python函数式编程(进阶2)
    python多线程
    Ternary Search Tree Java实现
    Trie和Ternary Search Tree介绍
    索引时利用K-邻近算法过滤重复歌曲
    Sql排名和分组排名
    Lucene和jackson冲突
  • 原文地址:https://www.cnblogs.com/Geek-xiyang/p/6050490.html
Copyright © 2011-2022 走看看