傳送門:羊喫草
題意:有n頭羊在一個有[1,400]區間的草原,每頭羊喫喜歡(只)喫[ai,bi]區間內的草,q次查詢在[L,R]區間在一個時刻內最多有多少隻羊在喫草(一個時刻羊只能在一點喫草,一點也只能有一隻羊在喫)。
做法:貪心。我們讓每頭羊都儘量在最右端點喫草。將牛按左端點爲第一關鍵字右端點爲第二關鍵字排序,從左往右掃麪點,讓左端點在當前點左邊的牛進堆,存右端點值,接着在看堆中的牛是否包含該點,不包含說明右端點小了,出堆,最後讓堆中符合條件右端點最小的牛喫草。
這道題好像還有完美匹配的做法
代碼:
#include<bits/stdc++.h>
using namespace std;
const int N=410;
typedef pair<int,int> PII;
PII cow[N];
int n,q,l,r;
priority_queue<int> head;
void solve(){
head=priority_queue<int>();
int cnt=0,j=1;
for(int i=l;i<=r;++i){
while(j<=n&&cow[j].first<=i){
head.push(-cow[j].second);
++j;
}
while(!head.empty()&&-head.top()<i){
head.pop();
}
if(!head.empty()){
head.pop();
cnt++;
}
}
cout<<cnt<<'\n';
}
int main(){
cin>>n>>q;
for(int i=1;i<=n;++i)
cin>>cow[i].first;
for(int i=1;i<=n;++i)
cin>>cow[i].second;
sort(cow+1,cow+1+n);
while(q--){
cin>>l>>r;
solve();
}
return 0;
}