巧妙的类似贪心的做法
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef pair<int, int> P;
const int maxn = 500;
P itv[maxn];
int room[maxn];
int main(int argc, char const *argv[]) {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d%d", &itv[i].first, &itv[i].second);
if (itv[i].first > itv[i].second) {
swap(itv[i].first, itv[i].second);
}
if (itv[i].first & 1) {
itv[i].first++;
}
if (itv[i].second & 1) {
itv[i].second++;
}
for (int j = itv[i].first; j <= itv[i].second; j += 2) {
room[j]++;
}
}
printf("%d\n", *max_element(room, room + maxn) * 10);
memset(room, 0, sizeof(room));
}
return 0;
}

本文介绍了一种基于贪心策略的区间分配算法实现,通过扫描区间并调整奇数边界值来确保每个区间的起始点为偶数。该算法利用数组记录每个位置被覆盖的次数,并找出最大覆盖次数以计算最终所需的房间数量。
875

被折叠的 条评论
为什么被折叠?



