学校小卖部找零钱,面额有 100、50、20、10、5、1 元。收银员想用最少的张数找给同学。请你用贪心(尽量用大面额)算出张数。
一行一个整数 n(1 ≤ n ≤ 10000),表示要找的钱数(元)。
输出一个整数,表示最少需要的纸币张数。
186
6
#include <iostream>
using namespace std;
int main() {
int n, cnt = 0;
int m[] = {100, 50, 20, 10, 5, 1};
cin >> n;
for (int i = 0; i < 6; i++) {
cnt = cnt + n / m[i];
n = n % m[i];
}
cout << cnt << endl;
return 0;
}课后延时服务开了 n 个兴趣班,每个班有开始和结束时间,时间重叠的不能同时报。请你用贪心(按结束时间早的优先)算出最多能报几个。
第一行一个整数 n(1 ≤ n ≤ 10000);接下来 n 行,每行两个整数 s 和 e(1 ≤ s ≤ e ≤ 100000)。
输出一个整数,表示最多能参加的兴趣班数量。
3 1 3 2 4 3 5
2
#include <iostream>
#include <algorithm>
using namespace std;
struct Act {
int s, e;
};
bool cmp(Act a, Act b) {
if (a.e != b.e) return a.e < b.e;
return a.s < b.s;
}
int main() {
int n, cnt = 0, last = 0;
Act a[10005];
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i].s >> a[i].e;
sort(a, a + n, cmp);
for (int i = 0; i < n; i++) {
if (a[i].s >= last) { cnt++; last = a[i].e; }
}
cout << cnt << endl;
return 0;
}食堂只有一个窗口,n 个同学打饭需要的时间各不相同。为了让所有人「等的时间总和」最小,应该让打得快的人先打。请你算出最小总等待时间。
第一行一个整数 n(1 ≤ n ≤ 10000);第二行 n 个整数(1 ≤ t ≤ 100)。
输出一个整数,表示所有人的等待时间总和(第一个人等待 0)。
3 3 1 2
4
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n, t[10005];
long long s = 0, cur = 0;
cin >> n;
for (int i = 0; i < n; i++) cin >> t[i];
sort(t, t + n);
for (int i = 0; i < n; i++) {
s = s + cur;
cur = cur + t[i];
}
cout << s << endl;
return 0;
}老师给排成一排的 n 个同学分糖果,规则是:每人至少 1 颗;相邻两人中分数高的必须比分数低的多拿。请你用贪心(两遍扫描)算出最少要多少颗糖。
第一行一个整数 n(1 ≤ n ≤ 10000);第二行 n 个整数(0 ≤ 分数 ≤ 100)。
输出一个整数,表示最少需要的糖果总数。
3 1 0 2
5
#include <iostream>
using namespace std;
int main() {
int n, r[10005], c[10005];
long long s = 0;
cin >> n;
for (int i = 0; i < n; i++) { cin >> r[i]; c[i] = 1; }
for (int i = 1; i < n; i++) if (r[i] > r[i - 1]) c[i] = c[i - 1] + 1;
for (int i = n - 2; i >= 0; i--)
if (r[i] > r[i + 1] && c[i] <= c[i + 1]) c[i] = c[i + 1] + 1;
for (int i = 0; i < n; i++) s = s + c[i];
cout << s << endl;
return 0;
}运动会要在跑道上设检查点,每个检查点能覆盖一个区间 [l, r]。请问最少设几个检查点,能让每个上报的区间都至少被覆盖一次?
第一行一个整数 n(1 ≤ n ≤ 10000);接下来 n 行,每行两个整数 l 和 r(1 ≤ l ≤ r ≤ 100000)。
输出一个整数,表示最少需要的检查点数量。
3 1 3 2 4 3 5
1
#include <iostream>
#include <algorithm>
using namespace std;
struct Seg {
int l, r;
};
bool cmp(Seg a, Seg b) { return a.r < b.r; }
int main() {
int n, cnt = 0, last = -1;
Seg a[10005];
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i].l >> a[i].r;
sort(a, a + n, cmp);
for (int i = 0; i < n; i++) {
if (a[i].l > last) { cnt++; last = a[i].r; }
}
cout << cnt << endl;
return 0;
}班里搬书,一共有 n 摞书,每摞重量不同,一趟最多能搬总重 W。每摞书不能拆开。请你用贪心(轻的先搬)算出最多能搬几摞。
第一行两个整数 n 和 W(1 ≤ n ≤ 10000,1 ≤ W ≤ 100000);第二行 n 个整数(1 ≤ w ≤ 1000)。
输出一个整数,表示最多能搬走的摞数。
5 10 4 8 1 5 2
3
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n, W, w[10005], cnt = 0, s = 0;
cin >> n >> W;
for (int i = 0; i < n; i++) cin >> w[i];
sort(w, w + n);
for (int i = 0; i < n; i++) {
if (s + w[i] <= W) { s = s + w[i]; cnt++; }
else break;
}
cout << cnt << endl;
return 0;
}劳动课上要把 n 堆果子合并成一大堆,每次只能合并两堆,花掉的力气等于两堆重量之和。请用贪心(每次合并最轻的两堆)算出最小总力气。
第一行一个整数 n(1 ≤ n ≤ 1000);第二行 n 个整数(1 ≤ 重量 ≤ 1000)。
输出一个整数,表示最小总力气。
3 1 2 9
15
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n, a[1005];
long long s = 0;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
for (int i = 0; i < n - 1; i++) {
a[i + 1] = a[i] + a[i + 1];
s = s + a[i + 1];
for (int j = i + 1; j < n - 1; j++)
if (a[j] > a[j + 1]) { int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; }
}
cout << s << endl;
return 0;
}社团活动要安排 n 个时段,同一时段只能在一间教室上课。请你算出最少需要几间教室(等价于同一时刻最多有几个活动在同时进行)。
第一行一个整数 n(1 ≤ n ≤ 10000);接下来 n 行,每行两个整数 l 和 r(1 ≤ l ≤ r ≤ 100000)。
输出一个整数,表示最少教室数。
3 1 3 2 4 3 5
3
#include <iostream>
using namespace std;
int d[100005];
int main() {
int n, l, r, mx = 0, cur = 0;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> l >> r;
d[l] = d[l] + 1;
d[r + 1] = d[r + 1] - 1;
}
for (int i = 1; i <= 100001; i++) {
cur = cur + d[i];
if (cur > mx) mx = cur;
}
cout << mx << endl;
return 0;
}班级义卖找零钱,面额只有 10、5、2、1 元。请你用贪心算出凑出 n 元最少要几张。
一行一个整数 n(1 ≤ n ≤ 10000)。
输出一个整数,表示最少张数。
28
5
#include <iostream>
using namespace std;
int main() {
int n, cnt = 0;
int m[] = {10, 5, 2, 1};
cin >> n;
for (int i = 0; i < 4; i++) {
cnt = cnt + n / m[i];
n = n % m[i];
}
cout << cnt << endl;
return 0;
}数学竞赛有道题:从 n 个整数里挑 3 个,让乘积最大(可能有负数)。请你用「排序后比较」的贪心思路解决。
第一行一个整数 n(3 ≤ n ≤ 10000);第二行 n 个整数(-1000 ≤ 数值 ≤ 1000)。
输出一个整数,表示最大的乘积。
5 -10 -10 1 2 3
300
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n, a[10005];
long long x, y;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
x = 1LL * a[n - 1] * a[n - 2] * a[n - 3];
y = 1LL * a[0] * a[1] * a[n - 1];
cout << (x > y ? x : y) << endl;
return 0;
}