题目链接:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1650 题目大意:会议安排问题加强版:有k群牛分别要从出发地s坐飞机到目的地e,飞机容量为c,求最多能运多少头牛。 分析:纯贪心算法自己百度一下。这里讲的算法以会议安排的贪心思想为基础:每次选择最先下飞机的牛上飞机。问题在于确定有多少只牛可以上飞机(虽然选择的牛下飞机早,但可能上飞机也早,这时飞机上还有其他牛),也就是要确定从出发地到目的地之间飞机上的空位最少的时候有多少,也就是飞机容量减去飞机上的牛的最大值,即求区间最大值,所以用线段树。每次牛上飞机后更新飞机上的牛的数量,即区间更新。 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define mp make_pair #define X first #define Y second #define LL(x) ((x) << 1) #define RR(x) ((x) << 1 | 1) #define MEMSET(a, b) memset(a, b, sizeof(a)) using namespace std; typedef unsigned int ui; typedef long long ll; typedef unsigned long long ull; typedef pair pii; typedef vector vi; typedef vi::iterator vi_it; typedef map mii; typedef priority_queue pqi; typedef priority_queue , greater > rpqi; typedef priority_queue pqp; typedef priority_queue , greater > rpqp; const int MAX_N = 100000 + 2; struct { int left; int right; int maxv; int add; inline int mid() { return (left + right) >> 1; } } st[MAX_N * 3]; struct node { int start; int end; int cnt; } coll[2][MAX_N]; void build(int l, int r, int idx) { st[idx].left = l; st[idx].right = r; st[idx].maxv = 0; st[idx].add = 0; if (l != r) { int mid = st[idx].mid(); build(l, mid, LL(idx)); build(mid + 1, r, RR(idx)); } } void update(int l, int r, int v, int idx) { if (l <= st[idx].left && r >= st[idx].right) { st[idx].add += v; st[idx].maxv += v; return; } int mid = st[idx].mid(), lc = LL(idx), rc = RR(idx); if (st[idx].add) { st[lc].add += st[idx].add; st[lc].maxv += st[idx].add; st[rc].add += st[idx].add; st[rc].maxv += st[idx].add; st[idx].add = 0; } if (l <= mid) update(l, r, v, lc); if (r > mid) update(l, r, v, rc); st[idx].maxv = max(st[lc].maxv, st[rc].maxv); } int query(int l, int r, int idx) { if (l == st[idx].left && r == st[idx].right) return st[idx].maxv; int mid = st[idx].mid(), lc = LL(idx), rc = RR(idx); if (st[idx].add) { st[lc].add += st[idx].add; st[lc].maxv += st[idx].add; st[rc].add += st[idx].add; st[rc].maxv += st[idx].add; st[idx].add = 0; } if (r <= mid) return query(l, r, lc); else if (l > mid) return query(l, r, rc); else return max(query(l, mid, lc), query(mid + 1, r, rc)); } bool cmp(const node &nd1, const node &nd2) { return nd1.end < nd2.end; } int main(int argc, char *argv[]) { // freopen("D:\\in.txt", "r", stdin); int k, n, c, cnt[2] = {0, 0}, i, num; cin >> k >> n >> c; for (i = 0; i < k; ++i) { int s, e, m; scanf("%d%d%d", &s, &e, &m); int idx = e < s; coll[idx][cnt[idx]].start = s; coll[idx][cnt[idx]].end = e; if (idx == 1) { coll[1][cnt[1]].start = n + 1 - coll[1][cnt[1]].start; coll[1][cnt[1]].end = n + 1 - coll[1][cnt[1]].end; } coll[idx][cnt[idx]++].cnt = m; } sort(coll[0], coll[0] + cnt[0], cmp); sort(coll[1], coll[1] + cnt[1], cmp); int ans = 0; for (int idx = 0; idx < 2; ++idx) { build(1, n - 1, 1); for (i = 0; i < cnt[idx]; ++i) { int free_num = c - query(coll[idx][i].start, coll[idx][i].end - 1, 1); if (free_num > coll[idx][i].cnt) free_num = coll[idx][i].cnt; ans += free_num; update(coll[idx][i].start, coll[idx][i].end - 1, free_num, 1); } } cout << ans << endl; return 0; }