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

#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. Android笔记--View绘制流程源码分析(一)

    Android笔记--View绘制流程源码分析 View绘制之前框架流程分析 View绘制的分析始终是离不开Activity及其内部的Window的.在Activity的源码启动流程中,一并包含 着A ...

  2. Arria II GX FPGA开法套件——初步使用

    1. 从官网下载使用手册和参考手册,以及开发包           下载地址:https://www.altera.com.cn/products/boards_and_kits/dev-kits/a ...

  3. ActiveX、OLE和COM/DCOM

    ActiveX:开放的集成平台 为开发人员. 用户和 Web生产商提供了一个快速而简便的在 Internet 和 Intranet 创建程序集成和内容的方法(就是提供了一个方法). 使用 Active ...

  4. python基础教程总结9——模块,包,标准库

    1. 模块 在python中一个文件可以被看成一个独立模块,而包对应着文件夹,模块把python代码分成一些有组织的代码段,通过导入的方式实现代码重用. 1.1 模块搜索路径 导入模块时,是按照sys ...

  5. 使用ABAP代码返回S/4HANA Material上维护的Attachment明细

    在事务码 MM02里为ID为16的material维护附件: 如何使用ABAP代码获得如下附件的名称和文件内容? REPORT zgos_api. DATA ls_appl_object     TY ...

  6. JDBC对数据库的简单操作

    /** * 获取数据库连接 */ public Connection GetConnection(){ Connection connection=null; try { Class.forName( ...

  7. 10.1 plan

    1951    [Sdoi2010]古代猪文  Sdoi2010 Contest2   807 1928 1566    [NOI2009]管道取珠       806 1429 2756    [S ...

  8. 通用的flash代码

    黑体字部分为常修改的部分 <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase=&quo ...

  9. JavaScript实用的例子

    ---恢复内容开始--- 1.发送验证码 <input id="send" type="button" value="发送验证码"&g ...

  10. PAT (Basic Level) Practise (中文)- 1009. 说反话 (20)

    http://www.patest.cn/contests/pat-b-practise/1009 给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式:测试输入包含一个测试用例,在 ...