161A - Dress'em in Vests! (source address from) ⇔CF161A)
Problem
tag:
⇔ violence, ⇔ greed, ⇔ dichotomy, ⇔ universal (* 1300)
Meaning:
There are \ (n \) soldiers and \ (m \) vests. Give the body shape of each person \ (a_i \) and the size of each vest \ (b_i \). When \ (a_i-x\leq b_j\leq a_i+y \) is satisfied, it indicates that the \ (J \) vest can be worn by the \ (I \) individual. Make as many soldiers wear vests as possible and output these matching pairs.
Train of thought 1 [dichotomy]:
For the \ (i \) soldier, binary search for the vests to be selected and find the one just put on \ (b_j \). Due to the orderly data, for the next soldier \ (i+1 \), the size of the one just put on must be greater than \ (b_{j+1} \), so binary search for the vests to be selected, ranging from \ (j+1 \) to \ (m \). Time complexity \ (O(n*log_2m) \).
Train of thought 2 [violence]:
The above idea has mentioned the change of search range. Since the data is orderly, it can be traversed.
For the \ (i \) th soldier, search for the vests to be selected in order and find the one just put on \ (b_j \). Due to the orderly data, for the next soldier \ (i+1 \), the size of the one he can just put on must be greater than \ (b_{j+1} \), so continue to search for the vests to be selected in order, ranging from \ (j+1 \) to \ (m \). Time complexity \ (O(n+m) \).
AC code 1 [dichotomy]:
//A WIDA Project #include <bits/stdc++.h> using namespace std; #define LL long long const int MAX=1e6+5; LL n, num, m, x, y, w, ans[MAX], Ans[MAX], start = 1, M[MAX], N[MAX], t; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> x >> y; for(int i = 1; i <= n; i ++) cin >> N[i]; for(int i = 1; i <= m; i ++) cin >> M[i]; for(int i = 1; i <= n; i ++) { t = lower_bound(M + start, M + 1 + m, N[i] - x) - M; if(M[t] <= N[i] + y && M[t] != 0) { num ++; ans[num] = i; Ans[num] = t; start = t + 1; } } cout << num << endl; for(int i = 1; i <= num; i ++) cout << ans[i] << " " << Ans[i] << endl; return 0; }
AC code 2 [violence]:
//A WIDA Project #include <bits/stdc++.h> using namespace std; #define LL long long const int MAX=1e6+5; LL n, num, m, x, y, w, ans[MAX], Ans[MAX], start = 1, M[MAX], N[MAX], t; int main() { cin >> n >> m >> x >> y; for(int i = 1; i <= n; i ++) cin >> N[i]; for(int i = 1; i <= m; i ++) cin >> M[i]; for(int i = 1; i <= n; i ++) { while(start <= m && M[start] < N[i] - x) start ++; if(start <= m && M[start] <= N[i] + y) { num ++; ans[num] = i; Ans[num] = start; start ++; } } cout << num << endl; for(int i = 1; i <= num; i ++) cout << ans[i] << " " << Ans[i] << endl; return 0; }
Number of errors: 7 times [supplementary questions: 3 times]
Reason: the new search range is not updated after the vest is used. The calculation of \ (b_j \) is repeated instead of starting from \ (b_{j+1} \), WA.
Cause: when the vest is not used (i.e. a soldier can't wear all the vests), the new search range is not updated, TLE.
Reason: after the vest is used, it updates the new search range and calculates one more piece, resulting in the search starting from \ (b_j+2 \) instead of \ (b_j{1} \), WA.
Reason: (same as the first time)
Reason: (the same as the second time)
Reason: (essentially the same as the second time, slightly optimized, but not modified to the essence, still TLE)
Reason: the range of dichotomy is wrong. The range of soldiers is taken for dichotomy instead of the range of vest, WA.
Text / WIDA
2021.11.6 written
It was launched in WIDA personal blog for learning and discussion only
Update Journal:
2021.11.6 written