大厂笔试题
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.1 KiB

  1. /*
  2. n的数组
  3. 1.k个非空子串()
  4. 2.k个子串的顺序
  5. 3.k个子串首尾相连
  6. ()k
  7. T(T < 5)
  8. ::n,k0<k<n<1e5
  9. n个数字a1,a2,,an0|ai|; 1e14
  10. Tue"(不含引号),否则输出“False(不含引号)
  11. 3
  12. 5 3
  13. 8 12 7 -6 5
  14. 4 2
  15. 2 3 -6 4
  16. 5 5
  17. 10 6 8 -5 -10
  18. True
  19. False
  20. True
  21. K则可以
  22. */
  23. #include <bits/stdc++.h>
  24. using namespace std;
  25. int main(){
  26. ios::sync_with_stdio(false);
  27. cin.tie(0);
  28. int T;
  29. cin >> T;
  30. while(T--){
  31. long long n, k;
  32. cin >> n >> k;
  33. vector<long long> arr(n);
  34. for(auto &x : arr)cin >> x;
  35. //Create a vector of pairs(value, original index)
  36. vector<pair<long long, long long>> sorted_arr(n);
  37. for(long long i = 0; i < n; i++){
  38. sorted_arr[i] = {arr[i], i+1};//1-based indexing
  39. }
  40. // Sort the array based on values
  41. sort(sorted_arr.begin(), sorted_arr.end());
  42. // Count the number of blocks where indices are consecutive
  43. long long m = 1;// At least one block
  44. for(long long i = 1; i < n; i++){
  45. if(sorted_arr[i].second != sorted_arr[i-1].second +1){
  46. m++;
  47. }
  48. }
  49. // If number of blocks <=k, it's possible
  50. if(m <= k){
  51. cout <<"True\n";
  52. }
  53. else{
  54. cout<<"False\n";
  55. }
  56. }
  57. }