https://www.luogu.org/problem/show?pid=3029

题目描述

Farmer John has hired a professional photographer to take a picture of some of his cows. Since FJ's cows represent a variety of different breeds, he would like the photo to contain at least one cow from each distinct breed present in his herd.

FJ's N cows are all standing at various positions along a line, each described by an integer position (i.e., its x coordinate) as well as an integer breed ID. FJ plans to take a photograph of a contiguous range of cows along the line. The cost of this photograph is equal its size -- that is, the difference between the maximum and minimum x coordinates of the cows in the range of the photograph.

Please help FJ by computing the minimum cost of a photograph in which there is at least one cow of each distinct breed appearing in FJ's herd.

依次给出N头牛的位置及种类,要求找出连续一段,使其中包含所有种类的牛,问:这连续的一段最小长度是多少?

输入输出格式

输入格式:

  • Line 1: The number of cows, N (1 <= N <= 50,000).

  • Lines 2..1+N: Each line contains two space-separated positive integers specifying the x coordinate and breed ID of a single cow. Both numbers are at most 1 billion.

输出格式:

  • Line 1: The smallest cost of a photograph containing each distinct breed ID.

输入输出样例

输入样例#1:

  1. 6
  2. 25 7
  3. 26 1
  4. 15 1
  5. 22 3
  6. 20 1
  7. 30 1
输出样例#1:

  1. 4

说明

There are 6 cows, at positions 25,26,15,22,20,30, with respective breed IDs 7,1,1,3,1,1.

The range from x=22 up through x=26 (of total size 4) contains each of the distinct breed IDs 1, 3, and 7 represented in FJ's herd.

感谢 wjcwinmt 提供题目简述

队列

  1. #include<cstdio>
  2. #include<algorithm>
  3. #include<iostream>
  4. using namespace std;
  5. int n;
  6. long long ans=2e15;
  7. struct node
  8. {
  9. int pos,bl;
  10. bool operator < (node p)const
  11. {
  12. return pos<p.pos;
  13. }
  14. }e[];
  15. int head,tail,que[];
  16. int hassh[],sum[],cnt;
  17. int main()
  18. {
  19. scanf("%d",&n);
  20. for(int i=;i<=n;i++) scanf("%d%d",&e[i].pos,&e[i].bl),hassh[i]=e[i].bl;
  21. sort(hassh+,hassh+n+);
  22. int tot=unique(hassh+,hassh+n+)-(hassh+);
  23. for(int i=;i<=n;i++) e[i].bl=lower_bound(hassh+,hassh+n+,e[i].bl)-hassh;
  24. sort(e+,e+n+);
  25. for(int i=;i<=n;i++)
  26. {
  27.  
  28. if(++sum[e[i].bl]==) cnt++;
  29. que[tail++]=i;
  30. while(head<tail && sum[e[que[head]].bl]>) sum[e[que[head++]].bl]--;
  31. if(cnt==tot) ans=min(ans,1ll*e[que[tail-]].pos-e[que[head]].pos);
  32. }
  33. cout<<ans;
  34. }

洛谷 3029 [USACO11NOV]牛的阵容Cow Lineup的更多相关文章

  1. 洛谷P3069 [USACO13JAN]牛的阵容Cow Lineup(尺取法)

    思路 考虑比较朴素的解法,枚举每个长度为\(k+1\)的区间,然后统计区间中出现次数最多的颜色.这样的话复杂度为\(O(n*k)\)的,显然不行. 观察到统计每个区间中出现次数最多的颜色中,可以只用看 ...

  2. 洛谷P3080 [USACO13MAR]牛跑The Cow Run

    P3080 [USACO13MAR]牛跑The Cow Run 题目描述 Farmer John has forgotten to repair a hole in the fence on his ...

  3. 洛谷——P2853 [USACO06DEC]牛的野餐Cow Picnic

    P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ ...

  4. 洛谷 P2853 [USACO06DEC]牛的野餐Cow Picnic

    P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ ...

  5. 洛谷P2853 [USACO06DEC]牛的野餐Cow Picnic

    题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ 100) cows is grazing in one of N ...

  6. 【USACO11NOV】牛的阵容Cow Lineup 尺取法+哈希

    题目描述 Farmer John has hired a professional photographer to take a picture of some of his cows. Since ...

  7. 洛谷 P2966 [USACO09DEC]牛收费路径Cow Toll Paths

    题目描述 Like everyone else, FJ is always thinking up ways to increase his revenue. To this end, he has ...

  8. 洛谷 P2909 [USACO08OPEN]牛的车Cow Cars

    传送门 题目大意: m个车道. 如果第i头牛前面有k头牛,那么这头牛的最大速度会 变为原本的速度-k*D,如果速度小于l这头牛就不能行驶. 题解:贪心 让初始速度小的牛在前面 代码: #include ...

  9. 洛谷2971 [USACO10HOL]牛的政治Cow Politics

    原题链接 假设只有一个政党,那么这题就退化成求树的直径的问题了,所以我们可以从此联想至\(k\)个政党的情况. 先处理出每个政党的最大深度,然后枚举每个政党的其它点,通过\(LCA\)计算长度取\(\ ...

随机推荐

  1. 4.安装hive

      下载安装包并解压安装元数据库配置hive添加hvie环境变量修改hive-env.sh修改hive配置文件初始化metastore使用hive cli配置hivemestore配置hiveserv ...

  2. Alpha版——版本控制报告(Thunder)

    Part One 回答问题: 0.在吹牛之前,先回答这个问题:如果你的团队来了一个新队员,有一台全新的机器,你们是否有一个文档,只要设置了相应的权限,她就可以根据文档,从头开始搭建环境,并成功地把最新 ...

  3. 课堂练习之找数字0-N中“1”出现的次数

    一.题目与要求 题目:给定一个十进制的正整数,写下从1开始,到N的所有整数,然后数一下其中出现“1”的个数. 要求:1.写一个函数 f(N) ,返回1 到 N 之间出现的“1”的个数.例如 f(12) ...

  4. 软件工程 speedsnail 第二次冲刺9

    20150526 完成任务:划线的优化,速度和谐: 遇到问题: 问题1 速度仍然不满意 解决1 未解决 明日任务: 蜗牛碰到线后速度方向的调整:(做优化)

  5. pfx 证书怎么打开

    其实双击就能够自动运行导入向导的 不行的话使用我的办法: 单击开始--运行--里输入mmc 然后单击文件--选择添加删除管理单元--再选择添加--拉动滚动条找到证书一项,点击添加再点击完成(不用做任何 ...

  6. 【alpha】Scrum站立会议第2次....10.17

    小组名称:nice! 小组成员:李权 于淼 杨柳 刘芳芳 项目内容:约跑app 1.任务进度 成员 已完成 今日完成 李权 数据库设计 消息发送代码实现 于淼 注册.登录界面,以及登录界面后台代码.发 ...

  7. spring ioc经典总结

    component-scan标签默认情况下自动扫描指定路径下的包(含所有子包),将带有 @Component @Repository @Service @Controller标签的类自动注册到spri ...

  8. JVM(一)运行机制

    1.启动流程 2.JVM基本结构 PC寄存器 >每个线程拥有一个PC寄存器 >在线程创建时创建 >指向下一条指令的地址 >执行本地方法时,PC的值为undefined 方法区 ...

  9. context.getResourceAsStream获取的是部署在服务器上面的文件位置 而不是我们本地的工程位置 意思是说获取的都是web下面的文件位置

    context.getResourceAsStream获取的是部署在服务器上面的文件位置 而不是我们本地的工程位置 意思是说获取的都是web下面的文件位置

  10. linux虚拟机磁盘扩展与分区大小调整

    有段时间觉得linux虚拟机上的磁盘不太够用,研究了下其磁盘扩展 1.linux虚拟机磁盘扩展 step1. 先关机在编辑虚拟机中,找到硬盘选项增加空间,进行扩展step2. 进入root fdisk ...