给定n个区间,求最多取多少区间使这些区间无交集
贪心:取能塞下区间中结束时间最早的即可
证明什么的,算法导论上找
#include<bits/stdc++.h>
#define FE "arrange"
using namespace std;
inline int getint(){
int x=0,p=1;
char c=getchar();
while(!isdigit(c)){
if(c=='-')p=-1;
c=getchar();
}
while(isdigit(c)){
x=(x<<3)+(x<<1)+(c^'0');
c=getchar();
}
return x*p;
}
struct acts{
int s,t;
}a[1000005];
inline bool cmp(const acts &a,const acts &b){
return a.t<b.t;
}
int main(){
int n=getint();
for(int i=1;i<=n;++i){
int sh=getint(),sm=getint(),th=getint(),tm=getint();
a[i].s=sh*60+sm,a[i].t=th*60+tm;
}
sort(a+1,a+n+1,cmp);
int end=-1,ans=0;
for(int i=1;i<=n;++i){
if(a[i].s>=end){
++ans,end=a[i].t;
}
}
cout<<ans<<endl;
return 0;
}
orz倍增爷scarlyw
/*
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() {
read_in();
solve();
return 0;
}