P2920 [USACO08NOV]Time Management S

news/2024/10/23 8:33:09/

难度:3
知识点:贪心

这道题的分类在二分里面,实际上就是一个裸的贪心,贪心的策略就是按照结束时间排序,从大到小排那么就从前往后遍历,记录答案即可,因为同一个时间里面只能完成一个工作,想一想能得出这个贪心策略是正确的,

#include <bits/stdc++.h>#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()using namespace std;typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pa;const int N = 1005;pa p[N];bool cmp(pa a, pa b) {return a.se > b.se;
}int main() {int n;cin >> n;for (int i = 0; i < n; i++) cin >> p[i].fi >> p[i].se;sort(p, p + n, cmp);int last = p[0].se;for (int i = 0; i < n; i++) {if (last > p[i].se) last = p[i].se;last -= p[i].fi;}cout << (last >= 0 ? last : -1);return 0;
}

http://www.ppmy.cn/news/505745.html

相关文章

power oj 2920: 第K大1.0 优先队列

题目链接 题意是在一组数组里面&#xff0c;对每一个数求它右边的最大值。 题解&#xff1a;我们把可以不断将数字都放进一个优先队列里面&#xff0c;做一个在线操作&#xff1a;对于每一次放入a[i]的数&#xff0c;通过比较将队列前面比他小的所有数字弹出&#xff0c;记录这…

Luogu P2920 时间管理【二分答案】

二分答案水题。 &#xff08;像我这么蒻的人都能十几分钟A掉&#xff09; https://www.luogu.org/problemnew/show/P2920 开始时间一定在从0到min(t[i]-s[i])的一段区间上&#xff0c;因此我们可以愉快地二分答案。 在二分答案之前&#xff0c;我们贪心地把结束时间从小到大排一…

P2920 [USACO08NOV]时间管理Time Management

https://www.luogu.org/problemnew/show/P2920 #include <ctime> #include <cmath> #include <cstdio> #include <string> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm>//头文件准备 us…

eclipse导入tomcat 8.0x源码

1、安装Ant Ant下载地址&#xff1a;http://ant.apache.org/bindownload.cgi 下载完成以后&#xff0c;解压到相应目录&#xff0c;例如我解压到了D:\open-soft\apache-ant-1.9.6文件夹 然后配置Ant的环境变量&#xff0c;增加 ANT_HOME 为D:\open-soft\apache-ant-1.9.6\ 然后…

poj 2920 Mine Map【BFS】

&#xfeff;&#xfeff; Mine Map Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 1023 Accepted: 493 题目大意&#xff1a; 给一个n*n的金库&#xff0c;金库中有地雷‘*’和空格?&#xff0c;现在你从金库中间出发&#xff0c;刚刚开始时&#xff0c;【遍历…

LUOGU P2920 [USACO08NOV]时间管理Time Management

题目描述 Ever the maturing businessman, Farmer John realizes that he must manage his time effectively. He has N jobs conveniently numbered 1..N (1 < N < 1,000) to accomplish (like milking the cows, cleaning the barn, mending the fences, and so on). …

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

好了&#xff0c;废话不多说&#xff0c;我们切入正题&#xff0c;首先&#xff0c;不懂得分治的可以去看这位大佬的文章&#xff0c; 这道题是让我们求最晚可以在什么时间起床&#xff0c;这里我们需要加入一个小小的贪心&#xff0c;就是结束时间短的放前面处理&#xff0c;至…

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

emm贪心题&#xff0c;但不知道怎么让我搞成了并查集 先将数组按结束时间排序&#xff0c;因为肯定先安排靠后的工作&#xff0c;后面处理时冲突会减小很多 然后如何并查集乱搞呢&#xff1f; 假如下图是一个没有加入任务的时间线{{20,5},{15,4},{12,3},{10,1},{5,2}}这是排…