题目描述

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. 20162321王彪-实验二-Java面向对象程序设计

    实验二Java面向对象程序设计 实验内容一 初步掌握单元测试和TDD 什么是单元测试:单元测试时开发者编写的一小段代码,用于检测被测代码的一个很小的,很明确的功能是否正确.执行单元测试,是为了证明某段 ...

  2. 顺企网 爬取16W数据保存到Mongodb

    import requests from bs4 import BeautifulSoup import pymongo from multiprocessing.dummy import Pool ...

  3. UWP 页面间传递参数(常见类型string、int以及自定义类型)

    这是一篇很基础的,大佬就不要看了,也不要喷,谢谢

  4. wireshark抓包分析tcp连接与断开

    其实对于网络通信的学习,最好还是能够自己抓到包详细地一下,不然只单单通过文字和图的描述印象不够深刻.本文通过实际的抓包操作来看一下tcp的连接与断开是怎样的. 首先需要去https://www.wir ...

  5. css3动画transition详解

    一.transition-property 语法: transition-property : none | all | [ <IDENT> ] [ ',' <IDENT> ] ...

  6. 【TensorFlow随笔】关于一个矩阵与多个矩阵相乘的问题

    问题描述: Specifically, I want to do matmul(A,B) where  'A' has shape (m,n)  'B' has shape (k,n,p) and t ...

  7. TP框架关于模版的使用技巧

    1.

  8. OpenShift实战(二):OpenShift节点扩容

    1.新增节点信息 增加节点如下,请将xxx改为自己的域名 node6.xxx.net Node 192.168.8.90 8G 20G/60G 4C node7.xxx.net Node 192.16 ...

  9. Spark入门(1-4)安装、运行Spark

    如何安装Spark 安装和使用Spark有几种不同方式.你可以在自己的电脑上将Spark作为一个独立的框架安装或者从诸如Cloudera,HortonWorks或MapR之类的供应商处获取一个Spar ...

  10. Codeforces Round #426 (Div. 2)

    http://codeforces.com/contest/834 A. The Useless Toy 题意: <,>,^,v这4个箭头符号,每一个都可以通过其他及其本身逆时针或者顺时针 ...