Greedy:Cleaning Shifts(POJ 2376)
题目大意:农夫有N只牛,这些牛要帮助打扫农舍,这些牛只能打扫固定位置(千万要注意这个位置不是连续的),每一段区间必须至少有一只牛打扫,问你至少需要多少只牛?(如果区间不能完全被覆盖,则输出-1)。
这一题有点像上次的萨鲁曼军队,就是一个区间贪婪算法,我们尽可能的由一个方向往前找到最大的区间就可以了(如果与本位置的endT的间隔相同,找最远的,贪婪的最基本的思想)。
这一题本身不难,但是特判很恶心,一不小心就要出错了,比如1 5 3 1这样的输入,就直接输出-1就可以了(一定要有牛打扫1),还有,更关键的是,区间是不连续的(也就是扫完了13,下一次还可以扫46),一定要注意这一点!
- #include <iostream>
- #include <functional>
- #include <algorithm>
- using namespace std;
- typedef struct cows_t
- {
- int start_T;
- int end_T;
- }Cow_Set;
- Cow_Set cows[];
- void Search(const int, const int);
- int fcmop(const void *a, const void *b)
- {
- return (*(Cow_Set *)a).start_T - (*(Cow_Set *)b).start_T;
- }
- int main(void)
- {
- int N, T;
- while (~scanf("%d%d", &N, &T))
- {
- for (int i = ; i < N; i++)
- scanf("%d%d", &cows[i].start_T, &cows[i].end_T);
- qsort(cows, N, sizeof(Cow_Set), fcmop);
- Search(N, T);
- }
- return ;
- }
- void Search(const int N, const int T)
- {
- int et = , pos = , tmp_pos = -, ans = , tmp_maxt, j;
- if (cows[].start_T != )
- {
- cout << - << endl;
- return;
- }
- while ()
- {
- if (cows[pos].start_T > et + )//说明没有牛可以突破当前这个上一个时间界限,不用找了
- {
- cout << - << endl;
- return;
- }
- ans++;//增加一头开始的那头牛
- if (cows[pos].end_T >= T)
- break;
- tmp_maxt = ; tmp_pos = pos;
- for (j = pos + ; j < N && (cows[j].start_T <= cows[pos].end_T + ); j++)//找到间隔比当前T还要长的牛(记得T可以间隔1)
- {
- if (cows[j].end_T - cows[pos].end_T >= tmp_maxt)//要带等号,尽量往前
- {
- tmp_pos = j;
- tmp_maxt = cows[j].end_T - cows[pos].end_T;
- }
- }
- if (tmp_pos == pos)
- {
- cout << - << endl;
- return;
- }
- pos = tmp_pos;
- et = cows[tmp_pos].start_T;
- }
- if (ans != )
- printf("%d\n", ans);
- else
- cout << - << endl;
- }
Greedy:Cleaning Shifts(POJ 2376)的更多相关文章
- Cleaning Shifts POJ - 2376 (贪心题)
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31194 Accepted: 7677 ...
- poj 2376 Cleaning Shifts
http://poj.org/problem?id=2376 Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ 2376 Cleaning Shifts(轮班打扫)
POJ 2376 Cleaning Shifts(轮班打扫) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] Farmer ...
- POJ 2376 Cleaning Shifts 贪心
Cleaning Shifts 题目连接: http://poj.org/problem?id=2376 Description Farmer John is assigning some of hi ...
- 【原创】poj ----- 2376 Cleaning Shifts 解题报告
题目地址: http://poj.org/problem?id=2376 题目内容: Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K ...
- Cleaning Shifts(POJ 2376 贪心)
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15143 Accepted: 3875 ...
- POJ 2376 Cleaning Shifts【贪心】
POJ 2376 题意: 给出一给大区间和n各小区间,问最少可以用多少小区间覆盖整个大区间. 分析: 贪心法.设t为当前所有已确定区间的最右端,那我们可以每次都取所有可选的小区间(左端点<=t+ ...
- POJ - 2376 Cleaning Shifts 贪心(最小区间覆盖)
Cleaning Shifts Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some clea ...
- 【POJ - 2376】Cleaning Shifts(贪心)
Cleaning Shifts Descriptions: 原文是English,我这就直接上Chinese了,想看原文的点一下链接哦 大表哥分配 N (1 <= N <= 25,000) ...
随机推荐
- 【CodeForces 489A】SwapSort
题 Description In this problem your goal is to sort an array consisting of n integers in at most n sw ...
- 【bzoj1562】 NOI2009—变换序列
http://www.lydsy.com/JudgeOnline/problem.php?id=1562 (题目链接) 题意 给出一个序列(0~n-1),这个序列经过某个变换会成为另外一个序列,但是其 ...
- BZOJ4196 软件包管理器
Description Linux用户和OSX用户一定对软件包管理器不会陌生. 通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖 ...
- Vijos1459 车展 (treap)
描述 遥控车是在是太漂亮了,韵韵的好朋友都想来参观,所以游乐园决定举办m次车展.车库里共有n辆车,从左到右依次编号为1,2,…,n,每辆车都有一个展台.刚开始每个展台都有一个唯一的高度h[i].主管已 ...
- 轻量级应用开发之(07) UIPickerView使用
#import "ViewController.h" @interface ViewController ()<UIPickerViewDataSource,UIPicker ...
- PHP文件包含漏洞剖析
一. 什么才是”远程文件包含漏洞”?回答是:服务器通过php的特性(函数)去包含任意文件时,由于要包含的这个文件来源过滤不严,从而可以去包含一个恶意文件,而我们可以构造这个恶意文件来达到邪恶的目的. ...
- 微信公众平台项目中遇到的小问题40016,Invalid button size
刚辞职的同事用JAVA给客户开发的微信公众平台,今天晚上客户给我打电话说出现错误,此时我正跟朋友在外吃饭,联系已辞职的同事也联系不上,便答应回去之后我给调试看下. 问明客户说就修改了appkey和ap ...
- 调用MySql 分页存储过程带有输入输出参数
Create PROCEDURE getuser ( IN pageIndex INT, IN pageSize INT, OUT count INT ) BEGIN )*pageSize; sele ...
- c#之Redis实践list,hashtable
写在前面 最近公司搞了一个活动,用到了redis的队列,就研究了下redis的相关内容.也顺手做了个demo. C#之使用Redis 可以通过Nuget安装Reidis的相关程序集.安装之后发现会引入 ...
- prob
void calc_probability(int num) { , j = , k = ; #define SIZE_NUM 8 int *array_num = NULL; int *rememb ...