NOIP模拟(11.02)T1 活动安排

30 篇文章 0 订阅
9 篇文章 0 订阅
这篇博客介绍了NOIP模拟赛中T1题目的活动安排问题。作者通过分析,发现该问题可以用贪心或动态规划(DP)或倍增算法来解决。最终,作者选择了贪心策略,通过右端点排序,每次选取当前可取的最大区间,并更新右端点,从而得到最优解。这是一个简化问题复杂性的有效方法。
摘要由CSDN通过智能技术生成

活动安排

题目背景:

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;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值