刚开始确实想不到并查集去,就莽写了一个贪心过了思路大概也就O(n),跑了63s,并查集的思路是网上查到的跑了43s,下面提供贪心和并查集的思路和代码
贪心思路:
按时间倒着来卖,这样没有卖出去的加入的堆中,堆中的对于前面的时间都可以卖,如果此时idx指向的商品没有堆顶大就卖堆顶的价值,将此时卖的加入堆中,反之则直接idx指向的商品,因为一天只能卖一个,所以卖了一次后后面时间也是now的直接也加入堆中,最后商品遍历完后要根据now清空堆中元素,也就是堆中在剩余的天数中卖出。
代码:
#include <iostream>
#include <algorithm>
#include <string>
#include <stdio.h>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <math.h>
#include <climits>
#include <iomanip>
#include <queue>
#include <vector>
#define fastio ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define debug(a) cout << "debug : " << (#a) << " = " << a << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 1e4 + 10;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const int mod = 998244353;
PII a[N];
bool cmp(PII x, PII y)
{
if (x.first > y.first)
return true;
else if (x.first == y.first && x.second > y.second)
return true;
return false;
}
int main()
{
int n;
while (~scanf("%d", &n))
{
ll ans = 0;
int Max_d = -INF;
priority_queue<int> pq; //存前面遍历的商品中未卖出去的
for (int i = 1; i <= n; i++)
{
scanf("%d%d", &a[i].second, &a[i].first);
Max_d = max(a[i].first, Max_d);
}
sort(a + 1, a + n + 1, cmp); //按照截止时间越大越靠前,时间相同则价值越大越靠前
int idx = 1, now = Max_d;
while (now >= 1)
{
if (idx > n) //当所有的物品都被遍历完,清空pq后break即可,这里wa了好多发
{
while (pq.size() && now >= 1)
{
ans += pq.top();
pq.pop();
now--;
}
break;
}
while (now > a[idx].first) //当此时的now比idx指向商品的截止时间要大的话就要一直减,每次减都可以贪心的卖出堆中的一个价值最大商品
{
if (pq.size())
{
ans += pq.top();
pq.pop();
}
now--;
}
int w = a[idx].second;
if (pq.size() && pq.top() > w) //当堆中未卖出的最大值大于此时要卖出物品的值就卖堆顶的
{
ans += pq.top();
pq.pop();
pq.push(w);
}
else
ans += w;
while (true) //处理此时a数组中idx后面也是时间now的
{
idx++;
if (a[idx].first == now)
pq.push(a[idx].second);
else
break;
}
now--;
}
printf("%lld\n", ans);
}
return 0;
}
并查集思路:
并查集维护每个截止时间点此时可以卖东西的最晚时间(父节点),奇妙的并查集维护,也是一种优化的贪心,先将所有的商品按照价值降序排序,贪心先遍历贵的商品,find函数找出父节点即此时商品截止日期最晚的可卖天,卖掉后父节点左移一格,可能左移的格已经被用了,但是它的父节点同样也更新了,这也是用并查集的精妙之处。
代码:
#include <iostream>
#include <algorithm>
#include <string>
#include <stdio.h>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <math.h>
#include <climits>
#include <iomanip>
#include <queue>
#include <vector>
#define fastio ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define debug(a) cout << "debug : " << (#a) << " = " << a << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 1e4 + 10;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const int mod = 998244353;
int f[N];
int find(int x)
{
if (f[x] != x)
f[x] = find(f[x]);
return f[x];
}
struct node
{
int p, d;
} a[N];
bool cmp(node x, node y)
{
if (x.p > y.p)
return true;
return false;
}
int main()
{
int n;
while (~scanf("%d", &n))
{
ll ans = 0;
for (int i = 0; i < N; i++)
f[i] = i;
for (int i = 1; i <= n; i++)
scanf("%d%d", &a[i].p, &a[i].d);
sort(a + 1, a + n + 1, cmp);
for (int i = 1; i <= n; i++)
{
int t = find(a[i].d);
if (t > 0)
{
f[t] = t - 1;
ans += a[i].p;
}
}
printf("%lld\n", ans);
}
return 0;
}