零点工作室暑假集训(AtCoder--ABC268)

文章包含三个编程问题的解答:A-FiveIntegers主要通过比较或利用Set容器找到不同数字的数量;B-Prefix?通过双指针判断字符串是否为前缀;C-ChineseRestaurant解决餐桌旋转问题,最大化人们面对喜欢的菜的概率。
摘要由CSDN通过智能技术生成

A - Five Integers

题意:就是找有几个不同的数字
思路:有两种方法
1.采取后面的字符逐一与前面的字符比较若无相同的字符则总数 + 1
2.可以采取set容器,因为Set不允许两个元素有相同的键值,即相同的元素不会存进去只会存一次该元素,所以可以将元素存到set里面 去,再求set的大小

AC代码:

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

typedef long long LL;

const int N = 100010;

int a, b, c, d, e;
int ans = 1;

int main()
{
    cin >> a >> b >> c >> d >> e;
    
    if(b != a) ans ++;
    if(c != a && c != b) ans ++;
    if(d != a && d != b && d != c) ans ++;
    if(e != a && e != b && e != c && e != d) ans ++;
    
    cout << ans;
    
    return 0;
}

2.

#include <iostream>
#include <set>

 
using namespace std;

set<int> s; //因为Set不允许两个元素有相同的键值,所以可以将元素存到set里面去,再求set的大小
int x;

int main()
{
    
    for (int i = 0; i < 5; i++) 
    {
        cin >> x;
        s.insert(x);
    }
    cout << s.size() << endl;
    
}

B - Prefix?

题意:给定两个字符串a, b,请问a是不是b的前缀。
思路:使用双指针同时移动对比字符是否相同
AC代码:

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

typedef long long LL;

const int N = 100010;

string s1, s2;

int main()
{
    cin >> s1 >> s2;
    
    int n = s1.size();
    int m = s2.size();
    
    for(int i = 0, j = 0; i < m, j < n; i ++, j ++)
    {
        if(s2[i] != s1[j])
        {
            cout << "No";
            return 0;
        }
    }
    
    cout << "Yes";
    
    return 0;
    }

C - Chinese Restaurant

题意:有n个人坐成一个桌,每个人面前都有一盆菜,人和菜都是一个1-n的排列,如果一个菜距离自己的距离为1,那么这个人就会很高兴。你作为服务员可以任意旋转桌子,使得高兴的人最多。
思路:假如你在i位置,你喜欢的菜的j位置,那么可以把移动(j - i) % n格,这样菜就移到了你的面前。我们先计算对于每个人菜移动到左边和右边还有面前需要旋转桌子几格,然后用mp记录一下贡献,然后枚举桌子旋转的格数,求最大值即可
AC代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>

using namespace std;

typedef long long LL;

const int N = 200010;

int n;
int a[N];
map<int,int>mp;

int main()
{
    cin >> n;
    
    for(int i = 1; i <= n; i ++ ) cin >> a[i];
    
    for(int i = 1; i <= n; i ++ )
    {
        int p = i - 1;
        mp[((a[i] - p) % n + n) % n] ++ ; 
        mp[((a[i] - p + 1) % n + n) % n] ++ ;
        mp[((a[i] - p - 1) % n + n) % n] ++ ;
    }
    
    int res = 0 ;
    
    for(int i = 0; i < n; i ++ )
    {
        res = max(res, mp[i]);
    }
        
    cout << res << endl;

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值