CF950B Intercepted Message_双指针法

本来想直接上权值线段树维护区间最值,不过可以用双指针法,就使问题变得简洁。
Code:

#include<iostream>
using namespace std;
const int maxn = 100000 + 5;
int ans[maxn], a[maxn], b[maxn];
int main(){
    int n, m; 
    cin >> n >> m;
    for(int i = 1;i <= n; ++i){ cin >> a[i]; a[i] += a[i - 1]; }
    for(int i = 1;i <= m; ++i){ cin >> b[i]; b[i] += b[i - 1]; }
    int p1 = 1, p2 = 1;
    for(;p1 <= n && p2 <= m;)
    {
        if(a[p1] == b[p2]) ans[p1] = 1, ++p1, ++p2;
        else if(a[p1] > b[p2]) ++p2;
        else ++p1;
    }
    int fin = 0;
    for(int i = 1;i <= n; ++i)fin += ans[i];
    cout << fin;
    return 0;
}
posted @ 2018-10-09 22:18  EM-LGH  阅读(185)  评论(0编辑  收藏  举报