12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- #include <stdio.h>
- #include <stdlib.h> // Include stdlib.h for qsort function
- typedef struct {
- int value;
- int originalIndex;
- } Element;
- // Compare function for sorting elements in descending order
- int compare(const void *a, const void *b) {
- Element *elem1 = (Element *)a;
- Element *elem2 = (Element *)b;
- return elem2->value - elem1->value; // Sort in descending order
- }
- int main() {
- int n; // Number of elements in the array
- scanf("%d", &n);
-
- Element arr[n]; // Array of elements with original indices
-
- // Read elements and store them with their original indices
- int i;
- for (i = 0; i < n; ++i) {
- scanf("%d", &arr[i].value);
- arr[i].originalIndex = i;
- }
-
- // Sort elements in descending order based on value
- qsort(arr, n, sizeof(Element), compare); // Use qsort from stdlib.h
-
- // Array to store the ranks
- int ranks[n];
-
- // Assign ranks
- ranks[arr[0].originalIndex] = 1; // Start with rank 1 for the largest element
- for (i = 1; i < n; ++i) {
- if (arr[i].value == arr[i - 1].value) {
- ranks[arr[i].originalIndex] = ranks[arr[i - 1].originalIndex];
- } else {
- ranks[arr[i].originalIndex] = i + 1; // Assign next rank
- }
- }
-
- // Output the ranks in the original order
- for (i = 0; i < n; ++i) {
- printf("%d", ranks[i]);
- if (i < n - 1) {
- printf(" ");
- }
- }
- printf("\n");
-
- return 0;
- }
|