標籤:style http color io os ar for div sp
C - Number of Ways
直接暴力從前往後尋找,如果找到1/3sum的位置,那麼標記++。找到2/3的位置,總數加上標記數。
#include<stdio.h>#include<iostream>#include<stdlib.h>#include<string.h>#include<algorithm>#include<vector>#include<math.h>#include<queue>#include<stack>#include<map>#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;#define maxn 550000#define mod 10000007#define LL __int64LL a[maxn];int main(){ int n; while(~scanf("%d",&n)) { LL sum=0; for(int i=1;i<=n;i++) { scanf("%I64d",&a[i]); sum+=a[i]; } if(sum%3) { cout<<"0"<<endl; continue; } sum=sum/3; LL ans=0; LL now=0; LL s=0; for(int i=1;i<n;i++) { now+=a[i]; if(sum*2==now) { ans+=s; } if(now==sum) { s++; } } cout<<ans<<endl; } return 0;}D - Increase Sequence
先把a數組變成需要加多少變成h。
然後在對a數組前向差分得出b數組。
cnt:標記到當前位置,有幾個l沒有和r匹配
如果b[i]==1:
說明當前位置有一個l,cnt++;
如果b[i]==0:
1,當前位置什麼都沒有
2,當前位置有一個l,一個r。
因為有一個l,所以cnt++.
有一個r,所以總數*=cnt,cnt--;
相當於總數*=(cnt+1);
如果b[i]==-1:
當前位置有一個r,所以總數*=cnt,cnt--;
如果b[i]不等於上面的三種情況,說明無解!
#include<stdio.h>#include<iostream>#include<stdlib.h>#include<string.h>#include<algorithm>#include<vector>#include<math.h>#include<queue>#include<stack>#include<map>#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;#define maxn 550000#define mod 1000000007#define LL __int64LL a[maxn];LL b[maxn];int main(){ int n,h; while(~scanf("%d%d",&n,&h)) { for(int i=1;i<=n;i++)scanf("%I64d",&a[i]); for(int i=1;i<=n;i++)a[i]=h-a[i]; for(int i=1;i<=n+1;i++) { b[i]=a[i]-a[i-1]; } LL ans=1; LL cnt=0; for(int i=1;i<=n+1;i++) { if(b[i]==1) { cnt++; } else if(b[i]==-1) { ans=ans*(cnt)%mod; cnt--; } else if(b[i]==0) { ans=ans*(cnt+1)%mod; } else { ans=0;break; } ans=ans%mod; } cout<<ans<<endl; } return 0;}
Codeforces Round #266 (Div. 2)-C,D