当前位置: 首页 > 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.jsqmd.com/news/43954/

相关文章:

  • 题解:AtCoder ARC192D Fraction Line
  • Linux如何安装利用Rust指南
  • tryhackme-网络安全基础-网络- 网络概念-24
  • 如何创建你的百Google度!!(实现双搜索引擎页面)
  • P7152 [USACO20DEC] Bovine Genetics G
  • CF1592E Bored Bakry
  • 如何在ISA-95体系中采用Apache Camel + MQTT Broker衔接L3与L4 Legacy应用
  • 11月18日日记
  • 一文讲清:数据清洗、数据中台、数据仓库、数据治理 - 智慧园区
  • 通过liquibase实现一个简单的数据库适配器,自动适配60+数据库
  • 人工智能之编程进阶 Python高级:第四章 数学类模块
  • Pandas GroupBy 的 10 个实用技巧
  • lvs详细配置
  • Lazarus使用cef打开文件和下载设置
  • 题解:P14435 [JOISC 2013] 收拾吉祥物 / Mascots
  • Solon AI 开发学习 - 1导引
  • linux c 线程池
  • linux c 文件是否存在
  • 2025 年 11 月滚珠丝杆厂家推荐排行榜,高负载滚珠丝杆,耐磨滚珠丝杆,检测仪器高速滚珠丝杆,螺母滚珠丝杆,医用自动化滚珠丝杆公司推荐
  • Pjudge #21741. 【NOIP Round #5】青鱼和区间 题解
  • 11月18日
  • UE4/UE5反射系统动态注册机制解析 - 实践
  • 完全平方和的推广
  • 三维偏序整体二分?
  • 做题随笔:P3403
  • 2025.11.18
  • 《从纪律委员到AI元人文开放者》
  • MEMS与CMOS的3D集成技术研究进展 - 指南
  • CSS学习笔记(六):CSS预处理器 - 实践
  • 「Solution」AGC008F Black Radius