题目描述

作为一名忙碌的商人,约翰知道必须高效地安排他的时间.他有N工作要 做,比如给奶牛挤奶,清洗牛棚,修理栅栏之类的.

为了高效,列出了所有工作的清单.第i分工作需要T_i单位的时间来完成,而 且必须在S_i或之前完成.现在是0时刻.约翰做一份工作必须直到做完才能停 止.

所有的商人都喜欢睡懒觉.请帮约翰计算他最迟什么时候开始工作,可以让所有工作按时完成.(如果无法完成全部任务,输出-1)

输入输出格式

输入格式:

* Line 1: A single integer: N

* Lines 2..N+1: Line i+1 contains two space-separated integers: T_i and S_i

输出格式:

* Line 1: The latest time Farmer John can start working or -1 if Farmer John cannot finish all the jobs on time.

说明

Farmer John has 4 jobs to do, which take 3, 8, 5, and 1 units of time, respectively, and must be completed by time 5, 14, 20, and 16, respectively.

Farmer John must start the first job at time 2. Then he can do the second, fourth, and third jobs in that order to finish on time.

思路:

一道大水题,然而我还是错了……

我们知道他的持续时间和结束时间,那么我们肯定要在结束之前完成所有(否则输出-1)

所以我们按照结束时间排序

因为题目让你求最晚什么时候开始,所以我们尽可能地将任务往后放

对应过来就是从最大的开始,时光倒流

用一个time指针维护上一个开始的时刻

如果time比当前一个的结束点早,那么当前一个的实际结束点就是time

反之实际结束点就是最晚结束点

o(n)扫一遍即可

加上排序时间复杂度总共是O(nlogn+n)

很优化

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#define rii register int i
using namespace std;
struct deal{
int keep,start,last;
}x[];
int n,time;
bool cmp(deal ltt,deal kkk)
{
return ltt.last>kkk.last;
}
int main()
{
// freopen("manage.in","r",stdin);
// freopen("manage.out","w",stdout);
scanf("%d",&n);
for(rii=;i<=n;i++)
{
scanf("%d%d",&x[i].keep,&x[i].last);
x[i].start=x[i].last-x[i].keep;
}
sort(x+,x+n+,cmp);
time=x[].start;
for(rii=;i<=n;i++)
{
if(x[i].last<time)
{
time=x[i].start;
}
else
{
time=time-x[i].keep;
}
}
if(time<)
{
printf("-1");
return ;
}
printf("%d",time);
return ;
}

[USACO08NOV]时间管理Time Management(排序,贪心)的更多相关文章

  1. bzoj1620 / P2920 [USACO08NOV]时间管理Time Management

    P2920 [USACO08NOV]时间管理Time Management 显然的贪心. 按deadline从大到小排序,然后依次填充时间. 最后时间为负的话那么就是无解 #include<io ...

  2. P2920 [USACO08NOV]时间管理Time Management

    P2920 [USACO08NOV]时间管理Time Management 题目描述 Ever the maturing businessman, Farmer John realizes that ...

  3. 题解 P2920 【[USACO08NOV]时间管理Time Management】

    题面 作为一名忙碌的商人,约翰知道必须高效地安排他的时间.他有N工作要 做,比如给奶牛挤奶,清洗牛棚,修理栅栏之类的. 为了高效,列出了所有工作的清单.第i分工作需要T_i单位的时间来完成,而 且必须 ...

  4. [USACO08NOV]时间管理Time Management

    题目描述 Ever the maturing businessman, Farmer John realizes that he must manage his time effectively. H ...

  5. 洛谷 P2920 [USACO08NOV]时间管理Time Management

    传送门 题目大意: 每个工作有截至时间和耗费时间,n个工作求最小开始时间. 题解: 贪心 从n-1安排,让结束时间尽量的晚. 注意:优先级 cout<<st<0?-1:st;  (X ...

  6. [LUOGU] P2920 [USACO08NOV]时间管理Time Management

    见7.3测试 #include<iostream> #include<algorithm> #include<cstdio> using namespace std ...

  7. CMSIS-RTOS 时间管理之时间延迟Time Delay

    时间管理 Time Management 此RTOS除了可以把你的应用代码作为线程运行,它还可以提供一些时间服务功能,使用这些功能你就可以访问RTOS的一些系统调用. 时间延迟Time Delay 在 ...

  8. 1620: [Usaco2008 Nov]Time Management 时间管理

    1620: [Usaco2008 Nov]Time Management 时间管理 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 506  Solved: ...

  9. BZOJ 1620: [Usaco2008 Nov]Time Management 时间管理( 二分答案 )

    二分一下答案就好了... --------------------------------------------------------------------------------------- ...

随机推荐

  1. android 短期计划

    http://www.jianshu.com/p/2a9fcf3c11e4 http://www.jianshu.com/p/5f6d79323923 activity启动模式: http://www ...

  2. 详解WebApp与Native App的区别

    一篇真的很棒关于html5的Web App与Native App的技术分析 ! 自Iphone和Android这两个牛逼的手机操作系统发布以来,在互联网界从此就多了一个新的名词-WebApp(意为基于 ...

  3. C#中.Net的值传递和引用传递

    /// <summary> /// 电脑类 /// </summary> public class Computer { public string Type { get; s ...

  4. Shader之ShaderUI使用方法

    shader中的宏定义在material中Inspector中打开 Shader "Custom/Redify" { Properties{ _MainTex("Base ...

  5. Android学习——ViewPager的使用(三)

    这一节来介绍一下在ViewPager中常用到的一个控件,标题栏. 标题栏分为PagerTabStrip和PagerTitleStrip两种,用法类似,这里介绍第一种. 具体做法 在layout文件中的 ...

  6. 1.6 WEB API NET CORE 使用Redis

    一.Redis安装 https://www.cnblogs.com/cvol/p/9174078.html 二.新建.net core web api程序 选webapi 或者应用程序都可以 三.使用 ...

  7. visual studio 调试 不进断点 断点失效 提示当前不会命中该断点等问题解决

    1.首先看一下  当前调试模式是否为debug 2. 点击[调试]>[选项和设置] 将[要求源文件与原始文件完全匹配]勾选掉 3.点击调试的最后一个选项 点击[web] 将调试器内部勾选上需要测 ...

  8. 配置环境变量时,cmd下运行java -version,报错:找不到或无法加载主类 -version

    这个方面适用于报错为:java 找不到或无法加载主类,一般是找不到类的路径,问题出在CLASSPATH环境变量上,当然这是大多数.大概率的出错点 不排除根据个人情况不况,所以想起来伟大领袖毛主席的话: ...

  9. CRM中间件里的CSA队列有什么用

    我们有时候会在中间件的事务码SMQ2即Inbound队列查看器里观察到以CSA开头的队列: 这些队列的作用是什么呢?在SAP community上已经有很多朋友提出了相同的问题,也有专家在下列两个连接 ...

  10. OC block声明和使用

    #import "Button.h" typedef int (^MySum) (int, int); void test() { // 定义了一个block,这个block返回值 ...