CodeForces - 1593G Changing Brackets(思维)
生活随笔
收集整理的這篇文章主要介紹了
CodeForces - 1593G Changing Brackets(思维)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目鏈接:點(diǎn)擊查看
題目大意:給出一個(gè)長(zhǎng)度為 nnn 的括號(hào)序列,其中包含了 {(,),[,]}\{(,),[,]\}{(,),[,]} 四種括號(hào),現(xiàn)在可以進(jìn)行兩種操作:
給出 qqq 次詢問,每次詢問需要回答使得區(qū)間 [l,r][l,r][l,r] 中的括號(hào)序列合法的最小代價(jià)。
題目分析:因?yàn)榉崔D(zhuǎn)括號(hào)是沒有代價(jià)的,所以只需要關(guān)心括號(hào)的個(gè)數(shù)即可。
手玩一下樣例不難發(fā)現(xiàn),相鄰的兩個(gè)中括號(hào),當(dāng)且僅當(dāng)其下標(biāo)的奇偶不相同時(shí),才可以無代價(jià)匹配。
所以我們只需要關(guān)心奇偶位置的中括號(hào)個(gè)數(shù)就可以了。
代碼:
// 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; }總結(jié)
以上是生活随笔為你收集整理的CodeForces - 1593G Changing Brackets(思维)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2021CCPC(桂林) - Suffi
- 下一篇: CodeForces - 1607D B