Codeforces466C Number of Ways
題目連結:
http://codeforces.com/problemset/problem/466/C
題意:
給一個長度為n的陣列,將其分成連續的三段使三段的和相等,求有幾種這樣的組合
分析:
從頭掃到尾,將所有的字首和為(sum/3)的點統計起來,然後再從尾開始統計,找到統計所有字尾和為(sum/3)的節點 然後這種方案的數為
這個點之前所有字首和為sum/3的個數
程式碼如下:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 500010;
typedef long long LL;
LL a[maxn];
int cnt[maxn];
int main()
{
int n;
LL x;
while(~scanf("%d",&n)){
LL s=0,p=0;
memset(cnt,0,sizeof(cnt));
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
s+=a[i];
}
if(s%3){puts("0");continue;}
s/=3;
int com=0;
for(int i=1;i<=n;i++){
p+=a[i];
if(p==s)
cnt[com++]=i;
}
LL ans = 0;
p=0;
for(int i=n;i>=1;i--){
p+=a[i];
// cout<<"p "<<p<<endl;
if(p==s){
int pos=lower_bound(cnt,cnt+com,i-1)-cnt;
// cout<<"pos "<<pos<<endl;
ans+=pos;
}
}
cout<<ans<<endl;
}
return 0;
}
相關文章
- Three ways to create Multi Thread in JavathreadJava
- 【轉載】Kano Model — Ways to use it and NOT use it
- Leetcode-Decode WaysLeetCode
- Decode Ways leetcode javaLeetCodeJava
- leetcode-91-Decode WaysLeetCode
- 10 Ways We Hurt Our Romantic Relationships
- LeetCode-Different Ways to Add ParenthesesLeetCode
- 2 ways to solve ASM1 on node 2ASM
- Three ways to quickly login into EBS 11iUI
- HDU 2157 How many ways?? (矩陣快速冪)矩陣
- How to fix elements to the bottom of the container in css? (four ways)AICSS
- 動態規劃 hdu 1978 How many ways動態規劃
- JavaScript Number()JavaScript
- 5 Ways to Prevent the 300ms Click Delay on Mobile Devicesdev
- Oracle System Change Number (SCN) Number 完全筆記Oracle筆記
- 【NUMBER】Oracle的NUMBER資料型別特點Oracle資料型別
- [譯]The other ways to detect OllyDbg 檢測OllyDbg的另類方法
- JavaScript Number 物件JavaScript物件
- Number.NaNNaN
- Number of BoomerangsOOM
- react input[type='number']React
- JavaScript Number toLocaleString()JavaScript
- JavaScript Number toString()JavaScript
- Number.POSITIVE_INFINITY
- Number.ATIVE_INFINITY
- Number.isNaN()方法NaN
- Number.isFinite()方法
- Js中的NumberJS
- Where is the SCN number written?
- D. The Number of Imposters
- LeetCode解題報告 241. Different Ways to Add Parentheses [medium]LeetCode
- 7.34 BITMAP_BUCKET_NUMBER
- 7.104 ITERATION_NUMBER
- Leetcode Number of islandsLeetCode
- Java Number和Math 類Java
- Last digit of a huge numberASTGit
- Python Number(數字)Python
- 447. Number of BoomerangsOOM