结队编程(95%用例)D卷(Java&&Python&&C++&&&&C语言)

时间:2025-01-14 11:35:37
  • #include <>
  • #include <>
  • // Function to calculate the result
  • int calc(int arr[], int size) {
  • int ret = 0;
  • for (int i = 0; i < size; ++i) {
  • int left = 0;
  • int right = 0;
  • // Calculate the number of elements less than arr[i] to the left
  • for (int j = 0; j < i; ++j) {
  • if (arr[j] < arr[i]) {
  • ++left;
  • }
  • }
  • // Calculate the number of elements greater than arr[i] to the right
  • for (int j = i + 1; j < size; ++j) {
  • if (arr[j] > arr[i]) {
  • ++right;
  • }
  • }
  • ret += left * right;
  • }
  • return ret;
  • }
  • int main() {
  • int n, size;
  • int *level;
  • // Read first line of input
  • if (scanf("%d", &n) == 1) {
  • // Read the rest of the input based on n
  • level = (int *)malloc(n * sizeof(int));
  • for (int i = 0; i < n; ++i) {
  • scanf("%d", &level[i]);
  • }
  • size = n;
  • } else {
  • printf("Invalid input\n");
  • return 1;
  • }
  • // Calculate result
  • int result = calc(level, size);
  • // Reverse the array
  • int *reverse = (int *)malloc(size * sizeof(int));
  • for (int i = 0; i < size; ++i) {
  • reverse[i] = level[size - 1 - i];
  • }
  • result += calc(reverse, size);
  • printf("%d\n", result);
  • // Free allocated memory
  • free(level);
  • free(reverse);
  • return 0;
  • }