Codeforces Round 1069 (Div. 2)

A. Little Fairy's Painting

ideas:

我感觉题目一点都不好读懂

题目的意思是:要去填充1e181e^{18}1e18个格子,有无限种颜色,给出n个,找规律去填充后面的。

比如案例三:
58 10 15 20 25 5 \\ 8\ 10\ 15\ 20\ 25 \\ 58 10 15 20 25

第0个位置的颜色数是8,第一个是10,这样不同的颜色数就是2,每一个数代表不同的颜色,这个案例中就是有5个不同颜色的数。此时第n+1位就填5,此时不同颜色数增加到6(因为5不再原来的数组中)。

后续填充的规则是:不同颜色数作为颜色编号。

那么当,不同颜色数等于前n位颜色编号中的某一位时,不同颜色数就不再增加,答案也就因此固定。

code:

cpp 复制代码
    int n;
    cin>>n;
    set<int>st;
    for(int i=0;i<n;i++){
        cin>>a[i];
        st.insert(a[i]);
    }
    int k=st.size();
    int ans=INT_MAX;
    for(int i=0;i<n;i++){
        if(a[i]>=k)
            ans=min(ans,a[i]);
    }
    cout<<ans<<endl;

B. XOR Array

ideas:

题目含义是:

1.在l−rl-rl−r这个区间内的XOR 的结果为0;

2.在l−rl-rl−r这个区间外的XOR 的结果不为0。
结论:
al⊕al+1⊕...⊕ar=prer⊕prel−1al ⊕ al+1 ⊕ ... ⊕ ar = prer ⊕ prel-1al⊕al+1⊕...⊕ar=prer⊕prel−1

推导:
a1⊕a2⊕a3....⊕al....⊕ar=prera_1\oplus a_2 \oplus a_3.... \oplus a_l.... \oplus a_r = prera1⊕a2⊕a3....⊕al....⊕ar=prer
a1⊕a2⊕a3....⊕al=prela_1\oplus a_2 \oplus a_3.... \oplus a_l = prela1⊕a2⊕a3....⊕al=prel

根据异或的性质:
a⊕a=0a\oplus a = 0a⊕a=0

很轻易得出上面的结论。

要想到答案为0,得出:
prer=prel−1prer = prel-1prer=prel−1

于是我们可以得出构造方法:

1.只需要prer=prel−1prer = prel-1prer=prel−1

2.其他位置的任意,我们选择prei=iprei=iprei=i

3.我们构造的aiaiai数组是前缀异或值,反解计算出每一位的值。

code:

cpp 复制代码
    int n,l,r;
    cin>>n>>l>>r;
    vector<int>pre(n+2);
    for(int i=0;i<n+2;i++)pre[i]=i;
    pre[l-1]=pre[r];
    for(int i=0;i<n;i++){
        cout<<(pre[i]^pre[i+1])<<" ";
    }
    cout<<endl;

C. Needle in a Haystack

ideas:

一个模拟题。

思路是:

1.可行性检查:

如果长度一样,一个字符t中没有就输出"Impossible"

如果有一个字符的次数,t中不够,就输出"Impossible"

2.分离字符:

把s中的每一个字符分离出来,剩下的字符串排序

3.合并字符:

对于每一个s中的字符,找到合适的位置放进去就可以

最后把剩余的字符加进去。

code:

cpp 复制代码
    string s,t;
    cin>>s>>t;
    map<char,int>mpa,mpb;
    for(auto c:s)mpa[c]++;
    for(auto c:t)mpb[c]++;

    //可行性检查
    for(auto c:s){
        if(mpb[c]<mpa[c]){
            cout<<"Impossible"<<endl;
            return ;
        }
    }

    //分离字符
    string tmp="";
    for(char c='a';c<='z';c++){
        int d=mpb[c]-mpa[c];
        tmp+=string(d,c);
    }
    sort(tmp.begin(),tmp.end());

    //合并字符
    string ans="";
    int pos=0;
    for(auto c:s){
        while(pos<tmp.size()&&tmp[pos]<c){
            ans+=tmp[pos];
            pos++;
        }
        ans+=c;
    }

    while(pos<tmp.size()){
        ans+=tmp[pos];
        pos++;
    }
    cout<<ans<<endl;
相关推荐
basketball616几秒前
动态规划第一章:引论
算法·动态规划·1024程序员节
星夜离尘565 分钟前
MATLAB 因果采样保持:用历史匹配和数据年龄防止未来信息泄漏
数据结构·算法·matlab
思麟呀43 分钟前
BTree的原理与 C++ 完整实现
c语言·数据结构·c++·算法·1024程序员节
Tsuki_tl1 小时前
【优选算法 — 双指针】经典例题题解+算法总结
算法·蓝桥杯·力扣·刷题·双指针·1024程序员节·优选算法
拓恒技术2 小时前
内河航道无人机智能巡检系统与算法应用
人工智能·深度学习·算法·无人机·智慧城市
如何原谅奋力过但无声2 小时前
【LeetCode hot100】代码随想录未cover部分
数据结构·算法·leetcode
INGNIGHT2 小时前
377.组合总和 Ⅳ(背包)
c++·算法·1024程序员节
拓恒技术3 小时前
城市管理中的无人机AI识别算法:架构、场景与落地
人工智能·算法·视觉检测·无人机·1024程序员节
吴可可1233 小时前
Accumark二次开发环境搭建与API入门
算法
木井巳5 小时前
【动态规划: 线性dp】解码方法
java·算法·leetcode·动态规划·推荐算法