题目链接
LOJ #10008. 「一本通 1.1 练习 4」家庭作业
解题思路
好题!
这道题和这题很像,几乎一模一样。但是不同的是,这道题的数据范围有些改变
显然
O
(
N
t
)
O(Nt)
O(Nt)的算法过不了
我们考虑其他做法。
显然我们还是使用同一种贪心方法,以学分为标准降序排序,但是我们需要优化我们的判断。
用
f
[
t
]
f[t]
f[t]表示从
t
t
t时间往前推,最远可到达的空位时间。
我们可以使用并查集维护!
详细代码
#define USEFASTERREAD 1
#define rg register
#define inl inline
#define DEBUG printf("[Passing [%s] in line %d.]\n", __func__, __LINE__)
#define putline putchar('\n')
#define putsp putchar(' ')
#define Rep(a, s, t) for(rg int a = s; a <= t; a++)
#define Repdown(a, t, s) for(rg int a = t; a >= s; a--)
typedef long long ll;
#include<cstdio>
#if USEFASTERREAD
char In[1 << 20], *ss = In, *tt = In;
#define getchar() (ss == tt && (tt = (ss = In) + fread(In, 1, 1 << 20, stdin), ss == tt) ? EOF : *ss++)
#endif
struct IO {
void RS() {freopen("test.in", "r", stdin), freopen("test.out", "w", stdout);}
template<typename T> inline IO r(T& x)const {
x = 0; T f = 1; char ch = getchar();
for(; ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1;
for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + int(ch - '0');
x *= f; return *this;
}
template<typename T> inline IO w(T x)const {
if(x < 0) {putchar('-'); x = -x;}
if(x >= 10) w(x / 10);
putchar(x % 10 + '0'); return *this;
}
template<typename T> inline IO wl(const T& x)const {w(x), putline; return *this;}
template<typename T> inline IO ws(const T& x)const {w(x), putsp; return *this;}
inline IO l() {putline; return *this;}
inline IO s() {putsp; return *this;}
}io;
template<typename T> inline T Max(const T& x, const T& y) {return y < x ? x : y;}
template<typename T> inline T Min(const T& x, const T& y) {return y < x ? y : x;}
template<typename T> inline void Swap(T& x, T& y) {T tmp = x; x = y; y = tmp;}
template<typename T> inline T Abs(const T& x) {return x > 0 ? x : -x;}
#include<algorithm>
using namespace std;
const int MAXN = 1000005;
const int MAXT = 700005;
int n;
int fa[MAXT];
int getfa(int x) {
return fa[x] == x ? x : (fa[x] = getfa(fa[x]));
}
struct Node {
int t, w;
bool operator < (const Node& x)const {
return w > x.w;
}
}A[MAXN];
bool flag[MAXT];
int ans;
int main() {
//io.RS();
io.r(n);
int maxt = 0;
for(rg int i = 1; i <= n; i++) io.r(A[i].t).r(A[i].w), maxt = Max(maxt, A[i].t);
sort(A + 1, A + 1 + n);
for(rg int i = 1; i <= maxt; i++) fa[i] = i;
for(rg int i = 1; i <= n; i++) {
int ft = getfa(A[i].t);
if(ft) ans += A[i].w, fa[ft] = getfa(ft - 1);
}
io.wl(ans);
return 0;
}