Submission #893515

# Submission time Handle Problem Language Result Execution time Memory
893515 2023-12-27T06:09:49 Z vjudge1 Road Construction (JOI21_road_construction) C++17
5 / 100
408 ms 26684 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 = 1e5 + 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(){
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 51 ms 9144 KB Output is correct
2 Correct 51 ms 9144 KB Output is correct
3 Correct 36 ms 7072 KB Output is correct
4 Correct 32 ms 7124 KB Output is correct
5 Correct 49 ms 7860 KB Output is correct
6 Correct 17 ms 7888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 408 ms 20632 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 169 ms 26684 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 169 ms 26684 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 51 ms 9144 KB Output is correct
2 Correct 51 ms 9144 KB Output is correct
3 Correct 36 ms 7072 KB Output is correct
4 Correct 32 ms 7124 KB Output is correct
5 Correct 49 ms 7860 KB Output is correct
6 Correct 17 ms 7888 KB Output is correct
7 Incorrect 63 ms 15444 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 51 ms 9144 KB Output is correct
2 Correct 51 ms 9144 KB Output is correct
3 Correct 36 ms 7072 KB Output is correct
4 Correct 32 ms 7124 KB Output is correct
5 Correct 49 ms 7860 KB Output is correct
6 Correct 17 ms 7888 KB Output is correct
7 Incorrect 408 ms 20632 KB Output isn't correct
8 Halted 0 ms 0 KB -