NEUQ-2021寒假训练-前缀和与差分

A - Balanced Substring

#include<bits/stdc++.h>
using namespace std;

const int maxn = 100000 + 5;
char t[maxn];
int a[maxn], sum[maxn], idx[3 * maxn];

int main() 
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	int n;
	cin >> n;
	
	int ans = 0;
    for (int i = 1; i <= n; i ++)
    {
    	cin >> t[i];
    	
        if(t[i] == '0') a[i] = -1;
        else a[i] = 1;   
			
		sum[i] = sum[i - 1] + a[i];
		
		if (sum[i] == 0)
		{
			ans = i;
			continue;
		}
		
		int temp = sum[i] + maxn;
		if(!idx[temp]) idx[temp] = i;
		else ans = max(ans, i - idx[temp]);
	}
	cout << ans;
    
    return 0;
}

B - The Number of Products

#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main() 
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	int n;
	cin >> n;
	
	int pos_cnt = 0, neg_cnt = 0;
	int pos = 0, neg = 0;
	
	for(int i = 0; i < n; i ++) 
	{
		int t;
		cin >> t;
		if(t > 0) 
		{
			pos++;
		}
		if (t < 0)
		{
			swap(pos, neg);
			neg ++;
		}
		pos_cnt += pos;
		neg_cnt += neg;
	}
	cout << neg_cnt << ' ' << pos_cnt;
	
	return 0;
}

C - Molly’s Chemicals

#include <bits/stdc++.h>
#define int long long
using namespace std;

const int maxn = 1e5 + 5;
int sum[maxn];
map <int, int> m;

signed main() 
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	
	int n, k;
	cin >> n >> k;
	
	int all = 0;
	for(int i = 1; i <= n; i ++)
	{
		cin >> sum[i];
		all += abs(sum[i]);
		sum[i] += sum[i - 1];
	}
		
	int ans = 0, t = 1;
	while(abs(t) <= all) 
	{
		m.clear();
		m[0] ++;
		for(int i = 1; i <= n; i ++)
		{
			ans += m[sum[i] - t];
			m[sum[i]] ++;
		}	
		t *= k;
		if (t == 1) break;
	}
	cout << ans;
	
	return 0;
}

D - 子段求和

#include <bits/stdc++.h>
#define int long long
using namespace std;

const int maxn = 50000 + 5;
int a[maxn], sum[maxn];

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	int n;
	cin >> n;
	
	for (int i = 1; i <= n; i ++)
	{	
		cin >> a[i];
		sum[i] = sum[i - 1] + a[i];
	}
	
	int q;
	cin >> q;
	while (q --)
	{
		int l, r;
		cin >> l >> r;
		cout << sum[l + r - 1] - sum[l - 1] << endl;
	}
	
	
	return 0;
}

E - 01串

#include<bits/stdc++.h>
using namespace std;

const int maxn = 1000000 + 5;
int a[maxn], idx[maxn], l[maxn], r[maxn];

int main() 
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	string s;
    cin >> s; 
	int n = s.size();
	
    for (int i = 0; i < n; i ++)
    {
        if(s[i] == '0') a[i + 1] = -1;
        else a[i + 1] = 1;   	
	}
	
    int suml = 0;
    memset(idx, -1, sizeof idx);
    for (int i = 1; i <= n; i ++) 
	{
        suml += a[i];
        if (suml < 0)
        {
        	l[i] = i;
		}  
        else 
		{
            if (idx[suml + 1] == -1)
            {
             	idx[suml] = i;
				l[i] = 0;           	
			}  
            else
            {
				l[i] = i - idx[suml + 1];
			}
        }
    }

	int sumr = 0;
    memset(idx, -1, sizeof idx);
    for (int i = n; i >= 1; i --) 
	{
        sumr += a[i];
        if (sumr > 0)
        {
        	r[i] = n - i + 1;
		}
        else 
		{
            if (idx[-sumr + 1] == -1)
            {
             	idx[-sumr] = i;
				r[i] = 0;           	
			}  
            else
            {
           		r[i] = idx[-sumr + 1] - i;
			}  
        }
    }
    
    int ans = 0;
    for (int i = 1; i <= n; i ++)
	{
    	if (l[i] > 0 && r[i + 1] > 0)
        {
        	ans = max(ans, l[i] + r[i + 1]);
		}
    }
    cout << ans;
    
    return 0;
}

F - Color the ball

#include <iostream>
#define int long long
using namespace std;

const int maxn =  100000 + 5;
int a[maxn], sum[maxn];

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	int n;
	while (cin >> n && n)
	{
		for (int i = 1; i <= n; i ++)
		{
			a[i] = sum[i] = 0;
		}
		
		for (int i = 1; i <= n; i ++)
		{	
			int l, r;
			cin >> l >> r;
			a[l] ++;
			a[r + 1] --;
		}
		
		for (int i = 1; i <= n; i ++)
		{
			sum[i] = sum[i - 1] + a[i];
		}
		
		for (int i = 1; i < n; i ++)
		{
			cout << sum[i] << ' ';
		}
		
		cout << sum[n] << endl;
	}
	
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值