• codeforces


    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.

    题意;

    给你两行,一行有n个数, 表示第i天的状态。如果ai为0,表示今天没考试。否则表示该天可以参加第ai门考试。

    另行有m个数,表示有第i门考试需要准备的准备时间。

    思路:

    明显的二分,但是每次二分都有策略,贪心一下。

    #include <map>
    #include <set>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <iostream>
    #include <stack>
    #include <cmath>
    #include <string>
    #include <vector>
    #include <cstdlib>
    //#include <bits/stdc++.h>
    //#define LOACL
    #define space " "
    using namespace std;
    typedef long long LL;
    typedef __int64 Int;
    typedef pair<int, int> paii;
    const int INF = 0x3f3f3f3f;
    const double ESP = 1e-5;
    const double PI = acos(-1.0);
    const int MOD = 1e9 + 7;
    const int MAXN = 1e5 + 10;
    int ar[MAXN], data[MAXN];
    bool vis[MAXN];
    int n, m;
    bool judge(int x) {
        int cnt = x - 1;
        memset(vis, false, sizeof(vis));
        //贪心策略,从最后开始,这样可以保证最优解
        //如果这种策略不合法,那么一定误解
        for (int i = x; i >= 1; i--) {
            cnt = min(cnt, i - 1);
            if (data[i] && !vis[data[i]] && ar[data[i]] <= cnt) {
                vis[data[i]] = true;
                cnt -= ar[data[i]] + 1;
            }
        }
        for (int i = 1; i <= m; i++) {
            if (!vis[i]) return false;
        }
        return true;
    }
    int main() {
        while (scanf("%d%d", &n, &m) != EOF) {
            for (int i = 1; i <= n; i++) scanf("%d", &data[i]);
            for (int i = 1; i <= m; i++) scanf("%d", &ar[i]);
            sort(ar + 1, ar + 1 + m);
            int ans = -1;
            int lb = 0, ub = n;
            while (ub >= lb) {
                int mid = (ub + lb) >> 1;
                if (judge(mid)) {
                    ans = mid; ub = mid - 1;
                }
                else lb = mid + 1;
            }
            printf("%d
    ", ans);
        }
        return 0;
    }
    
    
    


  • 相关阅读:
    _DataStructure_C_Impl:稀疏矩阵十字链表存储
    _DataStructure_C_Impl:稀疏矩阵三元组
    _DataStructure_C_Impl:Array
    _DataStructure_C_Impl:KMP模式匹配
    _DataStructure_C_Impl:链串
    _DataStructure_C_Impl:堆串
    _DataStructure_C_Impl:顺序串
    _DataStructure_C_Impl:双端队列
    _DataStructure_C_Impl:链式队列
    _DataStructure_C_Impl:只有队尾指针的链式循环队列
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770761.html
Copyright © 2020-2023  润新知