fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b, int mod=M) {
  14. int x = 1;
  15. a %= mod;
  16. while (b) {
  17. if (b & 1) x = (x * a) % mod;
  18. a = (a * a) % mod;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27.  
  28.  
  29.  
  30. vector<int> a;
  31.  
  32. //* Template 1
  33.  
  34. int consistency1(int n, int l, int r){
  35.  
  36. unordered_map<int,int> mp;
  37. for(int i=l; i<=r; i++) mp[i]++;
  38.  
  39. int s = 0, e = 0;
  40. int count = r-l+1;
  41.  
  42. int len = INF;
  43.  
  44. while(e<n){
  45. if(a[e] >= l && a[e] <= r){
  46. mp[a[e]]--;
  47. if(mp[a[e]] == 0) count--;
  48. }
  49.  
  50. if(count > 0){
  51. e++;
  52. }
  53. else{
  54. while(s<=e && count == 0){
  55. len = min(len, e-s+1);
  56. if(a[s] >= l && a[s] <= r){
  57. mp[a[s]]++;
  58. if(mp[a[s]] == 1) count++;
  59. }
  60. s++;
  61. }
  62. e++;
  63. }
  64. }
  65.  
  66. if(len == INF) return -1;
  67. return len;
  68. }
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76. //* Template 2
  77.  
  78. int consistency2(int n, int l, int r){
  79.  
  80. unordered_map<int,int> mp;
  81. for(int i=l; i<=r; i++) mp[i]++;
  82.  
  83. int count = r-l+1;
  84.  
  85. int len = INF;
  86.  
  87. for(int i=0, j=0; i<n; i++){
  88. if(a[i] >= l && a[i] <= r){
  89. mp[a[i]]--;
  90. if(mp[a[i]] == 0) count--;
  91. }
  92.  
  93. while(j<=i && count == 0){
  94. len = min(len, i-j+1);
  95. if(a[j] >= l && a[j] <= r){
  96. mp[a[j]]++;
  97. if(mp[a[j]] == 1) count++;
  98. }
  99. j++;
  100. }
  101. }
  102.  
  103. if(len == INF) return -1;
  104. return len;
  105. }
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127. int practice(int n, int l, int r){
  128.  
  129.  
  130. return 0;
  131. }
  132.  
  133.  
  134.  
  135.  
  136.  
  137. void solve() {
  138.  
  139. int n, l, r;
  140. cin>> n >> l >> r;
  141.  
  142. a.resize(n);
  143. for(int i=0; i<n; i++) cin >> a[i];
  144.  
  145. cout << consistency1(n, l, r) << " " << consistency2(n, l, r) << endl;
  146.  
  147.  
  148. }
  149.  
  150.  
  151.  
  152.  
  153.  
  154. int32_t main() {
  155. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  156.  
  157. int t = 1;
  158. cin >> t;
  159. while (t--) {
  160. solve();
  161. }
  162.  
  163. return 0;
  164. }
Success #stdin #stdout 0.01s 5288KB
stdin
3
8 2 4
2 1 4 3 2 1 1 4
7 99 100
100 1 1 1 1 1 99
4 3 5
1 3 4 7
stdout
3 3
7 7
-1 -1