题目描述
你有两张CD,第一张CD有N首歌,第i首歌的时长是A[i]秒;第二张CD有M首歌,第i首歌的时长是B[i]秒。这两张CD没有相同的歌曲。你总共有S分钟的时间去听歌,如果你选择听某首歌,你必须完整的听完这首歌。还有一个规则,对于第一张CD,你至少要听其中的T首歌,对于第二张CD,你也至少要听其中T首歌。在此前提下,你最多可以听多少首不同的歌?忽略在不同CD之间切换歌曲的时间。如果输入数据无法满足题目的规则,那么输出-1.
输入输出格式
输入格式
第一行,4个整数: N, M, S, T。 其中1 <= N, M <= 100, 1<= S <= 12000, 1 <= T <= 100。
第二行,N个整数,其中第i个整数是A[i],满足: 60 <= A[i] <= 3600。
第三行,M个整数,其中第i个整数是B[i],满足: 60 <= B[i] <= 3600。
输出格式
一个整数,你最多可以听多少首不同的歌。或者-1。
输入输出样例
输入样例
3 3 17 1
300 200 100
400 500 600
输出样例
4
题解
简单贪心,排序一下即可,注意题目中没有标明n,m<=t。
#include <iostream> #include <algorithm> #define MAX_N (100 + 5) #define MAX_M (100 + 5) using namespace std; int n, m, s, t; int a[MAX_N], b[MAX_M]; int ans; int main() { cin >> n >> m >> s >> t; if(n < t || m < t) return cout << -1, 0; s *= 60; ans = t + t; for(register int i = 1; i <= n; ++i) { cin >> a[i]; } for(register int i = 1; i <= m; ++i) { cin >> b[i]; } sort(a + 1, a + n + 1); sort(b + 1, b + m + 1); for(register int i = 1; i <= t; ++i) { s -= a[i] + b[i]; } if(s < 0) return cout << -1, 0; int i = t + 1, j = t + 1; while(i <= n && j <= m) { if(a[i] <= b[j]) { if(a[i] <= s) { s -= a[i++]; ++ans; } else break; } else { if(b[j] <= s) { s -= b[j++]; ++ans; } else break; } } while(i <= n) { if(a[i] <= s) { s -= a[i++]; ++ans; } else break; } while(j <= m) { if(b[j] <= s) { s -= b[j++]; ++ans; } else break; } cout << ans; return 0; }