Nothing for Nothing( 十五 )

题目:Maximum Xor Secondary

题意:给你一个数组让你求一个区间的最大值异或上这个区间第二大的值,问你最大值是多大。
思路:本题用单调栈去维护区间第二大的数,正着扫一遍,反着再扫一遍。

#include<bits/stdc++.h>
using namespace std;

const int maxn = 1e5+10;
int a[maxn];
stack<int>s;
int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
    {
        scanf("%d", &a[i]);
    }
    int ans = 0;
    for(int i = 1; i <= n; i++)
    {
        if(s.empty())
        {
            s.push(i);
            continue;
        }

        if(a[s.top()] >= a[i] && !s.empty())
        {
            ans = max(ans, a[s.top()]^a[i]);
            s.push(i);
        }
        else
        {
            while( !s.empty()&&a[s.top()] < a[i])s.pop();
            if(!s.empty())ans = max(ans, a[s.top()]^a[i]);
            s.push(i);
        }
    }
    while(!s.empty())s.pop();
    for(int i = n; i >= 1; i--)
    {
        if(s.empty())
        {
            s.push(i);
            continue;
        }
        if(a[s.top()] >= a[i])
        {
            ans = max(ans, a[s.top()]^a[i]);
            s.push(i);
        }
        else
        {
            while(!s.empty()&&a[s.top()] < a[i])s.pop();
            if(!s.empty())
                ans = max(ans, a[s.top()]^a[i]);
            s.push(i);
        }
    }
    printf("%d\n", ans);

    return 0;
}

题目:k-Multiple Free Set

题意:给你一个数组,让你求这个数组中选择一些数,不存在一个数是另外一个数的k倍的关系的这些数集合,问你这个集合有多大。
思路:我采用队列去模拟,先从大到小排一个序,然后开始模拟。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e5+10;
int n, k;
LL a[maxn];
queue<LL>q;
bool cmp(LL a, LL b){
    return a > b;
}

int main(){
    scanf("%d %d", &n, &k);
    for(int i = 0; i < n; i++){
        scanf("%lld", &a[i]);
    }
    sort(a, a+n, cmp);
    int cn = 0, i = 0;
    while(i < n){
        if(q.empty()){//队列为空的情况下,集合的个数加1
            q.push(a[i]);
            cn++;
            i++;
            continue;
        }
        if(LL(a[i]*k) > q.front()){//若果当前数值的k倍大于队首元素,那么一定不会存在y=kx的这种情况

            q.push(a[i]);
            cn++;
            i++;
        }
        else if((LL)(a[i]*k) == q.front()) {//等于的这种情况直接跳过
            i++;
            continue;
        }
        else{
            if(LL(a[i]*k) < q.front() && !q.empty()){//如果当前的数值的k倍大于队首元素,那么队首的元素弹出,重复上述操作,i不自增
                q.pop();

            }
        }

    }
    printf("%d\n", cn);

return 0;
}

题目:Valera and Elections

题意:给你一棵树,这棵树边的权值有2和1,2代表这棵树的的路径是坏的,想要修复路径,只需要选择某个节点,那么这个节点到1的所有路径都会修复完毕,问你想要修复这棵树最少需要选择几个节点,并且把这些节点输出来。
思路:就是dfs,从1开始搜索,在回溯的过程中,如果当前节点的状态是2并且子节点中没有出现过2,那么就存储一下。
代码:

#include<bits/stdc++.h>
using namespace std;

const int maxn  = 2e5+10;
int h[maxn], e[maxn], nex[maxn], val[maxn];
int vis[maxn];
int cn = 0;
void add(int u, int v, int w){
    val[cn] = w;
    e[cn] = v;
    nex[cn] = h[u];
    h[u] = cn++;
}
int n, tot = 0;
void dfs(int u, int fa, int status){
     int num = tot;
     for(int i = h[u]; ~i; i = nex[i]){
        int w = val[i], to = e[i];
        if(to == fa)continue;
        dfs(to, u, w);
     }
     if(status == 2 && num == tot)vis[++tot] = u;
}
int main(){
    scanf("%d", &n);
    memset(h, -1, sizeof h);
    for(int i = 1; i < n; i++){
        int u, v, w;
        scanf("%d %d %d", &u, &v, &w);
        add(u, v, w);
        add(v, u, w);
    }
    tot = 0;
    dfs(1, -1, -1);
    printf("%d\n", tot);
    for(int i = 1; i <= tot; i++){
        if(i != 1)printf(" ");
        printf("%d", vis[i]);
    }
    printf("\n");
}


题目: Color Stripe

题意:给你一个字符串,每个相邻的字符串不能相同,然后再告诉你最多使用多少种字符串。
思路:我一开始的想法就是如果这个字符是连续的两个相同的,那么我就把第二个重复的字符串随意附一个不同于左右两边的字符,但是k为2的时候会产生死循环,所以我就把k=2的这种情况特判一下。

#include<bits/stdc++.h>
using namespace std;
int n, k;
const int maxn = 5e5+10;
char a[maxn];
set<char>s;
int main()
{
    scanf("%d %d", &n, &k);
    scanf("%s", a);

    if(k == 2 )
    {
        int c1 = 0, c2 = 0;
        for(int i = 0 ; i < n; i++)
        {
            if(i % 2 == 1)
            {
                if(a[i] == 'A') c1++;
                else c2++;
            }
            else
            {
                if(a[i] == 'A') c2++;
                else c1++;
            }
        }
        int ans = min(c1, c2);
        for(int i = 0; i < n; i++)
        {
            if(ans == c1)
            {
                if(i % 2 == 1)
                {
                    a[i] = 'B';
                }
                else a[i] = 'A';
            }
            else
            {
                if(i % 2 == 1)
                {
                    a[i] = 'A';
                }
                else a[i] = 'B';
            }
        }
        printf("%d\n", ans);
        printf("%s\n",a);
        return 0;
    }

    int cn = 0;
    int pre = a[0], i = 1;
    while(i < n)
    {
        if(a[i] != pre)
        {
            pre = a[i];
            i++;
        }
        else
        {
            cn++;
            int t1 = pre-'A', t2 = a[i+1]-'A';
            t1++;
            t1 %= k;
            while(t1 == t2)
            {
                t1++;
                t1 %= k;
            }
            a[i] = 'A'+t1;
        }
    }
    printf("%d\n", cn);
    printf("%s",a);
    printf("\n");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值