您的位置:首页 > 其它

POJ2376-Cleaning Shifts

2016-04-05 17:36 288 查看
在t个shift,n头牛中选择用最少的牛完成所有的shift。

明显是个贪心的题。

只要每次找出最晚结束shift的牛即可找到最优解。

#include <cstdio>
#include <utility>
#include <algorithm>

using namespace std;

const int maxn = 25000;

typedef pair<int, int> cow;
cow itv[maxn+2];

int comp(cow a, cow b)
{
if(a.first == b.first) {
return a.second > b.second;
}
return a.first < b.first;
}

int main()
{
int n, t;
scanf("%d%d", &n, &t);

int max_end = 0;
for (int i = 0; i < n; i++) {
scanf("%d%d", &itv[i].first, &itv[i].second);
max_end = max(max_end, itv[i].second);
}

sort(itv, itv + n, comp);

if (max_end < t || itv[0].first != 1) {
printf("-1\n");
return 0;
}

int cnt = 1;
int mark = 0;
int i = 0;
int cur = itv[0].second;
while (cur < t) {
bool flag = false;
for (i = mark; i < n && itv[i].first <= cur + 1; i++) {
if (itv[i].second > itv[mark].second) {
flag = true;
mark = i;
}
}
if (!flag) {
cnt = -1;
break;
}
cur = itv[mark].second;
cnt++;
}

printf("%d\n", cnt);

return 0;
}


加油。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: