제출 #1111596

#제출 시각아이디문제언어결과실행 시간메모리
1111596YSH2020Fire (BOI24_fire)C++17
61 / 100
451 ms6704 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
	int n, m; cin >> n >> m;
	vector<pair<int, int>> shifts;
	int max_dist = 0;
	int start_x = 0;
	for (int i = 0; i < n; i++) {
		int x, y; cin >> x >> y;
		if (x > y) y += m;
		if (y-x > max_dist) {max_dist = y-x;start_x = x;}
		shifts.push_back({x, y});
	}
	for (int i=0; i < n; i++) {
		shifts[i] = {(shifts[i].first-start_x+2*m)%m, (shifts[i].second-start_x+2*m)%m};
		if (shifts[i].first > shifts[i].second) shifts[i]={shifts[i].first, shifts[i].second+m};
	}
	sort(shifts.begin(), shifts.end());
	if (n > 5000) {
			int time = 0;
	int furthest_time = 0;
	int c = 0;
	for (int i=0; i < n; i++) {
		if (shifts[i].first <= time) furthest_time = max(furthest_time, shifts[i].second);
		else {
			c++;
			time = furthest_time;
			if (time >= m) {cout << c; return 0;}
			if (shifts[i].first > time) {cout << -1; return 0;}
			furthest_time = shifts[i].second;
		}
	}
	c++;
	time = furthest_time;
	if (time >= m) {cout << c; return 0;}
	else cout << -1;
	}
	else {
		//brute force
			int ans = 10000000;

		for (int t = 0; t < n; t++) {
			int tmp = shifts[1].first;
			for (int i=0; i < n; i++) {
		shifts[i] = {(shifts[i].first-tmp+2*m)%m, (shifts[i].second-tmp+2*m)%m};
		if (shifts[i].first > shifts[i].second) shifts[i]={shifts[i].first, shifts[i].second+m};
	}
	sort(shifts.begin(), shifts.end());
int time = 0;
	int furthest_time = 0;
	int c = 0;
	int exit = 0;
	for (int i=0; i < n; i++) {
		if (shifts[i].first <= time) furthest_time = max(furthest_time, shifts[i].second);
		else {
			c++;
			time = furthest_time;
			if (time >= m) {ans = min(ans, c); exit=1; break;}
			if (shifts[i].first > time) {exit = 1; break;}
			furthest_time = shifts[i].second;
		}
	}
	if (exit == 0) {
	c++;
	time = furthest_time;
	if (time >= m) {ans = min(ans, c);}}
	}if (ans == 10000000) cout << -1; else cout << ans;
		} 
	}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...