活动安排
题目背景:
11.02 NOIP模拟T1
分析:贪心 or DP or 倍增
第一眼:这个题不是贪心吗?
第二眼:不对啊,怎么数据才100000啊?
第三眼:咦,可以保留互不包含的区间然后倍增啊
第四眼:稳了
看到就想到了之前做过的无数类似题目,然后脑海里就剩下了倍增······首先明确一点,如果一个区间包含另一个区间,那肯定选择被包含的那个区间更优秀,因为较短可以给其他活动留下更多空间,那么我们就可以先把有包含关系的区间给踢掉,然后单调扫一遍获得每一个区间之后第一个没有交集的区间,之后就可以倍增了······然后统计每一个位置可以跳多少个区间。
Source:
/*
created by scarlyw
*/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <ctime>
inline char read() {
static const int IN_LEN = 1024 * 1024;
static char buf[IN_LEN], *s, *t;
if (s == t) {
t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
if (s == t) return -1;
}
return *s++;
}
///*
template<class T>
inline void R(T &x) {
static char c;
static bool iosig;
for (c = read(), iosig = false; !isdigit(c); c = read()) {
if (c == -1) return ;
if (c == '-') iosig = true;
}
for (x = 0; isdigit(c); c = read())
x = ((x << 2) + x << 1) + (c ^ '0');
if (iosig) x = -x;
}
//*/
const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN], *oh = obuf;
inline void write_char(char c) {
if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
*oh++ = c;
}
template<class T>
inline void W(T x) {
static int buf[30], cnt;
if (x == 0) write_char('0');
else {
if (x < 0) write_char('-'), x = -x;
for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
while (cnt) write_char(buf[cnt--]);
}
}
inline void flush() {
fwrite(obuf, 1, oh - obuf, stdout);
}
/*
template<class T>
inline void R(T &x) {
static char c;
static bool iosig;
for (c = getchar(), iosig = false; !isdigit(c); c = getchar()) {
if (c == -1) return ;
if (c == '-') iosig = true;
}
for (x = 0; isdigit(c); c = getchar())
x = ((x << 2) + x << 1) + (c ^ '0');
if (iosig) x = -x;
}
//*/
const int MAXN = 100000 + 10;
int n, x, y, z, w;
int fa[MAXN][20];
struct seg {
int l, r;
seg() {}
seg(int l, int r) : l(l), r(r) {}
inline bool operator < (const seg &a) const {
return (l == a.l) ? (r < a.r) : (l < a.l);
}
} s[MAXN];
inline void read_in() {
R(n);
for (int i = 1; i <= n; ++i) {
R(x), R(y), R(z), R(w);
x = x * 60 + y, z = z * 60 + w, s[i] = seg(x, z);
}
}
inline void solve() {
int temp = 1, j;
std::sort(s + 1, s + n + 1);
for (int i = 2; i <= n; ++i) if (s[i].l != s[i - 1].l) s[++temp] = s[i];
for (int i = j = temp - 1, r = temp; i >= 1; --i)
if (s[i].r < s[r].r) s[j--] = s[i], r = j + 1;
int cnt = temp - j;
for (int i = 1; i <= cnt; ++i) s[i] = s[i + j];
for (int i = 1, head = 1; i <= cnt; ++i) {
while (head < cnt && s[head].l < s[i].r) ++head;
if (s[head].l >= s[i].r) fa[i][0] = head;
}
for (int j = 1; j <= 17; ++j)
for (int i = 1; i <= cnt; ++i)
fa[i][j] = fa[fa[i][j - 1]][j - 1];
int ans = 0;
for (int i = 1; i <= cnt; ++i) {
int x = i, temp = 0;
for (int j = 17; j >= 0; --j)
if (fa[x][j] != 0) x = fa[x][j], temp |= (1 << j);
ans = std::max(ans, temp + 1);
}
std::cout << ans;
}
int main() {
freopen("arrange.in", "r", stdin);
freopen("arrange.out", "w", stdout);
read_in();
solve();
return 0;
}
然而这个题,贪心就可以了,直接右端点排序,然后每次开始取,只要能取就取上,然后更新目前的右端点,这样就是最大值了,因为右端点一定最前,所以肯定是最优的······
Source (orz xehoth dalao):
#include <bits/stdc++.h>
namespace IO {
inline char read() {
static const int IN_LEN = 1000000;
static char buf[IN_LEN], *s, *t;
s == t ? t = (s = buf) + fread(buf, 1, IN_LEN, stdin) : 0;
return s == t ? -1 : *s++;
}
template <typename T>
inline bool read(T &x) {
static char c;
static bool iosig;
for (c = read(), iosig = false; !isdigit(c); c = read()) {
if (c == -1) return false;
c == '-' ? iosig = true : 0;
}
for (x = 0; isdigit(c); c = read()) x = x * 10 + (c ^ '0');
iosig ? x = -x : 0;
return true;
}
inline void read(char &c) {
while (c = read(), isspace(c) && c != -1)
;
}
inline int read(char *buf) {
register int s = 0;
register char c;
while (c = read(), isspace(c) && c != -1)
;
if (c == -1) {
*buf = 0;
return -1;
}
do
buf[s++] = c;
while (c = read(), !isspace(c) && c != -1);
buf[s] = 0;
return s;
}
const int OUT_LEN = 1000000;
char obuf[OUT_LEN], *oh = obuf;
inline void print(char c) {
oh == obuf + OUT_LEN ? (fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf) : 0;
*oh++ = c;
}
template <typename T>
inline void print(T x) {
static int buf[30], cnt;
if (x == 0) {
print('0');
} else {
x < 0 ? (print('-'), x = -x) : 0;
for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 | 48;
while (cnt) print((char)buf[cnt--]);
}
}
inline void print(const char *s) {
for (; *s; s++) print(*s);
}
inline void flush() { fwrite(obuf, 1, oh - obuf, stdout); }
struct InputOutputStream {
template <typename T>
inline InputOutputStream &operator>>(T &x) {
read(x);
return *this;
}
template <typename T>
inline InputOutputStream &operator<<(const T &x) {
print(x);
return *this;
}
~InputOutputStream() { flush(); }
} io;
}
namespace {
using IO::io;
const int MAXN = 100010;
struct Node {
int l, r;
inline bool operator<(const Node &p) const {
return r < p.r;
}
} d[MAXN + 1];
inline void solve() {
register int n;
io >> n;
for (register int i = 1, x, y; i <= n; i++) {
io >> x >> y, d[i].l = x * 60 + y;
io >> x >> y, d[i].r = x * 60 + y;
}
std::sort(d + 1, d + n + 1);
register int now = d[1].r, ans = 1;
for (register int i = 2; i <= n; i++) {
if (now <= d[i].l) {
ans++;
now = d[i].r;
}
}
std::cout << ans;
}
}
int main() {
freopen("arrange.in", "r", stdin);
freopen("arrange.out", "w", stdout);
solve();
return 0;
}