10.12-10.18题解

· · 个人记录

::::info[P7502]

link

第一眼看到题目会认为这是一道要用集合的题目,但是观察完数据范围 m <= 2e5 后 O(m^2) 的时间复杂度肯定是过不了的。所以要考虑一些 trick 去优化它。

首先看到此集合有三种操作,1 2 操作是互补的,主要是要去思考 操作 3 的做法。

看到有 xor 运算那么不难想到拆位,拆成二进制会好做很多。但是需要多个数对和一个数对比大小且用二进制的话,十分快速可以想到运用 01trie 做。但是这个用普通的 01trie 做似乎是行不通的,于是衍生出一种四叉的 01trie 用来维护数对此时的状态。

首先我们考虑到 x \oplus a > y \oplus b 意味着:在高位前 i-1 位相同的情况下,在第 i 位上,x_i \oplus a_i = 1,y_i \oplus b_i = 0。此时 x \oplus a > y \oplus b 一定成立,证明比较显然。

于是我们开始考虑按照什么因素建 trie 树。

由于通过移项可以发现相同的高位要满足 x \oplus y = a \oplus b。所以我们按照 a \oplus b 建树,只建 01 两个子节点,且存下了 a 与 b 可以更好操作。

从而操作 12 的插入删除也做完了,插入直接插入 1,要删除直接 -1 覆盖掉即可。

那么查询我们从根节点开始从高位往下查找,当前处理第 i 位,u 的前缀表示的是前面的高位都满足 x \oplus y = a \oplus b。

因为我们按照 x \oplus y 向下寻找,我们要在第 i 位寻找到第一次出现 x_i \oplus a_i = 1,y_i \oplus b_i = 0。从而继续移项求出当前 a_i 和 b_i。a_i = x_i - 1,b_i = y_i。

然后把它们累加到答案里去,继续往低位寻找,如果没有子节点了说明没有这一位还在保持相同的数对,那么直接退出。

*tips:由于建树在 4 叉,所以数组空间要开大 2-4 倍,不然会 RE。

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define y1 y_1
#define f1 first
#define s2 second
const int N=2e7+10;
int ans,tot;
int tr[N][2],cnt[N][2][2];
void add(int a,int b,int v){
    int u=0;
    for(int i=62;i>=0;i--){
        int a1=(a>>i)&1,b1=(b>>i)&1;
        cnt[u][a1][b1]+=v;
        int c=a1^b1;
        if(!tr[u][c]) tr[u][c]=++tot;
        u=tr[u][c];
    }
    return ;
}
void query(int x,int y){
    int u=0;
    for(int i=62;i>=0;i--){
        int a1=1-((x>>i)&1),b1=(y>>i)&1;
        ans+=cnt[u][a1][b1];
        int c=((x>>i)&1)^((y>>i)&1);
        if(!tr[u][c]) break;
        u=tr[u][c];
    }
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int m;
    cin>>m;
    while(m--){
        int op,x,y;
        cin>>op>>x>>y;
        if(op==1) add(x,y,1);
        else if(op==2) add(x,y,-1);
        else query(x,y),cout<<ans<<'\n',ans=0;
    }
    return 0;
}

::::

::::info[P11412]

link

由于上题用了 01trie 还去查看了一眼 trie 板子,从而再来一题练练手。

其实看完题这题的算法框架就已经显现出来了。字符串前缀的判断可以用 hash 和 trie。然后其余变量用一个结构体去维护一下即可。

但是此题的 trie 是一类变种,由于它还要记录其深度才能判断它能不能加,于是我们在 cnt 数组内存的是编号而并非数量。

然后最后在主函数内通过前缀的判断,然后遍历对应的记忆,最终累加获得答案。

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define y1 y_1
#define f1 first
#define s2 second
const int N=4e5+10;
int n,m;
struct node{int r,v;string s;}c[N];
int tr[N][65],tot;
vector<int> cnt[N];
int get(char c){return c-'a';}
void add(string &s,int id){
    int u=0;
    for(char c:s){
        int v=get(c);
        if(!tr[u][v]) tr[u][v]=++tot;
        u=tr[u][v];
        cnt[u].push_back(id);
    }
}
int query(string &s){
    int u=0;
    for(char c:s){
        int v=get(c);
        if(!tr[u][v]) return 0;
        u=tr[u][v];
    }
    return u;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>c[i].r>>c[i].v>>c[i].s,add(c[i].s,i);
    for(int i=1;i<=m;i++){
        int d;
        string q;
        cin>>d>>q;
        int k=query(q),ans=0;
        for(auto i:cnt[k]) if(c[i].r<=d) ans+=c[i].v;
        cout<<ans<<'\n';
    }
    return 0;
}

::::

::::info[P6786]

link

首先看到这个数据范围,能够看出这道题 O(n^2 log a_i) 是无法通过的,那么我们需要对于题目进行一个分析从而进行优化。

通过一个手玩以及打表,得出了一个结论:当 x + y + gcd(x,y) = lcm(x,y)时,有 x = \frac{2y}{3} 或 y = \frac{2x}{3}。

并不会证明所以跳过了(悲)。

那么我们根据这个结论可以发现当我们将原数列排序去重之后,相邻的两个元素成 \frac{2}{3} 倍的关系。

于是我们只需要枚举最小值然后用 map 即可做出此题。

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define y1 y_1
#define f1 first
#define s2 second
const int N=3e5+10;
int n,ans;
int a[N];
map<int,int> ma;
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i],ma[a[i]]++;
    sort(a+1,a+n+1);
    for(int i=1;i<=n;i++){
        int w=a[i],sum=0;
        while(ma[w]){
            sum+=w*ma[w];
            ma[w]=0;
            if(w%2==0) w=w/2*3;
            else break;
        }
        ans=max(ans,sum);
    } 
    cout<<ans;
    return 0;
}

::::

::::info[P13369]

link

超级毒瘤题,给了不超过误差还不给样例,导致一堆锅调不出来/ll。

由于 V 的总和不会超过 1e6,那么我们可以把每一个点都存下来。

然后观察整道题的题意,可以发现答案是绝对具有单调性的,于是可以进行一个二分答案。

然后 check 运用一个贪心的思想,经过时间为 t 时,所有摊贩只能在[p_0-x,p_n+x] 内进行一个移动,然后我们要让所有的摊贩尽量往左移且距离保持 D,如果会超出两者之一的边界,那么就继续二分。

还有一个最重要的就是输出在 : 后有一个空格,因为这个 WA 了一发/ll。

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define y1 y_1
#define f1 first
#define s2 second
int len;
vector<int> a;
bool check(double x,int d){
    double w=a[0]-x;
    for(int i=1;i<len;i++){
        if(w>=a[i]-x-d) w+=d;
        else w=a[i]-x;
        if(w>a[i]+x) return false;
    }
    return true;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int T;
    cin>>T;
    int t=0;
    while(T--){
        t++;
        len=0;
        a.clear();
        int c,d;
        cin>>c>>d;
        for(int i=1;i<=c;i++){
            int p,v;
            cin>>p>>v;
            for(int j=1;j<=v;j++) a.push_back(p),len++;
        }
        sort(a.begin(),a.end());
        double l=0,r=1e13,ans=0;
        for(int i=1;i<=75;i++){
            double mid=(l+r)/2.0;
            if(check(mid,d)) ans=mid,r=mid;
            else l=mid;
        }
        cout<<"Case #"<<t<<": ";
        cout<<fixed<<setprecision(10)<<ans<<'\n';
    }
    return 0;
}

::::

::::info[P5963]

link

tips:此题数据范围不清楚,反正开 1e5 是过不去的,要开大一点。

看完题目可以想到两种思路,一个是贪心,一个是 DP,但是 DP 十分明显的是时间复杂度会超时,那么我们直接往贪心想,如果贪心是假的后面再来考虑 DP 优化。

列举出两组数,有两种情况:

假设第一种成立,那么 -x_i+y_j < -x_j+y_i,然后进行一个移项,可以发现 x_j+y_j < x_i+y_i。这就是两者的和。

于是我们得出了当一组数的和大于另一组的和时,我们将大的选择为减数,小的为加数。

直接排序,前面小的一半当加数,后面大的一半当减数。

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define y1 y_1
#define f1 first
#define s2 second
const int N=1e6+10;
int n;
struct node{int a,b,sum;}c[N];
bool cmp(node x,node y){return x.sum<y.sum;}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++) cin>>c[i].a>>c[i].b,c[i].sum=c[i].a+c[i].b;
    sort(c+1,c+n+1,cmp);
    int ans=0;
    for(int i=1;i<=n/2;i++) ans+=min(c[i].a,c[i].b);
    for(int i=n/2+1;i<=n;i++) ans-=max(c[i].a,c[i].b);
    cout<<ans;
    return 0;
}

::::