답안 #893518

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
893518 2023-12-27T06:11:57 Z vjudge1 Road Construction (JOI21_road_construction) C++17
18 / 100
588 ms 31168 KB
#include <bits/stdc++.h>

#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define int long long

using namespace std;

int pow(int a,int b,int m){int ans=1;while(b){if(b&1){ans=(ans*a)%m;}b>>=1;a=(a*a)%m;}return ans;}
int binpow(int a,int b){int ans=1;while(b){if(b&1){ans=(ans*a);}b>>=1;a=(a*a);}return ans;}
	
const int N = 3e5 + 2, inf = 1e9, MAXN = 3e5;

struct pt {
	int x, y, id;
};

int x[N], y[N];

inline bool cmp_x (const pt & a, const pt & b) {
	return a.x < b.x || a.x == b.x && a.y < b.y;
}
 
inline bool cmp_y (const pt & a, const pt & b) {
	return a.y < b.y;
}
 
pt a[MAXN];
int mindist;
int ansa, ansb;
 
inline void upd_ans (const pt & a, const pt & b) {
	int dist = abs(a.x-b.x) + abs(a.y-b.y);
	if (dist < mindist)
		mindist = dist,  ansa = a.id,  ansb = b.id;
}

void rec (int l, int r) {
	if (r - l <= 3) {
		for (int i=l; i<=r; ++i)
			for (int j=i+1; j<=r; ++j)
				upd_ans (a[i], a[j]);
		sort (a+l, a+r+1, &cmp_y);
		return;
	}
 
	int m = (l + r) >> 1;
	int midx = a[m].x;
	rec (l, m),  rec (m+1, r);
	static pt t[MAXN];
	merge (a+l, a+m+1, a+m+1, a+r+1, t, &cmp_y);
	copy (t, t+r-l+1, a+l);
 
	int tsz = 0;
	for (int i=l; i<=r; ++i)
		if (abs (a[i].x - midx) < mindist) {
			for (int j=tsz-1; j>=0 && a[i].y - t[j].y < mindist; --j)
				upd_ans (a[i], t[j]);
			t[tsz++] = a[i];
		}
}

main(){
	iostream::sync_with_stdio(false);	
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, k;
	cin >> n >> k;
	set <int> sty;
	for(int i = 1; i <= n; i++){
		cin >> x[i] >> y[i];
		sty.insert(y[i]);
	}
	if(n <= 1000){
		vector <int> dists;
		for(int i = 1; i <= n; i++){
			for(int j = i + 1; j <= n; j++){
				dists.pb(abs(x[i] - x[j]) + abs(y[i] - y[j]));
			}
		}
		sort(all(dists));
		for(int i = 0; i < k; i++){
			cout << dists[i] <<"\n";
		}
	}else if(sty.size() == 1){
		multiset <pair <int,int> > st;
		sort(x + 1, x + n + 1);
		for(int i = 1; i <= n - 1; i++){
			st.insert({x[i + 1] - x[i], i + 1});
		}
		while(k--){
			cout << st.begin() -> ff << endl;
			pair <int,int> f = *st.begin();
			st.erase(st.begin());
			if(f.ss == n)continue;
			st.insert({f.ff + x[f.ss + 1] - x[f.ss], f.ss + 1});
		}
		
	}else{
		for(int i = 0; i < n; i++){
		a[i].x = x[i + 1];
		a[i].y = y[i + 1];
		a[i].id = i;
	}
		sort (a, a+n, &cmp_x);
		mindist = inf * 10;
		rec (0, n-1);
		cout << mindist << endl;
	}
	
	
}

Compilation message

road_construction.cpp: In function 'bool cmp_x(const pt&, const pt&)':
road_construction.cpp:24:33: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   24 |  return a.x < b.x || a.x == b.x && a.y < b.y;
      |                      ~~~~~~~~~~~^~~~~~~~~~~~
road_construction.cpp: At global scope:
road_construction.cpp:66:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   66 | main(){
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 11192 KB Output is correct
2 Correct 52 ms 11104 KB Output is correct
3 Correct 32 ms 9156 KB Output is correct
4 Correct 32 ms 9156 KB Output is correct
5 Correct 48 ms 10184 KB Output is correct
6 Correct 17 ms 10188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 588 ms 23508 KB Output is correct
2 Correct 544 ms 23380 KB Output is correct
3 Correct 37 ms 9160 KB Output is correct
4 Correct 411 ms 23252 KB Output is correct
5 Correct 420 ms 23296 KB Output is correct
6 Correct 418 ms 23380 KB Output is correct
7 Correct 417 ms 22508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 181 ms 30808 KB Output is correct
2 Correct 183 ms 31168 KB Output is correct
3 Correct 1 ms 4440 KB Output is correct
4 Correct 85 ms 22224 KB Output is correct
5 Correct 94 ms 19092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 181 ms 30808 KB Output is correct
2 Correct 183 ms 31168 KB Output is correct
3 Correct 1 ms 4440 KB Output is correct
4 Correct 85 ms 22224 KB Output is correct
5 Correct 94 ms 19092 KB Output is correct
6 Incorrect 174 ms 30692 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 11192 KB Output is correct
2 Correct 52 ms 11104 KB Output is correct
3 Correct 32 ms 9156 KB Output is correct
4 Correct 32 ms 9156 KB Output is correct
5 Correct 48 ms 10184 KB Output is correct
6 Correct 17 ms 10188 KB Output is correct
7 Incorrect 68 ms 17604 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 11192 KB Output is correct
2 Correct 52 ms 11104 KB Output is correct
3 Correct 32 ms 9156 KB Output is correct
4 Correct 32 ms 9156 KB Output is correct
5 Correct 48 ms 10184 KB Output is correct
6 Correct 17 ms 10188 KB Output is correct
7 Correct 588 ms 23508 KB Output is correct
8 Correct 544 ms 23380 KB Output is correct
9 Correct 37 ms 9160 KB Output is correct
10 Correct 411 ms 23252 KB Output is correct
11 Correct 420 ms 23296 KB Output is correct
12 Correct 418 ms 23380 KB Output is correct
13 Correct 417 ms 22508 KB Output is correct
14 Correct 181 ms 30808 KB Output is correct
15 Correct 183 ms 31168 KB Output is correct
16 Correct 1 ms 4440 KB Output is correct
17 Correct 85 ms 22224 KB Output is correct
18 Correct 94 ms 19092 KB Output is correct
19 Incorrect 174 ms 30692 KB Output isn't correct
20 Halted 0 ms 0 KB -