informaticage

ansi C - OII - Rispetta i versi

Oct 23rd, 2021
1,123
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void solve(int N, char* signs, int* sols) {
  5.   int curr = N;
  6.   for (int i = 0; i < N; i++) {
  7.     if (signs[i] == '<') curr--;
  8.   }
  9.  
  10.   sols[0] = curr;
  11.   int curr_max = curr, curr_min = curr;
  12.   for (int i = 0; i < N; i++) {
  13.     if (signs[i] == '<') curr = ++curr_max;
  14.     if (signs[i] == '>') curr = --curr_min;
  15.  
  16.     sols[i] = curr;
  17.   }
  18. }
  19.  
  20. int main(void) {
  21.   int N;
  22.   scanf("%d", &N);
  23.   char* signs = (char*)malloc(sizeof(char) * N);
  24.  
  25.   for (int i = 0; i < N; i++) {
  26.     scanf("%c", &signs[i]);
  27.   }
  28.  
  29.   int* sols = (int*)malloc(sizeof(int) * N + 1);
  30.   solve(N, signs, sols);
  31.  
  32.   for (int i = 0; i < N; i++) {
  33.     printf("%d ", sols[i]);
  34.   }
  35.  
  36.   free(sols);
  37.   free(signs);
  38.   return 0;
  39. }
RAW Paste Data