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

#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;
}
else l = mid + 1;
}
cout<<ans<<endl;
return 0;
}

luogu2564 [SCOI2009]生日礼物的更多相关文章

  1. 1293: [SCOI2009]生日礼物

    1293: [SCOI2009]生日礼物 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1096  Solved: 584[Submit][Statu ...

  2. BZOJ 1293: [SCOI2009]生日礼物【单调队列】

    1293: [SCOI2009]生日礼物 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2534  Solved: 1383[Submit][Stat ...

  3. bzoj1293[SCOI2009]生日礼物 尺取法

    1293: [SCOI2009]生日礼物 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2838  Solved: 1547[Submit][Stat ...

  4. P2564 [SCOI2009]生日礼物(尺取法)

    P2564 [SCOI2009]生日礼物 三个字.尺取法......... 坐标按x轴排序. 蓝后尺取一下.......... #include<iostream> #include< ...

  5. 【BZOJ1293】[SCOI2009]生日礼物(单调队列)

    [BZOJ1293][SCOI2009]生日礼物(单调队列) 题面 BZOJ 洛谷 题解 离散之后随便拿单调队列维护一下就好了. #include<iostream> #include&l ...

  6. bzoj1293: [SCOI2009]生日礼物(stl堆)

    1293: [SCOI2009]生日礼物 题目:传送门 题解: 据说这道题乱搞随便就水过了 本蒟蒻想到了一个用堆的水法(还专门学了学queue): 如果把每一种颜色的下一个位置都记录一下的话,一开始就 ...

  7. NC20565 [SCOI2009]生日礼物

    NC20565 [SCOI2009]生日礼物 题目 题目描述 小西有一条很长的彩带,彩带上挂着各式各样的彩珠.已知彩珠有 \(N\) 个,分为 \(K\) 种.简单的说,可以将彩带考虑为 \(x\) ...

  8. 1293: [SCOI2009]生日礼物 - BZOJ

    Description 小西有一条很长的彩带,彩带上挂着各式各样的彩珠.已知彩珠有N个,分为K种.简单的说,可以将彩带考虑为x轴,每一个彩珠有一个对应的坐标(即位置).某些坐标上可以没有彩珠,但多个彩 ...

  9. bzoj1293: [SCOI2009]生日礼物

    单调队列 用一个堆维护目前每个颜色在里面的点,每回取出队首点,并更新答案.一旦哪个颜色的点都被用完,跳出循环. #include<cstdio> #include<algorithm ...

随机推荐

  1. Spring 设计原则

    Spring 框架有四大原则(Spring所有的功能和设计和实现都基于四大原则): 1. 使用POJO进行轻量级和最小侵入式开发. 2. 通过依赖注入和基本接口编程实现松耦合. 3. 通过AOP和基于 ...

  2. .NET CORE IIS 500.21

    最近遇到的.NET CORE 500.21的错误 官方解决方案地址:https://docs.microsoft.com/en-us/dynamics-nav/troubleshooting-http ...

  3. layout_weight属性

    layout_weight 某个控件text多行,第一行对齐,baselineAligned = "false" 可使控件对齐 weight 计算规则 剩余尺寸=父布局尺寸-子布局 ...

  4. LayUI 完美兼容Vue.js

    <div id="app"> <form class="layui-form" action=""> <div ...

  5. COGS 2794. 爱摔跤的比利海灵顿

    ★☆   输入文件:find_k.in   输出文件:find_k.out   简单对比时间限制:1.4 s   内存限制:128 MB [题目描述] B•海灵顿•雷想要和n个巨人比试摔♂跤,他想先和 ...

  6. 洛谷 P1057 传球游戏

    题目描述 上体育课的时候,小蛮的老师经常带着同学们一起做游戏.这次,老师带着同学们一起做传球游戏. 游戏规则是这样的:n个同学站成一个圆圈,其中的一个同学手里拿着一个球,当老师吹哨子时开始传球,每个同 ...

  7. 如何在SAP云平台的Cloud Foundry环境下添加新的Service(服务)

    我想在SAP云平台的Cloud Foundry环境下使用MongoDB的服务,但是我在Service Marketplace上找不到这个服务. cf marketplace返回的结果也没有. 解决方案 ...

  8. Adobe登陆出现Access denied解决方法

    当我安装好Adobe Acrobat Reader DC时,想要登陆Adobe账号时被Access denied. 一般说需要梯子,然而本人亲测这种方法不行(香港主机,全局模式下). 一个简单有效的方 ...

  9. 2018.4.27 Java的Swing常用事件

    Swing常用事件 1.Java Swing中处理事件的一般步骤是: (1)新建一个组件(JButton) (2)将该组件添加到面板中(JPanel) (3)注册监听器以监听事件源产生的事件(如Act ...

  10. opencv将rgb图像转换成灰度图

    python写法: import cv2 img = cv2.imread(img_dir, cv2.IMREAD_GRAYSCALE) cv2.imwrite(dis_dir, img) imrea ...