2024牛客寒假算法基础集训营2

A. Tokitsukaze and Bracelet

分情况讨论计数,时间复杂度 O ( n ) O(n) O(n)

#include <bits/stdc++.h>
#define int long long
#define YES "YES"
#define NO "NO"
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 lll;
typedef __uint128_t ulll;
typedef pair<int, int> pii;
const double eps = 1e-9;
const int N = 1e6 + 10;
const int INF = 1e18;
const int mod = 998244353;
const int base = 13331;
mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
void solve()
{
    int n;
    cin >> n;
    for(int i = 1;i <= n;i++)
    {
        int ans = 0;
        int a, b, c;
        cin >> a >> b >> c;
        if(a == 150)
        ans++;
        if(a == 200)
        ans += 2;
        if(b == 45)
        ans += 2;
        else if(b >= 34 && b <= 40)
        ans++;
        if(c == 45)
        ans += 2;
        else if(c >= 34 && c <= 40)
        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();
    return 0;
}

B. Tokitsukaze and Cats

考虑当前猫和左边,右边的猫共用一条边

遍历,遇到一只猫答案加 4 4 4,如果左边或者上边有猫,答案分别减 1 1 1,时间复杂度 O ( n m ) O(nm) O(nm)

#include <bits/stdc++.h>
#define int long long
#define YES "YES"
#define NO "NO"
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 lll;
typedef __uint128_t ulll;
typedef pair<int, int> pii;
const double eps = 1e-9;
const int N = 1e6 + 10;
const int INF = 1e18;
const int mod = 998244353;
const int base = 13331;
mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
int a[305][305];
void solve()
{
    int n, m, k;
    cin >> n >> m >> k;
    for(int i = 1;i <= k;i++)
    {
        int x, y;
        cin >> x >> y;
        a[x][y] = 1;
    }
    int ans = 0;
    for(int i = 1;i <= n;i++)
    {
        for(int j = 1;j <= m;j++)
        {
            if(a[i][j] == 1)
            {
                ans += 4;
                if(a[i - 1][j] == 1)
                ans--;
                if(a[i][j - 1] == 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();
    return 0;
}

D. Tokitsukaze and Slash Draw

题解是用最短路算法,但是在赛时用 d p dp dp瞎搞出来了

d p [ i ] [ j ] dp[i][j] dp[i][j], i i i为当前卡片的编号, j j j为目标卡片的位置

考虑三个转移态

1. 1. 1.从前一张卡片的当前位置转移来, d p [ i ] [ j ] = m a x ( d p [ i ] [ j ] , d p [ i − 1 ] [ j ] ) dp[i][j] = max(dp[i][j],dp[i-1][j]) dp[i][j]=max(dp[i][j],dp[i1][j])

2. 2. 2.从前一张卡片的前一个位置转移来, d p [ i ] [ j ] = m a x ( d p [ i ] [ j ] , d p [ i − 1 ] [ ( j + v [ i ] + n − 1 ) % n + 1 ] ) dp[i][j]=max(dp[i][j],dp[i-1][(j + v[i] + n - 1) \% n + 1]) dp[i][j]=max(dp[i][j],dp[i1][(j+v[i]+n1)%n+1])

3. 3. 3.从当前卡片的前一个位置转移来,由于可能出现后面决定前面的情况,婆娑一这种情况下我用了一个 q u e u e queue queue去维护有变化的 d p dp dp

时间复杂度 O ( n m l o g n ) O(nmlogn) O(nmlogn)

(顺便吐槽一下,手调记得删掉调试代码,硬吃了几发罚时)

#include <bits/stdc++.h>
#define int long long
#define YES "YES"
#define NO "NO"
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 lll;
typedef __uint128_t ulll;
typedef pair<int, int> pii;
const double eps = 1e-9;
const int N = 1e6 + 10;
const int INF = 1e18;
const int mod = 998244353;
const int base = 13331;
mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
int dp[1005][5005];
int v[N], w[N];
void solve()
{
    int n, m, k;
    cin >> n >> m >> k;
    for(int i = 1;i <= m;i++)
    cin >> v[i] >> w[i];
    for(int i = 0;i <= m;i++)
    {
        for(int j = 0;j <= n;j++)
        dp[i][j] = INF;
    }
    dp[0][k] = 0;
    for(int i = 1;i <= m;i++)
    {
        for(int j = 1;j <= n;j++)
        {
            int pos = (j + n - 1) % n + 1;
            int ppos = (j - v[i] + n - 1) % n + 1;
            dp[i][pos] = min(dp[i][pos], dp[i - 1][pos]);
            dp[i][pos] = min(dp[i][pos], dp[i - 1][ppos] + w[i]);
            //cout << i << " " << pos << " " << dp[i][pos] << '\n';
        }
        for(int j = 1;j <= n;j++)
        {
            queue<int> q;
            int pos = (j + n - 1) % n + 1;
            int npos = (j + v[i] + n - 1) % n + 1;
            if(dp[i][npos] > dp[i][pos] + w[i])
            {
                dp[i][npos] = dp[i][pos] + w[i];
                q.push(npos);
            }
            while(!q.empty())
            {
                int tp = q.front();
                q.pop();
                pos = (tp + n - 1) % n + 1;
                npos = (tp + v[i] + n - 1) % n + 1;
                if(dp[i][npos] > dp[i][pos] + w[i])
                {
                    dp[i][npos] = dp[i][pos] + w[i];
                    q.push(npos);
                }
            }
        }
    }
    int ans = INF;
    for(int i = 1;i <= m;i++)
    ans = min(ans, dp[i][n]);
    if(ans == INF)
    cout << "-1" << '\n';
    else
    cout << ans << '\n';
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
    solve();
    return 0;
}

EF. Tokitsukaze and Eliminate

看每种宝石中最后出现的那颗宝石的位置,每次操作消除最前面的宝石即可

用一个变量记录每次消除后剩余的宝石种类,从后往前遍历,时间复杂度 O ( n ) O(n) O(n)

#include <bits/stdc++.h>
#define int long long
#define YES "YES"
#define NO "NO"
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 lll;
typedef __uint128_t ulll;
typedef pair<int, int> pii;
const double eps = 1e-9;
const int N = 1e6 + 10;
const int INF = 1e18;
const int mod = 998244353;
const int base = 13331;
mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
int a[N];
void solve()
{
    map<int, int> m;
    map<int, int> nm;
    int n;
    cin >> n;
    int zl = 0, ans = 0;
    for(int i = 1;i <= n;i++)
    {
        cin >> a[i];
        m[a[i]]++;
        if(m[a[i]] == 1)
        zl++;
    }
    int nzl = 0, nex = zl;
    for(int i = n;i >= 1;i--)
    {
        nm[a[i]]++;
        m[a[i]]--;
        if(m[a[i]] == 0)
        nex--;
        if(nm[a[i]] == 1)
        nzl++;
        if(nzl == zl)
        {
            nm.clear();
            ans++;
            nzl = 0;
            zl = nex;
        }
    }
    cout << ans << '\n';
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
    solve();
    return 0;
}

G. Tokitsukaze and Power Battle (easy)

好怪啊,赛时没想到这样的时间复杂度是可行的

由题目可知,给对方留一个数才能取到最大值

用线段树维护区间和,每次查询从后往前在各个区间里找最大值,时间复杂度 O ( q l o g 2 n ) O(qlog^2n) O(qlog2n)

#include <bits/stdc++.h>
#define int long long
#define YES "YES"
#define NO "NO"
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 lll;
typedef __uint128_t ulll;
typedef pair<int, int> pii;
const double eps = 1e-9;
const int N = 1e6 + 10;
const int INF = 1e18;
const int mod = 1e9 + 7;
const int base = 13331;
mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
int ls(int x) { return 2 * x; }
int rs(int x) { return 2 * x + 1; }
ll num[N], a[N * 4], tag[4 * N];
void build(int s, int t, int p) {//建树调用
	// 对 [s,t] 区间建立线段树,当前根的编号为 p
	if (s == t) {
		a[p] = num[s];
		return;
	}
	int m = (s + t) / 2;
	build(s, m, ls(p));
	build(m + 1, t, rs(p));
	a[p] = a[ls(p)] + a[rs(p)];
}
void push_down(int s, int t, int p) {//查询过程和更新过程调用
	if (!tag[p] || s == t) return;
	int m = (s + t) / 2;
	a[ls(p)] += tag[p] * (m - s + 1); a[rs(p)] += tag[p] * (t - m);
	tag[ls(p)] += tag[p]; tag[rs(p)] += tag[p];
	tag[p] = 0;
}
void update(int l, int r, ll c, int s, int t, int p) {//区间加减时调用
	// [l, r] 为修改区间, c 为被修改的元素的变化量, [s, t] 为当前节点包含的区间, p为当前节点的编号
	if (l <= s && t <= r) {
		a[p] += (ll)(t - s + 1) * c; tag[p] += c;
		return;
	}
	int m = (s + t) / 2;
	push_down(s, t, p);
	if (m >= l) update(l, r, c, s, m, ls(p));
	if (m < r) update(l, r, c, m + 1, t, rs(p));
	a[p] = a[ls(p)] + a[rs(p)];
}
ll query(int l, int r, int s, int t, int p) {//查询区间和调用
	// [l, r] 为查询区间, [s, t] 为当前节点包含的区间, p 为当前节点的编号
	if (l <= s && t <= r)
		return a[p];
	push_down(s, t, p);
	int m = (s + t) / 2;
	ll sum = 0;
	if (m >= l) sum += query(l, r, s, m, ls(p));
	// 如果左儿子代表的区间 [s, m] 与询问区间有交集, 则递归查询左儿子
	if (m < r) sum += query(l, r, m + 1, t, rs(p));
	// 如果右儿子代表的区间 [m + 1, t] 与询问区间有交集, 则递归查询右儿子
	return sum;
}
int b[N];
void solve()
{
    int n, q;
    cin >> n >> q;
    for(int i = 1;i <= n;i++)
    cin >> num[i];
    build(1, n, 1);
    while(q--)
    {
        int tt, x, y;
        cin >> tt >> x >> y;
        if(tt == 1)
        update(x, x, y - query(x, x, 1, n, 1), 1, n, 1);
        else
        {
            int ans = query(x, y, 1, n, 1);
            ans = ans - 2 * query(y, y, 1, n, 1);
            for(int i = y - 1;i > x;i--)
            {
                if(query(x, i, 1, n, 1) <= ans)
                break;
                ans = max(ans, query(x, i, 1, n, 1) - 2 * query(i, i, 1, n, 1));
            }
            cout << ans << '\n';
        }
    }
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
    solve();
    return 0;
}

I. Tokitsukaze and Short Path (plus)

对于任意两个点,它们两个的最小权值即为两个顶点值中大的那个 ∗ 2 *2 2,所以我们只需要把权值进行排序,每个点都统领着比它小的点,时间复杂度 O ( n l o g n ) O(nlogn) O(nlogn)

#include <bits/stdc++.h>
#define int long long
#define YES "YES"
#define NO "NO"
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 lll;
typedef __uint128_t ulll;
typedef pair<int, int> pii;
const double eps = 1e-9;
const int N = 1e6 + 10;
const int INF = 1e18;
const int mod = 998244353;
const int base = 13331;
mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
int a[N];
void solve()
{
    int n;
    cin >> n;
    for(int i = 1;i <= n;i++)
    cin >> a[i];
    sort(a+1,a+n+1);
    int ans = 0;
    for(int i = n;i >= 1;i--)
    {
        int cnt = i - 1;
        ans = ans + cnt * 4 * a[i];
    }
    cout << ans << '\n';
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
    solve();
    return 0;
}

J. Tokitsukaze and Short Path (minus)

对于任意两个点,有两种选择:直接到达和先到最小权值点再到达

将权值从小到大排序后,两个点的路径权值为 m i n ( a [ 1 ] ∗ 4 , a [ i ] ∗ 2 ) min(a[1] * 4, a[i] * 2) min(a[1]4,a[i]2) a [ i ] a[i] a[i]为两个点中权值较小的那个点,时间复杂度 O ( n l o g n ) O(nlogn) O(nlogn)

#include <bits/stdc++.h>
#define int long long
#define YES "YES"
#define NO "NO"
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 lll;
typedef __uint128_t ulll;
typedef pair<int, int> pii;
const double eps = 1e-9;
const int N = 1e6 + 10;
const int INF = 1e18;
const int mod = 998244353;
const int base = 13331;
mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
int a[N];
void solve()
{
    int n;
    cin >> n;
    for(int i = 1;i <= n;i++)
    cin >> a[i];
    sort(a+1,a+n+1);
    int ans = 0;
    for(int i = 1;i <= n;i++)
    {
        int cnt = (n - i) * 2;
        ans = ans + cnt * min(a[1] * 4, a[i] * 2);
    }
    cout << ans << '\n';
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
    solve();
    return 0;
}

K. Tokitsukaze and Password (easy)

暴力搜索就能过,时间复杂度算不太出来

一开始考虑的太多了,担心时间超限,所以加了很多限制条件,怎么都过不去。然后死马当作活马医,删了六十多行代码后居然过了…)
这个故事告诉我们,要敬畏暴力之神,不要想七想八(祭拜暴力之神cc)

#include <bits/stdc++.h>
#define int long long
#define YES "YES"
#define NO "NO"
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 lll;
typedef __uint128_t ulll;
typedef pair<int, int> pii;
const double eps = 1e-9;
const int N = 1e6 + 10;
const int INF = 1e18;
const int mod = 1e9 + 7;
const int base = 13331;
mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
int n, ans, mx;
string a, b;
int x[26];
map<int, int> mp;
void dfs(int pos, int cnt)
{
    //cout << pos << " " << cnt << '\n';
    if(pos == n)
    {
        if(cnt % 8 == 0 && cnt > 0 && cnt <= mx)
        ans = (ans + 1) % mod;
        return;
    }
    if(pos == 0)
    {
        if(a[pos] >= '0' && a[pos] <= '9')
        dfs(pos + 1, cnt * 10 + a[pos] - '0');
        else if(a[pos] == '_')
        {
            for(int i = 1;i <= 9;i++)
            dfs(pos + 1, cnt * 10 + i);
        }
        else
        {
            for(int i = 1;i <= b[pos] - '0';i++)
            {
                mp[i] = 1;
                x[a[pos] - 'a'] = i;
                dfs(pos + 1, cnt * 10 + i);
                mp[i] = 0;
                x[a[pos] - 'a'] = -1;
            }
        }
    }
    else
    {
        if(a[pos] >= '0' && a[pos] <= '9')
        dfs(pos + 1, cnt * 10 + a[pos] - '0');
        else if(a[pos] == '_')
        {
            for(int i = 0;i <= 9;i++)
            dfs(pos + 1, cnt * 10 + i);
        }
        else
        {
            if(x[a[pos] - 'a'] != -1)
            dfs(pos + 1, cnt * 10 + x[a[pos] - 'a']);
            else
            {
                for(int i = 0;i <= 9;i++)
                {
                    if(mp[i] == 0)
                    {
                        x[a[pos] - 'a'] = i;
                        mp[i] = 1;
                        dfs(pos + 1, cnt * 10 + i);
                        mp[i] = 0;
                        x[a[pos] - 'a'] = -1;
                    }
                }
            }
        }
    }
}
void solve()
{
    mp.clear();
    ans = 0;
    mx = 0;
    cin >> n;
    cin >> a;
    cin >> b;
    for(int i = 0;i <= 25;i++)
    x[i] = -1;
    for(int i = 0;i < n;i++)
    mx = mx * 10 + b[i] - '0';
    for(int i = 0;i < n;i++)
    {
        if(a[i] >= '0' && a[i] <= '9')
        {
            if(a[i] > b[i])
            {
                cout << "0" << '\n';
                return;
            }
            else if(a[i] < b[i])
            break;
        }
        else
        break;
    }
    if(a[0] == '0' && n > 1)
    {
        cout << "0" << '\n';
        return;
    }
    dfs(0, 0);
    if(n == 1)
    {
        if(a[0] >= '1' && a[0] <= '9')
        ;
        else
        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();
    return 0;
}
  • 18
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值