UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#212078#3816. 元素yangtianming001401427ms1428kbC++111.2kb2024-10-13 10:59:552024-10-13 12:20:30

answer

#include <iostream>
#include <vector>
#include <unordered_map>
#include <set>
using namespace std;

int main() {
	int n, q;
	cin >> n >> q;
	vector<int> a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	vector<pair<int, int>> q1(q);
	for (int i = 0; i < q; i++) {
		int l, r;
		cin >> l >> r;
		q1[i] = {l - 1, r - 1};  
	}
	
	for (auto query : q1) {
		int l = query.first, r = query.second;
		unordered_map<int, int> count;
		set<int> distinct_elements;     
		for (int i = l; i <= r; i++) {
			distinct_elements.insert(a[i]);
			count[a[i]]++;
		}
		
		int required = distinct_elements.size();  
		unordered_map<int, int> cnt;    
		int have = 0;                      
		int min_length = r - l + 1;           
		int left = l;
		for (int right = l; right <= r; right++) {
			int elem = a[right];
			cnt[elem]++;
			if (cnt[elem] == count[elem]) {
				have++;
			}
			while (have == required) {
				min_length = min(min_length, right - left + 1);
				int left_elem = a[left];
				cnt[left_elem]--;
				if (cnt[left_elem] < count[left_elem]) {
					have--;
				}
				left++;
			}
		}
		cout << min_length << endl;
	}
	return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 0ms
memory: 1248kb

input:

10 10
492 1887 1028 1209 252 1860 1527 699 1656 928
6 9
2 8
3 10
2 3
1 8
6 9
3 5
4 7
1 4
2 7

output:

4
7
8
2
8
4
3
4
4
6

result:

ok 10 numbers

Test #2:

score: 10
Accepted
time: 0ms
memory: 1260kb

input:

50 50
413 1208 1780 279 1403 1706 11 1583 333 355 1068 1270 1835 1042 957 423 1140 1153 1709 284 281...

output:

10
15
34
19
3
24
16
26
47
15
25
10
8
11
6
6
24
2
31
5
14
5
1
11
10
14
21
13
26
42
1
2
15
26
19
19
24...

result:

ok 50 numbers

Test #3:

score: 0
Wrong Answer
time: 0ms
memory: 1260kb

input:

50 50
407 1089 1459 1626 1917 555 1611 492 443 1149 406 1259 1729 1554 577 1310 1899 1578 1005 1459 ...

output:

32
10
3
14
1
26
1
37
16
49
27
29
14
21
5
18
2
2
25
1
12
15
23
2
36
6
21
28
7
7
5
9
29
4
32
27
33
21
...

result:

wrong answer 8th numbers differ - expected: '36', found: '37'

Test #4:

score: 0
Wrong Answer
time: 0ms
memory: 1256kb

input:

50 50
1792 843 1272 547 698 1086 1174 1607 1277 1055 506 881 1445 372 1076 1770 647 1086 320 1912 18...

output:

34
19
38
21
5
9
19
41
9
12
6
35
38
19
10
23
27
48
9
25
4
27
1
19
48
5
16
16
5
44
1
8
50
14
32
13
26
...

result:

wrong answer 17th numbers differ - expected: '26', found: '27'

Test #5:

score: 10
Accepted
time: 0ms
memory: 1260kb

input:

50 50
44 1463 245 1177 1728 1335 768 475 790 623 1466 98 672 230 339 1428 872 1905 405 1260 1503 813...

output:

19
25
1
20
10
13
46
41
31
1
24
24
6
11
4
3
19
41
7
20
36
17
19
2
2
3
4
33
19
12
29
23
18
9
28
21
22
...

result:

ok 50 numbers

Test #6:

score: 10
Accepted
time: 0ms
memory: 1260kb

input:

50 50
695 974 1078 231 500 498 1308 1666 1925 766 1994 1776 1955 1766 1191 1021 1581 1202 615 1230 1...

output:

11
3
15
43
24
10
46
5
12
28
30
5
36
17
22
14
14
8
35
4
18
43
2
7
28
5
24
11
12
14
28
9
8
16
40
12
33...

result:

ok 50 numbers

Test #7:

score: 0
Wrong Answer
time: 8ms
memory: 1276kb

input:

200 200
403 329 389 1103 695 1865 818 671 750 1868 1422 422 1186 1431 172 1569 460 1580 406 1049 138...

output:

131
104
20
9
45
60
159
47
137
37
81
80
35
134
79
13
141
33
15
107
110
28
79
159
38
64
52
6
59
168
49...

result:

wrong answer 15th numbers differ - expected: '78', found: '79'

Test #8:

score: 0
Wrong Answer
time: 6ms
memory: 1280kb

input:

200 200
147 1386 1788 1493 1919 1136 861 871 1895 1006 1776 346 381 1666 296 289 568 1616 1535 531 2...

output:

103
96
133
64
2
2
59
154
67
102
11
67
2
78
2
108
130
8
32
22
10
123
133
30
29
57
158
43
192
6
56
12
...

result:

wrong answer 3rd numbers differ - expected: '132', found: '133'

Test #9:

score: 0
Wrong Answer
time: 766ms
memory: 1428kb

input:

2000 2000
1154 852 1088 1530 639 1318 1250 1222 1341 1070 205 656 903 1491 808 317 756 1962 102 1445...

output:

830
411
1265
84
779
1561
1619
1734
1045
864
1045
560
756
130
180
1649
484
616
227
111
383
1186
869
6...

result:

wrong answer 2nd numbers differ - expected: '410', found: '411'

Test #10:

score: 0
Wrong Answer
time: 647ms
memory: 1420kb

input:

2000 2000
1232 731 1651 991 1681 1739 776 240 829 1337 493 248 214 1852 716 325 1280 1207 380 724 11...

output:

1067
1428
1526
1700
639
385
909
1515
1197
182
1044
324
276
200
959
993
1061
76
995
248
558
141
208
1...

result:

wrong answer 1st numbers differ - expected: '1062', found: '1067'