【USACO11NOV】牛的阵容Cow Lineup 尺取法+哈希
题目描述
输入
* 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.
样例输入
25 7
26 1
15 1
22 3
20 1
30 1
样例输出
提示
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 尺取法+哈希的更多相关文章
- 洛谷 3029 [USACO11NOV]牛的阵容Cow Lineup
https://www.luogu.org/problem/show?pid=3029 题目描述 Farmer John has hired a professional photographer t ...
- bzoj3048[Usaco2013 Jan]Cow Lineup 尺取法
3048: [Usaco2013 Jan]Cow Lineup Time Limit: 2 Sec Memory Limit: 128 MBSubmit: 225 Solved: 159[Subm ...
- 洛谷P3069 [USACO13JAN]牛的阵容Cow Lineup(尺取法)
思路 考虑比较朴素的解法,枚举每个长度为\(k+1\)的区间,然后统计区间中出现次数最多的颜色.这样的话复杂度为\(O(n*k)\)的,显然不行. 观察到统计每个区间中出现次数最多的颜色中,可以只用看 ...
- [Luogu3069][USACO13JAN]牛的阵容Cow Lineup
题目描述 Farmer John's N cows (1 <= N <= 100,000) are lined up in a row. Each cow is identified by ...
- LuoguP3069 【[USACO13JAN]牛的阵容Cow Lineup
题目链接 看了看其他大佬的文章,为什么要控制右端呢 其实就是一个很简单的模拟队列趴... 难点就在于根据题意我们可以分析得一段合法区间内,不同种类个数不能超过k+2 哦当然,由于种类数范围过大,要对种 ...
- Luogu P3033 [USACO11NOV]牛的障碍Cow Steeplechase(二分图匹配)
P3033 [USACO11NOV]牛的障碍Cow Steeplechase 题意 题目描述 --+------- -----+----- ---+--- | | | | --+-----+--+- ...
- 【题解】P3069 [USACO13JAN]牛的阵容Cow Lineup-C++
题目传送门 思路这道题目可以通过尺取法来完成 (我才不管什么必须用队列)什么是尺取法呢?顾名思义,像尺子一样取一段,借用挑战书上面的话说,尺取法通常是对数组保存一对下标,即所选取的区间的左右端点,然后 ...
- [USACO11NOV]牛的障碍Cow Steeplechase
洛谷传送门 题目描述: 给出N平行于坐标轴的线段,要你选出尽量多的线段使得这些线段两两没有交点(顶点也算),横的与横的,竖的与竖的线段之间保证没有交点,输出最多能选出多少条线段. 因为横的与横的,竖的 ...
- [USACO11NOV]牛的障碍Cow Steeplechase(匈牙利算法)
洛谷传送门 题目描述: 给出N平行于坐标轴的线段,要你选出尽量多的线段使得这些线段两两没有交点(顶点也算),横的与横的,竖的与竖的线段之间保证没有交点,输出最多能选出多少条线段. 因为横的与横的,竖的 ...
随机推荐
- java unicode和字符串间的转换
package ykxw.web.jyf; /** * Created by jyf on 2017/5/16. */ public class unicode { public static voi ...
- NYOJ 炫舞家st
#include <iostream>#include <cstring>#include <algorithm>using namespace std; cons ...
- 01-JavaScript之变量
这个系列的文章主要讲解JavaScript的常见用法,适合于初中级的前端开发人员,也可以对比TypeScript的系列文章来看. 先介绍JavaScript的变量与常见变量的函数,代码如下: //变量 ...
- 彻底搞懂shell的高级I/O重定向
本文目录: 1.1 文件描述符(file description,fd) 1.2 文件描述符的复制 1.3 重定向顺序很重要:">file 2>&1"和&quo ...
- elk调试环境
http://10.110.22.30:9100/
- SpringCloud的服务消费者 (一):(rest+ribbon)访问注册的微服务
采用Ribbon或Feign方式访问注册到EurekaServer中的微服务.1.Ribbon实现了客户端负载均衡,Feign底层调用Ribbon2.注册在EurekaServer中的微服务api,不 ...
- Python 编写登录接口
流程图: 源码:
- windbg分析Kernel32.dll导出表
写在前面的话: 继续上篇,在获得了Kernel32.dll基址的基础上,分析它的导出表结构: 对PE结构不太熟悉的同学,可以参考看雪论坛里的一篇帖子:https://bbs.pediy.com/thr ...
- java stream 原理
java stream 原理 需求 从"Apple" "Bug" "ABC" "Dog"中选出以A开头的名字,然后从中选 ...
- word2vec初探(用python简单实现)
为什么要用这个? 因为看论文和博客的时候很常见,不论是干嘛的,既然这么火,不妨试试. 如何安装 从网上爬数据下来 对数据进行过滤.分词 用word2vec进行近义词查找等操作 完整的工程传到了我的gi ...