File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ #include < vector>
3+ #include < algorithm>
4+ #include < cstdlib>
5+
6+ using namespace std ;
7+
8+ void solve () {
9+ int n, k;
10+ cin >> n >> k;
11+
12+ vector<long long > a (n);
13+ for (int i = 0 ; i < n; i++) {
14+ cin >> a[i];
15+ }
16+
17+ if (k >= 3 ) {
18+ cout << 0 << ' \n ' ;
19+ return ;
20+ }
21+
22+ sort (a.begin (), a.end ());
23+
24+ long long ans = a[0 ];
25+
26+ for (int i = 1 ; i < n; i++) {
27+ ans = min (ans, a[i] - a[i - 1 ]);
28+ }
29+
30+ if (k == 1 ) {
31+ cout << ans << ' \n ' ;
32+ return ;
33+ }
34+
35+ for (int i = 0 ; i < n; i++) {
36+ for (int j = i + 1 ; j < n; j++) {
37+ long long d = a[j] - a[i];
38+ ans = min (ans, d);
39+
40+ auto it = lower_bound (a.begin (), a.end (), d);
41+
42+ if (it != a.end ()) {
43+ ans = min (ans, abs (*it - d));
44+ }
45+
46+ if (it != a.begin ()) {
47+ --it;
48+ ans = min (ans, abs (*it - d));
49+ }
50+
51+ if (ans == 0 ) {
52+ cout << 0 << ' \n ' ;
53+ return ;
54+ }
55+ }
56+ }
57+
58+ cout << ans << ' \n ' ;
59+ }
60+
61+ int main () {
62+ ios::sync_with_stdio (false );
63+ cin.tie (nullptr );
64+
65+ int t;
66+ cin >> t;
67+
68+ while (t--) {
69+ solve ();
70+ }
71+
72+ return 0 ;
73+ }
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ #include < vector>
3+ #include < algorithm>
4+ using namespace std ;
5+
6+ void solve () {
7+ int N;
8+ cin >> N;
9+
10+ vector<int > a (N);
11+
12+ for (int i = 0 ; i < N; i++) {
13+ cin >> a[i];
14+ }
15+
16+ vector<int > dp (N + 1 , 0 );
17+
18+ for (int i = N - 1 ; i >= 0 ; i--) {
19+ dp[i] = 1 + dp[i + 1 ];
20+
21+ if (i + a[i] < N) {
22+ dp[i] = min (dp[i], dp[i + a[i] + 1 ]);
23+ }
24+ }
25+
26+ cout << dp[0 ] << ' \n ' ;
27+ }
28+
29+ int main () {
30+ ios::sync_with_stdio (false );
31+ cin.tie (nullptr );
32+
33+ int T;
34+ cin >> T;
35+
36+ while (T--) {
37+ solve ();
38+ }
39+
40+ return 0 ;
41+ }
You can’t perform that action at this time.
0 commit comments