题目描述

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++.

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=,MAXN=,M=;
int gi(){
int str=;char ch=getchar();
while(ch>'' || ch<'')ch=getchar();
while(ch>='' && ch<='')str=str*+ch-'',ch=getchar();
return str;
}
struct Ques{
int x,id;
}a[N];
bool comp(const Ques &p,const Ques &q){
return p.x<q.x;
}
int head[M],num=,ans=MAXN,sum=;
struct Lin{
int next,x,cnt;
}b[N];
void mark(int x,int t)
{
int p=x%M;
for(int i=head[p];i;i=b[i].next)if(x==b[i].x){
if(t==)
{
if(b[i].cnt==)sum++;
b[i].cnt++;
}
else
{
if(b[i].cnt==)sum--;
b[i].cnt--;
}
}
}
int Ask(int x)
{
int p=x%M;
for(int i=head[p];i;i=b[i].next)if(x==b[i].x)return b[i].cnt;
return ;
}
void Clear()
{
for(int i=;i<=num;i++)b[i].cnt=;
sum=;
}
void init(int x)
{
int y=x%M;
b[++num].next=head[y];
b[num].x=x;
b[num].cnt=;
head[y]=num;
}
int main()
{
int n=gi(),k=;
for(int i=;i<=n;i++){
a[i].x=gi(),a[i].id=gi();
if(!Ask(a[i].id))k++,init(a[i].id);
}
Clear();
sort(a+,a+n+,comp);
int l=,r=;
mark(a[].id,);
while(l<=r && r<=n)
{
if(sum==k){
ans=min(a[r].x-a[l].x,ans);
mark(a[l].id,-);
l++;
}
else
{
r++;
mark(a[r].id,);
}
}
printf("%d",ans);
return ;
}

【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. 项目Alpha冲刺Day4

    一.会议照片 二.项目进展 1.今日安排 学习熟悉前台框架且搭建前台页面框架. 2.问题困难 使用了前端的构建工具webpack,困难在于怎么使用gradle结合它连同后台框架中的配置一起打包,因为本 ...

  2. iOS开发UIKit框架-可视化编程-XIB

    1. Interface Builder 可视化编程 1> 概述 GUI : 图形用户界面(Graphical User Interface, 简称GUI, 又称图形化界面) 是指采用图形方式显 ...

  3. 20145237 实验二 “Java面向对象程序设计”

    20145237 实验二 “Java面向对象程序设计” 实验内容 • 理解并掌握面向对象三要素:封装.继承.多态 • 初步掌握UML建模 • 熟悉S.O.L.I.D原则 • 使用TDD设计实现复数类 ...

  4. 在linux中关闭防火墙

    1) 重启后生效 开启: chkconfig iptables on 关闭: chkconfig iptables off 2) 即时生效,重启后失效 开启: service iptables sta ...

  5. Flask 学习 十一 关注者

    数据库关系 1.1多对多关系 添加第三张表(关联表),多对多关系可以分解成原表和关联表之间的两个一对多的关系 多对多仍然使用db.relationship()方法定义,但是secondary参数必须设 ...

  6. NOIP2017 列队

    https://www.luogu.org/problemnew/show/P3960 p<=500 50分 模拟 每个人的出队只会影响当前行和最后一列 p<=500,有用的行只有500行 ...

  7. EasyUI导航栏。

    html: <div data-options="region:'west',split:true" title="导航栏菜单" style=" ...

  8. 【深度学习】深入理解Batch Normalization批标准化

    这几天面试经常被问到BN层的原理,虽然回答上来了,但还是感觉答得不是很好,今天仔细研究了一下Batch Normalization的原理,以下为参考网上几篇文章总结得出. Batch Normaliz ...

  9. php的格式化数字函数

    php格式化数字:位数不足前面加0补足 php格式化数字:位数不足前面加0补足 感谢:http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/12 ...

  10. Python-面向对象(一)-Day7

    Day7-面向对象基础 1一.isinstance(obj, cls) 1二.issubclass(sub, super) 1三.异常处理 11.异常基础 12.异常种类 23.异常其他结构 54.主 ...