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

#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. @PropertySource

    功能 加载指定的属性文件(*.properties)到 Spring 的 Environment 中.可以配合 @Value 和 @ConfigurationProperties 使用. @Prope ...

  2. 1044 拦截导弹 1999年NOIP全国联赛提高组 个人博客:attack.cf

    1044 拦截导弹 1999年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold         题目描述 Description 某国为了防御敌 ...

  3. JavaScript中三种字符串连接方式及其性能比较

    参考地址: https://www.cnblogs.com/programs/p/5554742.html 工作中经常会碰到要把2个或多个字符串连接成一个字符串的问题,在JS中处理这类问题一般有三种方 ...

  4. FZU 2204 7

    题意: n个有标号的球围成一个圈.每个球有两种颜色可以选择黑或白染色.问有多少种方案使得没有出现连续白球7个或连续黑球7个? 思路: 如果出现连续的8,9...个球同色,那么也必定含有7个同色.需要统 ...

  5. HDU 2147 kiki's game kiki的游戏(博弈,找规律)

    题意: 给一个有n*m 个格子的棋盘,将一个硬币放在右上角一格,每次可以往左/下/左下移动一格,碰到不能移动的局面者输. 思路: 找P/N状态.先将(n,1)归为P状态,那么能一步到达此位置的有3个位 ...

  6. lastlog命令

    lastlog——检查某特定用户上次登录的时间 命令所在路径:/usr/bin/lastlog 示例1: # lastlog 列出所有用户,并显示用户最后一次登录的时间等信息 示例2: # lastl ...

  7. ASP.NET WebForm & MongoDB

    ASP.NET WebForm & MongoDB 最近在朋友介绍下,也跟着看AngularJS 买了一本三合一的书,Node.JS+MongoDB+AngularJS http://www. ...

  8. ProtoBuff3 unity_TCP网络发包解包&&消息订阅

    using Google.Protobuf; //using Google.Protobuf.Examples.AddPerson; using Google.Protobuf.WellKnownTy ...

  9. 【UML】构件图Component diagram(实现图)(转)

    http://blog.csdn.net/sds15732622190/article/details/49048887 前言 下面要介绍UML中的构建图,它属于实现图的一种,五种静态图之一. 定义 ...

  10. [学习总结] python语言学习总结 (三)

    函数闭包 定义 延伸了作用域的函数(能访问定义体之外定义的非全局变量 作用 共享变量的时候避免使用了不安全的全局变量 允许将函数与某些数据关联起来,类似于简化版面向对象编程 相同代码每次生成的闭包,其 ...