當前位置:
首頁 >
CodeForces - 1593G Changing Brackets(思维)
發布時間:2024/4/11
31
豆豆
生活随笔
收集整理的這篇文章主要介紹了
CodeForces - 1593G Changing Brackets(思维)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:給出一個長度為 nnn 的括號序列,其中包含了 {(,),[,]}\{(,),[,]\}{(,),[,]} 四種括號,現在可以進行兩種操作:
給出 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(思维)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2021CCPC(桂林) - Suffi
- 下一篇: CodeForces - 1607D B