Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Read inputs from stdin. Write outputs to stdout.
- #include <stdlib.h>
- #include <stdio.h>
- typedef struct
- {
- int v;
- int i;
- } Couple;
- static int n;
- static int *v;
- static Couple *cp;
- int compare(const void *a, const void *b)
- {
- return (((Couple*)b)->v - ((Couple*)a)->v);
- }
- void init()
- {
- int i;
- scanf("%d\n", &n);
- v = malloc(n * sizeof(int));
- for (i=0; i<n; i++)
- scanf("%d", v+i);
- cp = malloc(n * sizeof(Couple));
- for (i=0; i<n; i++)
- {
- cp[i].v = v[i];
- cp[i].i = i;
- }
- qsort(cp, n, sizeof(Couple), compare);
- }
- int result()
- {
- int i, j;
- int loss = 0;
- for (i=0; i<n; i++)
- {
- for (j=n-1; cp[j].v<cp[i].v; j--)
- {
- if (cp[j].i > cp[i].i)
- {
- if (cp[j].v-cp[i].v < loss)
- loss = cp[j].v-cp[i].v;
- break;
- }
- }
- }
- return loss;
- }
- int main()
- {
- init();
- printf("%d", result());
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement