国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Codeforces 732D Exams【貪心+二分】

2019-11-06 06:42:20
字體:
來源:轉載
供稿:網友

D. Examstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

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

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 numberi. Vasiliy can switch subjects while preparing for exams, it is not necessary to prepare continuously duringai days for the exam numberi. 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 andm (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), wheredi is the number of subject, the exam of which can be passed on the day numberi. If di equals 0, it is not allowed to pass any exams on the day numberi.

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

Output

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

ExamplesInput
7 20 1 0 2 1 0 22 1Output
5Input
10 30 0 1 2 3 0 2 0 1 21 1 4Output
9Input
5 11 1 1 1 15Output
-1Note

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天(按照時間順序給出),其中有M科目需要通過。

每個科目需要復習的天數是ai.

對應給出di為時間表,di==0的,可以選擇復習任意一科,di!=0的,可以選擇考di這一科,也可以選擇復習任意一科。

問你能否通過所有考試,如果可以,輸出最小天數。

思路:

1、對于天數來講,時間越長,越有可能通過所有考試,那么這里就包含了一個單調性,我們可以二分天數,然后進行貪心處理。

2、對于當前枚舉的天數mid.

我們要進行貪心判斷:

①首先在從第一天到第mid天之內,必須要有di包含所有的科目,如果有一些科目沒有出現過,那么這些科目是不能進行考試的,所以這種情況是不行的。

②貪心的去想,我們肯定考試越晚越好,所以我們取每一科最后出現的位子作為考這一科的時間。

③那么其他時間都是可以進行任意復習的時間,我們過程維護可以進行復習的天數還有多少天have.當遇到一個考試時間點,那么對應have-=需要復習這科的時間,如果have出現了負數的情況,那么對應增大時間,否則全程沒遇到,就可以相應的減少時間了。

Ac代碼:

#include<stdio.h>#include<string.h>#include<queue>using namespace std;int a[100050];int ned[100050];int last[100050];int n,m;int Slove(int mid){    int have=0;    memset(last,-1,sizeof(last));    for(int i=1;i<=mid;i++)    {        if(a[i]!=0)        {            last[a[i]]=i;        }    }    for(int i=1;i<=m;i++)if(last[i]==-1)return 0;    for(int i=1;i<=mid;i++)    {        if(a[i]==0)have++;        else        {            if(last[a[i]]==i)            {                if(have>=ned[a[i]])                {                    have-=ned[a[i]];                }                else return 0;            }            else have++;        }    }    return 1;}int main(){    while(~scanf("%d%d",&n,&m))    {        for(int i=1;i<=n;i++)scanf("%d",&a[i]);        for(int i=1;i<=m;i++)scanf("%d",&ned[i]);        int ans=-1;        int l=1;        int r=n;        while(r-l>=0)        {            int mid=(l+r)/2;            if(Slove(mid)==1)            {                ans=mid;                r=mid-1;            }            else l=mid+1;        }        printf("%d/n",ans);    }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 和平区| 天津市| 安平县| 荆州市| 屯留县| 格尔木市| 秦安县| 成武县| 郧西县| 英德市| 家居| 阿拉尔市| 阜平县| 修文县| 资阳市| 滦平县| 湘潭县| 嘉祥县| 托克托县| 简阳市| 廉江市| 英德市| 左权县| 垦利县| 巴马| 通化市| 永平县| 河西区| 中卫市| 陇南市| 错那县| 会宁县| 江阴市| 肇州县| 邵武市| 阳高县| 台州市| 昌邑市| 娱乐| 岐山县| 康马县|