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

A - Balanced Substring

#include
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 
#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 
#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 
#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
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 
#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;
}

你可能感兴趣的:(NEUQ-预备队)