fork download
  1. /*
  2.   Cred : SunnyYeahBoi
  3.   It's my last chance (⌐■_■)
  4.   Problem :
  5. */
  6.  
  7. #include<bits/stdc++.h>
  8.  
  9. using namespace std;
  10.  
  11. #define int long long
  12. #define double long double
  13. #define endl "\n"
  14. #define NAME "a"
  15.  
  16. const int MAXN = 1e6 + 5;
  17. const int inf = 1e18;
  18. const int MOD = 1e9 + 7;
  19.  
  20. void FileInput(){
  21. if(fopen(NAME".inp" , "r") == NULL)
  22. freopen(NAME".inp" , "w" , stdout);
  23. freopen(NAME".inp" , "r" , stdin);
  24. freopen(NAME".out" , "w" , stdout);
  25. }
  26.  
  27. int n;
  28. string s;
  29. int pref[MAXN];
  30.  
  31. void solve(){
  32. cin >> n;
  33. cin >> s;
  34. s = "*" + s;
  35.  
  36. for(int i = 1 ; i <= n ; i++){
  37. pref[i] = pref[i - 1] + (s[i] == '(' ? 1 : -1);
  38. }
  39.  
  40. for(int i = n + 1 ; i <= 2 * n ; i++)
  41. pref[i] = pref[i - 1] + (s[i - n] == '(' ? 1 : -1);
  42.  
  43.  
  44. int res = 0;
  45. deque<int> q; // queue tìm min
  46. for(int i = 1 ; i <= 2 * n ; i++){
  47. while(!q.empty() && pref[q.back()] >= pref[i])
  48. q.pop_back();
  49. q.push_back(i);
  50. if(q.front() < i - n + 1) q.pop_front();
  51.  
  52. // i - n + 1 ; i
  53. if(i >= n){
  54. bool ok1 = (pref[i] - pref[i - n] == 0);
  55. bool ok2 = (pref[q.front()] - pref[i - n] >= 0);
  56. res += (ok1 && ok2);
  57. }
  58. }
  59.  
  60. cout << res << endl;
  61. }
  62.  
  63. int32_t main(){
  64. FileInput();
  65. ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  66. int t = 1;
  67. // cin >> t;
  68. while(t--)
  69. solve();
  70. return 0;
  71. }
  72.  
Success #stdin #stdout 0.01s 5236KB
stdin
Standard input is empty
stdout
Standard output is empty