답안 #666917

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
666917 2022-11-29T22:05:17 Z kirakaminski968 Circus (Balkan15_CIRCUS) C++17
100 / 100
700 ms 25476 KB
#include "circus.h"
#pragma GCC optimize("Ofast")
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <bitset>
#include <set>
#include <queue>
#include <stack>
#include <assert.h>
#include <cmath>
#include <iomanip>
#include <random>
#include <unordered_map>
#include <chrono>
using namespace std;
template<class T> void pary(T l, T r) {
	while (l != r) cout << *l << " ", l++;
	cout << endl;
};
#define ll long long
#define ld long double
#define maxn 100005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
const int inf = 1<<30;
struct BIT{
	int bit[maxn];
	void init() {
		for (int i = 0;i < maxn;i++) bit[i] = inf;
	}
	void modify(int ind, int val) {
		ind++;
		for (;ind < maxn;ind += ind & (-ind)) bit[ind] = min(bit[ind], val);
	}
	int query(int ind) {
		ind++;
		int ret = inf;
		for (;ind > 0;ind -= ind & (-ind)) ret = min(ret, bit[ind]);
		return ret;
	}
} pref, suf;
int N, M;
int d[maxn], P[maxn];
priority_queue<pii, vector<pii>, greater<pii> > pq;	
set<pii> se;
vector<pii> front, back;
vector<int> fval, bval;
void init(int n, int m, int p[]){
	sort(p, p + n);
	for (int i = 0;i < n;i++) {
		d[i] = inf, P[i] = p[i];	
		if (i < n - 1) se.insert({p[i], i});
	}
	pref.init(), suf.init();
	suf.modify(maxn - n - 2, m), pref.modify(n, -m);
	pq.push({m - p[n-1], n-1});
	d[n-1] = m - p[n-1];
	while (pq.size()) {
		pii cur = pq.top();pq.pop();
		if (cur.ff != d[cur.ss]) continue;
		if (se.find({p[cur.ss], cur.ss}) != se.end()) {
			se.erase(se.find({p[cur.ss], cur.ss}));	
		}
		auto add = [&] (int x) {
			int val = min(suf.query(maxn - 2 - x) - p[x], pref.query(x) + p[x]);
			if (val < d[x]) {
				d[x] = val;
				pq.push({d[x], x});
			}
		};
		auto ind = se.upper_bound({p[cur.ss] - d[cur.ss], inf});
		if (ind != se.begin()) {
			ind = prev(ind);
			int id = ind->ss;
			suf.modify(maxn - 2 - id, p[cur.ss]);
			add(id);	
		}
		ind = se.lower_bound({p[cur.ss] + d[cur.ss], 0});
		if (ind != se.end()) {
			int id = ind->ss;
			pref.modify(id, -p[cur.ss]);
			add(id);
		}
		ind = se.lower_bound({p[cur.ss], 0});
		if (ind != se.end()) add(ind->ss);
		ind = se.upper_bound({p[cur.ss], inf});
		if (ind != se.begin()) add((prev(ind))->ss);
	}
	for (int i = 0;i < n;i++) {
		back.push_back({p[i] - d[i], p[i]});	
		front.push_back({p[i] + d[i], p[i]});
	}
	sort(back.begin(), back.end()), sort(front.begin(), front.end());
	fval.resize(n), bval.resize(n);
	int cur = inf;
	for (int i = n - 1;i >= 0;i--) {
		cur = min(cur, back[i].ss);
		bval[i] = cur;
	}
	N = n, M = m;
	cur = 0;
	for (int i = 0;i < n;i++) {
		cur = max(cur, front[i].ss);
		fval[i] = cur;
	}
}
int minLength(int x) {
	int ind = lower_bound(back.begin(), back.end(), make_pair(x, -1)) - back.begin();
	int ans = M - x;
	if (ind < front.size()) ans = min(ans, bval[ind] - x);	
	ind = upper_bound(front.begin(), front.end(), make_pair(x, inf)) - front.begin() - 1;
	if (ind >= 0) ans = min(ans, x - fval[ind]);
	return ans;
}

Compilation message

circus.cpp: In function 'int minLength(int)':
circus.cpp:114:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  114 |  if (ind < front.size()) ans = min(ans, bval[ind] - x);
      |      ~~~~^~~~~~~~~~~~~~
grader.cpp: In function 'int main()':
grader.cpp:14:12: warning: unused variable 'max_code' [-Wunused-variable]
   14 |  long long max_code;
      |            ^~~~~~~~
grader.cpp:16:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |  scanf("%d%d", &N, &M);
      |  ~~~~~^~~~~~~~~~~~~~~~
grader.cpp:18:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |   scanf("%d", &P[i]);
      |   ~~~~~^~~~~~~~~~~~~
grader.cpp:21:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |  scanf("%d", &Q);
      |  ~~~~~^~~~~~~~~~
grader.cpp:23:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |   scanf("%d", &d);
      |   ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 138 ms 7628 KB Output is correct
2 Correct 140 ms 7856 KB Output is correct
3 Correct 142 ms 7756 KB Output is correct
4 Correct 138 ms 7612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1108 KB Output is correct
2 Correct 1 ms 1108 KB Output is correct
3 Correct 1 ms 1080 KB Output is correct
4 Correct 1 ms 1108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1108 KB Output is correct
2 Correct 1 ms 1108 KB Output is correct
3 Correct 1 ms 1080 KB Output is correct
4 Correct 1 ms 1108 KB Output is correct
5 Correct 3 ms 1236 KB Output is correct
6 Correct 4 ms 1108 KB Output is correct
7 Correct 4 ms 1236 KB Output is correct
8 Correct 4 ms 1220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1108 KB Output is correct
2 Correct 1 ms 1108 KB Output is correct
3 Correct 1 ms 1080 KB Output is correct
4 Correct 1 ms 1108 KB Output is correct
5 Correct 3 ms 1236 KB Output is correct
6 Correct 4 ms 1108 KB Output is correct
7 Correct 4 ms 1236 KB Output is correct
8 Correct 4 ms 1220 KB Output is correct
9 Correct 349 ms 18516 KB Output is correct
10 Correct 318 ms 11456 KB Output is correct
11 Correct 332 ms 10728 KB Output is correct
12 Correct 353 ms 19556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 138 ms 7628 KB Output is correct
2 Correct 140 ms 7856 KB Output is correct
3 Correct 142 ms 7756 KB Output is correct
4 Correct 138 ms 7612 KB Output is correct
5 Correct 1 ms 1108 KB Output is correct
6 Correct 1 ms 1108 KB Output is correct
7 Correct 1 ms 1080 KB Output is correct
8 Correct 1 ms 1108 KB Output is correct
9 Correct 3 ms 1236 KB Output is correct
10 Correct 4 ms 1108 KB Output is correct
11 Correct 4 ms 1236 KB Output is correct
12 Correct 4 ms 1220 KB Output is correct
13 Correct 145 ms 7904 KB Output is correct
14 Correct 141 ms 7800 KB Output is correct
15 Correct 142 ms 7520 KB Output is correct
16 Correct 142 ms 7612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 138 ms 7628 KB Output is correct
2 Correct 140 ms 7856 KB Output is correct
3 Correct 142 ms 7756 KB Output is correct
4 Correct 138 ms 7612 KB Output is correct
5 Correct 1 ms 1108 KB Output is correct
6 Correct 1 ms 1108 KB Output is correct
7 Correct 1 ms 1080 KB Output is correct
8 Correct 1 ms 1108 KB Output is correct
9 Correct 3 ms 1236 KB Output is correct
10 Correct 4 ms 1108 KB Output is correct
11 Correct 4 ms 1236 KB Output is correct
12 Correct 4 ms 1220 KB Output is correct
13 Correct 349 ms 18516 KB Output is correct
14 Correct 318 ms 11456 KB Output is correct
15 Correct 332 ms 10728 KB Output is correct
16 Correct 353 ms 19556 KB Output is correct
17 Correct 145 ms 7904 KB Output is correct
18 Correct 141 ms 7800 KB Output is correct
19 Correct 142 ms 7520 KB Output is correct
20 Correct 142 ms 7612 KB Output is correct
21 Correct 700 ms 19716 KB Output is correct
22 Correct 656 ms 19864 KB Output is correct
23 Correct 666 ms 23136 KB Output is correct
24 Correct 671 ms 25476 KB Output is correct