题意:
有c只牛,l个瓶子。
每只牛有一个防晒区间,minSpf 和 maxSpf,在这个区间内才能保持良好的防晒姿势。
现在每个瓶子里的防晒值是spf,然后每个瓶子能搞cover只牛。
问最多能够让几头牛保持良好的防晒姿势。
解析:
莫名其妙就搞了一个早上。
另一道dp搞不出来。
这贪心是把maxSpf压入优先队列当中然后与牛的最小值比较就行了。
我开始的时候的贪心是用优先队列保存bottle,然后一个一个去试牛,wa了一个早上。
见代码:
ac:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long
#define lson lo, mi, rt << 1
#define rson mi + 1, hi, rt << 1 | 1
using namespace std;
const int maxn = 2500 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
const double ee = exp(1.0);
struct Bottle
{
int spf, cover;
bool operator < (const Bottle a) const
{
return spf < a.spf;
}
} bottle[maxn];
struct Cow
{
int minSpf, maxSpf;
bool operator < (const Cow a) const
{
return minSpf < a.minSpf;
}
} cow[maxn];
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
int c, l;
while (~scanf("%d%d", &c, &l))
{
for (int i = 0; i < c; i++)
{
scanf("%d%d", &cow[i].minSpf, &cow[i].maxSpf);
}
for (int i = 0; i < l; i++)
{
scanf("%d%d", &bottle[i].spf, &bottle[i].cover);
}
sort(bottle, bottle + l);
sort(cow, cow + c);
priority_queue<int, vector<int>, greater<int> > q;
int j = 0, ans = 0;
for (int i = 0; i < l; i++)
{
while (j < c && cow[j].minSpf <= bottle[i].spf)
{
q.push(cow[j].maxSpf);
j++;
}
while (!q.empty() && bottle[i].cover)
{
int now = q.top();
q.pop();
if (now >= bottle[i].spf)
{
ans++;
bottle[i].cover--;
}
}
}
printf("%d\n", ans);
}
return 0;
}
wa:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long
#define lson lo, mi, rt << 1
#define rson mi + 1, hi, rt << 1 | 1
using namespace std;
const int maxn = 2500 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
const double ee = exp(1.0);
struct Bottle
{
int spf, cover;
bool operator < (const Bottle a) const
{
return a.spf < spf;
}
} bottle[maxn];
struct Cow
{
int minSpf, maxSpf;
bool operator < (const Cow a) const
{
if (minSpf == a.minSpf)
{
return maxSpf < a.maxSpf;
}
return minSpf < a.minSpf;
}
} cow[maxn];
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
int c, l;
while (~scanf("%d%d", &c, &l))
{
priority_queue<Bottle> q;
for (int i = 0; i < c; i++)
{
scanf("%d%d", &cow[i].minSpf, &cow[i].maxSpf);
}
for (int i = 0; i < l; i++)
{
scanf("%d%d", &bottle[i].spf, &bottle[i].cover);
q.push(bottle[i]);
}
sort(cow, cow + c);
int index = 0;
int ans = 0;
while (!q.empty() && index < c)
{
Bottle now = q.top();
q.pop();
if (cow[index].minSpf < now.spf && now.spf < cow[index].maxSpf)
{
ans++;
now.cover--;
index++;
if (now.cover != 0)
q.push(now);
// cout << now.spf << " " << now.cover << endl;
// cout << "ans:" << ans << endl;
}
else if (now.spf <= cow[index].minSpf)
{
continue;
}
else // cow[index].maxSpf < now.spf
{
index++;
q.push(now);
}
}
printf("%d\n", ans);
}
return 0;
}