Submission #1349909

#TimeUsernameProblemLanguageResultExecution timeMemory
1349909adscoding Martian DNA (BOI18_dna)C++20
100 / 100
15 ms2528 KiB
#include <bits/stdc++.h>
#define SZ(x) ((int)x.size())
#define fi first
#define se second
#define FOR(i, a, b) for (int i = a, _b = b; i <= _b; ++i)
#define FORD(i, a, b) for (int i = a, _b = b; i >= _b; --i)
#define FORLL(i, a, b) for (ll i = a, _b = b; i <= _b; ++i)
#define FORDLL(i, a, b) for (ll i = a, _b = b; i >= _b; --i)
#define all(x) x.begin(), x.end()
#define uni(x) sort(all(x)), x.erase(unique(all(x)), x.end())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define dbg(...) debug(#__VA_ARGS__, __VA_ARGS__)

template<typename T>
void __prine_one(const char *&s, const T &x)
{
	while (*s == ' ') ++s;
	const char *p = s;
	int bal = 0;
	while (*s)
	{
		if (*s == '(') ++bal;
		else if (*s == ')') --bal;
		else if (*s == ',' && bal == 0) break;
		++s;
	}
	cerr.write(p, s - p) << " = " << x;
	if (*s == ',')
	{
		cerr << "  ,  ";
		++s;
	}
}

template<typename... Args>
void debug(const char *s, Args... args)
{
	cerr << "[  ";
	int dummy[] = {0, (__prine_one(s, args), 0)...};
	(void)dummy;
	cerr << "  ]\n\n";
}

template<class X>
bool maximize(X &a, const X &b)
{
	if (b > a)
	{
		a = b;
		return true;
	}
	return false;
}

template<class X>
bool minimize(X &a, const X &b)
{
	if (b < a)
	{
		a = b;
		return true;
	}
	return false;
}

// --------------------------------------------------------------------------------------------

constexpr int maxn = 2e5 + 3;
int n, K, R, a[maxn], cnt[maxn];
int rq[maxn];

// --------------------------------------------------------------------------------------------


void readInput()
{
	cin >> n >> K >> R;
	FOR(i, 1, n)
		cin >> a[i];

	FOR(i, 1, R)
	{
		int x, y; cin >> x >> y;
		rq[x] = y;
	}
}


void solve()
{
	int res = 1e9;

	int sl = 0;
	FOR(i, 0, K - 1)
		if (cnt[i] >= rq[i])
			++sl;

	int j = 1;
	FOR(i, 1, n)
	{
		sl -= cnt[a[i]] >= rq[a[i]];
		++cnt[a[i]];
		sl += cnt[a[i]] >= rq[a[i]]; 
		while (j <= i && (sl == K && cnt[a[j]] > rq[a[j]]))
		{
			sl -= cnt[a[j]] >= rq[a[j]];
			--cnt[a[j]];
			sl += cnt[a[j]] >= rq[a[j]];
			++j;
		}

		if (sl == K)
			minimize(res, i - j + 1);
	}

	if (res == int(1e9)) cout << "impossible";
	else cout << res;
}

signed main()
{
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	#define TASK "A"
	if (fopen(TASK".INP", "r"))
	{
		freopen(TASK".INP", "r", stdin);
		freopen(TASK".OUT", "w", stdout);
	}
	readInput();
	solve();
	return 0;
}

Compilation message (stderr)

dna.cpp: In function 'int main()':
dna.cpp:131:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  131 |                 freopen(TASK".INP", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
dna.cpp:132:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  132 |                 freopen(TASK".OUT", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...