P2564 [SCOI2009]生日礼物 贪心
题意
有n个珠子,包括k中颜色,找出长度最小的一个区间,使得这个区间中包含所有的颜色。
思路
把n个珠子按照位子排序,然后维护每个珠子最右边能到的位子就行了。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#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> 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 << 17; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e9 + 7; const double esp = 1e-8; 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 = 0; int f = 0; char ch = getchar(); while (ch < '0' || ch > '9') f |= (ch == '-'), ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', 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 + 9; pll a[maxn]; ll d[100]; int main() { int n, k, tot = 0; scanf("%d%d", &n, &k); for (int i = 1; i <= k; i++) { int num; scanf("%d", &num); for (int j = 1; j <= num; j++) { int x; scanf("%d", &x); a[++tot].fi = x; a[tot].se = i; } } sort(a + 1, a + 1 + tot); ll ans = 1ll << 31; for (int i = 1; i <= tot; i++) { d[a[i].se] = a[i].fi; ll mx = 0, flag = 1, mn = 1ll << 31; for (int j = 1; j <= k; j++) { mx = max(mx, d[j]); mn = min(mn, d[j]); if (d[j] == 0) flag = 0; } if (flag) ans = min(ans, mx - mn); } printf("%lld\n", ans); return 0; }
skr
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步