题目描述

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.

样例输入

6
25 7
26 1
15 1
22 3
20 1
30 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.

题解:

迟取法:

设k为总牛数

1.L-R之间牛的等于k,L++.

2.小于k,R++.

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6. const int N=,MAXN=,M=;
  7. int gi(){
  8. int str=;char ch=getchar();
  9. while(ch>'' || ch<'')ch=getchar();
  10. while(ch>='' && ch<='')str=str*+ch-'',ch=getchar();
  11. return str;
  12. }
  13. struct Ques{
  14. int x,id;
  15. }a[N];
  16. bool comp(const Ques &p,const Ques &q){
  17. return p.x<q.x;
  18. }
  19. int head[M],num=,ans=MAXN,sum=;
  20. struct Lin{
  21. int next,x,cnt;
  22. }b[N];
  23. void mark(int x,int t)
  24. {
  25. int p=x%M;
  26. for(int i=head[p];i;i=b[i].next)if(x==b[i].x){
  27. if(t==)
  28. {
  29. if(b[i].cnt==)sum++;
  30. b[i].cnt++;
  31. }
  32. else
  33. {
  34. if(b[i].cnt==)sum--;
  35. b[i].cnt--;
  36. }
  37. }
  38. }
  39. int Ask(int x)
  40. {
  41. int p=x%M;
  42. for(int i=head[p];i;i=b[i].next)if(x==b[i].x)return b[i].cnt;
  43. return ;
  44. }
  45. void Clear()
  46. {
  47. for(int i=;i<=num;i++)b[i].cnt=;
  48. sum=;
  49. }
  50. void init(int x)
  51. {
  52. int y=x%M;
  53. b[++num].next=head[y];
  54. b[num].x=x;
  55. b[num].cnt=;
  56. head[y]=num;
  57. }
  58. int main()
  59. {
  60. int n=gi(),k=;
  61. for(int i=;i<=n;i++){
  62. a[i].x=gi(),a[i].id=gi();
  63. if(!Ask(a[i].id))k++,init(a[i].id);
  64. }
  65. Clear();
  66. sort(a+,a+n+,comp);
  67. int l=,r=;
  68. mark(a[].id,);
  69. while(l<=r && r<=n)
  70. {
  71. if(sum==k){
  72. ans=min(a[r].x-a[l].x,ans);
  73. mark(a[l].id,-);
  74. l++;
  75. }
  76. else
  77. {
  78. r++;
  79. mark(a[r].id,);
  80. }
  81. }
  82. printf("%d",ans);
  83. return ;
  84. }

【USACO11NOV】牛的阵容Cow Lineup 尺取法+哈希的更多相关文章

  1. 洛谷 3029 [USACO11NOV]牛的阵容Cow Lineup

    https://www.luogu.org/problem/show?pid=3029 题目描述 Farmer John has hired a professional photographer t ...

  2. bzoj3048[Usaco2013 Jan]Cow Lineup 尺取法

    3048: [Usaco2013 Jan]Cow Lineup Time Limit: 2 Sec  Memory Limit: 128 MBSubmit: 225  Solved: 159[Subm ...

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

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

  4. [Luogu3069][USACO13JAN]牛的阵容Cow Lineup

    题目描述 Farmer John's N cows (1 <= N <= 100,000) are lined up in a row. Each cow is identified by ...

  5. LuoguP3069 【[USACO13JAN]牛的阵容Cow Lineup

    题目链接 看了看其他大佬的文章,为什么要控制右端呢 其实就是一个很简单的模拟队列趴... 难点就在于根据题意我们可以分析得一段合法区间内,不同种类个数不能超过k+2 哦当然,由于种类数范围过大,要对种 ...

  6. Luogu P3033 [USACO11NOV]牛的障碍Cow Steeplechase(二分图匹配)

    P3033 [USACO11NOV]牛的障碍Cow Steeplechase 题意 题目描述 --+------- -----+----- ---+--- | | | | --+-----+--+- ...

  7. 【题解】P3069 [USACO13JAN]牛的阵容Cow Lineup-C++

    题目传送门 思路这道题目可以通过尺取法来完成 (我才不管什么必须用队列)什么是尺取法呢?顾名思义,像尺子一样取一段,借用挑战书上面的话说,尺取法通常是对数组保存一对下标,即所选取的区间的左右端点,然后 ...

  8. [USACO11NOV]牛的障碍Cow Steeplechase

    洛谷传送门 题目描述: 给出N平行于坐标轴的线段,要你选出尽量多的线段使得这些线段两两没有交点(顶点也算),横的与横的,竖的与竖的线段之间保证没有交点,输出最多能选出多少条线段. 因为横的与横的,竖的 ...

  9. [USACO11NOV]牛的障碍Cow Steeplechase(匈牙利算法)

    洛谷传送门 题目描述: 给出N平行于坐标轴的线段,要你选出尽量多的线段使得这些线段两两没有交点(顶点也算),横的与横的,竖的与竖的线段之间保证没有交点,输出最多能选出多少条线段. 因为横的与横的,竖的 ...

随机推荐

  1. 冲刺NO.1

    Alpha冲刺第一天 站立式会议 项目进展 项目的第一天,主要工作是对项目的开发进行规划,以及将规划的成果转化为燃尽图与博客文章.依据项目需求分析报告与开题报告中已经完成的设计任务和项目规划,我们将系 ...

  2. django搭建web (三) admin.py -- 待续

    demo 关于模型myQuestion,myAnswer将在后述博客提及 # -*- coding: utf-8 -*- from __future__ import unicode_literals ...

  3. verilog学习笔记(4)_有限状态机

    有限状态机: 有限状态机是由寄存器组和组合逻辑构成的硬件时序电路: - 其状态(即由寄存器组的1和0的组合状态所构成的有限个状态)只能在同一时钟跳变沿的情况下才能从一个状态转向另一个状态: - 究竟转 ...

  4. 【iOS】swift init构造器

    这几天在使用 Swift 重写原来的一个运动社交应用 SportJoin. 为什么要重写呢? 首先因为实在找不到设计师给我作图; 其次, 我也闲不下来, 想找一些项目做, 所以只好将原来的代码重写了. ...

  5. 技术文档分享_linux中生成考核用的GPT分区表结构修复

    注:历史版本,后期改用python实现了 实验一: 目的:用于生成大量模拟破坏GPT分区结构案例,并生成唯一方式修复后的评判方法.故障:在一个完整的GPT分区磁盘上,丢失了GPT主分区表,或备份分区表 ...

  6. JAVA_SE基础——编码规范&代码编写规则

    这次我来给大家说明下编码规范&代码编写规则  ↓ 编码规范可以帮助程序员在编程时注意一些细节问题,提高程序的可读性,让程序员能够尽快地理解新的代码,并帮助大家编写出规范的利于维护的Java代码 ...

  7. phalcon框架命名空间

    命名空间第一影像就是实际上就相当宏定义,就是需要把一个很长的带有路径的类文件指定一个空间,然后就可直接用简单简写模式 当然如果是外部文件需要首先引入外部文件,如果不引入外部文件还是会报错.一般最会出错 ...

  8. SpringBoot的HelloWorld 应用及解释

    参考链接: Spring Data JPA - Reference Documentation Spring Data JPA--参考文档 中文版 纯洁的微笑:http://www.ityouknow ...

  9. eclipse+Maven插件报错:-Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable and mvn script match.

    问题描述: eclipse indigo+maven3.3.3+jdk1.70 maven插件执行报错:-Dmaven.multiModuleProjectDirectory system prope ...

  10. Linux上 ps 命令的用法

    ps a 显示现行终端机下的所有程序,包括其他用户的程序.2)ps -A 显示所有程序. 3)ps c 列出程序时,显示每个程序真正的指令名称,而不包含路径,参数或常驻服务的标示. 4)ps -e 此 ...