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

#题解#洛谷 P3029 Cow Lineup S #双指针#离散化#

P3029 [USACO11NOV] Cow Lineup S - 洛谷

分析

  1. 离散化,双指针

代码实现

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
struct cow
{int pos, x;
} a[N];
bool cmp(cow x, cow y)
{return x.pos < y.pos;
}
map<int, int >m;
int main()
{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);int n;cin >> n;for (int i = 1; i <= n; i++){cin >> a[i].pos >> a[i].x;m[a[i].x] = 0;}int kind = m.size();sort(a + 1, a + 1 + n, cmp);
//	for(int i=1;i<=n;i++)
//		cout<<a[i].x<<" "<<a[i].pos<<'\n';int cnt = 0;int min_cost = 1e9;int j = 1;for (int i = 1; i <= n; i++){while (cnt < kind && j <= n){if (m[a[j].x] == 0)cnt++;m[a[j].x]++;j++;}if (kind == cnt)min_cost = min(min_cost, a[j - 1].pos - a[i].pos);m[a[i].x]--;if (m[a[i].x] == 0)cnt--;}cout << min_cost;return 0;
}
http://www.gsyq.cn/news/53499.html

相关文章:

  • 如何创建你的百Google度!!(实现双搜索引擎页面)
  • P7152 [USACO20DEC] Bovine Genetics G
  • CF1592E Bored Bakry
  • 如何在ISA-95体系中采用Apache Camel + MQTT Broker衔接L3与L4 Legacy应用
  • 11月18日日记
  • 一文讲清:数据清洗、数据中台、数据仓库、数据治理 - 智慧园区
  • 人工智能之编程进阶 Python高级:第四章 数学类模块
  • Solon AI 开发学习 - 1导引
  • 2025 年 11 月滚珠丝杆厂家推荐排行榜,高负载滚珠丝杆,耐磨滚珠丝杆,检测仪器高速滚珠丝杆,螺母滚珠丝杆,医用自动化滚珠丝杆公司推荐
  • UE4/UE5反射系统动态注册机制解析 - 实践
  • 做题随笔:P3403
  • 《从纪律委员到AI元人文开放者》
  • 「Solution」AGC008F Black Radius
  • 人工智能之编程进阶 Python高级:第二章 面向对象
  • OI vs Group Theory, Do You Guys Know?
  • 2025年11月穿戴式吸奶器,电动吸奶器,百元吸奶器品牌测评排名,高性价比选购指南!
  • 2025年11月百元吸奶器,静音吸奶器,便携吸奶器品牌测评排名,高性价比选购指南!
  • Q:R2R(Row-to-Row)映射 XML 是数据同步“源表字段→目标表字段” 的转换规则基础教程。
  • 【Azure Developer】解决在中国区 Microsoft Graph 命令Get-MgUserAuthenticationPhoneMethod 不可用的问题
  • 聊聊deepseek对latex的辅助
  • 【LVGL】图片部件
  • 10大 spring源码设计模式 (图解+秒懂+史上最全)
  • TypeScript-安装安装
  • ThreadLocal 、TransmittableThreadLocal 底层原理 (图解+秒懂+史上最全)
  • linux c ini
  • Spring AI Alibaba 项目源码学习(十二)-完结:Tool
  • linux busybox
  • C# 常用控件(学习笔记8)
  • 模拟赛记录 11/18
  • 代码随想录Day14_