P2564 [SCOI2009]生日礼物 贪心
题意
有n个珠子,包括k中颜色,找出长度最小的一个区间,使得这个区间中包含所有的颜色。
思路
把n个珠子按照位子排序,然后维护每个珠子最右边能到的位子就行了。
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> /* ⊂_ヽ
\\ Λ_Λ 来了老弟
\('ㅅ')
> ⌒ヽ
/ へ\
/ / \\
レ ノ ヽ_つ
/ /
/ /|
( (ヽ
| |、\
| 丿 \ ⌒)
| | ) /
'ノ ) Lノ */ using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define boost ios::sync_with_stdio(false);cin.tie(0)
#define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<;
const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e9+;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} inline void cmax(int &x,int y){if(x<y)x=y;}
inline void cmax(ll &x,ll y){if(x<y)x=y;}
inline void cmin(int &x,int y){if(x>y)x=y;}
inline void cmin(ll &x,ll y){if(x>y)x=y;} /*-----------------------showtime----------------------*/ const int maxn = 1e6+;
pll a[maxn];
ll d[];
int main(){
int n,k,tot=;
scanf("%d%d", &n, &k);
for(int i=; i<=k; i++){
int num; scanf("%d", &num);
for(int j=; j<=num; j++) {
int x; scanf("%d", &x);
a[++tot].fi = x;
a[tot].se = i;
}
}
sort(a +, a++tot);
ll ans = 1ll<<;
for(int i=; i<=tot; i++){
d[a[i].se] = a[i].fi;
ll mx = , flag = , mn = 1ll<<; for(int j=; j<=k; j++){
mx = max(mx, d[j]);
mn = min(mn, d[j]);
if(d[j] == ) flag = ;
}
if(flag) ans = min(ans, mx - mn);
}
printf("%lld\n", ans);
return ;
}
P2564 [SCOI2009]生日礼物 贪心的更多相关文章
- P2564 [SCOI2009]生日礼物(尺取法)
P2564 [SCOI2009]生日礼物 三个字.尺取法......... 坐标按x轴排序. 蓝后尺取一下.......... #include<iostream> #include< ...
- P2564 [SCOI2009]生日礼物
题目背景 四川2009NOI省选 题目描述 小西有一条很长的彩带,彩带上挂着各式各样的彩珠.已知彩珠有N个,分为K种.简单的说,可以将彩带考虑为x轴,每一个彩珠有一个对应的坐标(即位置).某些坐标上可 ...
- 洛谷P2564 [SCOI2009]生日礼物
题目背景 四川2009NOI省选 题目描述 小西有一条很长的彩带,彩带上挂着各式各样的彩珠.已知彩珠有N个,分为K种.简单的说,可以将彩带考虑为x轴,每一个彩珠有一个对应的坐标(即位置).某些坐标上可 ...
- 洛谷P2564 [SCOI2009]生日礼物(单调队列)
传送门 准确的来说这个应该叫尺取法? 先对所有的点按$x$坐标进行排序 我们维护两个指针$l,r$,每一次令$r$不断右移直到所有颜色齐全,再不断右移$l$直到颜色数不足,那么此时$[l-1,r]$这 ...
- LUOGU P2564 [SCOI2009]生日礼物 (队列+模拟)
传送门 解题思路 还是比较好想的,用一个队列,然后把所有点放在一起排个序,依次入队.每次检查队头元素的种类是否为当前入队元素种类,是的话就一直\(pop\),每次更新答案即可. 代码 #include ...
- 1293: [SCOI2009]生日礼物
1293: [SCOI2009]生日礼物 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1096 Solved: 584[Submit][Statu ...
- BZOJ 1293: [SCOI2009]生日礼物【单调队列】
1293: [SCOI2009]生日礼物 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2534 Solved: 1383[Submit][Stat ...
- bzoj1293[SCOI2009]生日礼物 尺取法
1293: [SCOI2009]生日礼物 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2838 Solved: 1547[Submit][Stat ...
- 【BZOJ1293】[SCOI2009]生日礼物(单调队列)
[BZOJ1293][SCOI2009]生日礼物(单调队列) 题面 BZOJ 洛谷 题解 离散之后随便拿单调队列维护一下就好了. #include<iostream> #include&l ...
随机推荐
- 【iOS】获取视图的中心和宽高
示例代码: NSLog(@"%f, %f", self.view.center.x, self.view.center.y); NSLog(@"%f, %f", ...
- 简易数据分析 08 | Web Scraper 翻页——点击「更多按钮」翻页
这是简易数据分析系列的第 8 篇文章. 我们在Web Scraper 翻页--控制链接批量抓取数据一文中,介绍了控制网页链接批量抓取数据的办法. 但是你在预览一些网站时,会发现随着网页的下拉,你需要点 ...
- Linux常用的命令及使用方法
1.请用命令查出ifconfig命令程序的绝对路径 [root@localhost ~]# which ifconfig(ifconfig是linux中用于显示或配置网络设备(网络接口卡)的命令) / ...
- Python 之父再发文:构建一个 PEG 解析器
花下猫语: Python 之父在 Medium 上开了博客,现在写了两篇文章,本文是第二篇的译文.前一篇的译文 在此 ,宣布了将要用 PEG 解析器来替换当前的 pgen 解析器. 本文主要介绍了构建 ...
- redis缓存介绍以及常见问题浅析
# 没缓存的日子: 对于web来说,是用户量和访问量支持项目技术的更迭和前进.随着服务用户提升.可能会出现一下的一些状况: 页面并发量和访问量并不多,mysql足以支撑自己逻辑业务的发展.那么其实可以 ...
- AQS之CountDownLatch、Semaphore、CyclicBarrier
CountDownLatch A synchronization aid that allows one or more threads to wait until a set of operatio ...
- 并发编程(3)——ThreadPoolExecutor
ThreadPoolExecutor 1. ctl(control state) 线程池控制状态,包含两个概念字段:workerCount(线程有效数量)和runState(表示是否在运行.关闭等状态 ...
- 单纯的xlistview
public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener{ private ...
- 前端插件之Datatables使用--下篇
工欲善其事,必先利其器 本系列文章介绍我在运维系统开发过程中用到的那些顺手的前端插件,上一篇文章介绍了Datatables插件的基本使用,这一篇文章作为上一篇的延续,会介绍Databases的一些高级 ...
- Jmeter 01 Jmeter下载安装及入门
jmeter简介 Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测试,但后来扩展到其他测试领域.--百度百科 下载 下载 ...