1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <algorithm> #include <cstdio> #include <cstring> #define ll long long int n, k; int a[200007]; int ans[200007]; int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); } std::sort(a + 1, a + n + 1); n = std::unique(a + 1, a + n + 1) - a - 1; for (int i = 1, j = 0; i <= n; ++i) { for (; j < i && a[i] - a[j + 1] > k; ++j) ; ans[i] = ans[j] + 1; } printf("%d\n", ans[n]); return 0; } |
0 of 6 Questions completed
Questions:
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
0 of 6 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
1、当输入为“3 1 3 2 1”时,输出结果为2。( )
2、假设输入的n为正整数,输出的答案一定小于等于n,大于等于1。( )
3、将第14行的n = std::unique(a + 1, a + n + 1) – a – 1;删去后,有可能出现与原本代码不同的输出结果。 ( )
4. 假设输入的 a 数组和 k 均为正整数,执行第 18 行代码时,一定满足的条件不包括( )。
当输入的n=100、k=2、a={1,2,…,100}时,输出为( )。
6、假设输入的 a 数组和 k 均为正整数,但 a 数组不一定有序,若误删去第13行的 std::sort(a+1,a+n+1);,程序有可能出现的问题有( )。
