首页 技术 正文
技术 2022年11月10日
0 收藏 759 点赞 3,078 浏览 1499 个字

排序枚举左端点,则右端点必定不降

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
struct Node{
int pos, val;
}nd[1000005];
int n, k, cnt[63], uu, vv=0, rig=0, re=0, ans=0x3f3f3f3f;
void rn(int &x){
x = 0;
char ch=getchar();
while(ch<'0' || ch>'9')ch = getchar();
while(ch>='0' && ch<='9'){
x = x * 10 + ch - '0';
ch = getchar();
}
}
bool cmp(Node x, Node y){
return x.pos<y.pos;
}
int main(){
rn(n); rn(k);
for(int i=1; i<=k; i++){
rn(uu);
for(int j=1; j<=uu; j++){
rn(nd[++vv].pos);
nd[vv].val = i;
}
}
sort(nd+1, nd+1+vv, cmp);
for(int i=1; i<=n; i++){
while(re<k && rig<n){
rig++;
if(++cnt[nd[rig].val]==1)
re++;
}
if(re==k)
ans = min(ans, nd[rig].pos-nd[i].pos);
if(--cnt[nd[i].val]==0)re--;
}
cout<<ans<<endl;
return 0;
}

当然也可以用滑动窗口的思想做,就是比较慢

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
struct Node{
int pos, val;
}nd[1000005];
int n, k, cnt[63], uu, vv=0, lev=1, rig=0, a[1000005];
void rn(int &x){
x = 0;
char ch=getchar();
while(ch<'0' || ch>'9')ch = getchar();
while(ch>='0' && ch<='9'){
x = x * 10 + ch - '0';
ch = getchar();
}
}
bool cmp(Node x, Node y){
return x.pos<y.pos;
}
bool check(int lim){
memset(cnt, 0, sizeof(cnt));
lev = 1, rig = 0;
int re=0;
for(int i=1; i<=n; i++){
while(lev<=rig && nd[a[lev]].pos<nd[i].pos-lim){
if(--cnt[nd[a[lev]].val]==0)re--;
lev++;
}
a[++rig] = i;
if(++cnt[nd[i].val]==1)re++;
if(re==k)return true;
}
return false;
}
int main(){
rn(n); rn(k);
for(int i=1; i<=k; i++){
rn(uu);
for(int j=1; j<=uu; j++){
rn(nd[++vv].pos);
nd[vv].val = i;
}
}
sort(nd+1, nd+1+vv, cmp);
int l=0, r=nd[vv].pos, mid, ans=0;
while(l<=r){
mid = (l + r) >> 1;
if(check(mid)){
ans = mid;
r = mid - 1;
}
elsel = mid + 1;
}
cout<<ans<<endl;
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,994
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,507
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,350
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,135
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,768
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,845