当前位置: 首页 > news >正文

【贪心】P4873 [USACO14DEC] Cow Jog G|省选-

本文涉及知识点

C++贪心

P4873 [USACO14DEC] Cow Jog G

题目描述

Farmer John 的 $ N $ 头奶牛 $ ( 1 ≤ N ≤ 10^5 ) $ 正在一条长度无限的跑道上慢跑,每头奶牛都有一个不同的开始位置,以及不同的跑步速度。

为了方便奶牛们互相超越,整个跑道被分成了若干条赛道。在同一时刻,不可能有在同一条赛道上的两头奶牛占据相同的位置。

现在奶牛们要跑 $ T $ 分钟,在跑步过程中,他们不会改变自己所在的赛道和自己跑步的速度。FJ想要知道,为了奶牛间不会发生冲突,他需要至少准备多少条赛道。

输入格式

输出格式

输入输出样例 #1

输入 #1

5 3
0 1
1 2
2 3
3 2
6 1

输出 #1

3

贪心

假定奶牛的运动方向为正方向。追赶可以分两种情况:
一,初始位置小追赶初始位置大的奶牛。
二,初始位置相等,速度大的是追赶者。
性质一:两种情况都是追赶者的速度v1大于等于被追赶者速度v2。令最终追赶着位于y1,被追赶着位于y2。
如果没追上,y1<y2y1 < y2y1<y2
如果追上,则y1≥y2y1 \ge y2y1y2,令追赶者在y3追上被追赶者,又跑了t秒。则y1=y3+v1×ty1=y3+v1 \times ty1=y3+v1×t,被追干者的位置y2=y3+v2×ty2=y3+v2 \times ty2=y3+v2×t
因为t1≥t2t1 \ge t2t1t2,故y1≥y2y1\ge y2y1y2

实现

v记录各奶牛的如下信息{初始位置,速度×\times×-1,最终位置},升序排序。
对于奶牛i,追赶者一定小于i,故按升序可以保证无后续性。
多键有序集合s,记录各赛道的最大 最终位置。
令当前奶牛的最终位置y。
如果s中所有元素都≥y\ge yy,则此奶牛单独一个赛道。否则 选择小于y的最大赛道。
令y1 < y2 < y。
如果选择y1赛道,则两个赛道变化y,y2。
如果选择y2赛道,则两个赛道变为y1,y2。
显然后者优化前者。
新建赛道⟺\iff存在一条无穷小的赛道。

代码

核心代码


#include <iostream>
#include <sstream>
#include <vector>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<string>
#include<algorithm>
#include<functional>
#include<queue>
#include <stack>
#include<iomanip>
#include<numeric>
#include <math.h>
#include <climits>
#include<assert.h>
#include<cstring>
#include<list>#include <bitset>
using namespace std;template<class T1, class T2>
std::istream& operator >> (std::istream& in, pair<T1, T2>& pr) {in >> pr.first >> pr.second;return in;
}template<class T1, class T2, class T3 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3>& t) {in >> get<0>(t) >> get<1>(t) >> get<2>(t);return in;
}template<class T1, class T2, class T3, class T4 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4>& t) {in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t);return in;
}template<class T = int>
vector<T> Read() {int n;cin >> n;vector<T> ret(n);for (int i = 0; i < n; i++) {cin >> ret[i];}return ret;
}
template<class T = int>
vector<T> ReadNotNum() {vector<T> ret;T tmp;while (cin >> tmp) {ret.emplace_back(tmp);if ('\n' == cin.get()) { break; }}return ret;
}template<class T = int>
vector<T> Read(int n) {vector<T> ret(n);for (int i = 0; i < n; i++) {cin >> ret[i];}return ret;
}template<int N = 1'000'000>
class COutBuff
{
public:COutBuff() {m_p = puffer;}template<class T>void write(T x) {int num[28], sp = 0;if (x < 0)*m_p++ = '-', x = -x;if (!x)*m_p++ = 48;while (x)num[++sp] = x % 10, x /= 10;while (sp)*m_p++ = num[sp--] + 48;AuotToFile();}void writestr(const char* sz) {strcpy(m_p, sz);m_p += strlen(sz);AuotToFile();}inline void write(char ch){*m_p++ = ch;AuotToFile();}inline void ToFile() {fwrite(puffer, 1, m_p - puffer, stdout);m_p = puffer;}~COutBuff() {ToFile();}
private:inline void AuotToFile() {if (m_p - puffer > N - 100) {ToFile();}}char  puffer[N], * m_p;
};template<int N = 1'000'000>
class CInBuff
{
public:inline CInBuff() {}inline CInBuff<N>& operator>>(char& ch) {FileToBuf();ch = *S++;return *this;}inline CInBuff<N>& operator>>(int& val) {FileToBuf();int x(0), f(0);while (!isdigit(*S))f |= (*S++ == '-');while (isdigit(*S))x = (x << 1) + (x << 3) + (*S++ ^ 48);val = f ? -x : x; S++;//忽略空格换行		return *this;}inline CInBuff& operator>>(long long& val) {FileToBuf();long long x(0); int f(0);while (!isdigit(*S))f |= (*S++ == '-');while (isdigit(*S))x = (x << 1) + (x << 3) + (*S++ ^ 48);val = f ? -x : x; S++;//忽略空格换行return *this;}template<class T1, class T2>inline CInBuff& operator>>(pair<T1, T2>& val) {*this >> val.first >> val.second;return *this;}template<class T1, class T2, class T3>inline CInBuff& operator>>(tuple<T1, T2, T3>& val) {*this >> get<0>(val) >> get<1>(val) >> get<2>(val);return *this;}template<class T1, class T2, class T3, class T4>inline CInBuff& operator>>(tuple<T1, T2, T3, T4>& val) {*this >> get<0>(val) >> get<1>(val) >> get<2>(val) >> get<3>(val);return *this;}template<class T = int>inline CInBuff& operator>>(vector<T>& val) {int n;*this >> n;val.resize(n);for (int i = 0; i < n; i++) {*this >> val[i];}return *this;}template<class T = int>vector<T> Read(int n) {vector<T> ret(n);for (int i = 0; i < n; i++) {*this >> ret[i];}return ret;}template<class T = int>vector<T> Read() {vector<T> ret;*this >> ret;return ret;}
private:inline void FileToBuf() {const int canRead = m_iWritePos - (S - buffer);if (canRead >= 100) { return; }if (m_bFinish) { return; }for (int i = 0; i < canRead; i++){buffer[i] = S[i];//memcpy出错			}m_iWritePos = canRead;buffer[m_iWritePos] = 0;S = buffer;int readCnt = fread(buffer + m_iWritePos, 1, N - m_iWritePos, stdin);if (readCnt <= 0) { m_bFinish = true; return; }m_iWritePos += readCnt;buffer[m_iWritePos] = 0;S = buffer;}int m_iWritePos = 0; bool m_bFinish = false;char buffer[N + 10], * S = buffer;
};class Solution {
public:int Ans(const int T, vector<pair<int, int>>& sv) {const int N = sv.size();vector<tuple<int, int, long long>> vt;for (const auto& [s, v] : sv) {vt.emplace_back(s, -v, s + (long long)v * T);}sort(vt.begin(), vt.end());multiset<long long> s;for (const auto& [t1, t2, y] : vt) {auto it = s.lower_bound(y);if (it != s.begin()) {--it;s.erase(it);}s.emplace(y);}return s.size();}
};int main() {
#ifdef _DEBUGfreopen("a.in", "r", stdin);
#endif // DEBUGios::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr);	int N, T;cin >> N >> T;auto sv = Read<pair<int, int>>(N);
#ifdef _DEBUGprintf("T=%d", T);Out(sv, ",sv=");		//Out(edge, ",edge=");//Out(ope, ",ope=");
#endif // DEBUGauto res = Solution().Ans(T, sv);		cout << res << "\n";return 0;
}

单元测试

int T;vector<pair<int, int>> sv;TEST_METHOD(TestMethod11){T = 3, sv = { {0,1},{1,2},{2,3},{3,2},{6,1} };auto res = Solution().Ans(T,sv);AssertEx(3, res);}

扩展阅读

我想对大家说的话
工作中遇到的问题,可以按类别查阅鄙人的算法文章,请点击《算法与数据汇总》。
学习算法:按章节学习《喜缺全书算法册》,大量的题目和测试用例,打包下载。重视操作
有效学习:明确的目标 及时的反馈 拉伸区(难度合适) 专注
闻缺陷则喜(喜缺)是一个美好的愿望,早发现问题,早修改问题,给老板节约钱。
子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。
如果程序是一条龙,那算法就是他的是睛
失败+反思=成功 成功+反思=成功

视频课程

先学简单的课程,请移步CSDN学院,听白银讲师(也就是鄙人)的讲解。
https://edu.csdn.net/course/detail/38771
如何你想快速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程
https://edu.csdn.net/lecturer/6176

测试环境

操作系统:win7 开发环境: VS2019 C++17
或者 操作系统:win10 开发环境: VS2022 C++17
如无特殊说明,本算法用**C++**实现。

http://www.dtcms.com/a/322795.html

相关文章:

  • MBR分区nvme固态硬盘安装win7--非UEFI启动和GPT分区
  • llm本地部署+web访问+交互
  • Oracle字段操作
  • [TryHackMe]Challenges---Game Zone游戏区
  • 力扣热题100-----118.杨辉三角
  • Kettle ETL 工具存在的问题以及替代方案的探索
  • Arm Development Studio 安全通告:CVE-2025-7427
  • 什么情况下需要JVM调优?
  • Java-file类
  • 力扣 30 天 JavaScript 挑战 第二题笔记
  • 每日算法刷题Day59:8.9:leetcode 队列8道题,用时2h30min
  • 【攻防实战】从外到内全链路攻防实战纪实
  • python---类型别名
  • 1073. 沙漏
  • sqli-labs通关笔记-第40关 GET字符型堆叠注入(单引号括号闭合 手工注入+脚本注入两种方法)
  • J2000平赤道系、瞬时平赤道系与瞬时真赤道系
  • (论文速读)重新思考CNN生成网络中的上采样操作
  • 优先队列,链表优化
  • 2025-08-09通过授权码的方式给exe程序充值
  • 如何搭建ELK
  • C# DataGridView 添加进度条
  • 五、RuoYi-Cloud-Plus 前端项目部署以及如何改后端请求地址。
  • 《从零实现哈希表:详解设计、冲突解决与优化》
  • 09 【C++ 初阶】C/C++内存管理
  • 容器技术基础与实践:从镜像管理到自动运行配置全攻略
  • 【机器学习深度学习】模型选型:如何根据模型的参数算出合适的设备匹配?
  • Java 字符流与字节流详解
  • bms部分
  • 系统调用性能剖析在云服务器应用优化中的火焰图生成方法
  • 比亚迪第五代DM技术:AI能耗管理的深度解析与实测验证