zoukankan      html  css  js  c++  java
  • Codeforces_732D_(二分贪心)

    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.

    题意:在满足复习够天数的条件下每天可以通过指定的一科,要么选择通过一科,要么选择复习某一科,问最少几天能全部通过。(题目中不能看出每天只能复习一科,在NOTE中才能看出每天只能复习一科)

    看CF上2400多人做出来,以为可以轻松做出来,结果硬是没想出怎么做,看了题解说是二分贪心。

    对1—n天进行二分check,若check(mid)==1,l=mid+1;若check(mid)==0,r=mid-1。若能通过,则最终将接近最早通过的天数。check的思路是,从mid这天开始往前,让每一科在能通过的最晚的一天通过,再从头求每科复习的天数。

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    #define N 100005
    
    int day[N];
    int cost[N];
    int n,k;
    bool check(int x)
    {
            int dt[N];
            memset(dt,0,sizeof(dt));
            for(int i=x;i>0;i--)
                    if(!dt[day[i]])
                            dt[day[i]]=i;
            int pre=0;
            for(int i=1;i<=x;i++)
            {
                    if(day[i]==0)
                            pre++;
                    else if(dt[day[i]]!=i)
                            pre++;
                    else
                    {
                            if(cost[day[i]]>pre)
                                    return 0;
                            else
                                    pre-=cost[day[i]];
                    }
            }
            for(int i=1;i<=k;i++)
                    if(dt[i]==0)
                            return 0;
            return 1;
    }
    
    int main()
    {
        while(scanf("%d%d",&n,&k)!=EOF)
        {
            for(int i=1; i<=n; i++)
                scanf("%d",&day[i]);
            for(int i=1; i<=k; i++)
                scanf("%d",&cost[i]);
            int l=1,r=n,mid,ans=-1;
            while(l<=r)
            {
                mid=(l+r)>>1;
                if(check(mid))
                    {
                            ans=mid;
                            r=mid-1;
                    }
                else
                    l=mid+1;
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    第二章 逻辑代数及其简化
    小知识:三极管ie==ic+ib
    第二章.2 真值表→表达式的转换
    C# 静态变量及静态函数
    第四章(1):变量静态变量和实例变量
    转义大括号
    能被15整除的最大整数
    动态规划矩阵连乘问题
    [转]三极管的集电结反向偏置电压
    anddroid App, Framework, Sdk编译
  • 原文地址:https://www.cnblogs.com/jasonlixuetao/p/6057525.html
Copyright © 2011-2022 走看看