CodeForces - 1562D2 Two Hundred Twenty One (hard version)(二分)
題目鏈接:點擊查看
題目大意:定義一個前綴和公式 a1?a2+a3?a4+…=∑i=1n(?1)i?1?aia_1 - a_2 + a_3 - a_4 + \ldots=\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_ia1??a2?+a3??a4?+…=i=1∑n?(?1)i?1?ai?,再給出一個長度為 nnn 的序列,只包含 {?1,1}\{-1,1\}{?1,1},然后 qqq 次詢問,每次需要回答最少刪除多少個數字可以使得區間 [l,r][l,r][l,r] 的前綴和等于 000
題目分析:感覺 D1D1D1 和 D2D2D2 結合起來一起考真的不錯。證明的話我不會,可以右轉官方題解。
如果直接放 D2D2D2 的話可能讓人無從下手,但是放了一個 D1D1D1 可以猜出一個差不多的結論,首先設 sum[i]sum[i]sum[i] 是按照題目給出的公式的前綴和,下文中的區間和指的都是這個 sumsumsum:
這也就告訴我們,奇數情況下一定有解,偶數情況下,可以通過隨便刪掉一個數字然后變成奇數情況。
其實題目中的公式將加號藏起來了不太好看,如果將加號展開的話公式變成了(以 n=5n=5n=5 為例):
a1+(?a2)+a3+(?a4)+a5a_1 +(- a_2) + a_3 +(- a_4) + a_5a1?+(?a2?)+a3?+(?a4?)+a5?
分兩種情況討論,首先刪掉一個奇數位置,這里刪掉 a3a_3a3? 后,新的序列變成了:
a1+(?a2)+a4+(?a5)a_1 +(- a_2) + a_4 +(- a_5)a1?+(?a2?)+a4?+(?a5?)
不難發現如果想要讓 a1+(?a2)+a4+(?a5)=0a_1 +(- a_2) + a_4 +(- a_5)=0a1?+(?a2?)+a4?+(?a5?)=0,則需要滿足 a1+(?a2)=(?a4)+a5a_1 +(- a_2)=(- a_4) + a_5a1?+(?a2?)=(?a4?)+a5?
刪掉偶數位置亦是如此,假設刪掉 a2a_2a2? 后,新的序列變成了:
a1+(?a3)+a4+(?a5)a_1 +(- a_3) + a_4 +(- a_5)a1?+(?a3?)+a4?+(?a5?)
同理需要滿足 a1=a3+(?a4)+a5a_1=a_3 +(- a_4) + a_5a1?=a3?+(?a4?)+a5?
推廣一下,在區間 [l,r][l,r][l,r] 中(保證 r?l+1r-l+1r?l+1 是奇數)如果刪掉 xxx 可以滿足題意,那么刪掉 xxx 后,需要滿足 sum[r]?sum[x]=sum[x?1]?sum[l?1]sum[r]-sum[x]=sum[x-1]-sum[l-1]sum[r]?sum[x]=sum[x?1]?sum[l?1]
移一下項得到 sum[r]+sum[l?1]=sum[x]+sum[x?1]sum[r]+sum[l-1]=sum[x]+sum[x-1]sum[r]+sum[l?1]=sum[x]+sum[x?1]
將 sum[x]+sum[x?1]sum[x]+sum[x-1]sum[x]+sum[x?1] 的位置扔到桶里去對于每次詢問直接二分答案就好啦
代碼:
// Problem: D2. Two Hundred Twenty One (hard version) // Contest: Codeforces - Codeforces Round #741 (Div. 2) // URL: https://codeforces.com/contest/1562/problem/D2 // Memory Limit: 512 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]; map<int,vector<int>>node; int cal(int l,int r) {int x=sum[r]+sum[l-1];return *lower_bound(node[x].begin(),node[x].end(),l); } 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--) {node.clear();int n,m;read(n),read(m);scanf("%s",s+1);for(int i=1;i<=n;i++) {sum[i]=sum[i-1]+(i%2?1:-1)*(s[i]=='+'?1:-1);node[sum[i]+sum[i-1]].push_back(i);}while(m--) {int l,r;read(l),read(r);if(sum[r]-sum[l-1]==0) puts("0");else if((r-l+1)&1) {puts("1");printf("%d\n",cal(l,r));} else {puts("2");printf("%d %d\n",l,cal(l+1,r));}}}return 0; }總結
以上是生活随笔為你收集整理的CodeForces - 1562D2 Two Hundred Twenty One (hard version)(二分)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CodeForces - 468C Ha
- 下一篇: CodeForces - 1562E R