日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

CodeForces - 1593G Changing Brackets(思维)

發布時間:2024/4/11 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CodeForces - 1593G Changing Brackets(思维) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:點擊查看

題目大意:給出一個長度為 nnn 的括號序列,其中包含了 {(,),[,]}\{(,),[,]\}{(,),[,]} 四種括號,現在可以進行兩種操作:

  • 將括號反轉,代價為 000,比如 [[[ 變為 ]]]))) 變為 (((
  • 將中括號變為小括號,代價為 111,比如 [[[ 變成 (((
  • 給出 qqq 次詢問,每次詢問需要回答使得區間 [l,r][l,r][l,r] 中的括號序列合法的最小代價。

    題目分析:因為反轉括號是沒有代價的,所以只需要關心括號的個數即可。

    手玩一下樣例不難發現,相鄰的兩個中括號,當且僅當其下標的奇偶不相同時,才可以無代價匹配。

    所以我們只需要關心奇偶位置的中括號個數就可以了。

    代碼:

    // Problem: G. Changing Brackets // Contest: Codeforces - Codeforces Round #748 (Div. 3) // URL: https://codeforces.com/contest/1593/problem/G // Memory Limit: 256 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org)// #pragma GCC optimize(2) // #pragma GCC optimize("Ofast","inline","-ffast-math") // #pragma GCC target("avx,sse2,sse3,sse4,mmx") #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<bitset> #include<list> #include<unordered_map> #define lowbit(x) (x&-x) using namespace std; typedef long long LL; typedef unsigned long long ull; template<typename T> inline void read(T &x) {T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f; } template<typename T> inline void write(T x) {if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0'); } const int inf=0x3f3f3f3f; const int N=1e6+100; char s[N]; int sum[N][2]; int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);int w;cin>>w;while(w--) {scanf("%s",s+1);int n=strlen(s+1);for(int i=1;i<=n;i++) {for(int j=0;j<2;j++) {sum[i][j]=sum[i-1][j];}if(s[i]=='['||s[i]==']') {sum[i][i&1]++;}}int q;read(q);while(q--) {int l,r;read(l),read(r);printf("%d\n",abs((sum[r][0]-sum[l-1][0])-(sum[r][1]-sum[l-1][1])));}}return 0; }

    總結

    以上是生活随笔為你收集整理的CodeForces - 1593G Changing Brackets(思维)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。