2019 Multi-University Training Contest 9

本文探讨了可持久化鸽算法的实现细节,通过分析不同操作类型对数据结构的影响,设计并实现了高效的查询和更新策略。文章展示了如何利用离散化、线段树和前缀和等技术来优化算法性能,同时提供了多个具体示例,包括游戏策略优化和硬币收集问题的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

可持久化鸽

1002 - Rikka with Cake 

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 1e5 + 7;
int N, M, K, lsan_x[maxN], lsan_y[maxN], tx, ty, al, ar, au, ad;
struct node
{
    int x, y;
    node(int a=0, int b=0):x(a), y(b) {}
}le[maxN], ri[maxN], up[maxN], down[maxN];
bool cmp1(node e1, node e2) { return e1.y < e2.y; }
bool cmp2(node e1, node e2) { return e1.x > e2.x; }
bool cmp3(node e1, node e2) { return e1.x < e2.x; }
int trie[maxN];
inline void add(int x)
{
    while(x <= K)
    {
        trie[x]++;
        x += lowbit(x);
    }
}
inline int sum(int x)
{
    int ans = 0;
    while(x)
    {
        ans += trie[x];
        x -= lowbit(x);
    }
    return ans;
}
inline void init()
{
    tx = ty = 0;
    al = ar = au = ad = 0;
}
int main()
{
    int Cas; scanf("%d", &Cas);
    while(Cas--)
    {
        scanf("%d%d%d", &N, &M, &K);
        init();
        int xi, yi; char op[3];
        for(int i=1; i<=K; i++)
        {
            scanf("%d%d%s", &xi, &yi, op);
            lsan_x[i] = xi;
            lsan_y[i] = yi;
            if(op[0] == 'U')
            {
                up[++au] = node(xi, yi);
            }
            else if(op[0] == 'L')
            {
                le[++al] = node(xi, yi);
            }
            else if(op[0] == 'R')
            {
                ri[++ar] = node(xi, yi);
            }
            else
            {
                down[++ad] = node(xi, yi);
            }
        }
        sort(lsan_x + 1, lsan_x + K + 1);
        sort(lsan_y + 1, lsan_y + K + 1);
        tx = unique(lsan_x + 1, lsan_x + K + 1) - lsan_x - 1;
        ty = unique(lsan_y + 1, lsan_y + K + 1) - lsan_y - 1;
        for(int i=1; i<=al; i++)
        {
            le[i].x = lower_bound(lsan_x + 1, lsan_x + tx + 1, le[i].x) - lsan_x;
            le[i].y = lower_bound(lsan_y + 1, lsan_y + ty + 1, le[i].y) - lsan_y;
        }
        for(int i=1; i<=ar; i++)
        {
            ri[i].x = lower_bound(lsan_x + 1, lsan_x + tx + 1, ri[i].x) - lsan_x;
            ri[i].y = lower_bound(lsan_y + 1, lsan_y + ty + 1, ri[i].y) - lsan_y;
        }
        for(int i=1; i<=ad; i++)
        {
            down[i].x = lower_bound(lsan_x + 1, lsan_x + tx + 1, down[i].x) - lsan_x;
            down[i].y = lower_bound(lsan_y + 1, lsan_y + ty + 1, down[i].y) - lsan_y;
        }
        for(int i=1; i<=au; i++)
        {
            up[i].x = lower_bound(lsan_x + 1, lsan_x + tx + 1, up[i].x) - lsan_x;
            up[i].y = lower_bound(lsan_y + 1, lsan_y + ty + 1, up[i].y) - lsan_y;
        }
        ll ans = 0, tmp;
        //exc 1
        sort(down + 1, down + ad + 1, cmp1);
        sort(ri + 1, ri + ar + 1, cmp1);
        for(int i=0; i<=K; i++) trie[i] = 0;    //clear
        int j=1;
        for(int i=1; i<=ad; i++)
        {
            while(ri[j].y <= down[i].y && j <= ar)
            {
                add(ri[j].x);
                j++;
            }
            tmp = sum(down[i].x);
            ans += tmp;
        }
        //exc 2
        for(int i=0; i<=K; i++) trie[i] = 0;    //clear
        sort(le + 1, le + al + 1, cmp1);
        j = 1;
        for(int i=1; i<=ad; i++)
        {
            while(le[j].y <= down[i].y && j <= al)
            {
                add(le[j].x);
                j++;
            }
            tmp = j - 1 - sum(down[i].x);
            ans += tmp;
        }
        //exc 3
        sort(ri + 1, ri + ar + 1, cmp2);
        sort(up + 1, up + au + 1, cmp2);
        for(int i=0; i<=K; i++) trie[i] = 0;    //clear
        j = 1;
        for(int i=1; i<=ar; i++)
        {
            while(up[j].x >= ri[i].x && j <= au)
            {
                add(up[j].y);
                j++;
            }
            tmp = sum(ri[i].y);
            ans += tmp;
        }
        //exc 4
        sort(le + 1, le + al + 1, cmp3);
        sort(up + 1, up + au + 1, cmp3);
        for(int i=0; i<=K; i++) trie[i] = 0;    //clear
        j = 1;
        for(int i=1; i<=al; i++)
        {
            while(up[j].x <= le[i].x && j <= au)
            {
                add(up[j].y);
                j++;
            }
            tmp = sum(le[i].y);
            ans += tmp;
        }
        printf("%lld\n", ans + 1);
    }
    return 0;
}

1005 - Rikka with Game 

#include "bits/stdc++.h"
using namespace std;
int main(){
    int t;cin>>t;
    string s;
    while(t--){
        cin>>s;
        string t = s;
        for (int i = 0; i < s.length(); ++i) {
            if(s[i]=='z'){
                s[i]='a';
                for (int j = 0; j <= i; ++j) {
                    if(s[j]!='y')s[j]++;
                }
                break;
            }
        }
        cout<<min(s,t)<<endl;
    }
}

1006 - Rikka with Coin 

#include "bits/stdc++.h"

using namespace std;
int a[104];
int txwd[200];
int ans[4];
vector<vector<int>> v;
int mini;

bool check(int x, int num[4]) {
    for (int i = 0; i <= num[0]; ++i) {
        for (int j = 0; j <= num[1]; ++j) {
            for (int k = 0; k <= num[2]; ++k) {
                for (int l = 0; l <= num[3]; ++l) {
                    int res = i * 50 + j * 20 + k * 10 + l * 100;
                    if (res == x)return 1;
                }

            }
        }
    }
    return 0;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        v.clear();
        mini = 1e9;
        int n;
        scanf("%d", &n);
        bool ok = 1;
        int maxi = 0;
        memset(txwd, 0, sizeof(txwd));
        for (int i = 1; i <= n; ++i) {
            scanf("%d", &a[i]);
            if (a[i] % 10 != 0)ok = 0;
            maxi = max(maxi, a[i]);
            txwd[a[i] % 100] = 1;
        }
        if (!ok) {
            puts("-1");
            continue;
        }
        int tt[4] = {0};
        for (int i = 0; i < 2; ++i) {
            for (int j = 0; j < 5; ++j) {
                for (int k = 0; k < 10; ++k) {
                    tt[0] = i;
                    tt[1] = j;
                    tt[2] = k;
                    bool ok = 1;
                    for (int o = 1; o < 10 && ok; ++o) {
                        if (txwd[o * 10])
                            if (!check(o * 10, tt)) ok = 0;
                    }
                    if (ok) {
                        int res = i + j + k;
                        if (res < mini) {
                            mini = res;
                            vector<int> tv;
                            for (int l = 0; l < 4; ++l) {
                                tv.push_back(tt[l]);
                            }
                            v.clear();
                            v.push_back(tv);
                        } else if (res == mini) {
                            vector<int> tv;
                            for (int l = 0; l < 4; ++l) {
                                tv.push_back(tt[l]);
                            }
                            v.push_back(tv);
                        }
                    }
                }
            }
        }
        int fans = 1e9;
        for (int j = 0; j < v.size(); ++j) {
            int tt[4];
            for (int k = 0; k < 4; ++k) {
                tt[k] = v[j][k];
            }

            int nans = 0;
            for (int i = 1; i <= n; ++i) {
                int l = 0, r = a[i] / 100;
                int yans = -1;
                if (check(a[i] - r * 100, tt))yans = r;
                if (check(a[i] - r * 100 + 100, tt))yans = r - 1;
                nans = max(nans, yans);
            }
            fans = min(fans, nans);
        }
        fans += mini;

        mini = 1e9;
        memset(txwd, 0, sizeof(txwd));
        for (int i = 1; i <= n; ++i) {
            maxi = max(maxi, a[i]);
            if (a[i] % 100 == 10 && a[i] > 100)txwd[110] = 1;
            else
                txwd[a[i] % 100] = 1;
        }
        v.clear();
        for (int i = 0; i < 2; ++i) {
            for (int j = 0; j < 5; ++j) {
                for (int k = 0; k < 10; ++k) {
                    for (int m = 0; m < 2; ++m) {
                        tt[0] = i;
                        tt[1] = j;
                        tt[2] = k;
                        tt[3] = m;
                        bool ok = 1;
                        for (int o = 1; o < 12 && ok; ++o) {
                            if (txwd[o * 10])
                                if (!check(o * 10, tt)) ok = 0;
                        }
                        if (ok) {
                            int res = i + j + k + m;
                            if (res < mini) {
                                mini = res;
                                vector<int> tv;
                                for (int l = 0; l < 4; ++l) {
                                    tv.push_back(tt[l]);
                                }
                                v.clear();
                                v.push_back(tv);
                            } else if (res == mini) {
                                vector<int> tv;
                                for (int l = 0; l < 4; ++l) {
                                    tv.push_back(tt[l]);
                                }
                                v.push_back(tv);
                            }
                        }

                    }
                }
            }
        }
        for (int j = 0; j < v.size(); ++j) {
            int tt[4];
            for (int k = 0; k < 4; ++k) {
                tt[k] = v[j][k];
            }

            int nans = 0;
            for (int i = 1; i <= n; ++i) {
                int l = 0, r = a[i] / 100;
                int yans = -1;
                if (check(a[i] - r * 100, tt))yans = r;
                if (check(a[i] - r * 100 + 100, tt))yans = r - 1;
                nans = max(nans, yans);
            }
            fans = min(fans, nans + mini);
        }
        cout << fans << endl;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值