Kattis - froshweek2 Frosh Week 类二分图匹配

Professor Zac is trying to finish a collection of tasks during the first week at the start of the term. He knows precisely how long each task will take, down to the millisecond. Unfortunately, it is also Frosh Week. Zac’s office window has a clear view of the stage where loud music is played. He cannot focus on any task when music is blaring.

The event organizers are also very precise. They supply Zac with intervals of time when music will not be playing. These intervals are specified by their start and end times down to the millisecond.

Each task that Zac completes must be completed in one quiet interval. He cannot pause working on a task when music plays (he loses his train of thought). Interstingly, the lengths of the tasks and quiet intervals are such that it is impossible to finish more than one task per quiet interval!

Given a list of times ti (in milliseconds) that each task will take and a list of times ℓj (in milliseconds) specifying the lengths of the intervals when no music is being played, what is the maximum number of tasks that Zac can complete?

Input
The first line of input contains a pair of integers n and m, where n is the number of tasks and m is the number of time intervals when no music is played. The second line consists of a list of integers t1,t2,…,tn indicating the length of time of each task. The final line consists of a list of times ℓ1,ℓ2,…,ℓm indicating the length of time of each quiet interval when Zac is at work this week.

You may assume that 1≤n,m≤200000 and 100000≤ti,ℓj≤199999 for each task i and each quiet interval j.

Output
Output consists of a single line containing a single integer indicating the number of tasks that Zac can accomplish from his list during this first week.

Sample Input 1    Sample Output 1
5 4
150000 100000 160000 100000 180000
190000 170000 140000 160000
4
Sample Input 2    Sample Output 2
4 4
180000 185000 199999 100000
199999 180000 170000 120000
3
Sample Input 3    Sample Output 3
3 3
199999 180000 170001
199999 170000 180000
2

题意:给出n个工作任务的时间长度,给出m个可工作时长

思路:sort2个数组后,都从初始处开始寻找

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <vector>
using namespace std;
const int MAXN=2e5+5;
typedef long long ll;
int  a[MAXN];
int  b[MAXN];
int  vis[MAXN];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(int i=0;i<m;i++)
        scanf("%d",&b[i]);
    sort(a,a+n);
    sort(b,b+m);
    memset(vis,0,sizeof(vis));
    int sum=0;
    for(int i=0,j=0;j<m&&i<n;)
    {
        if(a[i]<=b[j]){
            sum++;
            i++;
            j++;
        }
        else
            j++;
    }
    printf("%d\n",sum);

}

猜你喜欢

转载自blog.csdn.net/deepseazbw/article/details/81281336