1009 Package Delivery
题意
一共有n件快递,每个快递都必须在 [li,ri]时间内领取,小Q一次最多可以拿k件,问小Q最少可以跑几次
思路
按照每个区间的右端点排序,对于某个快递,若其左端点大于前面所有的右端点,则其必须单独取件,答案+1,并将其右端点插入mp中,否则,则意味着该快点后面有某个区间的右端点,此快递可以与之一起取,记录此时取的快递数量+1,若数量达到k则从mp中删除。
int main() {
int T;
cin >> T;
while (T--) {
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> a[i].first >> a[i].second;
sort(a + 1, a + 1 + n, [](pii A, pii B) {
if (A.second == B.second) return A.first < B.first;
return A.second < B.second;
});
map<int, int> mp;
int ans = 0;
for (int i = 1; i <= n; i++) {
auto pos = mp.lower_bound(a[i].first);
if (pos == mp.end()) {
if (k == 1) {
ans++;
continue;
}
mp[a[i].second] = 1;
ans++;
}
else {
mp[pos->first]++;
if (mp[pos->first] == k) mp.erase(pos);
}
}
cout << ans << endl;
}
return 0;
}
1012 Two Permutations
int a[N], b[N], c[N], n;
ll dp[N][2];
int dfs(int x, int y, int pos) {
if (x > n && y > n) return 1;
if (dp[x + y - 1][pos] != -1) return dp[x + y - 1][pos];
ll res = 0;
if (x <= n && a[x] == c[x + y - 1]) {
res += dfs(x + 1, y, 0);
}
if (y <= n && b[y] == c[x + y - 1]) {
res += dfs(x, y + 1, 1);
}
dp[x + y - 1][pos] = res;
return res % MOD;
}
int main() {
int T;
cin >> T;
while (T--) {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> b[i];
for (int i = 1; i <= n + n; i++) cin >> c[i];
for (int i = 0; i <= n + n; i++) dp[i][0] = dp[i][1] = -1;
ll ans = 0;
if (a[1] == c[1]) ans += dfs(2, 1, 0);
if (b[1] == c[1]) ans += dfs(1, 2, 1);
cout << ans % MOD << endl;
}
return 0;
}