Codeforces Round 882 (Div. 2)(视频讲解A——D)

@[TOC](Codeforces Round 882 (Div. 2)(视频讲解A——D))

讲解在B站:Codeforces Round 882 (Div. 2)(视频讲解A——D)
Codeforces Round 882 (Div. 2)(视频讲解A——D)_第1张图片

A The Man who became a God

#include
#define endl '\n'
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N = 1e5 + 10;

bool cmp(int x, int y)
{
	return x > y;
}
void solve()
{
	int n, k;
	cin >> n >> k;
	vector<int>a(n);
	for(int i = 0 ; i < n; i ++)
		cin >> a[i];
	vector<int>b(n - 1);
	int sum = 0;
	for(int i = 0; i < n - 1; i ++)
	{
		b[i] = abs(a[i] - a[i + 1]);
		sum += b[i];
	}
	sort(b.begin(), b.end(), cmp);
	for(int i = 0; i < k - 1; i ++)
	{
		sum -= b[i];
	}
	cout << sum << endl;
}

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	while(t--)
	solve();
}

B Hamon Odyssey

#include
#define endl '\n'
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N = 1e5 + 10;

void solve()
{
	int n;
	cin >> n;
	vector<int>a(n);
	for(int i = 0; i < n; i ++)
		cin >> a[i];
	int sum = a[0], ans = 0;
	for(int i = 0; i < n; i ++)
	{
		sum &= a[i];
		if(sum == 0)
		{
			sum = a[i + 1];
			ans ++;
		}
	}
	if(ans == 0)
	{
		cout << 1 << endl;
	}
	else
	{
		cout << ans << endl;
	}
}

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	while(t--)
	solve();
}

C Vampiric Powers, anyone?

#include
#define endl '\n'
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N = 1 << 8 + 10;
int cnt[N];
void solve()
{
	memset(cnt, 0, sizeof cnt);
	
	int n;
	cin >> n;
	vector<int>a(n);
	for(int i = 0; i < n; i ++)
		cin >> a[i];
	cnt[0] = 1;
	int tmp = 0;
	int ans = 0;
	for(int i = 0; i < n; i ++)
	{
		tmp ^= a[i];
		for(int j = 0; j < 256; j ++)
		{
			if(cnt[j])
			{
				ans = max(ans, j ^ tmp);
			}
		}
		cnt[tmp] = 1;
	}
	cout << ans << endl;

}

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	while(t--)
	solve();
}

D Professor Higashikata

#include
#define endl '\n'
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N = 3e5 + 10;
int p[N], tr[N], f[N];
int n, m, q;

int find(int x)
{
    if(x != p[x])
        p[x] = find(p[x]);
    return p[x];
}

int lowbit(int x)
{
    return x & (-x);
}

void add(int pos, int x)
{
    for(int i = pos; i <= n; i+= lowbit(i))
    {
        tr[i] += x;
    }
}

int query(int pos)
{
    int res = 0;
    for(int i = pos; i; i -= lowbit(i))
        res += tr[i];
    return res;
}

void solve()
{
    cin >> n >> m >> q;
    string s;
    cin >> s;
    s = " " + s;
    int cnt = count(s.begin(),s.end(),'1');
    for(int i = 1; i <= n + 1; i ++)
    {
        p[i] = i;
    }
    int tot = 0;
    for(int i = 0; i < m; i ++)
    {   
        int l, r;
        cin >> l >> r;
        for(int j = find(l); j <= r; j = find(j))
        {
            p[j] = j + 1;
            f[j] = ++tot;
            if(s[j] == '1')
            {
                add(f[j], 1);
            }
        }
    }

    while(q--)
    {
        int x;
        cin >> x;
        if(f[x])
        {
            if(s[x] == '1')
            {
                s[x] = '0';
                cnt --;
                add(f[x], -1);
            }
            else
            {
                s[x] = '1';
                cnt ++;
                add(f[x], 1);
            }
        }
        else
        {
            if(s[x] == '1')
            {
                s[x] = '0';
                cnt --;
            }   
            else
            {
                s[x] = '1';
                cnt++;
            }
        }
        if(cnt >= tot)
        {
            cout << tot - query(tot) << endl;
        }
        else
        {
            cout << cnt - query(cnt) << endl;
        }
    }
}

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

    solve();
}

你可能感兴趣的:(codeforces,算法,数据结构)