fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define PII pair <int, int>
  4. PII arr[1205];
  5. string solve (int pos1, int pos2, int a, int b)
  6. // pos1, pos2=positions allowed
  7. // a, b=pairs to be used
  8. {
  9. if (pos1>=pos2) return "";
  10. if (a>b) return "";
  11. int lower=pos1+arr[a].first;
  12. int upper=min(pos2, pos1+arr[a].second);
  13. if (upper<lower) return "";
  14. int start=((lower-1-pos1)&1)?lower+1:lower;
  15. for (int z=start; z<=upper; z+=2)
  16. {
  17. int l=(z-1-pos1)/2;
  18. string first=solve(pos1+1,z-1, a+1, a+l);
  19. string second=solve(z+1, pos2, a+l+1, b);
  20. if (pos2-pos1+1==first.length()+second.length()+2) return '('+first+')'+second;
  21. }
  22. return "";
  23. }
  24. main()
  25. {
  26. int a;scanf("%d", &a);
  27. for (int g=0; g<a; g++)
  28. {
  29. int b,c; scanf("%d %d", &b, &c);
  30. arr[g+1]=PII(b,c);
  31. }
  32. string l= solve(1, 2*a, 1, a);
  33. if (2*a!=l.length()){cout << "IMPOSSIBLE"; return 0;}
  34. cout << l;
  35. return 0;
  36.  
  37. }
Success #stdin #stdout 0s 3156KB
stdin
Standard input is empty
stdout
IMPOSSIBLE