2024.1.24 寒假训练记录(7)

白天写了会儿洛谷的数据结构题单,按顺序写的没写到什么难题就不写在这了,然后就是准备明天的讲课加一场div2,但div2还没补d,d等过几天再写 ^ o ^

文章目录

  • CF 1778A Flip Flop Sum
  • CF 1778B The Forbidden Permutation
  • CF 1778C Flexible String

CF 1778A Flip Flop Sum

题目链接

大水题

#include 

using namespace std;

typedef pair<int, int> PII;

#define int long long

const int N = 100010;

void solve()
{
	int n;
	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; i ++ ) cin >> a[i];
	int sum = 0;
	bool flag = false, haha = false, is = false;
	for (int i = 0; i < n; i ++ )
	{
		sum += a[i];
		if (a[i] == 1) flag = false;
		else
		{
			is = true;
			if (!flag) flag = true;
			else haha = true;
		}
	}
	if (haha) sum += 4;
	else if (!is) sum -= 4;

	cout << sum << '\n';
}

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

	int t = 1;
	cin >> t;
	while (t -- )
	{
		solve();
	}
}

CF 1778B The Forbidden Permutation

题目链接

一开始题看错了整好久没写出来,只要有一对不满足题目给定条件即可

所以直接遍历,如果有不满足的直接退出

如果满足,就考虑让其不满足的最小代价,一种方法是交换二者顺序,另一种方法是让二者距离大于d,取最小值即可

#include 

using namespace std;

typedef pair<int, int> PII;

#define int long long

const int N = 100010;

void solve()
{
	int n, m, d;
    cin >> n >> m >> d;
    vector<int> p(n), a(m);
    map<int, int> pos;
    for (int i = 0; i < n; i ++ )
    {
        cin >> p[i];
        pos[p[i]] = i;
    }
    for (int i = 0; i < m; i ++ ) cin >> a[i];
    int ans = 0x3f3f3f3f;
    for (int i = 1; i < m; i ++ )
    {
        int x = pos[a[i - 1]], y = pos[a[i]];
        if (x > y || y - x > d)
        {
            cout << 0 << '\n';
            return;
        }
        else
        {
            ans = min(ans, y - x);
            if (d < n - 1) ans = min(d - (y - x) + 1, ans);
        }
    }
    cout << ans << '\n';
}

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

	int t = 1;
	cin >> t;
	while (t -- )
	{
		solve();
	}
}

CF 1778C Flexible String

题目链接

万万没想到是直接暴力

#include 

using namespace std;

typedef pair<int, int> PII;

#define int long long

const int N = 100010;

void solve()
{
	int n, k;
	cin >> n >> k;
	string a, b;
	cin >> a >> b;
	set<char> stc;
	for (int i = 0; i < n; i ++ ) stc.insert(a[i]);
	vector<char> ver;
	for (auto i : stc) ver.push_back(i);
	map<char, bool> st;
	int ans = 0, tmp = 0;
	vector<int> pos;

	function<void(int, int)> dfs = [&](int u, int num)
	{
		if (num == k || u == ver.size())
		{
			tmp = 0;
			vector<bool> same(n);
			for (int i = 0; i < n; i ++ ) if (st[a[i]] || a[i] == b[i]) same[i] = true;
			int idx = 0;
			bool flag = false;
			for (int i = 0; i < n; i ++ )
			{
				if (same[i])
				{
					idx ++ ;
					flag = true;
				}
				else
				{
					tmp += (idx + 1) * idx / 2;
					idx = 0;
					flag = false;
				}
			}
			tmp += (idx + 1) * idx / 2;
			ans = max(ans, tmp);
			return;
		}
		for (int i = u; i <= ver.size(); i ++ )
		{
			if (i < ver.size()) st[ver[i]] = true;
			dfs(i + 1, num + 1);
			if (i < ver.size()) st[ver[i]] = false;
		}
	};

	dfs(0, 0);

	cout << ans << '\n';
}

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

	int t = 1;
	cin >> t;
	while (t -- )
	{
		solve();
	}
}

你可能感兴趣的:(2024寒假训练记录,算法,c++)