复习3-贪心与数据结构
A. 奶牛飞车
- 贪心去搞就好了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19const int N = 5e4 + 5;
int a[N];
int vis[N];
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n , m , d , l;
cin >> n >> m >> d >> l;
for (int i = 1;i <= n ;i ++){
cin >> a[i];
}
sort(a + 1 , a + n + 1);
int tot = 1;
int ans = 0;
for (int i = 1; i <= n ; i ++){
if (a[i] - (ans / m * d) >= l) ++ ans;
}
cout << ans << endl;
return 0;
}
B. 牛奶规划
- 1.反悔贪心
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21vector<int> t[10005];
int tmx,n;
int ans;
priority_queue<int> q;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
int x,y;
scanf("%d %d",&x,&y);
t[y].push_back(x);
tmx=max(tmx,y);
}
for(int i=tmx;i>=1;i--){
for(auto y:t[i]) q.push(y);
if(q.empty()) continue;
ans+=q.top();
q.pop();
}
cout<<ans;
return 0;
} - 2.DPC. 集市班车
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22struct _{
int g,d;
bool operator< (_ A){return d<A.d;}
}a[10010];
int n,dp[10010],s;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d%d",&a[i].g,&a[i].d);
}
sort(a+1,a+1+n);
for(int i=1;i<=n;i++){
for(int j=a[i].d-1;j>=1;j--){
dp[j]=max(dp[j-1]+a[i].g,dp[j]);
s=max(s,dp[j]);
}
dp[0]=max(dp[0],a[i].g);
s=max(s,dp[0]);
}
printf("%d",s);
return 0;
} - 反悔贪心
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
using namespace std;
int n,k,c;
int s[50005],t[50005],m[50005];
vector<pii> a[10005];
int b[105];
int ans;
int main(){
cin>>k>>n>>c;
for(int i=1;i<=k;i++){
scanf("%d%d%d",&s[i],&t[i],&m[i]);
a[t[i]].push_back(make_pair(s[i],m[i]));
}
for(int i=1;i<=n;i++){
for(int u=0;u<a[i].size();u++){
pii x=a[i][u];
for(int j=c;j&&x.second;j--){
if(b[j]<=x.first){
ans++;
b[j]=i;
x.second--;
}
}
sort(b+1,b+c+1);
}
}
cout<<ans<<endl;
return 0;
}
D. 炸树
1 |
|
E. 漫长的征途
- 反悔贪心 加满油再放回来
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
46pair <long long, long long> que[2000010];
int n, qhd = 1000005, qtl = 1000005, g, b, d;
pair <long long, long long> a[1000005];
long long ans = 0;
inline bool Move(int dis) {
while (qhd != qtl && dis) {
if (que[qhd].second <= dis) {
b -= que[qhd].second;
dis -= que[qhd].second; ans += que[qhd].first * que[qhd].second;
qhd++;
} else {
b -= dis;
que[qhd].second -= dis; ans += dis * que[qhd].first;
dis = 0;
}
}
return dis == 0;
}
inline void PopGas(int cost) {
while (qhd != qtl && que[qtl - 1].first >= cost) {
b -= que[qtl - 1].second;
qtl--;
}
que[qtl++] = make_pair(cost, g - b); b = g;
}
inline void Solve() {
a[++n] = make_pair(d, 0);
que[qtl++] = make_pair(0, b);
for (int i = 1;i <= n;i++) {
if (!Move(a[i].first - a[i - 1].first)) {
puts("-1");
return;
}
PopGas(a[i].second);
}
printf("%lld", ans);
}
int main() {
cin>>n>>g>>b>>d;
for (int i = 1;i <= n;i++) {
scanf("%d %d",&a[i].first,&a[i].second);
}
sort(a + 1, a + n + 1);
Solve();
return 0;
}
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Ysmmm的快乐小屋!