zoukankan      html  css  js  c++  java
  • Codeforces Round #377 (Div. 2) D. Exams 二分

    D. Exams
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Vasiliy has an exam period which will continue for n days. He has to pass exams on m subjects. Subjects are numbered from 1 to m.

    About every day we know exam for which one of m subjects can be passed on that day. Perhaps, some day you can't pass any exam. It is not allowed to pass more than one exam on any day.

    On each day Vasiliy can either pass the exam of that day (it takes the whole day) or prepare all day for some exam or have a rest.

    About each subject Vasiliy know a number ai — the number of days he should prepare to pass the exam number i. Vasiliy can switch subjects while preparing for exams, it is not necessary to prepare continuously during ai days for the exam number i. He can mix the order of preparation for exams in any way.

    Your task is to determine the minimum number of days in which Vasiliy can pass all exams, or determine that it is impossible. Each exam should be passed exactly one time.

    Input

    The first line contains two integers n and m (1 ≤ n, m ≤ 105) — the number of days in the exam period and the number of subjects.

    The second line contains n integers d1, d2, ..., dn (0 ≤ di ≤ m), where di is the number of subject, the exam of which can be passed on the day number i. If di equals 0, it is not allowed to pass any exams on the day number i.

    The third line contains m positive integers a1, a2, ..., am (1 ≤ ai ≤ 105), where ai is the number of days that are needed to prepare before passing the exam on the subject i.

    Output

    Print one integer — the minimum number of days in which Vasiliy can pass all exams. If it is impossible, print -1.

    Examples
    input
    7 2
    0 1 0 2 1 0 2
    2 1
    output
    5
    input
    10 3
    0 0 1 2 3 0 2 0 1 2
    1 1 4
    output
    9
    input
    5 1
    1 1 1 1 1
    5
    output
    -1
    Note

    In the first example Vasiliy can behave as follows. On the first and the second day he can prepare for the exam number 1 and pass it on the fifth day, prepare for the exam number 2 on the third day and pass it on the fourth day.

    In the second example Vasiliy should prepare for the exam number 3 during the first four days and pass it on the fifth day. Then on the sixth day he should prepare for the exam number 2 and then pass it on the seventh day. After that he needs to prepare for the exam number 1 on the eighth day and pass it on the ninth day.

    In the third example Vasiliy can't pass the only exam because he hasn't anough time to prepare for it.

    题意:每天可以准备任意一个科目或者通过一个科目,最少在第几天完成,全部科目;

    思路:二分答案,o(n)check(我的zz mlog(m));

    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define mod 1000000007
    #define esp 0.00000000001
    const int N=1e5+10,M=1e6+10,inf=1e9;
    int d[N];
    int a[N];
    int n,m;
    vector<int>v[N];
    int deep[N];
    int flag[N];
    int check(int x)
    {
        for(int i=1;i<=m;i++)
        {
            if(v[i].size()==0)
            return 0;
            int pos=upper_bound(v[i].begin(),v[i].end(),x)-v[i].begin();
            if(pos==0)
            return 0;
            deep[i]=v[i][pos-1];
        }
        for(int i=1;i<=m;i++)
        flag[deep[i]]=1;
        int st=0;
        for(int i=1;i<=x;i++)
        {
            if(flag[i])
            {
                st-=a[d[i]];
                if(st<0)
                return 0;
            }
            else
            st++;
        }
        return 1;
    }
    int main() {
        scanf("%d%d",&n,&m);
        for(int i = 1; i <= n; ++i) scanf("%d",&d[i]),v[d[i]].push_back(i);
        for(int i = 1; i <= m; ++i) scanf("%d",&a[i]);
        int st=1;
        int en=n;
        if(check(n)==0)
        return puts("-1");
        for(int i=1;i<=m;i++)
        flag[deep[i]]=0;
        int ans=-1;
        while(st<=en)
        {
            int mid=(st+en)>>1;
            if(check(mid)) en=mid-1,ans=mid;
            else st=mid+1;
            for(int i=1;i<=m;i++)
            flag[deep[i]]=0;
        }
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    KALI LINUX 工具大全---jd-gui(爪哇反编译器-图形界面)
    KAL1 LINUX 官方文档之政策---Kali Linux更新政策
    KAL1 LINUX 官方文档之政策---Kali Linux用户政策
    KAL1 LINUX 官方文档之社区---Kali Linux官方网站
    KAL1 LINUX 官方文档之社区---为kali做贡献
    KAL1 LINUX 官方文档之社区---官方Kali Linux镜像
    KAL1 LINUX 官方文档之kali开发---自定义 Beaglebone Black 镜像
    KAL1 LINUX 官方文档之kali开发---ARM交叉编译
    KAL1 LINUX 官方文档之kali开发---实时构建自定义的Kali ISO
    KAL1 LINUX 官方文档之kali开发---生成 更新的Kali ISO
  • 原文地址:https://www.cnblogs.com/jhz033/p/5985596.html
Copyright © 2011-2022 走看看