Nothing for nothing( 九 )

题目:Ugly Pairs

题意:给你一个字符串,让你改变字符的顺序,使字符任意相邻的字符在字母表中的位置都不相邻。
思路:就是把字符的奇数位置s1和偶数位置s2,单独提取出来,然后判断一下这两个字符串能否拼接在一起,如果不能就输出No answer.
参考博客:【题解】B. Ugly Pairs(贪心 思维)⭐⭐
代码:

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

int main()
{
    int T, num[30];
    string s, s1, s2;
    cin >> T;
    while(T--){
        memset(num, 0, sizeof(num)); s1 = s2 = "";
        cin >> s;
        for(int i = 0; i < s.length(); i++)
            ++num[s[i]-'a'];
        for(int i = 0; i < 26; i+=2)
            while(num[i]--) s1 += ('a'+i);
        for(int i = 1; i < 26; i+=2)
            while(num[i]--) s2 += ('a'+i);

        if(abs(s1.back()-s2.front())!=1) cout << s1 << s2 << endl;//判断能否拼接
        else if(abs(s2.back()-s1.front())!=1) cout << s2 << s1 << endl;
        else cout << "No answer" << endl;
    }

	return 0;
}


题目: Multicolored Cars

思路:去更新当前位置比cnt[A]大,如果小的就直接抛弃,然后遍历一遍,找到比最终cnt[A]大的。
参考博客:CodeForces - 818D - Multicolored Cars (贪心水题)被误导是线段树。。。
代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+10;
int n,m,x;
int cnt[maxn],ret=0;

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&x);
        if(x==m)  ret++;
        else  if(cnt[x]>=ret) cnt[x]++;///不选他作为答案
    }
    int ans=-1;
    for(int i=1;i<maxn;i++)
    {
        if(i==m) continue;
        if(cnt[i]>=ret) {ans=i;break;}
    }
    printf("%d\n",ans);
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值