我是靠谱客的博主 贪玩紫菜,这篇文章主要介绍codeforces 496e Distributing Parts 贪心,现在分享给大家,希望可以做个参考。

题意:

现在有n个曲子,每个曲子的范围为ai~bi。有m个演奏家,每个演奏家的范围为ci~di,并且可以出演次数为ki次。

如果ci<= ai<=bi<=di,则说明该曲子可以由演奏家演出。

让你找出合理的方案使得所有曲子都能被演奏,无方案输出“NO”;

思路:

这题跟区域赛上海站比较像,虽说当时挂0了。。。。

首先根据r值,也就是右边的值,把两个数组从小到大排个序。(降维,把r值的限制去掉)

开始遍历演奏家,把节目的bi值,小于等于演奏家的di值的节目,全扔到set里,set里根据ai排序。(看到铭神用stl用得灰起,有空我也得研究一下set - -)。

然后二分lower_bound找出与演奏家的ci值最相近的节目,说明该演奏家会演奏该节目。如果无合适的节目可选,跳到下一个演奏家。

code:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <set> #include <algorithm> #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <cstdlib> using namespace std; typedef long long LL; const int MAXN = 1e5+5; struct PP { int l, r, k; int rank, ans; bool operator < (const PP& b) const { if(r == b.r) return l < b.l; return r < b.r; } }a[MAXN], b[MAXN]; struct Point { int l, r, rank; bool operator < (const Point& b) const { if(l == b.l) { if(r == b.r) return rank < b.rank; return r < b.r; } return l < b.l; } }; int n, m; int res[MAXN]; void input() { cin>>n; for(int i = 0;i < n; i++) { cin>>a[i].l>>a[i].r; a[i].rank = i; } cin>>m; for(int i = 0;i < m; i++) { cin>>b[i].l>>b[i].r>>b[i].k; b[i].rank = i; } } bool cmp(const PP &a, const PP &b) { return a.rank < b.rank; } void solve() { sort(a, a+n); sort(b, b+m); set <Point> s; int cnt = n; for(int i = 0, j = 0;i < m; i++) { while(j < n && a[j].r <= b[i].r) { s.insert((Point){a[j].l, a[j].r, j}); j++; } for(int z = 0; z < b[i].k; z++) { auto it = s.lower_bound((Point){b[i].l, 0, 0}); if(it == s.end()) break; a[it->rank].ans = b[i].rank+1; s.erase(it); cnt--; } } //output answer if(s.size() != 0 || cnt != 0) { cout<<"NO"<<endl; return; } sort(a, a+n, cmp); cout<<"YES"<<endl; cout<<a[0].ans; for(int i = 1; i < n; i++) cout<<" "<<a[i].ans; cout<<endl; } int main() { ios::sync_with_stdio(false); input(); solve(); return 0; }


最后

以上就是贪玩紫菜最近收集整理的关于codeforces 496e Distributing Parts 贪心的全部内容,更多相关codeforces内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(70)

评论列表共有 0 条评论

立即
投稿
返回
顶部