日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

和为S(51Nod-2518)

發布時間:2025/3/17 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 和为S(51Nod-2518) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目

小b有一個01序列A,她想知道A有多少個非空連續子序列和為S。

你能幫幫她嗎?

輸入

第一行輸入一個數n,表示A的長度;
第二行輸入n個數‘0’或‘1’,表示A中的元素,以空格隔開;
第三行輸入一個非負整數S;
其中0≤S≤n≤30000。

輸出

輸出一個數,表示子數組的個數

輸入樣例

5
1 0 1 0 1
2

輸出樣例

4

思路:維護一個前綴和即可

源程序

#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<utility> #include<stack> #include<queue> #include<vector> #include<set> #include<map> #define EPS 1e-9 #define PI acos(-1.0) #define INF 0x3f3f3f3f #define LL long long const int MOD = 1E9+7; const int N = 40000+5; const int dx[] = {0,0,-1,1,-1,-1,1,1}; const int dy[] = {-1,1,0,0,-1,1,-1,1}; using namespace std;int a[N]; int sum[N]; map<int,int> mp; int main() {int n;scanf("%d",&n);for(int i=1; i<=n; i++){scanf("%d",&a[i]);sum[i]=a[i]+sum[i-1];}int k;scanf("%d",&k);int cnt=0;for(int i=1; i<=n; i++) {if(sum[i]==k)cnt++;int pos=sum[i]-k;cnt+=mp[pos];mp[sum[i]]++;}printf("%d\n",cnt);return 0; }

總結

以上是生活随笔為你收集整理的和为S(51Nod-2518)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。