Advertisement
a53

vecine

a53
Mar 18th, 2022
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. /*
  2. Implementare Dan Pracsiu - 100p
  3. */
  4. #include <iostream>
  5. #include <fstream>
  6. using namespace std;
  7.  
  8. ifstream fin("vecine.in");
  9. ofstream fout("vecine.out");
  10. int n, a[100003];
  11.  
  12. void F1()
  13. {
  14. int i, cnt = 0;
  15. for (i = 1; i < n; i++)
  16. if (a[i] + 1 == a[i + 1]) cnt++;
  17. fout << cnt << "\n";
  18. }
  19.  
  20. void F2()
  21. {
  22. int i, j, L, k;
  23. long long x, y, ans = -1;
  24. L = min(10, n / 2);
  25. for (k = L; k >= 1; k--)
  26. {
  27. /// cautam daca exista doua numere alaturate consecutive de k cifre
  28. for (i = 1; i <= n - 2 * k + 1; i++)
  29. if (a[i] != 0)
  30. {
  31. x = y = 0;
  32. for (j = 0; j < k; j++)
  33. x = x * 10 + a[i + j];
  34. for (j = 0; j < k; j++)
  35. y = y * 10 + a[i + k + j];
  36. if (x + 1 == y)
  37. ans = max(ans, x);
  38. /// verific daca x si y nu sunt de forma x=999, y=1000
  39. if (i + 2 * k <= n)
  40. {
  41. y = y * 10 + a[i + 2 * k];
  42. if (x + 1 == y) ans = max(ans, x);
  43. }
  44. }
  45. else if (k == 1 && a[i + 1] == 1)
  46. ans = max(ans, 0LL);
  47. }
  48. fout << ans << "\n";
  49. }
  50.  
  51. int main()
  52. {
  53. int task;
  54. fin >> task >> n;
  55. for (int i = 1; i <= n; i++)
  56. fin >> a[i];
  57. if (task == 1) F1();
  58. else F2();
  59. fin.close();
  60. fout.close();
  61. return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement