中文题意
给你长度为的一串数字序列,并且里面每个数
。你要从中间选出最短的非公共子序列。也就是你构造的子序列原序列找不到。
Solution
首先我们来看几个特别的例子,如果一组样例是:
4 3 1 1 2 1 那么显然答案是1,你可以构造一个子序列它的值是3个数中没出现的哪个。
再看一组样例
7 4 1 1 2 3 4 2 1 那么显然,对于前面5个数有着从[1,2,3,4]全部的数,无论你选那一个出来当子序列一定可以找到 但是,后面剩下的一段只有[1,2]说明你可以选择没出现过的两个数。
那么估计也看出答案来了把,直接根据出现次数分组,我们假设首先只需要一个数字就可以得到非公共子序列,如果出现一个的序列,说明我们就要填充一个新的数在最后才可以保证没出现过。
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define all(__vv__) (__vv__).begin(), (__vv__).end()
#define endl "\n"
#define pai pair<int, int>
#define ms(__x__,__val__) memset(__x__, __val__, sizeof(__x__))
#define rep(i, sta, en) for(int i=sta; i<=en; ++i)
#define repp(i, sta, en) for(int i=sta; i>=en; --i)
typedef long long ll; typedef unsigned long long ull; typedef long double ld;
inline ll read() { ll s = 0, w = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') w = -1; for (; isdigit(ch); ch = getchar()) s = (s << 1) + (s << 3) + (ch ^ 48); return s * w; }
inline void print(ll x, int op = 10) { if (!x) { putchar('0'); if (op) putchar(op); return; } char F[40]; ll tmp = x > 0 ? x : -x; if (x < 0)putchar('-'); int cnt = 0; while (tmp > 0) { F[cnt++] = tmp % 10 + '0'; tmp /= 10; } while (cnt > 0)putchar(F[--cnt]); if (op) putchar(op); }
inline ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll qpow(ll a, ll b) { ll ans = 1; while (b) { if (b & 1) ans *= a; b >>= 1; a *= a; } return ans; } ll qpow(ll a, ll b, ll mod) { ll ans = 1; while (b) { if (b & 1)(ans *= a) %= mod; b >>= 1; (a *= a) %= mod; }return ans % mod; }
const int dir[][2] = { {0,1},{1,0},{0,-1},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1} };
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
struct Node {
ll val;
int id;
bool operator < (const Node& opt) const {
return val < opt.val;
}
};
const int N = 1e4 + 7;
int n, m, vis[N];
void solve() {
n = read(), m = read();
int ans = 1, cnt = 0, x;
rep(i, 1, n) {
x = read();
if (!vis[x]) {
++cnt;
vis[x] = 1;
if (cnt == m) {
++ans;
cnt = 0;
ms(vis, 0);
}
}
}
print(ans);
}
int main() {
//int T = read(); while (T--)
solve();
return 0;
} 
京公网安备 11010502036488号