QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#69045#4583. Concerto de PandemicSanguineChameleonWA 2ms13776kbC++205.3kb2022-12-22 21:40:202022-12-22 21:40:21

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-12-22 21:40:21]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:13776kb
  • [2022-12-22 21:40:20]
  • 提交

answer

// BEGIN BOILERPLATE CODE

#include <bits/stdc++.h>
using namespace std;

#ifdef KAMIRULEZ
	const bool kami_loc = true;
	const long long kami_seed = 69420;
#else
	const bool kami_loc = false;
	const long long kami_seed = chrono::steady_clock::now().time_since_epoch().count();
#endif

const string kami_fi = "kamirulez.inp";
const string kami_fo = "kamirulez.out";
mt19937_64 kami_gen(kami_seed);

long long rand_range(long long rmin, long long rmax) {
	uniform_int_distribution<long long> rdist(rmin, rmax);
	return rdist(kami_gen);
}

long double rand_real(long double rmin, long double rmax) {
	uniform_real_distribution<long double> rdist(rmin, rmax);
	return rdist(kami_gen);
}

void file_io(string fi, string fo) {
	if (fi != "" && (!kami_loc || fi == kami_fi)) {
		freopen(fi.c_str(), "r", stdin);
	}
	if (fo != "" && (!kami_loc || fo == kami_fo)) {
		freopen(fo.c_str(), "w", stdout);
	}
}

void set_up() {
	if (kami_loc) {
		file_io(kami_fi, kami_fo);
	}
	ios_base::sync_with_stdio(0);
	cin.tie(0);
}

void just_do_it();

void just_exec_it() {
	if (kami_loc) {
		auto pstart = chrono::steady_clock::now();
		just_do_it();
		auto pend = chrono::steady_clock::now();
		long long ptime = chrono::duration_cast<chrono::milliseconds>(pend - pstart).count();
		string bar(50, '=');
		cout << '\n' << bar << '\n';
		cout << "Time: " << ptime << " ms" << '\n';
	}
	else {
		just_do_it();
	}
}

int main() {
	set_up();
	just_exec_it();
	return 0;
}

// END BOILERPLATE CODE

// BEGIN MAIN CODE

const int ms = 2e5 + 20;
const int st = 20;
const long long inf = 1e12 + 20;
long long a[ms];
int nxtf[ms];
int prvf[ms];
bool f[ms];
int dr[ms];
int dl[ms];
vector<int> q[ms];
int nxtr[ms];
long long dist[ms][st];
int n, m, k, p;

bool check(long long md) {
	dr[n - 1] = 0;
	long long cd = 0;
	for (int i = 0; i < n; i++) {
		int pv = (i + n - 1) % n;
		if (dr[pv] == 0) {
			dr[i] = 0;
			cd = 0;
		}
		else {
			dr[i] = dr[pv] - 1;
			cd -= a[i];
			cd -= 1;
		}
		int cc = (i + dr[i]) % n;
		while (dr[i] < n - 1) {
			cc = (cc + 1) % n;
			if (cd + 1 + a[cc] <= md) {
				dr[i]++;
				cd = cd + 1 + a[cc];
			}
			else {
				break;
			}
		}
	}
	cd = 0;
	dl[0] = 0;
	for (int i = n - 1; i >= 0; i--) {
		int pv = (i + 1) % n;
		if (dl[pv] == 0) {
			dl[i] = 0;
			cd = 0;
		}
		else {
			dl[i] = dl[pv] - 1;
			cd -= a[i];
			cd -= 1;
		}
		int cc = (i + n - dl[i]) % n;
		while (dl[i] < n - 1) {
			cc = (cc + n - 1) % n;
			if (cd + 1 + a[cc] <= md) {
				dl[i]++;
				cd = cd + 1 + a[cc];
			}
			else {
				break;
			}
		}
	}
	for (int i = 0; i < n; i++) {
		q[i].clear();
		nxtr[i] = -1;
	}
	for (int i = 0; i < n; i++) {
		if (a[i] > 0) {
			continue;
		}
		if (dl[i] + dr[i] >= n - 1) {
			return true;
		}
		dl[i] = (i + n - dl[i]) % n;
		dr[i] = (i + dr[i]) % n;
		q[dl[i]].push_back(i);
	}
	int ff = -1;
	for (int k = 0; k < 2; k++) {
		for (int i = 0; i < n; i++) {
			for (auto x: q[i]) {
				if (ff == -1) {
					ff = x;
					continue;
				}
				int d1 = (dr[ff] + n - dl[ff]) % n - (i + n - dl[ff]) % n;
				int d2 = (dr[x] + n - dl[x]) % n - (i + n - dl[x]) % n;
				if (d1 < d2) {
					ff = x;
				}
			}
			nxtr[i] = ff;
		}
	}
	for (int i = 0; i < n; i++) {
		if (f[i]) {
			dist[i][0] = (dr[nxtr[i]] + n - i) % n + 1;
		}
	}
	for (int k = 1; k < st; k++) {
		for (int i = 0; i < n; i++) {
			if (f[i]) {
				int j = (dist[i][k - 1] + i) % n;
				dist[i][k] = dist[i][k - 1] + 1 + (nxtf[j] + n - j) % n + dist[nxtf[j]][k - 1] - 1;
			}
		}
	}
	for (int i = 0; i < n; i++) {
		if (a[i] > 0) {
			continue;
		}
		int j = prvf[(dl[i] + n - 1) % n];
		int d1 = (dl[i] + n - j) % n - 1;
		int d2 = n - (dr[i] + n - dl[i]) % n - 1;
		if (d1 >= d2) {
			return true;
		}
		int mi = d2 - d1;
		int cc = (dr[i] + 1) % n;
		long long cd = (nxtf[cc] + n - cc) % n;
		if (cd >= mi) {
			return true;
		}
		cc = nxtf[cc];
		int cr = 1;
		for (int j = st - 1; j >= 0; j--) {
			if (cd + dist[cc][j] < mi) {
				cr += (1 << j);
				cd += dist[cc][j];
				cc = (dist[cc][j] - 1 + cc) % n;
				cc = (cc + 1) % n;
				cd += (nxtf[cc] + n - cc) % n;
				cc = nxtf[cc];
			}
		}
		cr += (1 << 0);
		cd += dist[cc][0];
		cc = (dist[cc][0] - 1 + cc) % n;
		cc = (cc + 1) % n;
		cd += (nxtf[cc] + n - cc) % n;
		cc = nxtf[cc];
		if (cd >= mi && cr <= p) {
			return true;
		}
	}
	return false;
}

void just_do_it() {
	cin >> n >> m >> k >> p;
	for (int i = 0; i < m; i++) {
		int u, w;
		cin >> u >> w;
		u--;
		a[u] = w;
	}
	for (int i = 0; i < k; i++) {
		int u;
		cin >> u;
		u--;
		f[u] = true;
	}
	for (int i = 0; i < n; i++) {
		if (f[i]) {
			nxtf[0] = i;
			break;
		}
	}
	for (int i = n - 1; i >= 1; i--) {
		if (f[i]) {
			nxtf[i] = i;
		}
		else {
			nxtf[i] = nxtf[(i + 1) % n];
		}
	}
	for (int i = n - 1; i >= 0; i--) {
		if (f[i]) {
			prvf[n - 1] = n - 1;
			break;
		}
	}
	for (int i = 0; i < n - 1; i++) {
		if (f[i]) {
			prvf[i] = i;
		}
		else {
			prvf[i] = prvf[(i + n - 1) % n];
		}
	}
	long long lt = 0;
	long long rt = inf;
	long long res = -1;
	while (lt <= rt) {
		long long mt = (lt + rt) / 2;
		if (check(mt)) {
			res = mt;
			rt = mt - 1;
		}
		else {
			lt = mt + 1;
		}
	}
	cout << res;
}

// END MAIN CODE

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 13680kb

input:

10 4 3 2
1 2
4 4
6 2
7 5
2 5 8

output:

4

result:

ok single line: '4'

Test #2:

score: 0
Accepted
time: 1ms
memory: 13776kb

input:

8 1 3 5
1 5
4 2 7

output:

0

result:

ok single line: '0'

Test #3:

score: 0
Accepted
time: 2ms
memory: 13684kb

input:

5 2 2 1
1 14
2 14
3 5

output:

1

result:

ok single line: '1'

Test #4:

score: -100
Wrong Answer
time: 2ms
memory: 13728kb

input:

2 1 1 1
1 200000
2

output:

200001

result:

wrong answer 1st lines differ - expected: '0', found: '200001'